MDL-64167 core_message: get_conversations() excludes self conversations
[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
fb04293b
SA
33use \core_message\tests\helper as testhelper;
34
8252b7c2 35class core_message_externallib_testcase extends externallib_advanced_testcase {
e6432668 36
6ff4464b
JL
37 /**
38 * Tests set up
39 */
40 protected function setUp() {
41 global $CFG;
42
43 require_once($CFG->dirroot . '/message/lib.php');
44 }
45
d6731600
FM
46 /**
47 * Send a fake message.
48 *
49 * {@link message_send()} does not support transaction, this function will simulate a message
50 * sent from a user to another. We should stop using it once {@link message_send()} will support
51 * transactions. This is not clean at all, this is just used to add rows to the table.
52 *
53 * @param stdClass $userfrom user object of the one sending the message.
54 * @param stdClass $userto user object of the one receiving the message.
55 * @param string $message message to send.
7d69958e 56 * @param int $notification is the message a notification.
6aa01968 57 * @param int $time the time the message was sent
d6731600 58 */
6aa01968 59 protected function send_message($userfrom, $userto, $message = 'Hello world!', $notification = 0, $time = 0) {
d6731600 60 global $DB;
6aa01968
MN
61
62 if (empty($time)) {
63 $time = time();
64 }
65
883ce421
MN
66 if ($notification) {
67 $record = new stdClass();
68 $record->useridfrom = $userfrom->id;
69 $record->useridto = $userto->id;
70 $record->subject = 'No subject';
71 $record->fullmessage = $message;
72 $record->smallmessage = $message;
73 $record->timecreated = $time;
74
75 return $DB->insert_record('notifications', $record);
76 }
77
b2cd17e6 78 if (!$conversationid = \core_message\api::get_conversation_between_users([$userfrom->id, $userto->id])) {
f2ac0a3e
MN
79 $conversation = \core_message\api::create_conversation(
80 \core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
81 [
82 $userfrom->id,
83 $userto->id
84 ]
85 );
86 $conversationid = $conversation->id;
883ce421
MN
87 }
88
89 // Ok, send the message.
d6731600
FM
90 $record = new stdClass();
91 $record->useridfrom = $userfrom->id;
883ce421 92 $record->conversationid = $conversationid;
d6731600
FM
93 $record->subject = 'No subject';
94 $record->fullmessage = $message;
883ce421 95 $record->smallmessage = $message;
6aa01968 96 $record->timecreated = $time;
4d146f1a 97
883ce421 98 return $DB->insert_record('messages', $record);
d6731600
FM
99 }
100
e6432668 101 /**
f219eac7 102 * Test send_instant_messages.
e6432668
JM
103 */
104 public function test_send_instant_messages() {
f219eac7 105 global $DB, $USER;
e6432668 106
f219eac7 107 $this->resetAfterTest();
e6432668 108
7356e732
EL
109 // Transactions used in tests, tell phpunit use alternative reset method.
110 $this->preventResetByRollback();
e6432668 111
f219eac7
MN
112 $user1 = self::getDataGenerator()->create_user();
113 $user2 = self::getDataGenerator()->create_user();
e6432668 114
f219eac7
MN
115 $this->setUser($user1);
116
117 // Create test message data.
118 $message1 = array();
119 $message1['touserid'] = $user2->id;
120 $message1['text'] = 'the message.';
121 $message1['clientmsgid'] = 4;
122 $messages = array($message1);
123
124 $sentmessages = core_message_external::send_instant_messages($messages);
125 $sentmessages = external_api::clean_returnvalue(core_message_external::send_instant_messages_returns(), $sentmessages);
f7dfa9ba
SA
126 $this->assertEquals(
127 get_string('usercantbemessaged', 'message', fullname(\core_user::get_user($message1['touserid']))),
128 array_pop($sentmessages)['errormessage']
129 );
130
131 // Add the user1 as a contact.
132 \core_message\api::add_contact($user1->id, $user2->id);
133
134 // Send message again. Now it should work properly.
135 $sentmessages = core_message_external::send_instant_messages($messages);
136 // We need to execute the return values cleaning process to simulate the web service server.
137 $sentmessages = external_api::clean_returnvalue(core_message_external::send_instant_messages_returns(), $sentmessages);
f219eac7
MN
138
139 $sentmessage = reset($sentmessages);
140
141 $sql = "SELECT m.*, mcm.userid as useridto
142 FROM {messages} m
143 INNER JOIN {message_conversations} mc
144 ON m.conversationid = mc.id
145 INNER JOIN {message_conversation_members} mcm
146 ON mcm.conversationid = mc.id
147 WHERE mcm.userid != ?
148 AND m.id = ?";
149 $themessage = $DB->get_record_sql($sql, [$USER->id, $sentmessage['msgid']]);
150
151 // Confirm that the message was inserted correctly.
152 $this->assertEquals($themessage->useridfrom, $user1->id);
153 $this->assertEquals($themessage->useridto, $message1['touserid']);
154 $this->assertEquals($themessage->smallmessage, $message1['text']);
155 $this->assertEquals($sentmessage['clientmsgid'], $message1['clientmsgid']);
156 }
157
158 /**
159 * Test send_instant_messages to a user who has blocked you.
160 */
161 public function test_send_instant_messages_blocked_user() {
162 global $DB;
163
164 $this->resetAfterTest();
165
166 // Transactions used in tests, tell phpunit use alternative reset method.
167 $this->preventResetByRollback();
e6432668
JM
168
169 $user1 = self::getDataGenerator()->create_user();
f219eac7
MN
170 $user2 = self::getDataGenerator()->create_user();
171
172 $this->setUser($user1);
173
174 \core_message\api::block_user($user2->id, $user1->id);
e6432668
JM
175
176 // Create test message data.
177 $message1 = array();
f219eac7 178 $message1['touserid'] = $user2->id;
e6432668
JM
179 $message1['text'] = 'the message.';
180 $message1['clientmsgid'] = 4;
181 $messages = array($message1);
182
183 $sentmessages = core_message_external::send_instant_messages($messages);
f219eac7 184 $sentmessages = external_api::clean_returnvalue(core_message_external::send_instant_messages_returns(), $sentmessages);
e6432668 185
f219eac7
MN
186 $sentmessage = reset($sentmessages);
187
f7dfa9ba 188 $this->assertEquals(get_string('usercantbemessaged', 'message', fullname($user2)), $sentmessage['errormessage']);
f219eac7
MN
189
190 $this->assertEquals(0, $DB->count_records('messages'));
191 }
192
193 /**
194 * Test send_instant_messages when sending a message to a non-contact who has blocked non-contacts.
195 */
196 public function test_send_instant_messages_block_non_contacts() {
197 global $DB;
198
199 $this->resetAfterTest(true);
200
201 // Transactions used in tests, tell phpunit use alternative reset method.
202 $this->preventResetByRollback();
203
204 $user1 = self::getDataGenerator()->create_user();
205 $user2 = self::getDataGenerator()->create_user();
206
207 $this->setUser($user1);
208
209 // Set the user preference so user 2 does not accept messages from non-contacts.
f7dfa9ba 210 set_user_preference('message_blocknoncontacts', \core_message\api::MESSAGE_PRIVACY_ONLYCONTACTS, $user2);
f219eac7
MN
211
212 // Create test message data.
213 $message1 = array();
214 $message1['touserid'] = $user2->id;
215 $message1['text'] = 'the message.';
216 $message1['clientmsgid'] = 4;
217 $messages = array($message1);
218
219 $sentmessages = core_message_external::send_instant_messages($messages);
220 $sentmessages = external_api::clean_returnvalue(core_message_external::send_instant_messages_returns(), $sentmessages);
221
222 $sentmessage = reset($sentmessages);
223
f7dfa9ba 224 $this->assertEquals(get_string('usercantbemessaged', 'message', fullname($user2)), $sentmessage['errormessage']);
f219eac7
MN
225
226 $this->assertEquals(0, $DB->count_records('messages'));
227 }
228
229 /**
230 * Test send_instant_messages when sending a message to a contact who has blocked non-contacts.
231 */
232 public function test_send_instant_messages_block_non_contacts_but_am_contact() {
233 global $DB, $USER;
234
235 $this->resetAfterTest(true);
236
237 // Transactions used in tests, tell phpunit use alternative reset method.
238 $this->preventResetByRollback();
239
240 $user1 = self::getDataGenerator()->create_user();
241 $user2 = self::getDataGenerator()->create_user();
242
243 $this->setUser($user1);
244
245 // Set the user preference so user 2 does not accept messages from non-contacts.
f7dfa9ba 246 set_user_preference('message_blocknoncontacts', \core_message\api::MESSAGE_PRIVACY_ONLYCONTACTS, $user2);
f219eac7
MN
247
248 \core_message\api::add_contact($user1->id, $user2->id);
249
250 // Create test message data.
251 $message1 = array();
252 $message1['touserid'] = $user2->id;
253 $message1['text'] = 'the message.';
254 $message1['clientmsgid'] = 4;
255 $messages = array($message1);
256
257 $sentmessages = core_message_external::send_instant_messages($messages);
fb695f6e
JM
258 $sentmessages = external_api::clean_returnvalue(core_message_external::send_instant_messages_returns(), $sentmessages);
259
f219eac7
MN
260 $sentmessage = reset($sentmessages);
261
883ce421
MN
262 $sql = "SELECT m.*, mcm.userid as useridto
263 FROM {messages} m
264 INNER JOIN {message_conversations} mc
265 ON m.conversationid = mc.id
266 INNER JOIN {message_conversation_members} mcm
267 ON mcm.conversationid = mc.id
268 WHERE mcm.userid != ?
269 AND m.id = ?";
f219eac7 270 $themessage = $DB->get_record_sql($sql, [$USER->id, $sentmessage['msgid']]);
e6432668
JM
271
272 // Confirm that the message was inserted correctly.
f219eac7 273 $this->assertEquals($themessage->useridfrom, $user1->id);
e6432668
JM
274 $this->assertEquals($themessage->useridto, $message1['touserid']);
275 $this->assertEquals($themessage->smallmessage, $message1['text']);
f219eac7
MN
276 $this->assertEquals($sentmessage['clientmsgid'], $message1['clientmsgid']);
277 }
278
279 /**
280 * Test send_instant_messages with no capabilities
281 */
282 public function test_send_instant_messages_no_capability() {
283 global $DB;
284
285 $this->resetAfterTest(true);
286
287 // Transactions used in tests, tell phpunit use alternative reset method.
288 $this->preventResetByRollback();
289
290 $user1 = self::getDataGenerator()->create_user();
291 $user2 = self::getDataGenerator()->create_user();
292
293 $this->setUser($user1);
294
295 // Unset the required capabilities by the external function.
296 $contextid = context_system::instance()->id;
297 $userrole = $DB->get_record('role', array('shortname' => 'user'));
298 $this->unassignUserCapability('moodle/site:sendmessage', $contextid, $userrole->id);
299
300 // Create test message data.
301 $message1 = array();
302 $message1['touserid'] = $user2->id;
303 $message1['text'] = 'the message.';
304 $message1['clientmsgid'] = 4;
305 $messages = array($message1);
306
307 $this->expectException('required_capability_exception');
308 core_message_external::send_instant_messages($messages);
309 }
310
311 /**
312 * Test send_instant_messages when messaging is disabled.
313 */
314 public function test_send_instant_messages_messaging_disabled() {
315 global $CFG;
316
317 $this->resetAfterTest(true);
318
319 // Transactions used in tests, tell phpunit use alternative reset method.
320 $this->preventResetByRollback();
321
322 $user1 = self::getDataGenerator()->create_user();
323 $user2 = self::getDataGenerator()->create_user();
324
325 $this->setUser($user1);
326
327 // Disable messaging.
328 $CFG->messaging = 0;
329
330 // Create test message data.
331 $message1 = array();
332 $message1['touserid'] = $user2->id;
333 $message1['text'] = 'the message.';
334 $message1['clientmsgid'] = 4;
335 $messages = array($message1);
336
337 $this->expectException('moodle_exception');
338 core_message_external::send_instant_messages($messages);
e6432668 339 }
d6731600
FM
340
341 /**
342 * Test create_contacts.
343 */
344 public function test_create_contacts() {
345 $this->resetAfterTest(true);
346
347 $user1 = self::getDataGenerator()->create_user();
348 $user2 = self::getDataGenerator()->create_user();
349 $user3 = self::getDataGenerator()->create_user();
350 $user4 = self::getDataGenerator()->create_user();
351 $user5 = self::getDataGenerator()->create_user();
352 $this->setUser($user1);
353
354 // Adding a contact.
355 $return = core_message_external::create_contacts(array($user2->id));
f219eac7 356 $this->assertDebuggingCalled();
d6731600
FM
357 $return = external_api::clean_returnvalue(core_message_external::create_contacts_returns(), $return);
358 $this->assertEquals(array(), $return);
359
360 // Adding a contact who is already a contact.
361 $return = core_message_external::create_contacts(array($user2->id));
f219eac7 362 $this->assertDebuggingCalled();
d6731600
FM
363 $return = external_api::clean_returnvalue(core_message_external::create_contacts_returns(), $return);
364 $this->assertEquals(array(), $return);
365
366 // Adding multiple contacts.
367 $return = core_message_external::create_contacts(array($user3->id, $user4->id));
f219eac7 368 $this->assertDebuggingCalledCount(2);
d6731600
FM
369 $return = external_api::clean_returnvalue(core_message_external::create_contacts_returns(), $return);
370 $this->assertEquals(array(), $return);
371
372 // Adding a non-existing user.
373 $return = core_message_external::create_contacts(array(99999));
f219eac7 374 $this->assertDebuggingCalled();
d6731600
FM
375 $return = external_api::clean_returnvalue(core_message_external::create_contacts_returns(), $return);
376 $this->assertCount(1, $return);
377 $return = array_pop($return);
378 $this->assertEquals($return['warningcode'], 'contactnotcreated');
379 $this->assertEquals($return['itemid'], 99999);
380
381 // Adding contacts with valid and invalid parameters.
382 $return = core_message_external::create_contacts(array($user5->id, 99999));
f219eac7 383 $this->assertDebuggingCalledCount(2);
d6731600
FM
384 $return = external_api::clean_returnvalue(core_message_external::create_contacts_returns(), $return);
385 $this->assertCount(1, $return);
386 $return = array_pop($return);
387 $this->assertEquals($return['warningcode'], 'contactnotcreated');
388 $this->assertEquals($return['itemid'], 99999);
343ba16c
SL
389
390 // Try to add a contact to another user, should throw an exception.
391 // All assertions must be added before this point.
392 $this->expectException('required_capability_exception');
393 core_message_external::create_contacts(array($user2->id), $user3->id);
d6731600
FM
394 }
395
396 /**
397 * Test delete_contacts.
398 */
399 public function test_delete_contacts() {
400 $this->resetAfterTest(true);
401
402 $user1 = self::getDataGenerator()->create_user();
403 $user2 = self::getDataGenerator()->create_user();
404 $user3 = self::getDataGenerator()->create_user();
405 $user4 = self::getDataGenerator()->create_user();
406 $user5 = self::getDataGenerator()->create_user();
407 $user6 = self::getDataGenerator()->create_user();
408 $this->setUser($user1);
f219eac7
MN
409
410 \core_message\api::add_contact($user1->id, $user3->id);
411 \core_message\api::add_contact($user1->id, $user4->id);
412 \core_message\api::add_contact($user1->id, $user5->id);
413 \core_message\api::add_contact($user1->id, $user6->id);
d6731600
FM
414
415 // Removing a non-contact.
416 $return = core_message_external::delete_contacts(array($user2->id));
417 $this->assertNull($return);
418
419 // Removing one contact.
420 $return = core_message_external::delete_contacts(array($user3->id));
421 $this->assertNull($return);
422
423 // Removing multiple contacts.
424 $return = core_message_external::delete_contacts(array($user4->id, $user5->id));
425 $this->assertNull($return);
426
427 // Removing contact from unexisting user.
428 $return = core_message_external::delete_contacts(array(99999));
429 $this->assertNull($return);
430
431 // Removing mixed valid and invalid data.
432 $return = core_message_external::delete_contacts(array($user6->id, 99999));
433 $this->assertNull($return);
343ba16c
SL
434
435 // Try to delete a contact of another user contact list, should throw an exception.
436 // All assertions must be added before this point.
437 $this->expectException('required_capability_exception');
438 core_message_external::delete_contacts(array($user2->id), $user3->id);
d6731600
FM
439 }
440
441 /**
442 * Test block_contacts.
443 */
444 public function test_block_contacts() {
445 $this->resetAfterTest(true);
446
447 $user1 = self::getDataGenerator()->create_user();
448 $user2 = self::getDataGenerator()->create_user();
449 $user3 = self::getDataGenerator()->create_user();
450 $user4 = self::getDataGenerator()->create_user();
451 $user5 = self::getDataGenerator()->create_user();
452 $this->setUser($user1);
f219eac7
MN
453
454 \core_message\api::add_contact($user1->id, $user3->id);
455 \core_message\api::add_contact($user1->id, $user4->id);
456 \core_message\api::add_contact($user1->id, $user5->id);
d6731600
FM
457
458 // Blocking a contact.
459 $return = core_message_external::block_contacts(array($user2->id));
f219eac7 460 $this->assertDebuggingCalled();
d6731600
FM
461 $return = external_api::clean_returnvalue(core_message_external::block_contacts_returns(), $return);
462 $this->assertEquals(array(), $return);
463
464 // Blocking a contact who is already a contact.
465 $return = core_message_external::block_contacts(array($user2->id));
f219eac7 466 $this->assertDebuggingCalled();
d6731600
FM
467 $return = external_api::clean_returnvalue(core_message_external::block_contacts_returns(), $return);
468 $this->assertEquals(array(), $return);
469
470 // Blocking multiple contacts.
471 $return = core_message_external::block_contacts(array($user3->id, $user4->id));
f219eac7 472 $this->assertDebuggingCalledCount(2);
d6731600
FM
473 $return = external_api::clean_returnvalue(core_message_external::block_contacts_returns(), $return);
474 $this->assertEquals(array(), $return);
475
476 // Blocking a non-existing user.
477 $return = core_message_external::block_contacts(array(99999));
f219eac7 478 $this->assertDebuggingCalled();
d6731600
FM
479 $return = external_api::clean_returnvalue(core_message_external::block_contacts_returns(), $return);
480 $this->assertCount(1, $return);
481 $return = array_pop($return);
482 $this->assertEquals($return['warningcode'], 'contactnotblocked');
483 $this->assertEquals($return['itemid'], 99999);
484
485 // Blocking contacts with valid and invalid parameters.
486 $return = core_message_external::block_contacts(array($user5->id, 99999));
f219eac7 487 $this->assertDebuggingCalledCount(2);
d6731600
FM
488 $return = external_api::clean_returnvalue(core_message_external::block_contacts_returns(), $return);
489 $this->assertCount(1, $return);
490 $return = array_pop($return);
491 $this->assertEquals($return['warningcode'], 'contactnotblocked');
492 $this->assertEquals($return['itemid'], 99999);
343ba16c
SL
493
494 // Try to block a contact of another user contact list, should throw an exception.
495 // All assertions must be added before this point.
496 $this->expectException('required_capability_exception');
497 core_message_external::block_contacts(array($user2->id), $user3->id);
d6731600
FM
498 }
499
500 /**
501 * Test unblock_contacts.
502 */
503 public function test_unblock_contacts() {
504 $this->resetAfterTest(true);
505
506 $user1 = self::getDataGenerator()->create_user();
507 $user2 = self::getDataGenerator()->create_user();
508 $user3 = self::getDataGenerator()->create_user();
509 $user4 = self::getDataGenerator()->create_user();
510 $user5 = self::getDataGenerator()->create_user();
511 $user6 = self::getDataGenerator()->create_user();
512 $this->setUser($user1);
f219eac7
MN
513
514 \core_message\api::add_contact($user1->id, $user3->id);
515 \core_message\api::add_contact($user1->id, $user4->id);
516 \core_message\api::add_contact($user1->id, $user5->id);
517 \core_message\api::add_contact($user1->id, $user6->id);
d6731600
FM
518
519 // Removing a non-contact.
520 $return = core_message_external::unblock_contacts(array($user2->id));
f219eac7 521 $this->assertDebuggingCalled();
d6731600
FM
522 $this->assertNull($return);
523
524 // Removing one contact.
525 $return = core_message_external::unblock_contacts(array($user3->id));
f219eac7 526 $this->assertDebuggingCalled();
d6731600
FM
527 $this->assertNull($return);
528
529 // Removing multiple contacts.
530 $return = core_message_external::unblock_contacts(array($user4->id, $user5->id));
f219eac7 531 $this->assertDebuggingCalledCount(2);
d6731600
FM
532 $this->assertNull($return);
533
534 // Removing contact from unexisting user.
535 $return = core_message_external::unblock_contacts(array(99999));
f219eac7 536 $this->assertDebuggingCalled();
d6731600
FM
537 $this->assertNull($return);
538
539 // Removing mixed valid and invalid data.
540 $return = core_message_external::unblock_contacts(array($user6->id, 99999));
f219eac7 541 $this->assertDebuggingCalledCount(2);
d6731600
FM
542 $this->assertNull($return);
543
343ba16c
SL
544 // Try to unblock a contact of another user contact list, should throw an exception.
545 // All assertions must be added before this point.
546 $this->expectException('required_capability_exception');
547 core_message_external::unblock_contacts(array($user2->id), $user3->id);
f219eac7 548 $this->assertDebuggingCalled();
d6731600
FM
549 }
550
52284186
MN
551 /**
552 * Test getting contact requests.
553 */
554 public function test_get_contact_requests() {
0866b336
RW
555 global $PAGE;
556
52284186
MN
557 $this->resetAfterTest();
558
559 $user1 = self::getDataGenerator()->create_user();
560 $user2 = self::getDataGenerator()->create_user();
561 $user3 = self::getDataGenerator()->create_user();
562
563 $this->setUser($user1);
564
565 // Block one user, their request should not show up.
566 \core_message\api::block_user($user1->id, $user3->id);
567
568 \core_message\api::create_contact_request($user2->id, $user1->id);
569 \core_message\api::create_contact_request($user3->id, $user1->id);
570
571 $requests = core_message_external::get_contact_requests($user1->id);
572 $requests = external_api::clean_returnvalue(core_message_external::get_contact_requests_returns(), $requests);
573
574 $this->assertCount(1, $requests);
575
576 $request = reset($requests);
0866b336
RW
577 $userpicture = new \user_picture($user2);
578 $profileimageurl = $userpicture->get_url($PAGE)->out(false);
52284186
MN
579
580 $this->assertEquals($user2->id, $request['id']);
daa33803
MN
581 $this->assertEquals(fullname($user2), $request['fullname']);
582 $this->assertArrayHasKey('profileimageurl', $request);
583 $this->assertArrayHasKey('profileimageurlsmall', $request);
584 $this->assertArrayHasKey('isonline', $request);
585 $this->assertArrayHasKey('showonlinestatus', $request);
586 $this->assertArrayHasKey('isblocked', $request);
587 $this->assertArrayHasKey('iscontact', $request);
52284186
MN
588 }
589
b3ce575e
MN
590 /**
591 * Test getting contact requests when there are none.
592 */
593 public function test_get_contact_requests_no_requests() {
594 $this->resetAfterTest();
595
596 $user1 = self::getDataGenerator()->create_user();
597
598 $this->setUser($user1);
599
600 $requests = core_message_external::get_contact_requests($user1->id);
601 $requests = external_api::clean_returnvalue(core_message_external::get_contact_requests_returns(), $requests);
602
603 $this->assertEmpty($requests);
604 }
605
606 /**
607 * Test getting contact requests with limits.
608 */
609 public function test_get_contact_requests_with_limits() {
610 $this->resetAfterTest();
611
612 $user1 = self::getDataGenerator()->create_user();
613 $user2 = self::getDataGenerator()->create_user();
614 $user3 = self::getDataGenerator()->create_user();
615
616 $this->setUser($user1);
617
618 \core_message\api::create_contact_request($user2->id, $user1->id);
619 \core_message\api::create_contact_request($user3->id, $user1->id);
620
621 $requests = core_message_external::get_contact_requests($user1->id, 0, 1);
622 $requests = external_api::clean_returnvalue(core_message_external::get_contact_requests_returns(), $requests);
623
624 $this->assertCount(1, $requests);
52284186
MN
625 }
626
627 /**
628 * Test getting contact requests with messaging disabled.
629 */
630 public function test_get_contact_requests_messaging_disabled() {
631 global $CFG;
632
633 $this->resetAfterTest();
634
635 // Create some skeleton data just so we can call the WS.
636 $user1 = self::getDataGenerator()->create_user();
637
638 $this->setUser($user1);
639
640 // Disable messaging.
641 $CFG->messaging = 0;
642
643 // Ensure an exception is thrown.
644 $this->expectException('moodle_exception');
645 core_message_external::get_contact_requests($user1->id);
646 }
647
648 /**
649 * Test getting contact requests with no permission.
650 */
651 public function test_get_contact_requests_no_permission() {
652 $this->resetAfterTest();
653
654 // Create some skeleton data just so we can call the WS.
655 $user1 = self::getDataGenerator()->create_user();
656 $user2 = self::getDataGenerator()->create_user();
657 $user3 = self::getDataGenerator()->create_user();
658
659 $this->setUser($user3);
660
661 // Ensure an exception is thrown.
662 $this->expectException('required_capability_exception');
663 core_message_external::create_contact_request($user1->id, $user2->id);
664 }
665
7d678923
MN
666 /**
667 * Test getting the number of received contact requests.
668 */
669 public function test_get_received_contact_requests_count() {
670 $this->resetAfterTest();
671
672 $user1 = self::getDataGenerator()->create_user();
673 $user2 = self::getDataGenerator()->create_user();
674 $user3 = self::getDataGenerator()->create_user();
675 $user4 = self::getDataGenerator()->create_user();
676
677 $this->setUser($user1);
678
679 $contactrequestnumber = core_message_external::get_received_contact_requests_count($user1->id);
680 $contactrequestnumber = external_api::clean_returnvalue(
681 core_message_external::get_received_contact_requests_count_returns(), $contactrequestnumber);
682 $this->assertEquals(0, $contactrequestnumber);
683
684 \core_message\api::create_contact_request($user2->id, $user1->id);
685
686 $contactrequestnumber = core_message_external::get_received_contact_requests_count($user1->id);
687 $contactrequestnumber = external_api::clean_returnvalue(
688 core_message_external::get_received_contact_requests_count_returns(), $contactrequestnumber);
689 $this->assertEquals(1, $contactrequestnumber);
690
691 \core_message\api::create_contact_request($user3->id, $user1->id);
692
693 $contactrequestnumber = core_message_external::get_received_contact_requests_count($user1->id);
694 $contactrequestnumber = external_api::clean_returnvalue(
695 core_message_external::get_received_contact_requests_count_returns(), $contactrequestnumber);
696 $this->assertEquals(2, $contactrequestnumber);
697
698 \core_message\api::create_contact_request($user1->id, $user4->id);
699
700 // Web service should ignore sent requests.
701 $contactrequestnumber = core_message_external::get_received_contact_requests_count($user1->id);
702 $contactrequestnumber = external_api::clean_returnvalue(
703 core_message_external::get_received_contact_requests_count_returns(), $contactrequestnumber);
704 $this->assertEquals(2, $contactrequestnumber);
705 }
706
707 /**
708 * Test getting the number of received contact requests with no permissions.
709 */
710 public function test_get_received_contact_requests_count_no_permission() {
711 $this->resetAfterTest();
712
713 // Create some skeleton data just so we can call the WS.
714 $user1 = self::getDataGenerator()->create_user();
715 $user2 = self::getDataGenerator()->create_user();
716
717 $this->setUser($user2);
718
719 // Ensure an exception is thrown.
720 $this->expectException('required_capability_exception');
721 core_message_external::get_received_contact_requests_count($user1->id);
722 }
723
724 /**
725 * Test getting the number of received contact requests with messaging disabled.
726 */
727 public function test_get_received_contact_requests_count_messaging_disabled() {
728 global $CFG;
729
730 $this->resetAfterTest();
731
732 // Create some skeleton data just so we can call the WS.
733 $user1 = self::getDataGenerator()->create_user();
734
735 $this->setUser($user1);
736
737 // Disable messaging.
738 $CFG->messaging = 0;
739
740 // Ensure an exception is thrown.
741 $this->expectException('moodle_exception');
742 core_message_external::get_received_contact_requests_count($user1->id);
743 }
744
52284186
MN
745 /**
746 * Test creating a contact request.
747 */
748 public function test_create_contact_request() {
0d203bbf 749 global $CFG, $DB;
52284186
MN
750
751 $this->resetAfterTest();
752
753 $user1 = self::getDataGenerator()->create_user();
754 $user2 = self::getDataGenerator()->create_user();
755
756 $this->setUser($user1);
757
0d203bbf
MN
758 // Allow users to message anyone site-wide.
759 $CFG->messagingallusers = 1;
760
52284186
MN
761 $return = core_message_external::create_contact_request($user1->id, $user2->id);
762 $return = external_api::clean_returnvalue(core_message_external::create_contact_request_returns(), $return);
0866b336 763 $this->assertEquals([], $return['warnings']);
52284186
MN
764
765 $request = $DB->get_records('message_contact_requests');
766
767 $this->assertCount(1, $request);
768
769 $request = reset($request);
770
0866b336
RW
771 $this->assertEquals($request->id, $return['request']['id']);
772 $this->assertEquals($request->userid, $return['request']['userid']);
773 $this->assertEquals($request->requesteduserid, $return['request']['requesteduserid']);
774 $this->assertEquals($request->timecreated, $return['request']['timecreated']);
52284186
MN
775 }
776
0d203bbf
MN
777 /**
778 * Test creating a contact request when not allowed.
779 */
780 public function test_create_contact_request_not_allowed() {
781 global $CFG;
782
783 $this->resetAfterTest();
784
785 $user1 = self::getDataGenerator()->create_user();
786 $user2 = self::getDataGenerator()->create_user();
787
788 $this->setUser($user1);
789
790 $CFG->messagingallusers = 0;
791
792 $return = core_message_external::create_contact_request($user1->id, $user2->id);
793 $return = external_api::clean_returnvalue(core_message_external::create_contact_request_returns(), $return);
794
0866b336 795 $warning = reset($return['warnings']);
0d203bbf
MN
796
797 $this->assertEquals('user', $warning['item']);
798 $this->assertEquals($user2->id, $warning['itemid']);
799 $this->assertEquals('cannotcreatecontactrequest', $warning['warningcode']);
800 $this->assertEquals('You are unable to create a contact request for this user', $warning['message']);
801 }
802
52284186
MN
803 /**
804 * Test creating a contact request with messaging disabled.
805 */
806 public function test_create_contact_request_messaging_disabled() {
807 global $CFG;
808
809 $this->resetAfterTest();
810
811 // Create some skeleton data just so we can call the WS.
812 $user1 = self::getDataGenerator()->create_user();
813 $user2 = self::getDataGenerator()->create_user();
814
815 $this->setUser($user1);
816
817 // Disable messaging.
818 $CFG->messaging = 0;
819
820 // Ensure an exception is thrown.
821 $this->expectException('moodle_exception');
822 core_message_external::create_contact_request($user1->id, $user2->id);
823 }
824
825 /**
826 * Test creating a contact request with no permission.
827 */
828 public function test_create_contact_request_no_permission() {
829 $this->resetAfterTest();
830
831 // Create some skeleton data just so we can call the WS.
832 $user1 = self::getDataGenerator()->create_user();
833 $user2 = self::getDataGenerator()->create_user();
834 $user3 = self::getDataGenerator()->create_user();
835
836 $this->setUser($user3);
837
838 // Ensure an exception is thrown.
839 $this->expectException('required_capability_exception');
840 core_message_external::create_contact_request($user1->id, $user2->id);
841 }
842
843 /**
844 * Test confirming a contact request.
845 */
846 public function test_confirm_contact_request() {
847 global $DB;
848
849 $this->resetAfterTest();
850
851 $user1 = self::getDataGenerator()->create_user();
852 $user2 = self::getDataGenerator()->create_user();
853
854 $this->setUser($user1);
855
856 \core_message\api::create_contact_request($user1->id, $user2->id);
857
858 $this->setUser($user2);
859
860 $return = core_message_external::confirm_contact_request($user1->id, $user2->id);
861 $return = external_api::clean_returnvalue(core_message_external::confirm_contact_request_returns(), $return);
862 $this->assertEquals(array(), $return);
863
864 $this->assertEquals(0, $DB->count_records('message_contact_requests'));
865
866 $contact = $DB->get_records('message_contacts');
867
868 $this->assertCount(1, $contact);
869
870 $contact = reset($contact);
871
872 $this->assertEquals($user1->id, $contact->userid);
873 $this->assertEquals($user2->id, $contact->contactid);
874 }
875
876 /**
877 * Test confirming a contact request with messaging disabled.
878 */
879 public function test_confirm_contact_request_messaging_disabled() {
880 global $CFG;
881
882 $this->resetAfterTest();
883
884 // Create some skeleton data just so we can call the WS.
885 $user1 = self::getDataGenerator()->create_user();
886 $user2 = self::getDataGenerator()->create_user();
887
888 $this->setUser($user1);
889
890 // Disable messaging.
891 $CFG->messaging = 0;
892
893 // Ensure an exception is thrown.
894 $this->expectException('moodle_exception');
895 core_message_external::confirm_contact_request($user1->id, $user2->id);
896 }
897
898 /**
899 * Test confirming a contact request with no permission.
900 */
901 public function test_confirm_contact_request_no_permission() {
902 $this->resetAfterTest();
903
904 // Create some skeleton data just so we can call the WS.
905 $user1 = self::getDataGenerator()->create_user();
906 $user2 = self::getDataGenerator()->create_user();
907 $user3 = self::getDataGenerator()->create_user();
908
909 $this->setUser($user3);
910
911 // Ensure an exception is thrown.
912 $this->expectException('required_capability_exception');
913 core_message_external::confirm_contact_request($user1->id, $user2->id);
914 }
915
916 /**
917 * Test declining a contact request.
918 */
919 public function test_decline_contact_request() {
920 global $DB;
921
922 $this->resetAfterTest();
923
924 $user1 = self::getDataGenerator()->create_user();
925 $user2 = self::getDataGenerator()->create_user();
926
927 $this->setUser($user1);
928
929 \core_message\api::create_contact_request($user1->id, $user2->id);
930
931 $this->setUser($user2);
932
933 $return = core_message_external::decline_contact_request($user1->id, $user2->id);
934 $return = external_api::clean_returnvalue(core_message_external::decline_contact_request_returns(), $return);
935 $this->assertEquals(array(), $return);
936
937 $this->assertEquals(0, $DB->count_records('message_contact_requests'));
938 $this->assertEquals(0, $DB->count_records('message_contacts'));
939 }
940
941 /**
942 * Test declining a contact request with messaging disabled.
943 */
944 public function test_decline_contact_request_messaging_disabled() {
945 global $CFG;
946
947 $this->resetAfterTest();
948
949 // Create some skeleton data just so we can call the WS.
950 $user1 = self::getDataGenerator()->create_user();
951 $user2 = self::getDataGenerator()->create_user();
952
953 $this->setUser($user1);
954
955 // Disable messaging.
956 $CFG->messaging = 0;
957
958 // Ensure an exception is thrown.
959 $this->expectException('moodle_exception');
960 core_message_external::decline_contact_request($user1->id, $user2->id);
961 }
962
963 /**
964 * Test declining a contact request with no permission.
965 */
966 public function test_decline_contact_request_no_permission() {
967 $this->resetAfterTest();
968
969 // Create some skeleton data just so we can call the WS.
970 $user1 = self::getDataGenerator()->create_user();
971 $user2 = self::getDataGenerator()->create_user();
972 $user3 = self::getDataGenerator()->create_user();
973
974 $this->setUser($user3);
975
976 // Ensure an exception is thrown.
977 $this->expectException('required_capability_exception');
978 core_message_external::decline_contact_request($user1->id, $user2->id);
979 }
980
981 /**
982 * Test blocking a user.
983 */
984 public function test_block_user() {
985 global $DB;
986
987 $this->resetAfterTest(true);
988
989 $user1 = self::getDataGenerator()->create_user();
990 $user2 = self::getDataGenerator()->create_user();
991
992 $this->setUser($user1);
993
994 // Blocking a user.
995 $return = core_message_external::block_user($user1->id, $user2->id);
996 $return = external_api::clean_returnvalue(core_message_external::block_user_returns(), $return);
997 $this->assertEquals(array(), $return);
998
999 // Get list of blocked users.
1000 $record = $DB->get_record('message_users_blocked', []);
1001
1002 $this->assertEquals($user1->id, $record->userid);
1003 $this->assertEquals($user2->id, $record->blockeduserid);
1004
1005 // Blocking a user who is already blocked.
1006 $return = core_message_external::block_user($user1->id, $user2->id);
1007 $return = external_api::clean_returnvalue(core_message_external::block_user_returns(), $return);
1008 $this->assertEquals(array(), $return);
1009
1010 $this->assertEquals(1, $DB->count_records('message_users_blocked'));
1011 }
1012
1013 /**
1014 * Test blocking a user with messaging disabled.
1015 */
1016 public function test_block_user_messaging_disabled() {
1017 global $CFG;
1018
1019 $this->resetAfterTest();
1020
1021 // Create some skeleton data just so we can call the WS.
1022 $user1 = self::getDataGenerator()->create_user();
1023 $user2 = self::getDataGenerator()->create_user();
1024
1025 $this->setUser($user1);
1026
1027 // Disable messaging.
1028 $CFG->messaging = 0;
1029
1030 // Ensure an exception is thrown.
1031 $this->expectException('moodle_exception');
1032 core_message_external::block_user($user1->id, $user2->id);
1033 }
1034
1035 /**
1036 * Test blocking a user with no permission.
1037 */
1038 public function test_block_user_no_permission() {
1039 $this->resetAfterTest();
1040
1041 // Create some skeleton data just so we can call the WS.
1042 $user1 = self::getDataGenerator()->create_user();
1043 $user2 = self::getDataGenerator()->create_user();
1044 $user3 = self::getDataGenerator()->create_user();
1045
1046 $this->setUser($user3);
1047
1048 // Ensure an exception is thrown.
1049 $this->expectException('required_capability_exception');
1050 core_message_external::block_user($user1->id, $user2->id);
1051 }
1052
1053 /**
1054 * Test unblocking a user.
1055 */
1056 public function test_unblock_user() {
1057 global $DB;
1058
1059 $this->resetAfterTest(true);
1060
1061 $user1 = self::getDataGenerator()->create_user();
1062 $user2 = self::getDataGenerator()->create_user();
1063
1064 $this->setUser($user1);
1065
1066 // Block the user.
1067 \core_message\api::block_user($user1->id, $user2->id);
1068
1069 // Unblocking a user.
1070 $return = core_message_external::unblock_user($user1->id, $user2->id);
1071 $return = external_api::clean_returnvalue(core_message_external::unblock_user_returns(), $return);
1072 $this->assertEquals(array(), $return);
1073
1074 $this->assertEquals(0, $DB->count_records('message_users_blocked'));
1075
1076 // Unblocking a user who is already unblocked.
1077 $return = core_message_external::unblock_user($user1->id, $user2->id);
1078 $return = external_api::clean_returnvalue(core_message_external::unblock_user_returns(), $return);
1079 $this->assertEquals(array(), $return);
1080
1081 $this->assertEquals(0, $DB->count_records('message_users_blocked'));
1082 }
1083
1084 /**
1085 * Test unblocking a user with messaging disabled.
1086 */
1087 public function test_unblock_user_messaging_disabled() {
1088 global $CFG;
1089
1090 $this->resetAfterTest();
1091
1092 // Create some skeleton data just so we can call the WS.
1093 $user1 = self::getDataGenerator()->create_user();
1094 $user2 = self::getDataGenerator()->create_user();
1095
1096 $this->setUser($user1);
1097
1098 // Disable messaging.
1099 $CFG->messaging = 0;
1100
1101 // Ensure an exception is thrown.
1102 $this->expectException('moodle_exception');
1103 core_message_external::unblock_user($user1->id, $user2->id);
1104 }
1105
1106 /**
1107 * Test unblocking a user with no permission.
1108 */
1109 public function test_unblock_user_no_permission() {
1110 $this->resetAfterTest();
1111
1112 // Create some skeleton data just so we can call the WS.
1113 $user1 = self::getDataGenerator()->create_user();
1114 $user2 = self::getDataGenerator()->create_user();
1115 $user3 = self::getDataGenerator()->create_user();
1116
1117 $this->setUser($user3);
1118
1119 // Ensure an exception is thrown.
1120 $this->expectException('required_capability_exception');
1121 core_message_external::unblock_user($user1->id, $user2->id);
1122 }
1123
d6731600
FM
1124 /**
1125 * Test get_contacts.
1126 */
1127 public function test_get_contacts() {
1128 $this->resetAfterTest(true);
1129
1130 $user1 = self::getDataGenerator()->create_user();
1131 $user_stranger = self::getDataGenerator()->create_user();
1132 $user_offline1 = self::getDataGenerator()->create_user();
1133 $user_offline2 = self::getDataGenerator()->create_user();
1134 $user_offline3 = self::getDataGenerator()->create_user();
1135 $user_online = new stdClass();
1136 $user_online->lastaccess = time();
1137 $user_online = self::getDataGenerator()->create_user($user_online);
1138 $user_blocked = self::getDataGenerator()->create_user();
0b074e88 1139 $noreplyuser = core_user::get_user(core_user::NOREPLY_USER);
d6731600
FM
1140
1141 // Login as user1.
1142 $this->setUser($user1);
f219eac7
MN
1143 \core_message\api::add_contact($user1->id, $user_offline1->id);
1144 \core_message\api::add_contact($user1->id, $user_offline2->id);
1145 \core_message\api::add_contact($user1->id, $user_offline3->id);
1146 \core_message\api::add_contact($user1->id, $user_online->id);
d6731600
FM
1147
1148 // User_stranger sends a couple of messages to user1.
1149 $this->send_message($user_stranger, $user1, 'Hello there!');
1150 $this->send_message($user_stranger, $user1, 'How you goin?');
1151 $this->send_message($user_stranger, $user1, 'Cya!');
0b074e88 1152 $this->send_message($noreplyuser, $user1, 'I am not a real user');
d6731600
FM
1153
1154 // User_blocked sends a message to user1.
1155 $this->send_message($user_blocked, $user1, 'Here, have some spam.');
1156
1157 // Retrieve the contacts of the user.
1158 $this->setUser($user1);
1159 $contacts = core_message_external::get_contacts();
1160 $contacts = external_api::clean_returnvalue(core_message_external::get_contacts_returns(), $contacts);
1161 $this->assertCount(3, $contacts['offline']);
1162 $this->assertCount(1, $contacts['online']);
0b074e88 1163 $this->assertCount(3, $contacts['strangers']);
d6731600 1164 core_message_external::block_contacts(array($user_blocked->id));
f219eac7 1165 $this->assertDebuggingCalled();
d6731600
FM
1166 $contacts = core_message_external::get_contacts();
1167 $contacts = external_api::clean_returnvalue(core_message_external::get_contacts_returns(), $contacts);
1168 $this->assertCount(3, $contacts['offline']);
1169 $this->assertCount(1, $contacts['online']);
0b074e88 1170 $this->assertCount(2, $contacts['strangers']);
d6731600
FM
1171
1172 // Checking some of the fields returned.
1173 $stranger = array_pop($contacts['strangers']);
01393790 1174
0b074e88
JL
1175 $this->assertEquals(core_user::NOREPLY_USER, $stranger['id']);
1176 $this->assertEquals(1, $stranger['unread']);
01393790
JL
1177
1178 // Check that deleted users are not returned.
1179 delete_user($user_offline1);
1180 delete_user($user_stranger);
1181 delete_user($user_online);
1182 $contacts = core_message_external::get_contacts();
1183 $contacts = external_api::clean_returnvalue(core_message_external::get_contacts_returns(), $contacts);
1184 $this->assertCount(2, $contacts['offline']);
1185 $this->assertCount(0, $contacts['online']);
1186 $this->assertCount(1, $contacts['strangers']);
d6731600
FM
1187 }
1188
1189 /**
1190 * Test search_contacts.
52f3e060 1191 * @expectedException moodle_exception
d6731600
FM
1192 */
1193 public function test_search_contacts() {
1194 global $DB;
1195 $this->resetAfterTest(true);
1196
1197 $course1 = $this->getDataGenerator()->create_course();
1198 $course2 = $this->getDataGenerator()->create_course();
1199
1200 $user1 = new stdClass();
1201 $user1->firstname = 'X';
1202 $user1->lastname = 'X';
1203 $user1 = $this->getDataGenerator()->create_user($user1);
1204 $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
1205 $this->getDataGenerator()->enrol_user($user1->id, $course2->id);
1206
1207 $user2 = new stdClass();
1208 $user2->firstname = 'Eric';
1209 $user2->lastname = 'Cartman';
1210 $user2 = self::getDataGenerator()->create_user($user2);
1211 $user3 = new stdClass();
1212 $user3->firstname = 'Stan';
1213 $user3->lastname = 'Marsh';
1214 $user3 = self::getDataGenerator()->create_user($user3);
1215 self::getDataGenerator()->enrol_user($user3->id, $course1->id);
1216 $user4 = new stdClass();
1217 $user4->firstname = 'Kyle';
1218 $user4->lastname = 'Broflovski';
1219 $user4 = self::getDataGenerator()->create_user($user4);
1220 $user5 = new stdClass();
1221 $user5->firstname = 'Kenny';
1222 $user5->lastname = 'McCormick';
1223 $user5 = self::getDataGenerator()->create_user($user5);
1224 self::getDataGenerator()->enrol_user($user5->id, $course2->id);
1225
d6731600 1226 $this->setUser($user1);
2e2d1977 1227
d6731600
FM
1228 $results = core_message_external::search_contacts('r');
1229 $results = external_api::clean_returnvalue(core_message_external::search_contacts_returns(), $results);
2e2d1977
AD
1230 $this->assertCount(5, $results); // Users 2 through 5 + admin
1231
d6731600
FM
1232 $results = core_message_external::search_contacts('r', true);
1233 $results = external_api::clean_returnvalue(core_message_external::search_contacts_returns(), $results);
1234 $this->assertCount(2, $results);
2e2d1977 1235
d6731600
FM
1236 $results = core_message_external::search_contacts('Kyle', false);
1237 $results = external_api::clean_returnvalue(core_message_external::search_contacts_returns(), $results);
1238 $this->assertCount(1, $results);
1239 $result = reset($results);
1240 $this->assertEquals($user4->id, $result['id']);
2e2d1977 1241
d6731600
FM
1242 $results = core_message_external::search_contacts('y', false);
1243 $results = external_api::clean_returnvalue(core_message_external::search_contacts_returns(), $results);
1244 $this->assertCount(2, $results);
2e2d1977 1245
d6731600
FM
1246 $results = core_message_external::search_contacts('y', true);
1247 $results = external_api::clean_returnvalue(core_message_external::search_contacts_returns(), $results);
1248 $this->assertCount(1, $results);
1249 $result = reset($results);
1250 $this->assertEquals($user5->id, $result['id']);
1251
1252 // Empty query, will throw an exception.
d6731600
FM
1253 $results = core_message_external::search_contacts('');
1254 }
6ff4464b
JL
1255
1256 /**
1257 * Test get_messages.
1258 */
1259 public function test_get_messages() {
ea21d637 1260 global $CFG, $DB;
6ff4464b
JL
1261 $this->resetAfterTest(true);
1262
1263 $this->preventResetByRollback();
1264 // This mark the messages as read!.
1265 $sink = $this->redirectMessages();
1266
1267 $user1 = self::getDataGenerator()->create_user();
1268 $user2 = self::getDataGenerator()->create_user();
1269 $user3 = self::getDataGenerator()->create_user();
1270
1271 $course = self::getDataGenerator()->create_course();
1272
1273 // Send a message from one user to another.
1274 message_post_message($user1, $user2, 'some random text 1', FORMAT_MOODLE);
1275 message_post_message($user1, $user3, 'some random text 2', FORMAT_MOODLE);
1276 message_post_message($user2, $user3, 'some random text 3', FORMAT_MOODLE);
1277 message_post_message($user3, $user2, 'some random text 4', FORMAT_MOODLE);
1278 message_post_message($user3, $user1, 'some random text 5', FORMAT_MOODLE);
1279
1280 $this->setUser($user1);
1281 // Get read conversations from user1 to user2.
1282 $messages = core_message_external::get_messages($user2->id, $user1->id, 'conversations', true, true, 0, 0);
1283 $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
1284 $this->assertCount(1, $messages['messages']);
1285
ea21d637
JL
1286 // Delete the message.
1287 $message = array_shift($messages['messages']);
883ce421 1288 \core_message\api::delete_message($user1->id, $message['id']);
ea21d637
JL
1289
1290 $messages = core_message_external::get_messages($user2->id, $user1->id, 'conversations', true, true, 0, 0);
1291 $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
1292 $this->assertCount(0, $messages['messages']);
1293
6ff4464b
JL
1294 // Get unread conversations from user1 to user2.
1295 $messages = core_message_external::get_messages($user2->id, $user1->id, 'conversations', false, true, 0, 0);
1296 $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
1297 $this->assertCount(0, $messages['messages']);
1298
1299 // Get read messages send from user1.
1300 $messages = core_message_external::get_messages(0, $user1->id, 'conversations', true, true, 0, 0);
1301 $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
ea21d637 1302 $this->assertCount(1, $messages['messages']);
6ff4464b
JL
1303
1304 $this->setUser($user2);
1305 // Get read conversations from any user to user2.
1306 $messages = core_message_external::get_messages($user2->id, 0, 'conversations', true, true, 0, 0);
1307 $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
1308 $this->assertCount(2, $messages['messages']);
1309
ea21d637
JL
1310 // Conversations from user3 to user2.
1311 $messages = core_message_external::get_messages($user2->id, $user3->id, 'conversations', true, true, 0, 0);
1312 $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
1313 $this->assertCount(1, $messages['messages']);
1314
1315 // Delete the message.
1316 $message = array_shift($messages['messages']);
883ce421 1317 \core_message\api::delete_message($user2->id, $message['id']);
ea21d637
JL
1318
1319 $messages = core_message_external::get_messages($user2->id, $user3->id, 'conversations', true, true, 0, 0);
1320 $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
1321 $this->assertCount(0, $messages['messages']);
1322
6ff4464b
JL
1323 $this->setUser($user3);
1324 // Get read notifications received by user3.
1325 $messages = core_message_external::get_messages($user3->id, 0, 'notifications', true, true, 0, 0);
1326 $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
1327 $this->assertCount(0, $messages['messages']);
1328
1329 // Now, create some notifications...
1330 // We are creating fake notifications but based on real ones.
1331
39d2c688 1332 // This one comes from a disabled plugin's provider and therefore is not sent.
cc350fd9
AD
1333 $eventdata = new \core\message\message();
1334 $eventdata->courseid = $course->id;
39d2c688 1335 $eventdata->notification = 1;
6ff4464b
JL
1336 $eventdata->modulename = 'moodle';
1337 $eventdata->component = 'enrol_paypal';
1338 $eventdata->name = 'paypal_enrolment';
1339 $eventdata->userfrom = get_admin();
1340 $eventdata->userto = $user1;
1341 $eventdata->subject = "Moodle: PayPal payment";
1342 $eventdata->fullmessage = "Your PayPal payment is pending.";
1343 $eventdata->fullmessageformat = FORMAT_PLAIN;
1344 $eventdata->fullmessagehtml = '';
1345 $eventdata->smallmessage = '';
1346 message_send($eventdata);
39d2c688
DM
1347 $this->assertDebuggingCalled('Attempt to send msg from a provider enrol_paypal/paypal_enrolment '.
1348 'that is inactive or not allowed for the user id='.$user1->id);
1349
1350 // This one omits notification = 1.
1351 $message = new \core\message\message();
1352 $message->courseid = $course->id;
1353 $message->component = 'enrol_manual';
1354 $message->name = 'expiry_notification';
1355 $message->userfrom = $user2;
1356 $message->userto = $user1;
1357 $message->subject = 'Test: This is not a notification but otherwise is valid';
1358 $message->fullmessage = 'Test: Full message';
1359 $message->fullmessageformat = FORMAT_MARKDOWN;
1360 $message->fullmessagehtml = markdown_to_html($message->fullmessage);
1361 $message->smallmessage = $message->subject;
1362 $message->contexturlname = $course->fullname;
1363 $message->contexturl = (string)new moodle_url('/course/view.php', array('id' => $course->id));
1364 message_send($message);
6ff4464b 1365
cc350fd9 1366 $message = new \core\message\message();
880fc15b 1367 $message->courseid = $course->id;
6ff4464b
JL
1368 $message->notification = 1;
1369 $message->component = 'enrol_manual';
1370 $message->name = 'expiry_notification';
1371 $message->userfrom = $user2;
1372 $message->userto = $user1;
1373 $message->subject = 'Enrolment expired';
1374 $message->fullmessage = 'Enrolment expired blah blah blah';
1375 $message->fullmessageformat = FORMAT_MARKDOWN;
1376 $message->fullmessagehtml = markdown_to_html($message->fullmessage);
1377 $message->smallmessage = $message->subject;
1378 $message->contexturlname = $course->fullname;
1379 $message->contexturl = (string)new moodle_url('/course/view.php', array('id' => $course->id));
1380 message_send($message);
1381
1382 $userfrom = core_user::get_noreply_user();
1383 $userfrom->maildisplay = true;
0e8b5160
EM
1384 $eventdata = new \core\message\message();
1385 $eventdata->courseid = $course->id;
6ff4464b
JL
1386 $eventdata->component = 'moodle';
1387 $eventdata->name = 'badgecreatornotice';
1388 $eventdata->userfrom = $userfrom;
1389 $eventdata->userto = $user1;
1390 $eventdata->notification = 1;
1391 $eventdata->subject = 'New badge';
1392 $eventdata->fullmessage = format_text_email($eventdata->subject, FORMAT_HTML);
1393 $eventdata->fullmessageformat = FORMAT_PLAIN;
1394 $eventdata->fullmessagehtml = $eventdata->subject;
1395 $eventdata->smallmessage = $eventdata->subject;
1396 message_send($eventdata);
1397
cc350fd9
AD
1398 $eventdata = new \core\message\message();
1399 $eventdata->courseid = $course->id;
6ff4464b
JL
1400 $eventdata->name = 'submission';
1401 $eventdata->component = 'mod_feedback';
1402 $eventdata->userfrom = $user1;
1403 $eventdata->userto = $user2;
1404 $eventdata->subject = 'Feedback submitted';
1405 $eventdata->fullmessage = 'Feedback submitted from an user';
1406 $eventdata->fullmessageformat = FORMAT_PLAIN;
1407 $eventdata->fullmessagehtml = '<strong>Feedback submitted</strong>';
1408 $eventdata->smallmessage = '';
1409 message_send($eventdata);
1410
1411 $this->setUser($user1);
1412 // Get read notifications from any user to user1.
1413 $messages = core_message_external::get_messages($user1->id, 0, 'notifications', true, true, 0, 0);
1414 $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
1415 $this->assertCount(3, $messages['messages']);
1416
1417 // Get one read notifications from any user to user1.
1418 $messages = core_message_external::get_messages($user1->id, 0, 'notifications', true, true, 0, 1);
1419 $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
1420 $this->assertCount(1, $messages['messages']);
1421
1422 // Get unread notifications from any user to user1.
1423 $messages = core_message_external::get_messages($user1->id, 0, 'notifications', false, true, 0, 0);
1424 $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
1425 $this->assertCount(0, $messages['messages']);
1426
1427 // Get read both type of messages from any user to user1.
1428 $messages = core_message_external::get_messages($user1->id, 0, 'both', true, true, 0, 0);
1429 $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
1430 $this->assertCount(4, $messages['messages']);
1431
1432 // Get read notifications from no-reply-user to user1.
1433 $messages = core_message_external::get_messages($user1->id, $userfrom->id, 'notifications', true, true, 0, 0);
1434 $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
1435 $this->assertCount(1, $messages['messages']);
1436
1437 // Get notifications send by user1 to any user.
1438 $messages = core_message_external::get_messages(0, $user1->id, 'notifications', true, true, 0, 0);
1439 $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
1440 $this->assertCount(1, $messages['messages']);
1441
1442 // Test warnings.
1443 $CFG->messaging = 0;
1444
1445 $messages = core_message_external::get_messages(0, $user1->id, 'both', true, true, 0, 0);
1446 $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
1447 $this->assertCount(1, $messages['warnings']);
1448
1449 // Test exceptions.
1450
1451 // Messaging disabled.
1452 try {
1453 $messages = core_message_external::get_messages(0, $user1->id, 'conversations', true, true, 0, 0);
1454 $this->fail('Exception expected due messaging disabled.');
1455 } catch (moodle_exception $e) {
1456 $this->assertEquals('disabled', $e->errorcode);
1457 }
1458
1459 $CFG->messaging = 1;
1460
1461 // Invalid users.
1462 try {
1463 $messages = core_message_external::get_messages(0, 0, 'conversations', true, true, 0, 0);
1464 $this->fail('Exception expected due invalid users.');
1465 } catch (moodle_exception $e) {
1466 $this->assertEquals('accessdenied', $e->errorcode);
1467 }
1468
1469 // Invalid user ids.
1470 try {
1471 $messages = core_message_external::get_messages(2500, 0, 'conversations', true, true, 0, 0);
1472 $this->fail('Exception expected due invalid users.');
1473 } catch (moodle_exception $e) {
1474 $this->assertEquals('invaliduser', $e->errorcode);
1475 }
1476
1477 // Invalid users (permissions).
1478 $this->setUser($user2);
1479 try {
1480 $messages = core_message_external::get_messages(0, $user1->id, 'conversations', true, true, 0, 0);
1481 $this->fail('Exception expected due invalid user.');
1482 } catch (moodle_exception $e) {
1483 $this->assertEquals('accessdenied', $e->errorcode);
1484 }
1485
1486 }
ff1f3739 1487
c57fadcc
MN
1488 /**
1489 * Test get_messages where we want all messages from a user, sent to any user.
1490 */
1491 public function test_get_messages_useridto_all() {
1492 $this->resetAfterTest(true);
1493
1494 $user1 = self::getDataGenerator()->create_user();
1495 $user2 = self::getDataGenerator()->create_user();
1496 $user3 = self::getDataGenerator()->create_user();
1497
1498 $this->setUser($user1);
1499
1500 // Send a message from user 1 to two other users.
1501 $this->send_message($user1, $user2, 'some random text 1', 0, 1);
1502 $this->send_message($user1, $user3, 'some random text 2', 0, 2);
1503
1504 // Get messages sent from user 1.
1505 $messages = core_message_external::get_messages(0, $user1->id, 'conversations', false, false, 0, 0);
1506 $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
1507
1508 // Confirm the data is correct.
1509 $messages = $messages['messages'];
1510 $this->assertCount(2, $messages);
1511
1512 $message1 = array_shift($messages);
1513 $message2 = array_shift($messages);
1514
1515 $this->assertEquals($user1->id, $message1['useridfrom']);
1516 $this->assertEquals($user2->id, $message1['useridto']);
1517
1518 $this->assertEquals($user1->id, $message2['useridfrom']);
1519 $this->assertEquals($user3->id, $message2['useridto']);
1520 }
1521
1522 /**
1523 * Test get_messages where we want all messages to a user, sent by any user.
1524 */
1525 public function test_get_messages_useridfrom_all() {
1526 $this->resetAfterTest();
1527
1528 $user1 = self::getDataGenerator()->create_user();
1529 $user2 = self::getDataGenerator()->create_user();
1530 $user3 = self::getDataGenerator()->create_user();
1531
1532 $this->setUser($user1);
1533
1534 // Send a message to user 1 from two other users.
1535 $this->send_message($user2, $user1, 'some random text 1', 0, 1);
1536 $this->send_message($user3, $user1, 'some random text 2', 0, 2);
1537
1538 // Get messages sent to user 1.
1539 $messages = core_message_external::get_messages($user1->id, 0, 'conversations', false, false, 0, 0);
1540 $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
1541
1542 // Confirm the data is correct.
1543 $messages = $messages['messages'];
1544 $this->assertCount(2, $messages);
1545
1546 $message1 = array_shift($messages);
1547 $message2 = array_shift($messages);
1548
1549 $this->assertEquals($user2->id, $message1['useridfrom']);
1550 $this->assertEquals($user1->id, $message1['useridto']);
1551
1552 $this->assertEquals($user3->id, $message2['useridfrom']);
1553 $this->assertEquals($user1->id, $message2['useridto']);
1554 }
1555
ff1f3739
JL
1556 /**
1557 * Test get_blocked_users.
1558 */
1559 public function test_get_blocked_users() {
1560 $this->resetAfterTest(true);
1561
1562 $user1 = self::getDataGenerator()->create_user();
1563 $userstranger = self::getDataGenerator()->create_user();
1564 $useroffline1 = self::getDataGenerator()->create_user();
1565 $useroffline2 = self::getDataGenerator()->create_user();
1566 $userblocked = self::getDataGenerator()->create_user();
1567
1568 // Login as user1.
1569 $this->setUser($user1);
f219eac7
MN
1570
1571 \core_message\api::add_contact($user1->id, $useroffline1->id);
1572 \core_message\api::add_contact($user1->id, $useroffline2->id);
ff1f3739
JL
1573
1574 // The userstranger sends a couple of messages to user1.
1575 $this->send_message($userstranger, $user1, 'Hello there!');
1576 $this->send_message($userstranger, $user1, 'How you goin?');
1577
1578 // The userblocked sends a message to user1.
1579 // Note that this user is not blocked at this point.
1580 $this->send_message($userblocked, $user1, 'Here, have some spam.');
1581
1582 // Retrieve the list of blocked users.
1583 $this->setUser($user1);
1584 $blockedusers = core_message_external::get_blocked_users($user1->id);
1585 $blockedusers = external_api::clean_returnvalue(core_message_external::get_blocked_users_returns(), $blockedusers);
1586 $this->assertCount(0, $blockedusers['users']);
1587
1588 // Block the $userblocked and retrieve again the list.
1589 core_message_external::block_contacts(array($userblocked->id));
f219eac7 1590 $this->assertDebuggingCalled();
ff1f3739
JL
1591 $blockedusers = core_message_external::get_blocked_users($user1->id);
1592 $blockedusers = external_api::clean_returnvalue(core_message_external::get_blocked_users_returns(), $blockedusers);
1593 $this->assertCount(1, $blockedusers['users']);
1594
01393790
JL
1595 // Remove the $userblocked and check that the list now is empty.
1596 delete_user($userblocked);
1597 $blockedusers = core_message_external::get_blocked_users($user1->id);
1598 $blockedusers = external_api::clean_returnvalue(core_message_external::get_blocked_users_returns(), $blockedusers);
1599 $this->assertCount(0, $blockedusers['users']);
ff1f3739
JL
1600 }
1601
b6795827
JL
1602 /**
1603 * Test mark_message_read.
1604 */
1605 public function test_mark_message_read() {
1606 $this->resetAfterTest(true);
1607
1608 $user1 = self::getDataGenerator()->create_user();
1609 $user2 = self::getDataGenerator()->create_user();
1610 $user3 = self::getDataGenerator()->create_user();
1611
1612 // Login as user1.
1613 $this->setUser($user1);
f219eac7
MN
1614 \core_message\api::add_contact($user1->id, $user2->id);
1615 \core_message\api::add_contact($user1->id, $user3->id);
b6795827
JL
1616
1617 // The user2 sends a couple of messages to user1.
1618 $this->send_message($user2, $user1, 'Hello there!');
1619 $this->send_message($user2, $user1, 'How you goin?');
1620 $this->send_message($user3, $user1, 'How you goin?');
1621 $this->send_message($user3, $user2, 'How you goin?');
1622
1623 // Retrieve all messages sent by user2 (they are currently unread).
1624 $lastmessages = message_get_messages($user1->id, $user2->id, 0, false);
1625
1626 $messageids = array();
1627 foreach ($lastmessages as $m) {
1628 $messageid = core_message_external::mark_message_read($m->id, time());
1629 $messageids[] = external_api::clean_returnvalue(core_message_external::mark_message_read_returns(), $messageid);
1630 }
1631
1632 // Retrieve all messages sent (they are currently read).
1633 $lastmessages = message_get_messages($user1->id, $user2->id, 0, true);
1634 $this->assertCount(2, $lastmessages);
1635 $this->assertArrayHasKey($messageids[0]['messageid'], $lastmessages);
1636 $this->assertArrayHasKey($messageids[1]['messageid'], $lastmessages);
1637
1638 // Retrieve all messages sent by any user (that are currently unread).
1639 $lastmessages = message_get_messages($user1->id, 0, 0, false);
1640 $this->assertCount(1, $lastmessages);
1641
1642 // Invalid message ids.
1643 try {
883ce421 1644 $messageid = core_message_external::mark_message_read(1337, time());
b6795827
JL
1645 $this->fail('Exception expected due invalid messageid.');
1646 } catch (dml_missing_record_exception $e) {
883ce421 1647 $this->assertEquals('invalidrecordunknown', $e->errorcode);
b6795827
JL
1648 }
1649
1650 // A message to a different user.
1651 $lastmessages = message_get_messages($user2->id, $user3->id, 0, false);
1652 $messageid = array_pop($lastmessages)->id;
1653 try {
1654 $messageid = core_message_external::mark_message_read($messageid, time());
1655 $this->fail('Exception expected due invalid messageid.');
1656 } catch (invalid_parameter_exception $e) {
1657 $this->assertEquals('invalidparameter', $e->errorcode);
1658 }
2b595d96
MN
1659 }
1660
1661 /**
1662 * Test mark_notification_read.
1663 */
1664 public function test_mark_notification_read() {
1665 $this->resetAfterTest(true);
1666
1667 $user1 = self::getDataGenerator()->create_user();
1668 $user2 = self::getDataGenerator()->create_user();
1669 $user3 = self::getDataGenerator()->create_user();
1670
1671 // Login as user1.
1672 $this->setUser($user1);
f219eac7
MN
1673 \core_message\api::add_contact($user1->id, $user2->id);
1674 \core_message\api::add_contact($user1->id, $user3->id);
b6795827 1675
2b595d96
MN
1676 // The user2 sends a couple of notifications to user1.
1677 $this->send_message($user2, $user1, 'Hello there!', 1);
1678 $this->send_message($user2, $user1, 'How you goin?', 1);
1679 $this->send_message($user3, $user1, 'How you goin?', 1);
1680 $this->send_message($user3, $user2, 'How you goin?', 1);
1681
1682 // Retrieve all notifications sent by user2 (they are currently unread).
1683 $lastnotifications = message_get_messages($user1->id, $user2->id, 1, false);
1684
1685 $notificationids = array();
1686 foreach ($lastnotifications as $n) {
1687 $notificationid = core_message_external::mark_notification_read($n->id, time());
1688 $notificationids[] = external_api::clean_returnvalue(core_message_external::mark_notification_read_returns(),
1689 $notificationid);
1690 }
1691
1692 // Retrieve all notifications sent (they are currently read).
1693 $lastnotifications = message_get_messages($user1->id, $user2->id, 1, true);
1694 $this->assertCount(2, $lastnotifications);
1695 $this->assertArrayHasKey($notificationids[1]['notificationid'], $lastnotifications);
1696 $this->assertArrayHasKey($notificationids[0]['notificationid'], $lastnotifications);
1697
1698 // Retrieve all notifications sent by any user (that are currently unread).
1699 $lastnotifications = message_get_messages($user1->id, 0, 1, false);
1700 $this->assertCount(1, $lastnotifications);
1701
1702 // Invalid notification ids.
1703 try {
1704 $notificationid = core_message_external::mark_notification_read(1337, time());
1705 $this->fail('Exception expected due invalid notificationid.');
1706 } catch (dml_missing_record_exception $e) {
1707 $this->assertEquals('invalidrecord', $e->errorcode);
1708 }
1709
1710 // A notification to a different user.
1711 $lastnotifications = message_get_messages($user2->id, $user3->id, 1, false);
1712 $notificationid = array_pop($lastnotifications)->id;
1713 try {
1714 $notificationid = core_message_external::mark_notification_read($notificationid, time());
1715 $this->fail('Exception expected due invalid notificationid.');
1716 } catch (invalid_parameter_exception $e) {
1717 $this->assertEquals('invalidparameter', $e->errorcode);
1718 }
b6795827
JL
1719 }
1720
419b1128
JL
1721 /**
1722 * Test delete_message.
1723 */
1724 public function test_delete_message() {
1725 global $DB;
1726 $this->resetAfterTest(true);
1727
1728 $user1 = self::getDataGenerator()->create_user();
1729 $user2 = self::getDataGenerator()->create_user();
1730 $user3 = self::getDataGenerator()->create_user();
1731 $user4 = self::getDataGenerator()->create_user();
1732
1733 // Login as user1.
1734 $this->setUser($user1);
f219eac7
MN
1735 \core_message\api::add_contact($user1->id, $user2->id);
1736 \core_message\api::add_contact($user1->id, $user3->id);
419b1128
JL
1737
1738 // User user1 does not interchange messages with user3.
1739 $m1to2 = message_post_message($user1, $user2, 'some random text 1', FORMAT_MOODLE);
1740 $m2to3 = message_post_message($user2, $user3, 'some random text 3', FORMAT_MOODLE);
1741 $m3to2 = message_post_message($user3, $user2, 'some random text 4', FORMAT_MOODLE);
1742 $m3to4 = message_post_message($user3, $user4, 'some random text 4', FORMAT_MOODLE);
1743
1744 // Retrieve all messages sent by user2 (they are currently unread).
1745 $lastmessages = message_get_messages($user1->id, $user2->id, 0, false);
1746
1747 // Delete a message not read, as a user from.
1748 $result = core_message_external::delete_message($m1to2, $user1->id, false);
1749 $result = external_api::clean_returnvalue(core_message_external::delete_message_returns(), $result);
1750 $this->assertTrue($result['status']);
1751 $this->assertCount(0, $result['warnings']);
883ce421
MN
1752 $mua = $DB->get_record('message_user_actions', array('messageid' => $m1to2, 'userid' => $user1->id));
1753 $this->assertEquals(\core_message\api::MESSAGE_ACTION_DELETED, $mua->action);
419b1128
JL
1754
1755 // Try to delete the same message again.
1756 $result = core_message_external::delete_message($m1to2, $user1->id, false);
1757 $result = external_api::clean_returnvalue(core_message_external::delete_message_returns(), $result);
1758 $this->assertFalse($result['status']);
1759
1760 // Try to delete a message that does not belong to me.
1761 try {
1762 $messageid = core_message_external::delete_message($m2to3, $user3->id, false);
1763 $this->fail('Exception expected due invalid messageid.');
1764 } catch (moodle_exception $e) {
1765 $this->assertEquals('You do not have permission to delete this message', $e->errorcode);
1766 }
1767
1768 $this->setUser($user3);
1769 // Delete a message not read, as a user to.
1770 $result = core_message_external::delete_message($m2to3, $user3->id, false);
1771 $result = external_api::clean_returnvalue(core_message_external::delete_message_returns(), $result);
1772 $this->assertTrue($result['status']);
1773 $this->assertCount(0, $result['warnings']);
883ce421
MN
1774 $this->assertTrue($DB->record_exists('message_user_actions', array('messageid' => $m2to3, 'userid' => $user3->id,
1775 'action' => \core_message\api::MESSAGE_ACTION_DELETED)));
419b1128
JL
1776
1777 // Delete a message read.
548936a6
MN
1778 $message = $DB->get_record('messages', ['id' => $m3to2]);
1779 \core_message\api::mark_message_as_read($user3->id, $message, time());
883ce421 1780 $result = core_message_external::delete_message($m3to2, $user3->id);
419b1128
JL
1781 $result = external_api::clean_returnvalue(core_message_external::delete_message_returns(), $result);
1782 $this->assertTrue($result['status']);
1783 $this->assertCount(0, $result['warnings']);
883ce421
MN
1784 $this->assertTrue($DB->record_exists('message_user_actions', array('messageid' => $m3to2, 'userid' => $user3->id,
1785 'action' => \core_message\api::MESSAGE_ACTION_DELETED)));
419b1128
JL
1786
1787 // Invalid message ids.
1788 try {
1789 $result = core_message_external::delete_message(-1, $user1->id);
1790 $this->fail('Exception expected due invalid messageid.');
1791 } catch (dml_missing_record_exception $e) {
08cb8a34 1792 $this->assertEquals('invalidrecord', $e->errorcode);
419b1128
JL
1793 }
1794
1795 // Invalid user.
1796 try {
1797 $result = core_message_external::delete_message($m1to2, -1, false);
1798 $this->fail('Exception expected due invalid user.');
1799 } catch (moodle_exception $e) {
1800 $this->assertEquals('invaliduser', $e->errorcode);
1801 }
1802
1803 // Not active user.
1804 delete_user($user2);
1805 try {
1806 $result = core_message_external::delete_message($m1to2, $user2->id, false);
1807 $this->fail('Exception expected due invalid user.');
1808 } catch (moodle_exception $e) {
1809 $this->assertEquals('userdeleted', $e->errorcode);
1810 }
1811
1812 // Now, as an admin, try to delete any message.
1813 $this->setAdminUser();
1814 $result = core_message_external::delete_message($m3to4, $user4->id, false);
1815 $result = external_api::clean_returnvalue(core_message_external::delete_message_returns(), $result);
1816 $this->assertTrue($result['status']);
1817 $this->assertCount(0, $result['warnings']);
883ce421
MN
1818 $this->assertTrue($DB->record_exists('message_user_actions', array('messageid' => $m3to4, 'userid' => $user4->id,
1819 'action' => \core_message\api::MESSAGE_ACTION_DELETED)));
419b1128
JL
1820
1821 }
1822
3274d5ca
RW
1823 public function test_mark_all_notifications_as_read_invalid_user_exception() {
1824 $this->resetAfterTest(true);
1825
6aa01968
MN
1826 $this->expectException('moodle_exception');
1827 core_message_external::mark_all_notifications_as_read(-2132131, 0);
3274d5ca
RW
1828 }
1829
1830 public function test_mark_all_notifications_as_read_access_denied_exception() {
1831 $this->resetAfterTest(true);
1832
1833 $sender = $this->getDataGenerator()->create_user();
1834 $user = $this->getDataGenerator()->create_user();
1835
1836 $this->setUser($user);
6aa01968
MN
1837 $this->expectException('moodle_exception');
1838 core_message_external::mark_all_notifications_as_read($sender->id, 0);
3274d5ca
RW
1839 }
1840
1841 public function test_mark_all_notifications_as_read_missing_from_user_exception() {
1842 $this->resetAfterTest(true);
1843
1844 $sender = $this->getDataGenerator()->create_user();
1845
1846 $this->setUser($sender);
6aa01968
MN
1847 $this->expectException('moodle_exception');
1848 core_message_external::mark_all_notifications_as_read($sender->id, 99999);
3274d5ca
RW
1849 }
1850
1851 public function test_mark_all_notifications_as_read() {
7d69958e
RW
1852 global $DB;
1853
3274d5ca
RW
1854 $this->resetAfterTest(true);
1855
1856 $sender1 = $this->getDataGenerator()->create_user();
1857 $sender2 = $this->getDataGenerator()->create_user();
1858 $sender3 = $this->getDataGenerator()->create_user();
1859 $recipient = $this->getDataGenerator()->create_user();
1860
1861 $this->setUser($recipient);
1862
6aa01968
MN
1863 $this->send_message($sender1, $recipient, 'Notification', 1);
1864 $this->send_message($sender1, $recipient, 'Notification', 1);
1865 $this->send_message($sender2, $recipient, 'Notification', 1);
1866 $this->send_message($sender2, $recipient, 'Notification', 1);
1867 $this->send_message($sender3, $recipient, 'Notification', 1);
1868 $this->send_message($sender3, $recipient, 'Notification', 1);
3274d5ca
RW
1869
1870 core_message_external::mark_all_notifications_as_read($recipient->id, $sender1->id);
883ce421
MN
1871 $readnotifications = $DB->get_records_select('notifications', 'useridto = ? AND timeread IS NOT NULL', [$recipient->id]);
1872 $unreadnotifications = $DB->get_records_select('notifications', 'useridto = ? AND timeread IS NULL', [$recipient->id]);
3274d5ca 1873
7d69958e
RW
1874 $this->assertCount(2, $readnotifications);
1875 $this->assertCount(4, $unreadnotifications);
3274d5ca
RW
1876
1877 core_message_external::mark_all_notifications_as_read($recipient->id, 0);
883ce421
MN
1878 $readnotifications = $DB->get_records_select('notifications', 'useridto = ? AND timeread IS NOT NULL', [$recipient->id]);
1879 $unreadnotifications = $DB->get_records_select('notifications', 'useridto = ? AND timeread IS NULL', [$recipient->id]);
3274d5ca 1880
7d69958e
RW
1881 $this->assertCount(6, $readnotifications);
1882 $this->assertCount(0, $unreadnotifications);
3274d5ca 1883 }
e86f0cb4
JL
1884
1885 /**
1886 * Test get_user_notification_preferences
1887 */
1888 public function test_get_user_notification_preferences() {
1889 $this->resetAfterTest(true);
1890
1891 $user = self::getDataGenerator()->create_user();
1892 $this->setUser($user);
1893
1894 // Set a couple of preferences to test.
1895 set_user_preference('message_provider_mod_assign_assign_notification_loggedin', 'popup', $user);
1896 set_user_preference('message_provider_mod_assign_assign_notification_loggedoff', 'email', $user);
1897
1898 $prefs = core_message_external::get_user_notification_preferences();
1899 $prefs = external_api::clean_returnvalue(core_message_external::get_user_notification_preferences_returns(), $prefs);
1900 // Check processors.
46c5c883 1901 $this->assertGreaterThanOrEqual(2, count($prefs['preferences']['processors']));
e86f0cb4
JL
1902 $this->assertEquals($user->id, $prefs['preferences']['userid']);
1903
1904 // Check components.
46c5c883 1905 $this->assertGreaterThanOrEqual(8, count($prefs['preferences']['components']));
e86f0cb4
JL
1906
1907 // Check some preferences that we previously set.
1908 $found = 0;
1909 foreach ($prefs['preferences']['components'] as $component) {
1910 foreach ($component['notifications'] as $prefdata) {
1911 if ($prefdata['preferencekey'] != 'message_provider_mod_assign_assign_notification') {
1912 continue;
1913 }
1914 foreach ($prefdata['processors'] as $processor) {
1915 if ($processor['name'] == 'popup') {
1916 $this->assertTrue($processor['loggedin']['checked']);
1917 $found++;
1918 } else if ($processor['name'] == 'email') {
1919 $this->assertTrue($processor['loggedoff']['checked']);
1920 $found++;
1921 }
1922 }
1923 }
1924 }
1925 $this->assertEquals(2, $found);
1926 }
1927
1928 /**
1929 * Test get_user_notification_preferences permissions
1930 */
1931 public function test_get_user_notification_preferences_permissions() {
1932 $this->resetAfterTest(true);
1933
1934 $user = self::getDataGenerator()->create_user();
1935 $otheruser = self::getDataGenerator()->create_user();
1936 $this->setUser($user);
1937
1938 $this->expectException('moodle_exception');
1939 $prefs = core_message_external::get_user_notification_preferences($otheruser->id);
1940 }
6aa01968
MN
1941
1942 /**
1943 * Tests searching users in a course.
1944 */
548cac7d 1945 public function test_data_for_messagearea_search_users_in_course() {
6aa01968
MN
1946 $this->resetAfterTest(true);
1947
1948 // Create some users.
1949 $user1 = new stdClass();
1950 $user1->firstname = 'User';
1951 $user1->lastname = 'One';
1952 $user1 = self::getDataGenerator()->create_user($user1);
1953
1954 // The person doing the search.
1955 $this->setUser($user1);
1956
1957 // Set the second user's status to online by setting their last access to now.
1958 $user2 = new stdClass();
1959 $user2->firstname = 'User';
1960 $user2->lastname = 'Two';
1961 $user2->lastaccess = time();
1962 $user2 = self::getDataGenerator()->create_user($user2);
1963
1964 // Block the second user.
f219eac7 1965 \core_message\api::block_user($user1->id, $user2->id);
6aa01968
MN
1966
1967 $user3 = new stdClass();
1968 $user3->firstname = 'User';
1969 $user3->lastname = 'Three';
1970 $user3 = self::getDataGenerator()->create_user($user3);
1971
1972 // Create a course.
1973 $course1 = new stdClass();
1974 $course1->fullname = 'Course';
1975 $course1->shortname = 'One';
1976 $course1 = $this->getDataGenerator()->create_course();
1977
1978 // Enrol the user we are doing the search for and one user in the course.
1979 $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
1980 $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
1981
1982 // Perform a search.
1983 $result = core_message_external::data_for_messagearea_search_users_in_course($user1->id, $course1->id, 'User');
1984
1985 // We need to execute the return values cleaning process to simulate the web service.
1986 $result = external_api::clean_returnvalue(core_message_external::data_for_messagearea_search_users_in_course_returns(),
1987 $result);
1988
1989 // Check that we only retrieved a user that was enrolled, and that the user performing the search was not returned.
1990 $users = $result['contacts'];
1991 $this->assertCount(1, $users);
1992
1993 $user = $users[0];
1994 $this->assertEquals($user2->id, $user['userid']);
1995 $this->assertEquals(fullname($user2), $user['fullname']);
1996 $this->assertFalse($user['ismessaging']);
1997 $this->assertFalse($user['sentfromcurrentuser']);
1998 $this->assertNull($user['lastmessage']);
1999 $this->assertNull($user['messageid']);
cb805753 2000 $this->assertNull($user['isonline']);
6aa01968
MN
2001 $this->assertFalse($user['isread']);
2002 $this->assertTrue($user['isblocked']);
2003 $this->assertNull($user['unreadcount']);
2004 }
2005
2006 /**
2007 * Tests searching users in course as another user.
2008 */
548cac7d 2009 public function test_data_for_messagearea_search_users_in_course_as_other_user() {
6aa01968
MN
2010 $this->resetAfterTest(true);
2011
2012 // The person doing the search for another user.
2013 $this->setAdminUser();
2014
2015 // Create some users.
2016 $user1 = new stdClass();
2017 $user1->firstname = 'User';
2018 $user1->lastname = 'One';
2019 $user1 = self::getDataGenerator()->create_user($user1);
2020
2021 $user2 = new stdClass();
2022 $user2->firstname = 'User';
2023 $user2->lastname = 'Two';
2024 $user2 = self::getDataGenerator()->create_user($user2);
2025
2026 $user3 = new stdClass();
2027 $user3->firstname = 'User';
2028 $user3->lastname = 'Three';
2029 $user3 = self::getDataGenerator()->create_user($user3);
2030
2031 // Create a course.
2032 $course1 = new stdClass();
2033 $course1->fullname = 'Course';
2034 $course1->shortname = 'One';
2035 $course1 = $this->getDataGenerator()->create_course();
2036
2037 // Enrol the user we are doing the search for and one user in the course.
2038 $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
2039 $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
2040
2041 // Perform a search.
2042 $result = core_message_external::data_for_messagearea_search_users_in_course($user1->id, $course1->id, 'User');
2043
2044 // We need to execute the return values cleaning process to simulate the web service server.
2045 $result = external_api::clean_returnvalue(core_message_external::data_for_messagearea_search_users_in_course_returns(),
2046 $result);
2047
2048 // Check that we got the user enrolled, and that the user we are performing the search on behalf of was not returned.
2049 $users = $result['contacts'];
2050 $this->assertCount(1, $users);
2051
2052 $user = $users[0];
2053 $this->assertEquals($user2->id, $user['userid']);
2054 $this->assertEquals(fullname($user2), $user['fullname']);
2055 $this->assertFalse($user['ismessaging']);
2056 $this->assertFalse($user['sentfromcurrentuser']);
2057 $this->assertNull($user['lastmessage']);
2058 $this->assertNull($user['messageid']);
2059 $this->assertFalse($user['isonline']);
2060 $this->assertFalse($user['isread']);
2061 $this->assertFalse($user['isblocked']);
2062 $this->assertNull($user['unreadcount']);
2063 }
2064
2065 /**
2066 * Tests searching users in course as another user without the proper capabilities.
2067 */
548cac7d 2068 public function test_data_for_messagearea_search_users_in_course_as_other_user_without_cap() {
6aa01968
MN
2069 $this->resetAfterTest(true);
2070
2071 // Create some users.
2072 $user1 = self::getDataGenerator()->create_user();
2073 $user2 = self::getDataGenerator()->create_user();
2074
2075 // The person doing the search for another user.
2076 $this->setUser($user1);
2077
2078 // Create a course.
2079 $course = $this->getDataGenerator()->create_course();
2080
2081 // Ensure an exception is thrown.
2082 $this->expectException('moodle_exception');
2083 core_message_external::data_for_messagearea_search_users_in_course($user2->id, $course->id, 'User');
548cac7d 2084 $this->assertDebuggingCalled();
6aa01968
MN
2085 }
2086
2087 /**
2088 * Tests searching users in course with messaging disabled.
2089 */
548cac7d 2090 public function test_data_for_messagearea_search_users_in_course_messaging_disabled() {
6aa01968
MN
2091 global $CFG;
2092
2093 $this->resetAfterTest(true);
2094
2095 // Create some skeleton data just so we can call the WS..
2096 $user = self::getDataGenerator()->create_user();
2097 $course = $this->getDataGenerator()->create_course();
2098
2099 // The person doing the search for another user.
2100 $this->setUser($user);
2101
2102 // Disable messaging.
2103 $CFG->messaging = 0;
2104
2105 // Ensure an exception is thrown.
2106 $this->expectException('moodle_exception');
2107 core_message_external::data_for_messagearea_search_users_in_course($user->id, $course->id, 'User');
548cac7d 2108 $this->assertDebuggingCalled();
6aa01968
MN
2109 }
2110
2111 /**
2112 * Tests searching users.
2113 */
548cac7d 2114 public function test_data_for_messagearea_search_users() {
6aa01968
MN
2115 $this->resetAfterTest(true);
2116
2117 // Create some users.
2118 $user1 = new stdClass();
2119 $user1->firstname = 'User';
2120 $user1->lastname = 'One';
2121 $user1 = self::getDataGenerator()->create_user($user1);
2122
2123 // Set as the user performing the search.
2124 $this->setUser($user1);
2125
2126 $user2 = new stdClass();
2127 $user2->firstname = 'User search';
2128 $user2->lastname = 'Two';
2129 $user2 = self::getDataGenerator()->create_user($user2);
2130
2131 $user3 = new stdClass();
2132 $user3->firstname = 'User search';
2133 $user3->lastname = 'Three';
2134 $user3 = self::getDataGenerator()->create_user($user3);
2135
2136 $user4 = new stdClass();
2137 $user4->firstname = 'User';
2138 $user4->lastname = 'Four';
2139 $user4 = self::getDataGenerator()->create_user($user4);
2140
2141 $user5 = new stdClass();
2142 $user5->firstname = 'User search';
2143 $user5->lastname = 'Five';
2144 $user5 = self::getDataGenerator()->create_user($user5);
2145
2146 $user6 = new stdClass();
2147 $user6->firstname = 'User';
2148 $user6->lastname = 'Six';
2149 $user6 = self::getDataGenerator()->create_user($user6);
2150
2151 // Create some courses.
2152 $course1 = new stdClass();
2153 $course1->fullname = 'Course search';
2154 $course1->shortname = 'One';
2155 $course1 = $this->getDataGenerator()->create_course($course1);
2156
2157 $course2 = new stdClass();
2158 $course2->fullname = 'Course';
2159 $course2->shortname = 'Two';
2160 $course2 = $this->getDataGenerator()->create_course($course2);
2161
2162 $course3 = new stdClass();
2163 $course3->fullname = 'Course';
2164 $course3->shortname = 'Three search';
2165 $course3 = $this->getDataGenerator()->create_course($course3);
2166
87d4ab65
AG
2167 $course4 = new stdClass();
2168 $course4->fullname = 'Course Four';
2169 $course4->shortname = 'CF100';
2170 $course4 = $this->getDataGenerator()->create_course($course4);
2171
2172 $this->getDataGenerator()->enrol_user($user1->id, $course1->id, 'student');
2173 $this->getDataGenerator()->enrol_user($user1->id, $course2->id, 'student');
2174 $this->getDataGenerator()->enrol_user($user1->id, $course3->id, 'student');
2175
6aa01968 2176 // Add some users as contacts.
f219eac7
MN
2177 \core_message\api::add_contact($user1->id, $user2->id);
2178 \core_message\api::add_contact($user1->id, $user3->id);
2179 \core_message\api::add_contact($user1->id, $user4->id);
6aa01968 2180
548cac7d
AA
2181 // Perform a search $CFG->messagingallusers setting enabled.
2182 set_config('messagingallusers', 1);
6aa01968
MN
2183 $result = core_message_external::data_for_messagearea_search_users($user1->id, 'search');
2184
2185 // We need to execute the return values cleaning process to simulate the web service server.
2186 $result = external_api::clean_returnvalue(core_message_external::data_for_messagearea_search_users_returns(),
2187 $result);
2188
2189 // Confirm that we returns contacts, courses and non-contacts.
2190 $contacts = $result['contacts'];
2191 $courses = $result['courses'];
2192 $noncontacts = $result['noncontacts'];
2193
2194 // Check that we retrieved the correct contacts.
2195 $this->assertCount(2, $contacts);
2196 $this->assertEquals($user3->id, $contacts[0]['userid']);
2197 $this->assertEquals($user2->id, $contacts[1]['userid']);
2198
2199 // Check that we retrieved the correct courses.
2200 $this->assertCount(2, $courses);
2201 $this->assertEquals($course3->id, $courses[0]['id']);
2202 $this->assertEquals($course1->id, $courses[1]['id']);
2203
2204 // Check that we retrieved the correct non-contacts.
2205 $this->assertCount(1, $noncontacts);
2206 $this->assertEquals($user5->id, $noncontacts[0]['userid']);
2207 }
2208
2209 /**
2210 * Tests searching users as another user.
2211 */
548cac7d 2212 public function test_data_for_messagearea_search_users_as_other_user() {
6aa01968
MN
2213 $this->resetAfterTest(true);
2214
2215 // The person doing the search.
2216 $this->setAdminUser();
2217
2218 // Create some users.
2219 $user1 = new stdClass();
2220 $user1->firstname = 'User';
2221 $user1->lastname = 'One';
2222 $user1 = self::getDataGenerator()->create_user($user1);
2223
2224 $user2 = new stdClass();
2225 $user2->firstname = 'User search';
2226 $user2->lastname = 'Two';
2227 $user2 = self::getDataGenerator()->create_user($user2);
2228
2229 $user3 = new stdClass();
2230 $user3->firstname = 'User search';
2231 $user3->lastname = 'Three';
2232 $user3 = self::getDataGenerator()->create_user($user3);
2233
2234 $user4 = new stdClass();
2235 $user4->firstname = 'User';
2236 $user4->lastname = 'Four';
2237 $user4 = self::getDataGenerator()->create_user($user4);
2238
2239 $user5 = new stdClass();
2240 $user5->firstname = 'User search';
2241 $user5->lastname = 'Five';
2242 $user5 = self::getDataGenerator()->create_user($user5);
2243
2244 $user6 = new stdClass();
2245 $user6->firstname = 'User';
2246 $user6->lastname = 'Six';
2247 $user6 = self::getDataGenerator()->create_user($user6);
2248
2249 // Create some courses.
2250 $course1 = new stdClass();
2251 $course1->fullname = 'Course search';
2252 $course1->shortname = 'One';
2253 $course1 = $this->getDataGenerator()->create_course($course1);
2254
2255 $course2 = new stdClass();
2256 $course2->fullname = 'Course';
2257 $course2->shortname = 'Two';
2258 $course2 = $this->getDataGenerator()->create_course($course2);
2259
2260 $course3 = new stdClass();
2261 $course3->fullname = 'Course';
2262 $course3->shortname = 'Three search';
2263 $course3 = $this->getDataGenerator()->create_course($course3);
2264
2265 // Add some users as contacts.
f219eac7
MN
2266 \core_message\api::add_contact($user1->id, $user2->id);
2267 \core_message\api::add_contact($user1->id, $user3->id);
2268 \core_message\api::add_contact($user1->id, $user4->id);
6aa01968 2269
548cac7d
AA
2270 // Perform a search $CFG->messagingallusers setting enabled.
2271 set_config('messagingallusers', 1);
6aa01968
MN
2272 $result = core_message_external::data_for_messagearea_search_users($user1->id, 'search');
2273
2274 // We need to execute the return values cleaning process to simulate the web service server.
2275 $result = external_api::clean_returnvalue(core_message_external::data_for_messagearea_search_users_returns(),
2276 $result);
2277
2278 // Confirm that we returns contacts, courses and non-contacts.
2279 $contacts = $result['contacts'];
2280 $courses = $result['courses'];
2281 $noncontacts = $result['noncontacts'];
2282
2283 // Check that we retrieved the correct contacts.
2284 $this->assertCount(2, $contacts);
2285 $this->assertEquals($user3->id, $contacts[0]['userid']);
2286 $this->assertEquals($user2->id, $contacts[1]['userid']);
2287
2288 // Check that we retrieved the correct courses.
87d4ab65 2289 $this->assertCount(0, $courses);
6aa01968
MN
2290
2291 // Check that we retrieved the correct non-contacts.
2292 $this->assertCount(1, $noncontacts);
2293 $this->assertEquals($user5->id, $noncontacts[0]['userid']);
2294 }
2295
2296 /**
2297 * Tests searching users as another user without the proper capabilities.
2298 */
548cac7d 2299 public function test_data_for_messagearea_search_users_as_other_user_without_cap() {
6aa01968
MN
2300 $this->resetAfterTest(true);
2301
2302 // Create some users.
2303 $user1 = self::getDataGenerator()->create_user();
2304 $user2 = self::getDataGenerator()->create_user();
2305
2306 // The person doing the search for another user.
2307 $this->setUser($user1);
2308
2309 // Ensure an exception is thrown.
2310 $this->expectException('moodle_exception');
2311 core_message_external::data_for_messagearea_search_users($user2->id, 'User');
548cac7d 2312 $this->assertDebuggingCalled();
6aa01968
MN
2313 }
2314
2315 /**
2316 * Tests searching users with messaging disabled.
2317 */
548cac7d 2318 public function test_data_for_messagearea_search_users_messaging_disabled() {
6aa01968
MN
2319 global $CFG;
2320
2321 $this->resetAfterTest(true);
2322
2323 // Create some skeleton data just so we can call the WS.
2324 $user = self::getDataGenerator()->create_user();
2325
2326 // The person doing the search.
2327 $this->setUser($user);
2328
2329 // Disable messaging.
2330 $CFG->messaging = 0;
2331
2332 // Ensure an exception is thrown.
2333 $this->expectException('moodle_exception');
2334 core_message_external::data_for_messagearea_search_users($user->id, 'User');
548cac7d
AA
2335 $this->assertDebuggingCalled();
2336 }
2337
2338 /**
41485be2
JD
2339 * Tests searching for users when site-wide messaging is disabled.
2340 *
2341 * This test verifies that any contacts are returned, as well as any non-contacts whose profile we can view.
2342 * If checks this by placing some users in the same course, where default caps would permit a user to view another user's
2343 * profile.
548cac7d 2344 */
41485be2
JD
2345 public function test_message_search_users_messagingallusers_disabled() {
2346 $this->resetAfterTest();
548cac7d
AA
2347
2348 // Create some users.
41485be2
JD
2349 $users = [];
2350 foreach (range(1, 7) as $i) {
2351 $user = new stdClass();
2352 $user->firstname = ($i == 4) ? 'User' : 'User search'; // Ensure the fourth user won't match the search term.
2353 $user->lastname = $i;
2354 $user = $this->getDataGenerator()->create_user($user);
2355 $users[$i] = $user;
2356 }
548cac7d 2357
41485be2
JD
2358 // Enrol a few users in the same course, but leave them as non-contacts.
2359 $course1 = $this->getDataGenerator()->create_course();
2360 $this->setAdminUser();
2361 $this->getDataGenerator()->enrol_user($users[1]->id, $course1->id);
2362 $this->getDataGenerator()->enrol_user($users[6]->id, $course1->id);
2363 $this->getDataGenerator()->enrol_user($users[7]->id, $course1->id);
548cac7d 2364
41485be2
JD
2365 // Add some other users as contacts.
2366 \core_message\api::add_contact($users[1]->id, $users[2]->id);
2367 \core_message\api::add_contact($users[3]->id, $users[1]->id);
2368 \core_message\api::add_contact($users[1]->id, $users[4]->id);
548cac7d 2369
41485be2 2370 // Create individual conversations between some users, one contact and one non-contact.
548cac7d 2371 \core_message\api::create_conversation(\core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
41485be2 2372 [$users[1]->id, $users[2]->id]);
548cac7d 2373 \core_message\api::create_conversation(\core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
41485be2 2374 [$users[6]->id, $users[1]->id]);
548cac7d 2375
41485be2
JD
2376 // Create a group conversation between 4 users, including a contact and a non-contact.
2377 \core_message\api::create_conversation(\core_message\api::MESSAGE_CONVERSATION_TYPE_GROUP,
2378 [$users[1]->id, $users[2]->id, $users[4]->id, $users[7]->id], 'Project chat');
548cac7d 2379
41485be2
JD
2380 // Set as the user performing the search.
2381 $this->setUser($users[1]);
548cac7d 2382
41485be2 2383 // Perform a search with $CFG->messagingallusers disabled.
548cac7d 2384 set_config('messagingallusers', 0);
41485be2
JD
2385 $result = core_message_external::message_search_users($users[1]->id, 'search');
2386 $result = external_api::clean_returnvalue(core_message_external::message_search_users_returns(), $result);
548cac7d
AA
2387
2388 // Confirm that we returns contacts and non-contacts.
41485be2
JD
2389 $this->assertArrayHasKey('contacts', $result);
2390 $this->assertArrayHasKey('noncontacts', $result);
548cac7d
AA
2391 $contacts = $result['contacts'];
2392 $noncontacts = $result['noncontacts'];
2393
2394 // Check that we retrieved the correct contacts.
2395 $this->assertCount(2, $contacts);
41485be2
JD
2396 $this->assertEquals($users[2]->id, $contacts[0]['id']);
2397 $this->assertEquals($users[3]->id, $contacts[1]['id']);
2398
2399 // Verify the correct conversations were returned for the contacts.
2400 $this->assertCount(2, $contacts[0]['conversations']);
2401 // We can't rely on the ordering of conversations within the results, so sort by id first.
2402 usort($contacts[0]['conversations'], function($a, $b) {
2403 return $a['id'] < $b['id'];
2404 });
2405 $this->assertEquals(\core_message\api::MESSAGE_CONVERSATION_TYPE_GROUP, $contacts[0]['conversations'][0]['type']);
2406 $this->assertEquals(\core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL, $contacts[0]['conversations'][1]['type']);
2407
2408 $this->assertCount(0, $contacts[1]['conversations']);
548cac7d
AA
2409
2410 // Check that we retrieved the correct non-contacts.
41485be2 2411 // When site wide messaging is disabled, we expect to see only those users whose profiles we can view.
548cac7d 2412 $this->assertCount(2, $noncontacts);
41485be2
JD
2413 $this->assertEquals($users[6]->id, $noncontacts[0]['id']);
2414 $this->assertEquals($users[7]->id, $noncontacts[1]['id']);
2415
2416 // Verify the correct conversations were returned for the non-contacts.
2417 $this->assertCount(1, $noncontacts[0]['conversations']);
2418 $this->assertEquals(\core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL, $noncontacts[0]['conversations'][0]['type']);
2419
2420 $this->assertCount(1, $noncontacts[1]['conversations']);
2421 $this->assertEquals(\core_message\api::MESSAGE_CONVERSATION_TYPE_GROUP, $noncontacts[1]['conversations'][0]['type']);
548cac7d
AA
2422 }
2423
2424 /**
41485be2
JD
2425 * Tests searching for users when site-wide messaging is enabled.
2426 *
2427 * This test verifies that any contacts are returned, as well as any non-contacts, regardless of whether the searching user
2428 * can view their respective profile.
548cac7d 2429 */
41485be2
JD
2430 public function test_message_search_users_messagingallusers_enabled() {
2431 $this->resetAfterTest();
548cac7d
AA
2432
2433 // Create some users.
41485be2
JD
2434 $users = [];
2435 foreach (range(1, 8) as $i) {
2436 $user = new stdClass();
2437 $user->firstname = ($i == 4) ? 'User' : 'User search'; // Ensure the fourth user won't match the search term.
2438 $user->lastname = $i;
2439 $user = $this->getDataGenerator()->create_user($user);
2440 $users[$i] = $user;
2441 }
548cac7d 2442
41485be2
JD
2443 // Enrol a few users in the same course, but leave them as non-contacts.
2444 $course1 = $this->getDataGenerator()->create_course();
2445 $this->setAdminUser();
2446 $this->getDataGenerator()->enrol_user($users[1]->id, $course1->id);
2447 $this->getDataGenerator()->enrol_user($users[6]->id, $course1->id);
2448 $this->getDataGenerator()->enrol_user($users[7]->id, $course1->id);
548cac7d 2449
41485be2
JD
2450 // Add some other users as contacts.
2451 \core_message\api::add_contact($users[1]->id, $users[2]->id);
2452 \core_message\api::add_contact($users[3]->id, $users[1]->id);
2453 \core_message\api::add_contact($users[1]->id, $users[4]->id);
548cac7d 2454
41485be2 2455 // Create individual conversations between some users, one contact and one non-contact.
548cac7d 2456 \core_message\api::create_conversation(\core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
41485be2 2457 [$users[1]->id, $users[2]->id]);
548cac7d 2458 \core_message\api::create_conversation(\core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
41485be2 2459 [$users[6]->id, $users[1]->id]);
548cac7d 2460
41485be2
JD
2461 // Create a group conversation between 5 users, including a contact and a non-contact, and a user NOT in a shared course.
2462 \core_message\api::create_conversation(\core_message\api::MESSAGE_CONVERSATION_TYPE_GROUP,
2463 [$users[1]->id, $users[2]->id, $users[4]->id, $users[7]->id, $users[8]->id], 'Project chat');
548cac7d 2464
41485be2
JD
2465 // Set as the user performing the search.
2466 $this->setUser($users[1]);
2467
2468 // Perform a search with $CFG->messagingallusers enabled.
2469 set_config('messagingallusers', 1);
2470 $result = core_message_external::message_search_users($users[1]->id, 'search');
2471 $result = external_api::clean_returnvalue(core_message_external::message_search_users_returns(), $result);
548cac7d
AA
2472
2473 // Confirm that we returns contacts and non-contacts.
41485be2
JD
2474 $this->assertArrayHasKey('contacts', $result);
2475 $this->assertArrayHasKey('noncontacts', $result);
548cac7d
AA
2476 $contacts = $result['contacts'];
2477 $noncontacts = $result['noncontacts'];
2478
2479 // Check that we retrieved the correct contacts.
2480 $this->assertCount(2, $contacts);
41485be2
JD
2481 $this->assertEquals($users[2]->id, $contacts[0]['id']);
2482 $this->assertEquals($users[3]->id, $contacts[1]['id']);
548cac7d 2483
41485be2
JD
2484 // Verify the correct conversations were returned for the contacts.
2485 $this->assertCount(2, $contacts[0]['conversations']);
2486 // We can't rely on the ordering of conversations within the results, so sort by id first.
2487 usort($contacts[0]['conversations'], function($a, $b) {
2488 return $a['id'] < $b['id'];
2489 });
2490 $this->assertEquals(\core_message\api::MESSAGE_CONVERSATION_TYPE_GROUP, $contacts[0]['conversations'][0]['type']);
2491 $this->assertEquals(\core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL, $contacts[0]['conversations'][1]['type']);
548cac7d 2492
41485be2 2493 $this->assertCount(0, $contacts[1]['conversations']);
548cac7d 2494
41485be2
JD
2495 // Check that we retrieved the correct non-contacts.
2496 // If site wide messaging is enabled, we expect to be able to search for any users.
2497 $this->assertCount(4, $noncontacts);
2498 $this->assertEquals($users[5]->id, $noncontacts[0]['id']);
2499 $this->assertEquals($users[6]->id, $noncontacts[1]['id']);
2500 $this->assertEquals($users[7]->id, $noncontacts[2]['id']);
2501 $this->assertEquals($users[8]->id, $noncontacts[3]['id']);
2502
2503 // Verify the correct conversations were returned for the non-contacts.
2504 $this->assertCount(0, $noncontacts[0]['conversations']);
548cac7d 2505
41485be2
JD
2506 $this->assertCount(1, $noncontacts[1]['conversations']);
2507 $this->assertEquals(\core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL, $noncontacts[1]['conversations'][0]['type']);
548cac7d 2508
41485be2
JD
2509 $this->assertCount(1, $noncontacts[2]['conversations']);
2510 $this->assertEquals(\core_message\api::MESSAGE_CONVERSATION_TYPE_GROUP, $noncontacts[2]['conversations'][0]['type']);
548cac7d 2511
41485be2
JD
2512 $this->assertCount(1, $noncontacts[3]['conversations']);
2513 $this->assertEquals(\core_message\api::MESSAGE_CONVERSATION_TYPE_GROUP, $noncontacts[3]['conversations'][0]['type']);
548cac7d
AA
2514 }
2515
2516 /**
41485be2 2517 * Verify searching for users works even if no matching users from either contacts, or non-contacts can be found.
548cac7d 2518 */
41485be2
JD
2519 public function test_message_search_users_with_empty_result() {
2520 $this->resetAfterTest();
548cac7d 2521
41485be2
JD
2522 // Create some users, but make sure neither will match the search term.
2523 $user1 = new stdClass();
2524 $user1->firstname = 'User';
2525 $user1->lastname = 'One';
2526 $user1 = $this->getDataGenerator()->create_user($user1);
2527 $user2 = new stdClass();
2528 $user2->firstname = 'User';
2529 $user2->lastname = 'Two';
2530 $user2 = $this->getDataGenerator()->create_user($user2);
548cac7d 2531
41485be2 2532 // Perform a search as user1.
548cac7d 2533 $this->setUser($user1);
41485be2
JD
2534 $result = core_message_external::message_search_users($user1->id, 'search');
2535 $result = external_api::clean_returnvalue(core_message_external::message_search_users_returns(), $result);
548cac7d 2536
41485be2
JD
2537 // Check results are empty.
2538 $this->assertCount(0, $result['contacts']);
2539 $this->assertCount(0, $result['noncontacts']);
548cac7d
AA
2540 }
2541
2542 /**
41485be2 2543 * Test verifying that limits and offsets work for both the contacts and non-contacts return data.
548cac7d 2544 */
41485be2
JD
2545 public function test_message_search_users_limit_offset() {
2546 $this->resetAfterTest();
548cac7d 2547
41485be2
JD
2548 // Create 20 users.
2549 $users = [];
2550 foreach (range(1, 20) as $i) {
2551 $user = new stdClass();
2552 $user->firstname = "User search";
2553 $user->lastname = $i;
2554 $user = $this->getDataGenerator()->create_user($user);
2555 $users[$i] = $user;
2556 }
2557
2558 // Enrol the first 9 users in the same course, but leave them as non-contacts.
2559 $this->setAdminUser();
2560 $course1 = $this->getDataGenerator()->create_course();
2561 foreach (range(1, 9) as $i) {
2562 $this->getDataGenerator()->enrol_user($users[$i]->id, $course1->id);
2563 }
2564
2565 // Add 5 users, starting at the 11th user, as contacts for user1.
2566 foreach (range(11, 15) as $i) {
2567 \core_message\api::add_contact($users[1]->id, $users[$i]->id);
2568 }
548cac7d
AA
2569
2570 // Set as the user performing the search.
41485be2 2571 $this->setUser($users[1]);
548cac7d 2572
41485be2
JD
2573 // Search using a limit of 3.
2574 // This tests the case where we have more results than the limit for both contacts and non-contacts.
2575 $result = core_message_external::message_search_users($users[1]->id, 'search', 0, 3);
2576 $result = external_api::clean_returnvalue(core_message_external::message_search_users_returns(), $result);
2577 $contacts = $result['contacts'];
2578 $noncontacts = $result['noncontacts'];
548cac7d 2579
41485be2
JD
2580 // Check that we retrieved the correct contacts.
2581 $this->assertCount(3, $contacts);
2582 $this->assertEquals($users[11]->id, $contacts[0]['id']);
2583 $this->assertEquals($users[12]->id, $contacts[1]['id']);
2584 $this->assertEquals($users[13]->id, $contacts[2]['id']);
548cac7d 2585
41485be2
JD
2586 // Check that we retrieved the correct non-contacts.
2587 $this->assertCount(3, $noncontacts);
2588 $this->assertEquals($users[2]->id, $noncontacts[0]['id']);
2589 $this->assertEquals($users[3]->id, $noncontacts[1]['id']);
2590 $this->assertEquals($users[4]->id, $noncontacts[2]['id']);
2591
2592 // Now, offset to get the next batch of results.
2593 // We expect to see 2 contacts, and 3 non-contacts.
2594 $result = core_message_external::message_search_users($users[1]->id, 'search', 3, 3);
2595 $result = external_api::clean_returnvalue(core_message_external::message_search_users_returns(), $result);
2596 $contacts = $result['contacts'];
2597 $noncontacts = $result['noncontacts'];
2598 $this->assertCount(2, $contacts);
2599 $this->assertEquals($users[14]->id, $contacts[0]['id']);
2600 $this->assertEquals($users[15]->id, $contacts[1]['id']);
548cac7d 2601
41485be2
JD
2602 $this->assertCount(3, $noncontacts);
2603 $this->assertEquals($users[5]->id, $noncontacts[0]['id']);
2604 $this->assertEquals($users[6]->id, $noncontacts[1]['id']);
2605 $this->assertEquals($users[7]->id, $noncontacts[2]['id']);
2606
2607 // Now, offset to get the next batch of results.
2608 // We expect to see 0 contacts, and 2 non-contacts.
2609 $result = core_message_external::message_search_users($users[1]->id, 'search', 6, 3);
2610 $result = external_api::clean_returnvalue(core_message_external::message_search_users_returns(), $result);
2611 $contacts = $result['contacts'];
2612 $noncontacts = $result['noncontacts'];
2613 $this->assertCount(0, $contacts);
548cac7d 2614
41485be2
JD
2615 $this->assertCount(2, $noncontacts);
2616 $this->assertEquals($users[8]->id, $noncontacts[0]['id']);
2617 $this->assertEquals($users[9]->id, $noncontacts[1]['id']);
2618 }
548cac7d 2619
41485be2
JD
2620 /**
2621 * Tests searching users as another user having the 'moodle/user:viewdetails' capability.
2622 */
2623 public function test_message_search_users_with_cap() {
2624 $this->resetAfterTest();
2625 global $DB;
548cac7d 2626
41485be2
JD
2627 // Create some users.
2628 $users = [];
2629 foreach (range(1, 8) as $i) {
2630 $user = new stdClass();
2631 $user->firstname = ($i == 4) ? 'User' : 'User search'; // Ensure the fourth user won't match the search term.
2632 $user->lastname = $i;
2633 $user = $this->getDataGenerator()->create_user($user);
2634 $users[$i] = $user;
2635 }
548cac7d 2636
41485be2
JD
2637 // Enrol a few users in the same course, but leave them as non-contacts.
2638 $course1 = $this->getDataGenerator()->create_course();
2639 $this->setAdminUser();
2640 $this->getDataGenerator()->enrol_user($users[1]->id, $course1->id);
2641 $this->getDataGenerator()->enrol_user($users[6]->id, $course1->id);
2642 $this->getDataGenerator()->enrol_user($users[7]->id, $course1->id);
548cac7d 2643
41485be2
JD
2644 // Add some other users as contacts.
2645 \core_message\api::add_contact($users[1]->id, $users[2]->id);
2646 \core_message\api::add_contact($users[3]->id, $users[1]->id);
2647 \core_message\api::add_contact($users[1]->id, $users[4]->id);
548cac7d 2648
41485be2
JD
2649 // Set as the user performing the search.
2650 $this->setUser($users[1]);
2651
2652 // Grant the authenticated user role the capability 'user:viewdetails' at site context.
2653 $authenticatedrole = $DB->get_record('role', ['shortname' => 'user'], '*', MUST_EXIST);
2654 assign_capability('moodle/user:viewdetails', CAP_ALLOW, $authenticatedrole->id, context_system::instance());
2655
2656 // Perform a search with $CFG->messagingallusers disabled.
2657 set_config('messagingallusers', 0);
2658 $result = core_message_external::message_search_users($users[1]->id, 'search');
2659 $result = external_api::clean_returnvalue(core_message_external::message_search_users_returns(), $result);
548cac7d
AA
2660 $contacts = $result['contacts'];
2661 $noncontacts = $result['noncontacts'];
2662
2663 // Check that we retrieved the correct contacts.
41485be2
JD
2664 $this->assertCount(2, $contacts);
2665 $this->assertEquals($users[2]->id, $contacts[0]['id']);
2666 $this->assertEquals($users[3]->id, $contacts[1]['id']);
548cac7d
AA
2667
2668 // Check that we retrieved the correct non-contacts.
41485be2
JD
2669 // Site-wide messaging is disabled, but since we can see all users, we expect to be able to search for any users.
2670 $this->assertCount(4, $noncontacts);
2671 $this->assertEquals($users[5]->id, $noncontacts[0]['id']);
2672 $this->assertEquals($users[6]->id, $noncontacts[1]['id']);
2673 $this->assertEquals($users[7]->id, $noncontacts[2]['id']);
2674 $this->assertEquals($users[8]->id, $noncontacts[3]['id']);
2675 }
548cac7d 2676
41485be2
JD
2677 /**
2678 * Tests searching users as another user without the 'moodle/user:viewdetails' capability.
2679 */
2680 public function test_message_search_users_without_cap() {
2681 $this->resetAfterTest();
2682
2683 // Create some users.
2684 $user1 = $this->getDataGenerator()->create_user();
2685 $user2 = $this->getDataGenerator()->create_user();
2686
2687 // The person doing the search for another user.
2688 $this->setUser($user1);
2689
2690 // Ensure an exception is thrown.
2691 $this->expectException('moodle_exception');
2692 core_message_external::message_search_users($user2->id, 'User');
2693 $this->assertDebuggingCalled();
548cac7d
AA
2694 }
2695
2696 /**
2697 * Tests searching users with messaging disabled.
2698 */
2699 public function test_message_search_users_messaging_disabled() {
41485be2 2700 $this->resetAfterTest();
548cac7d
AA
2701
2702 // Create some skeleton data just so we can call the WS.
41485be2 2703 $user = $this->getDataGenerator()->create_user();
548cac7d
AA
2704
2705 // The person doing the search.
2706 $this->setUser($user);
2707
2708 // Disable messaging.
2709 set_config('messaging', 0);
2710
2711 // Ensure an exception is thrown.
2712 $this->expectException('moodle_exception');
2713 core_message_external::message_search_users($user->id, 'User');
6aa01968
MN
2714 }
2715
2716 /**
2717 * Tests searching messages.
2718 */
2719 public function test_messagearea_search_messages() {
2720 $this->resetAfterTest(true);
2721
2722 // Create some users.
2723 $user1 = self::getDataGenerator()->create_user();
2724 $user2 = self::getDataGenerator()->create_user();
2725
2726 // The person doing the search.
2727 $this->setUser($user1);
2728
2729 // Send some messages back and forth.
2730 $time = time();
2731 $this->send_message($user1, $user2, 'Yo!', 0, $time);
2732 $this->send_message($user2, $user1, 'Sup mang?', 0, $time + 1);
2733 $this->send_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 2);
2734 $this->send_message($user2, $user1, 'Word.', 0, $time + 3);
d2708759 2735 $convid = \core_message\api::get_conversation_between_users([$user1->id, $user2->id]);
6aa01968
MN
2736
2737 // Perform a search.
2738 $result = core_message_external::data_for_messagearea_search_messages($user1->id, 'o');
2739
2740 // We need to execute the return values cleaning process to simulate the web service server.
d2708759 2741 $result = external_api::clean_returnvalue(core_message_external::data_for_messagearea_search_messages_returns(), $result);
6aa01968
MN
2742
2743 // Confirm the data is correct.
2744 $messages = $result['contacts'];
2745 $this->assertCount(2, $messages);
2746
2747 $message1 = $messages[0];
2748 $message2 = $messages[1];
2749
2750 $this->assertEquals($user2->id, $message1['userid']);
2751 $this->assertEquals(fullname($user2), $message1['fullname']);
2752 $this->assertTrue($message1['ismessaging']);
2753 $this->assertFalse($message1['sentfromcurrentuser']);
2754 $this->assertEquals('Word.', $message1['lastmessage']);
2755 $this->assertNotEmpty($message1['messageid']);
cb805753 2756 $this->assertNull($message1['isonline']);
6aa01968
MN
2757 $this->assertFalse($message1['isread']);
2758 $this->assertFalse($message1['isblocked']);
2759 $this->assertNull($message1['unreadcount']);
d2708759 2760 $this->assertEquals($convid, $message1['conversationid']);
6aa01968
MN
2761
2762 $this->assertEquals($user2->id, $message2['userid']);
2763 $this->assertEquals(fullname($user2), $message2['fullname']);
2764 $this->assertTrue($message2['ismessaging']);
2765 $this->assertTrue($message2['sentfromcurrentuser']);
2766 $this->assertEquals('Yo!', $message2['lastmessage']);
2767 $this->assertNotEmpty($message2['messageid']);
cb805753 2768 $this->assertNull($message2['isonline']);
6aa01968
MN
2769 $this->assertTrue($message2['isread']);
2770 $this->assertFalse($message2['isblocked']);
2771 $this->assertNull($message2['unreadcount']);
d2708759 2772 $this->assertEquals($convid, $message2['conversationid']);
6aa01968
MN
2773 }
2774
2775 /**
2776 * Tests searching messages as another user.
2777 */
2778 public function test_messagearea_search_messages_as_other_user() {
2779 $this->resetAfterTest(true);
2780
2781 // The person doing the search.
2782 $this->setAdminUser();
2783
2784 // Create some users.
2785 $user1 = self::getDataGenerator()->create_user();
2786 $user2 = self::getDataGenerator()->create_user();
2787
2788 // Send some messages back and forth.
2789 $time = time();
2790 $this->send_message($user1, $user2, 'Yo!', 0, $time);
2791 $this->send_message($user2, $user1, 'Sup mang?', 0, $time + 1);
2792 $this->send_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 2);
2793 $this->send_message($user2, $user1, 'Word.', 0, $time + 3);
2794
2795 // Perform a search.
2796 $result = core_message_external::data_for_messagearea_search_messages($user1->id, 'o');
2797
2798 // We need to execute the return values cleaning process to simulate the web service server.
2799 $result = external_api::clean_returnvalue(core_message_external::data_for_messagearea_search_messages_returns(),
2800 $result);
2801
2802 // Confirm the data is correct.
2803 $messages = $result['contacts'];
2804 $this->assertCount(2, $messages);
2805
2806 $message1 = $messages[0];
2807 $message2 = $messages[1];
2808
2809 $this->assertEquals($user2->id, $message1['userid']);
2810 $this->assertEquals(fullname($user2), $message1['fullname']);
2811 $this->assertTrue($message1['ismessaging']);
2812 $this->assertFalse($message1['sentfromcurrentuser']);
2813 $this->assertEquals('Word.', $message1['lastmessage']);
2814 $this->assertNotEmpty($message1['messageid']);
2815 $this->assertFalse($message1['isonline']);
2816 $this->assertFalse($message1['isread']);
2817 $this->assertFalse($message1['isblocked']);
2818 $this->assertNull($message1['unreadcount']);
2819
2820 $this->assertEquals($user2->id, $message2['userid']);
2821 $this->assertEquals(fullname($user2), $message2['fullname']);
2822 $this->assertTrue($message2['ismessaging']);
2823 $this->assertTrue($message2['sentfromcurrentuser']);
2824 $this->assertEquals('Yo!', $message2['lastmessage']);
2825 $this->assertNotEmpty($message2['messageid']);
2826 $this->assertFalse($message2['isonline']);
2827 $this->assertTrue($message2['isread']);
2828 $this->assertFalse($message2['isblocked']);
2829 $this->assertNull($message2['unreadcount']);
2830 }
2831
2832 /**
2833 * Tests searching messages as another user without the proper capabilities.
2834 */
2835 public function test_messagearea_search_messages_as_other_user_without_cap() {
2836 $this->resetAfterTest(true);
2837
2838 // Create some users.
2839 $user1 = self::getDataGenerator()->create_user();
2840 $user2 = self::getDataGenerator()->create_user();
2841
2842 // The person doing the search for another user.
2843 $this->setUser($user1);
2844
2845 // Ensure an exception is thrown.
2846 $this->expectException('moodle_exception');
2847 core_message_external::data_for_messagearea_search_messages($user2->id, 'Search');
2848 }
2849
2850 /**
2851 * Tests searching messages with messaging disabled
2852 */
2853 public function test_messagearea_search_messages_messaging_disabled() {
2854 global $CFG;
2855
2856 $this->resetAfterTest(true);
2857
2858 // Create some skeleton data just so we can call the WS.
2859 $user = self::getDataGenerator()->create_user();
2860
2861 // The person doing the search .
2862 $this->setUser($user);
2863
2864 // Disable messaging.
2865 $CFG->messaging = 0;
2866
2867 // Ensure an exception is thrown.
2868 $this->expectException('moodle_exception');
2869 core_message_external::data_for_messagearea_search_messages($user->id, 'Search');
2870 }
2871
2872 /**
2873 * Tests retrieving conversations.
2874 */
2875 public function test_messagearea_conversations() {
2876 $this->resetAfterTest(true);
2877
2878 // Create some users.
2879 $user1 = self::getDataGenerator()->create_user();
2880 $user2 = self::getDataGenerator()->create_user();
2881 $user3 = self::getDataGenerator()->create_user();
2882 $user4 = self::getDataGenerator()->create_user();
2883
2884 // The person retrieving the conversations.
2885 $this->setUser($user1);
2886
2887 // Send some messages back and forth, have some different conversations with different users.
2888 $time = time();
2889 $this->send_message($user1, $user2, 'Yo!', 0, $time);
2890 $this->send_message($user2, $user1, 'Sup mang?', 0, $time + 1);
2891 $this->send_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 2);
4d146f1a 2892 $messageid1 = $this->send_message($user2, $user1, 'Word.', 0, $time + 3);
6aa01968
MN
2893
2894 $this->send_message($user1, $user3, 'Booyah', 0, $time + 4);
2895 $this->send_message($user3, $user1, 'Whaaat?', 0, $time + 5);
2896 $this->send_message($user1, $user3, 'Nothing.', 0, $time + 6);
4d146f1a 2897 $messageid2 = $this->send_message($user3, $user1, 'Cool.', 0, $time + 7);
6aa01968
MN
2898
2899 $this->send_message($user1, $user4, 'Hey mate, you see the new messaging UI in Moodle?', 0, $time + 8);
2900 $this->send_message($user4, $user1, 'Yah brah, it\'s pretty rad.', 0, $time + 9);
4d146f1a 2901 $messageid3 = $this->send_message($user1, $user4, 'Dope.', 0, $time + 10);
6aa01968
MN
2902
2903 // Retrieve the conversations.
2904 $result = core_message_external::data_for_messagearea_conversations($user1->id);
2905
2906 // We need to execute the return values cleaning process to simulate the web service server.
2907 $result = external_api::clean_returnvalue(core_message_external::data_for_messagearea_conversations_returns(),
2908 $result);
2909
2910 // Confirm the data is correct.
2911 $messages = $result['contacts'];
2912 $this->assertCount(3, $messages);
2913
2914 $message1 = $messages[0];
2915 $message2 = $messages[1];
2916 $message3 = $messages[2];
2917
2918 $this->assertEquals($user4->id, $message1['userid']);
2919 $this->assertTrue($message1['ismessaging']);
2920 $this->assertTrue($message1['sentfromcurrentuser']);
2921 $this->assertEquals('Dope.', $message1['lastmessage']);
4d146f1a 2922 $this->assertEquals($messageid3, $message1['messageid']);
cb805753 2923 $this->assertNull($message1['isonline']);
4d146f1a 2924 $this->assertFalse($message1['isread']);
6aa01968 2925 $this->assertFalse($message1['isblocked']);
4d146f1a 2926 $this->assertEquals(1, $message1['unreadcount']);
6aa01968
MN
2927
2928 $this->assertEquals($user3->id, $message2['userid']);
2929 $this->assertTrue($message2['ismessaging']);
2930 $this->assertFalse($message2['sentfromcurrentuser']);
2931 $this->assertEquals('Cool.', $message2['lastmessage']);
4d146f1a 2932 $this->assertEquals($messageid2, $message2['messageid']);
cb805753 2933 $this->assertNull($message2['isonline']);
6aa01968
MN
2934 $this->assertFalse($message2['isread']);
2935 $this->assertFalse($message2['isblocked']);
2936 $this->assertEquals(2, $message2['unreadcount']);
2937
2938 $this->assertEquals($user2->id, $message3['userid']);
2939 $this->assertTrue($message3['ismessaging']);
2940 $this->assertFalse($message3['sentfromcurrentuser']);
2941 $this->assertEquals('Word.', $message3['lastmessage']);
4d146f1a 2942 $this->assertEquals($messageid1, $message3['messageid']);
cb805753 2943 $this->assertNull($message3['isonline']);
6aa01968
MN
2944 $this->assertFalse($message3['isread']);
2945 $this->assertFalse($message3['isblocked']);
2946 $this->assertEquals(2, $message3['unreadcount']);
2947 }
2948
2949 /**
2950 * Tests retrieving conversations as another user.
2951 */
2952 public function test_messagearea_conversations_as_other_user() {
2953 $this->resetAfterTest(true);
2954
2955 // Set as admin.
2956 $this->setAdminUser();
2957
2958 // Create some users.
2959 $user1 = self::getDataGenerator()->create_user();
2960 $user2 = self::getDataGenerator()->create_user();
2961 $user3 = self::getDataGenerator()->create_user();
2962 $user4 = self::getDataGenerator()->create_user();
2963
2964 // Send some messages back and forth, have some different conversations with different users.
2965 $time = time();
2966 $this->send_message($user1, $user2, 'Yo!', 0, $time);
2967 $this->send_message($user2, $user1, 'Sup mang?', 0, $time + 1);
2968 $this->send_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 2);
4d146f1a 2969 $messageid1 = $this->send_message($user2, $user1, 'Word.', 0, $time + 3);
6aa01968
MN
2970
2971 $this->send_message($user1, $user3, 'Booyah', 0, $time + 4);
2972 $this->send_message($user3, $user1, 'Whaaat?', 0, $time + 5);
2973 $this->send_message($user1, $user3, 'Nothing.', 0, $time + 6);
4d146f1a 2974 $messageid2 = $this->send_message($user3, $user1, 'Cool.', 0, $time + 7);
6aa01968
MN
2975
2976 $this->send_message($user1, $user4, 'Hey mate, you see the new messaging UI in Moodle?', 0, $time + 8);
2977 $this->send_message($user4, $user1, 'Yah brah, it\'s pretty rad.', 0, $time + 9);
4d146f1a 2978 $messageid3 = $this->send_message($user1, $user4, 'Dope.', 0, $time + 10);
6aa01968
MN
2979
2980 // Retrieve the conversations.
2981 $result = core_message_external::data_for_messagearea_conversations($user1->id);
2982
2983 // We need to execute the return values cleaning process to simulate the web service server.
2984 $result = external_api::clean_returnvalue(core_message_external::data_for_messagearea_conversations_returns(),
2985 $result);
2986
2987 // Confirm the data is correct.
2988 $messages = $result['contacts'];
2989 $this->assertCount(3, $messages);
2990
2991 $message1 = $messages[0];
2992 $message2 = $messages[1];
2993 $message3 = $messages[2];
2994
2995 $this->assertEquals($user4->id, $message1['userid']);
2996 $this->assertTrue($message1['ismessaging']);
2997 $this->assertTrue($message1['sentfromcurrentuser']);
2998 $this->assertEquals('Dope.', $message1['lastmessage']);
4d146f1a 2999 $this->assertEquals($messageid3, $message1['messageid']);
6aa01968 3000 $this->assertFalse($message1['isonline']);
4d146f1a 3001 $this->assertFalse($message1['isread']);
6aa01968 3002 $this->assertFalse($message1['isblocked']);
4d146f1a 3003 $this->assertEquals(1, $message1['unreadcount']);
6aa01968
MN
3004
3005 $this->assertEquals($user3->id, $message2['userid']);
3006 $this->assertTrue($message2['ismessaging']);
3007 $this->assertFalse($message2['sentfromcurrentuser']);
3008 $this->assertEquals('Cool.', $message2['lastmessage']);
4d146f1a 3009 $this->assertEquals($messageid2, $message2['messageid']);
6aa01968
MN
3010 $this->assertFalse($message2['isonline']);
3011 $this->assertFalse($message2['isread']);
3012 $this->assertFalse($message2['isblocked']);
3013 $this->assertEquals(2, $message2['unreadcount']);
3014
3015 $this->assertEquals($user2->id, $message3['userid']);
3016 $this->assertTrue($message3['ismessaging']);
3017 $this->assertFalse($message3['sentfromcurrentuser']);
3018 $this->assertEquals('Word.', $message3['lastmessage']);
4d146f1a 3019 $this->assertEquals($messageid1, $message3['messageid']);
6aa01968
MN
3020 $this->assertFalse($message3['isonline']);
3021 $this->assertFalse($message3['isread']);
3022 $this->assertFalse($message3['isblocked']);
3023 $this->assertEquals(2, $message3['unreadcount']);
3024 }
3025
3026 /**
3027 * Tests retrieving conversations as another user without the proper capabilities.
3028 */
3029 public function test_messagearea_conversations_as_other_user_without_cap() {
3030 $this->resetAfterTest(true);
3031
3032 // Create some users.
3033 $user1 = self::getDataGenerator()->create_user();
3034 $user2 = self::getDataGenerator()->create_user();
3035
3036 // The person retrieving the conversations for another user.
3037 $this->setUser($user1);
3038
3039 // Ensure an exception is thrown.
3040 $this->expectException('moodle_exception');
3041 core_message_external::data_for_messagearea_conversations($user2->id);
3042 }
3043
3044 /**
3045 * Tests retrieving conversations with messaging disabled.
3046 */
3047 public function test_messagearea_conversations_messaging_disabled() {
3048 global $CFG;
3049
3050 $this->resetAfterTest(true);
3051
3052 // Create some skeleton data just so we can call the WS.
3053 $user = self::getDataGenerator()->create_user();
3054
3055 // The person retrieving the conversations.
3056 $this->setUser($user);
3057
3058 // Disable messaging.
3059 $CFG->messaging = 0;
3060
3061 // Ensure an exception is thrown.
3062 $this->expectException('moodle_exception');
3063 core_message_external::data_for_messagearea_conversations($user->id);
3064 }
3065
3066 /**
3067 * Tests retrieving contacts.
3068 */
3069 public function test_messagearea_contacts() {
3070 $this->resetAfterTest(true);
3071
3072 // Create some users.
3073 $user1 = self::getDataGenerator()->create_user();
3074
3075 // Set as the user.
3076 $this->setUser($user1);
3077
3078 $user2 = new stdClass();
3079 $user2->firstname = 'User';
3080 $user2->lastname = 'A';
3081 $user2 = self::getDataGenerator()->create_user($user2);
3082
3083 $user3 = new stdClass();
3084 $user3->firstname = 'User';
3085 $user3->lastname = 'B';
3086 $user3 = self::getDataGenerator()->create_user($user3);
3087
3088 $user4 = new stdClass();
3089 $user4->firstname = 'User';
3090 $user4->lastname = 'C';
3091 $user4 = self::getDataGenerator()->create_user($user4);
3092
3093 $user5 = new stdClass();
3094 $user5->firstname = 'User';
3095 $user5->lastname = 'D';
3096 $user5 = self::getDataGenerator()->create_user($user5);
3097
3098 // Add some users as contacts.
f219eac7
MN
3099 \core_message\api::add_contact($user1->id, $user2->id);
3100 \core_message\api::add_contact($user1->id, $user3->id);
3101 \core_message\api::add_contact($user1->id, $user4->id);
6aa01968
MN
3102
3103 // Retrieve the contacts.
3104 $result = core_message_external::data_for_messagearea_contacts($user1->id);
3105
3106 // We need to execute the return values cleaning process to simulate the web service server.
3107 $result = external_api::clean_returnvalue(core_message_external::data_for_messagearea_contacts_returns(),
3108 $result);
3109
3110 // Confirm the data is correct.
3111 $contacts = $result['contacts'];
f219eac7 3112 usort($contacts, ['static', 'sort_contacts']);
6aa01968
MN
3113 $this->assertCount(3, $contacts);
3114
3115 $contact1 = $contacts[0];
3116 $contact2 = $contacts[1];
3117 $contact3 = $contacts[2];
3118
3119 $this->assertEquals($user2->id, $contact1['userid']);
3120 $this->assertFalse($contact1['ismessaging']);
3121 $this->assertFalse($contact1['sentfromcurrentuser']);
3122 $this->assertNull($contact1['lastmessage']);
3123 $this->assertNull($contact1['messageid']);
cb805753 3124 $this->assertNull($contact1['isonline']);
6aa01968
MN
3125 $this->assertFalse($contact1['isread']);
3126 $this->assertFalse($contact1['isblocked']);
3127 $this->assertNull($contact1['unreadcount']);
3128
3129 $this->assertEquals($user3->id, $contact2['userid']);
3130 $this->assertFalse($contact2['ismessaging']);
3131 $this->assertFalse($contact2['sentfromcurrentuser']);
3132 $this->assertNull($contact2['lastmessage']);
3133 $this->assertNull($contact2['messageid']);
cb805753 3134 $this->assertNull($contact2['isonline']);
6aa01968
MN
3135 $this->assertFalse($contact2['isread']);
3136 $this->assertFalse($contact2['isblocked']);
3137 $this->assertNull($contact2['unreadcount']);
3138
3139 $this->assertEquals($user4->id, $contact3['userid']);
3140 $this->assertFalse($contact3['ismessaging']);
3141 $this->assertFalse($contact3['sentfromcurrentuser']);
3142 $this->assertNull($contact3['lastmessage']);
3143 $this->assertNull($contact3['messageid']);
cb805753 3144 $this->assertNull($contact3['isonline']);
6aa01968
MN
3145 $this->assertFalse($contact3['isread']);
3146 $this->assertFalse($contact3['isblocked']);
3147 $this->assertNull($contact3['unreadcount']);
3148 }
3149
3150 /**
3151 * Tests retrieving contacts as another user.
3152 */
3153 public function test_messagearea_contacts_as_other_user() {
3154 $this->resetAfterTest(true);
3155
3156 $this->setAdminUser();
3157
3158 // Create some users.
3159 $user1 = self::getDataGenerator()->create_user();
3160
3161 $user2 = new stdClass();
3162 $user2->firstname = 'User';
3163 $user2->lastname = 'A';
3164 $user2 = self::getDataGenerator()->create_user($user2);
3165
3166 $user3 = new stdClass();
3167 $user3->firstname = 'User';
3168 $user3->lastname = 'B';
3169 $user3 = self::getDataGenerator()->create_user($user3);
3170
3171 $user4 = new stdClass();
3172 $user4->firstname = 'User';
3173 $user4->lastname = 'C';
3174 $user4 = self::getDataGenerator()->create_user($user4);
3175
3176 $user5 = new stdClass();
3177 $user5->firstname = 'User';
3178 $user5->lastname = 'D';
3179 $user5 = self::getDataGenerator()->create_user($user5);
3180
3181 // Add some users as contacts.
f219eac7
MN
3182 \core_message\api::add_contact($user1->id, $user2->id);
3183 \core_message\api::add_contact($user1->id, $user3->id);
3184 \core_message\api::add_contact($user1->id, $user4->id);
6aa01968
MN
3185
3186 // Retrieve the contacts.
3187 $result = core_message_external::data_for_messagearea_contacts($user1->id);
3188
3189 // We need to execute the return values cleaning process to simulate the web service server.
3190 $result = external_api::clean_returnvalue(core_message_external::data_for_messagearea_contacts_returns(),
3191 $result);
3192
3193 // Confirm the data is correct.
3194 $contacts = $result['contacts'];
f219eac7 3195 usort($contacts, ['static', 'sort_contacts']);
6aa01968
MN
3196 $this->assertCount(3, $contacts);
3197
3198 $contact1 = $contacts[0];
3199 $contact2 = $contacts[1];
3200 $contact3 = $contacts[2];
3201
3202 $this->assertEquals($user2->id, $contact1['userid']);
3203 $this->assertFalse($contact1['ismessaging']);
3204 $this->assertFalse($contact1['sentfromcurrentuser']);
3205 $this->assertNull($contact1['lastmessage']);
3206 $this->assertNull($contact1['messageid']);
3207 $this->assertFalse($contact1['isonline']);
3208 $this->assertFalse($contact1['isread']);
3209 $this->assertFalse($contact1['isblocked']);
3210 $this->assertNull($contact1['unreadcount']);
3211
3212 $this->assertEquals($user3->id, $contact2['userid']);
3213 $this->assertFalse($contact2['ismessaging']);
3214 $this->assertFalse($contact2['sentfromcurrentuser']);
3215 $this->assertNull($contact2['lastmessage']);
3216 $this->assertNull($contact2['messageid']);
3217 $this->assertFalse($contact2['isonline']);
3218 $this->assertFalse($contact2['isread']);
3219 $this->assertFalse($contact2['isblocked']);
3220 $this->assertNull($contact2['unreadcount']);
3221
3222 $this->assertEquals($user4->id, $contact3['userid']);
3223 $this->assertFalse($contact3['ismessaging']);
3224 $this->assertFalse($contact3['sentfromcurrentuser']);
3225 $this->assertNull($contact3['lastmessage']);
3226 $this->assertNull($contact3['messageid']);
3227 $this->assertFalse($contact3['isonline']);
3228 $this->assertFalse($contact3['isread']);
3229 $this->assertFalse($contact3['isblocked']);
3230 $this->assertNull($contact3['unreadcount']);
3231 }
3232
3233 /**
3234 * Tests retrieving contacts as another user without the proper capabilities.
3235 */
3236 public function test_messagearea_contacts_as_other_user_without_cap() {
3237 $this->resetAfterTest(true);
3238
3239 // Create some users.
3240 $user1 = self::getDataGenerator()->create_user();
3241 $user2 = self::getDataGenerator()->create_user();
3242
3243 // The person retrieving the contacts for another user.
3244 $this->setUser($user1);
3245
3246 // Perform the WS call and ensure an exception is thrown.
3247 $this->expectException('moodle_exception');
3248 core_message_external::data_for_messagearea_contacts($user2->id);
3249 }
3250
3251 /**
3252 * Tests retrieving contacts with messaging disabled.
3253 */
3254 public function test_messagearea_contacts_messaging_disabled() {
3255 global $CFG;
3256
3257 $this->resetAfterTest(true);
3258
3259 // Create some skeleton data just so we can call the WS.
3260 $user = self::getDataGenerator()->create_user();
3261
3262 // The person retrieving the contacts.
3263 $this->setUser($user);
3264
3265 // Disable messaging.
3266 $CFG->messaging = 0;
3267
3268 // Perform the WS call and ensure we are shown that it is disabled.
3269 $this->expectException('moodle_exception');
3270 core_message_external::data_for_messagearea_contacts($user->id);
3271 }
3272
c9b0f33f
MN
3273 /**
3274 * Tests retrieving contacts.
3275 */
3276 public function test_get_user_contacts() {
3277 $this->resetAfterTest(true);
3278
3279 // Create some users.
3280 $user1 = self::getDataGenerator()->create_user();
3281
3282 // Set as the user.
3283 $this->setUser($user1);
3284
3285 $user2 = new stdClass();
3286 $user2->firstname = 'User';
3287 $user2->lastname = 'A';
3288 $user2 = self::getDataGenerator()->create_user($user2);
3289
3290 $user3 = new stdClass();
3291 $user3->firstname = 'User';
3292 $user3->lastname = 'B';
3293 $user3 = self::getDataGenerator()->create_user($user3);
3294
3295 $user4 = new stdClass();
3296 $user4->firstname = 'User';
3297 $user4->lastname = 'C';
3298 $user4 = self::getDataGenerator()->create_user($user4);
3299
3300 $user5 = new stdClass();
3301 $user5->firstname = 'User';
3302 $user5->lastname = 'D';
3303 $user5 = self::getDataGenerator()->create_user($user5);
3304
3305 // Add some users as contacts.
3306 \core_message\api::add_contact($user1->id, $user2->id);
3307 \core_message\api::add_contact($user1->id, $user3->id);
3308 \core_message\api::add_contact($user1->id, $user4->id);
3309
3310 // Retrieve the contacts.
3311 $result = core_message_external::get_user_contacts($user1->id);
3312
3313 // We need to execute the return values cleaning process to simulate the web service server.
3314 $result = external_api::clean_returnvalue(core_message_external::get_user_contacts_returns(),
3315 $result);
3316
3317 // Confirm the data is correct.
3318 $contacts = $result;
3319 usort($contacts, ['static', 'sort_contacts_id']);
3320 $this->assertCount(3, $contacts);
3321
3322 $contact1 = array_shift($contacts);
3323 $contact2 = array_shift($contacts);
3324 $contact3 = array_shift($contacts);
3325
3326 $this->assertEquals($user2->id, $contact1['id']);
3327 $this->assertEquals(fullname($user2), $contact1['fullname']);
3328 $this->assertTrue($contact1['iscontact']);
3329
3330 $this->assertEquals($user3->id, $contact2['id']);
3331 $this->assertEquals(fullname($user3), $contact2['fullname']);
3332 $this->assertTrue($contact2['iscontact']);
3333
3334 $this->assertEquals($user4->id, $contact3['id']);
3335 $this->assertEquals(fullname($user4), $contact3['fullname']);
3336 $this->assertTrue($contact3['iscontact']);
3337 }
3338
3339 /**
3340 * Tests retrieving contacts as another user.
3341 */
3342 public function test_get_user_contacts_as_other_user() {
3343 $this->resetAfterTest(true);
3344
3345 $this->setAdminUser();
3346
3347 // Create some users.
3348 $user1 = self::getDataGenerator()->create_user();
3349
3350 $user2 = new stdClass();
3351 $user2->firstname = 'User';
3352 $user2->lastname = 'A';
3353 $user2 = self::getDataGenerator()->create_user($user2);
3354
3355 $user3 = new stdClass();
3356 $user3->firstname = 'User';
3357 $user3->lastname = 'B';
3358 $user3 = self::getDataGenerator()->create_user($user3);
3359
3360 $user4 = new stdClass();
3361 $user4->firstname = 'User';
3362 $user4->lastname = 'C';
3363 $user4 = self::getDataGenerator()->create_user($user4);
3364
3365 $user5 = new stdClass();
3366 $user5->firstname = 'User';
3367 $user5->lastname = 'D';
3368 $user5 = self::getDataGenerator()->create_user($user5);
3369
3370 // Add some users as contacts.
3371 \core_message\api::add_contact($user1->id, $user2->id);
3372 \core_message\api::add_contact($user1->id, $user3->id);
3373 \core_message\api::add_contact($user1->id, $user4->id);
3374
3375 // Retrieve the contacts.
3376 $result = core_message_external::get_user_contacts($user1->id);
3377
3378 // We need to execute the return values cleaning process to simulate the web service server.
3379 $result = external_api::clean_returnvalue(core_message_external::get_user_contacts_returns(),
3380 $result);
3381
3382 // Confirm the data is correct.
3383 $contacts = $result;
3384 usort($contacts, ['static', 'sort_contacts_id']);
3385 $this->assertCount(3, $contacts);
3386
3387 $contact1 = array_shift($contacts);
3388 $contact2 = array_shift($contacts);
3389 $contact3 = array_shift($contacts);
3390
3391 $this->assertEquals($user2->id, $contact1['id']);
3392 $this->assertEquals(fullname($user2), $contact1['fullname']);
3393 $this->assertTrue($contact1['iscontact']);
3394
3395 $this->assertEquals($user3->id, $contact2['id']);
3396 $this->assertEquals(fullname($user3), $contact2['fullname']);
3397 $this->assertTrue($contact2['iscontact']);
3398
3399 $this->assertEquals($user4->id, $contact3['id']);
3400 $this->assertEquals(fullname($user4), $contact3['fullname']);
3401 $this->assertTrue($contact3['iscontact']);
3402 }
3403
3404 /**
3405 * Tests retrieving contacts as another user without the proper capabilities.
3406 */
3407 public function test_get_user_contacts_as_other_user_without_cap() {
3408 $this->resetAfterTest(true);
3409
3410 // Create some users.
3411 $user1 = self::getDataGenerator()->create_user();
3412 $user2 = self::getDataGenerator()->create_user();
3413
3414 // The person retrieving the contacts for another user.
3415 $this->setUser($user1);
3416
3417 // Perform the WS call and ensure an exception is thrown.
3418 $this->expectException('moodle_exception');
3419 core_message_external::get_user_contacts($user2->id);
3420 }
3421
3422 /**
3423 * Tests retrieving contacts with messaging disabled.
3424 */
3425 public function test_get_user_contacts_messaging_disabled() {
3426 global $CFG;
3427
3428 $this->resetAfterTest(true);
3429
3430 // Create some skeleton data just so we can call the WS.
3431 $user = self::getDataGenerator()->create_user();
3432
3433 // The person retrieving the contacts.
3434 $this->setUser($user);
3435
3436 // Disable messaging.
3437 $CFG->messaging = 0;
3438
3439 // Perform the WS call and ensure we are shown that it is disabled.
3440 $this->expectException('moodle_exception');
3441 core_message_external::get_user_contacts($user->id);
3442 }
3443
3444 /**
3445 * Test getting contacts when there are no results.
3446 */
3447 public function test_get_user_contacts_no_results() {
3448 $this->resetAfterTest();
3449
3450 $user1 = self::getDataGenerator()->create_user();
3451
3452 $this->setUser($user1);
3453
3454 $requests = core_message_external::get_user_contacts($user1->id);
3455 $requests = external_api::clean_returnvalue(core_message_external::get_user_contacts_returns(), $requests);
3456
3457 $this->assertEmpty($requests);
3458 }
3459
6aa01968
MN
3460 /**
3461 * Tests retrieving messages.
3462 */
3463 public function test_messagearea_messages() {
3464 $this->resetAfterTest(true);
3465
3466 // Create some users.
3467 $user1 = self::getDataGenerator()->create_user();
3468 $user2 = self::getDataGenerator()->create_user();
3469
3470 // The person asking for the messages.
3471 $this->setUser($user1);
3472
3473 // Send some messages back and forth.
3474 $time = time();
3475 $this->send_message($user1, $user2, 'Yo!', 0, $time);
3476 $this->send_message($user2, $user1, 'Sup mang?', 0, $time + 1);
3477 $this->send_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 2);
3478 $this->send_message($user2, $user1, 'Word.', 0, $time + 3);
3479
3480 // Retrieve the messages.
3481 $result = core_message_external::data_for_messagearea_messages($user1->id, $user2->id);
3482
3483 // We need to execute the return values cleaning process to simulate the web service server.
3484 $result = external_api::clean_returnvalue(core_message_external::data_for_messagearea_messages_returns(),
3485 $result);
3486
3487 // Check the results are correct.
3488 $this->assertTrue($result['iscurrentuser']);
3489 $this->assertEquals($user1->id, $result['currentuserid']);
3490 $this->assertEquals($user2->id, $result['otheruserid']);
3491 $this->assertEquals(fullname($user2), $result['otheruserfullname']);
cb805753 3492 $this->assertNull($result['isonline']);
6aa01968
MN
3493
3494 // Confirm the message data is correct.