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