weekly release 3.6dev
[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
MN
76 if (!$conversationid = \core_message\api::get_conversation_between_users([$userfrom->id, $userto->id])) {
77 $conversationid = \core_message\api::create_conversation_between_users([$userfrom->id,
78 $userto->id]);
883ce421
MN
79 }
80
81 // Ok, send the message.
d6731600
FM
82 $record = new stdClass();
83 $record->useridfrom = $userfrom->id;
883ce421 84 $record->conversationid = $conversationid;
d6731600
FM
85 $record->subject = 'No subject';
86 $record->fullmessage = $message;
883ce421 87 $record->smallmessage = $message;
6aa01968 88 $record->timecreated = $time;
4d146f1a 89
883ce421 90 return $DB->insert_record('messages', $record);
d6731600
FM
91 }
92
e6432668
JM
93 /**
94 * Test send_instant_messages
95 */
96 public function test_send_instant_messages() {
97
98 global $DB, $USER, $CFG;
99
100 $this->resetAfterTest(true);
7356e732
EL
101 // Transactions used in tests, tell phpunit use alternative reset method.
102 $this->preventResetByRollback();
e6432668
JM
103
104 // Turn off all message processors (so nothing is really sent)
105 require_once($CFG->dirroot . '/message/lib.php');
106 $messageprocessors = get_message_processors();
107 foreach($messageprocessors as $messageprocessor) {
108 $messageprocessor->enabled = 0;
109 $DB->update_record('message_processors', $messageprocessor);
110 }
111
112 // Set the required capabilities by the external function
113 $contextid = context_system::instance()->id;
114 $roleid = $this->assignUserCapability('moodle/site:sendmessage', $contextid);
115
116 $user1 = self::getDataGenerator()->create_user();
117
118 // Create test message data.
119 $message1 = array();
120 $message1['touserid'] = $user1->id;
121 $message1['text'] = 'the message.';
122 $message1['clientmsgid'] = 4;
123 $messages = array($message1);
124
125 $sentmessages = core_message_external::send_instant_messages($messages);
126
fb695f6e
JM
127 // We need to execute the return values cleaning process to simulate the web service server.
128 $sentmessages = external_api::clean_returnvalue(core_message_external::send_instant_messages_returns(), $sentmessages);
129
883ce421
MN
130 $sql = "SELECT m.*, mcm.userid as useridto
131 FROM {messages} m
132 INNER JOIN {message_conversations} mc
133 ON m.conversationid = mc.id
134 INNER JOIN {message_conversation_members} mcm
135 ON mcm.conversationid = mc.id
136 WHERE mcm.userid != ?
137 AND m.id = ?";
138 $themessage = $DB->get_record_sql($sql, [$USER->id, $sentmessages[0]['msgid']]);
e6432668
JM
139
140 // Confirm that the message was inserted correctly.
141 $this->assertEquals($themessage->useridfrom, $USER->id);
142 $this->assertEquals($themessage->useridto, $message1['touserid']);
143 $this->assertEquals($themessage->smallmessage, $message1['text']);
144 $this->assertEquals($sentmessages[0]['clientmsgid'], $message1['clientmsgid']);
145 }
d6731600
FM
146
147 /**
148 * Test create_contacts.
149 */
150 public function test_create_contacts() {
151 $this->resetAfterTest(true);
152
153 $user1 = self::getDataGenerator()->create_user();
154 $user2 = self::getDataGenerator()->create_user();
155 $user3 = self::getDataGenerator()->create_user();
156 $user4 = self::getDataGenerator()->create_user();
157 $user5 = self::getDataGenerator()->create_user();
158 $this->setUser($user1);
159
160 // Adding a contact.
161 $return = core_message_external::create_contacts(array($user2->id));
162 $return = external_api::clean_returnvalue(core_message_external::create_contacts_returns(), $return);
163 $this->assertEquals(array(), $return);
164
165 // Adding a contact who is already a contact.
166 $return = core_message_external::create_contacts(array($user2->id));
167 $return = external_api::clean_returnvalue(core_message_external::create_contacts_returns(), $return);
168 $this->assertEquals(array(), $return);
169
170 // Adding multiple contacts.
171 $return = core_message_external::create_contacts(array($user3->id, $user4->id));
172 $return = external_api::clean_returnvalue(core_message_external::create_contacts_returns(), $return);
173 $this->assertEquals(array(), $return);
174
175 // Adding a non-existing user.
176 $return = core_message_external::create_contacts(array(99999));
177 $return = external_api::clean_returnvalue(core_message_external::create_contacts_returns(), $return);
178 $this->assertCount(1, $return);
179 $return = array_pop($return);
180 $this->assertEquals($return['warningcode'], 'contactnotcreated');
181 $this->assertEquals($return['itemid'], 99999);
182
183 // Adding contacts with valid and invalid parameters.
184 $return = core_message_external::create_contacts(array($user5->id, 99999));
185 $return = external_api::clean_returnvalue(core_message_external::create_contacts_returns(), $return);
186 $this->assertCount(1, $return);
187 $return = array_pop($return);
188 $this->assertEquals($return['warningcode'], 'contactnotcreated');
189 $this->assertEquals($return['itemid'], 99999);
343ba16c
SL
190
191 // Try to add a contact to another user, should throw an exception.
192 // All assertions must be added before this point.
193 $this->expectException('required_capability_exception');
194 core_message_external::create_contacts(array($user2->id), $user3->id);
d6731600
FM
195 }
196
197 /**
198 * Test delete_contacts.
199 */
200 public function test_delete_contacts() {
201 $this->resetAfterTest(true);
202
203 $user1 = self::getDataGenerator()->create_user();
204 $user2 = self::getDataGenerator()->create_user();
205 $user3 = self::getDataGenerator()->create_user();
206 $user4 = self::getDataGenerator()->create_user();
207 $user5 = self::getDataGenerator()->create_user();
208 $user6 = self::getDataGenerator()->create_user();
209 $this->setUser($user1);
210 $this->assertEquals(array(), core_message_external::create_contacts(
211 array($user3->id, $user4->id, $user5->id, $user6->id)));
212
213 // Removing a non-contact.
214 $return = core_message_external::delete_contacts(array($user2->id));
215 $this->assertNull($return);
216
217 // Removing one contact.
218 $return = core_message_external::delete_contacts(array($user3->id));
219 $this->assertNull($return);
220
221 // Removing multiple contacts.
222 $return = core_message_external::delete_contacts(array($user4->id, $user5->id));
223 $this->assertNull($return);
224
225 // Removing contact from unexisting user.
226 $return = core_message_external::delete_contacts(array(99999));
227 $this->assertNull($return);
228
229 // Removing mixed valid and invalid data.
230 $return = core_message_external::delete_contacts(array($user6->id, 99999));
231 $this->assertNull($return);
343ba16c
SL
232
233 // Try to delete a contact of another user contact list, should throw an exception.
234 // All assertions must be added before this point.
235 $this->expectException('required_capability_exception');
236 core_message_external::delete_contacts(array($user2->id), $user3->id);
d6731600
FM
237 }
238
239 /**
240 * Test block_contacts.
241 */
242 public function test_block_contacts() {
243 $this->resetAfterTest(true);
244
245 $user1 = self::getDataGenerator()->create_user();
246 $user2 = self::getDataGenerator()->create_user();
247 $user3 = self::getDataGenerator()->create_user();
248 $user4 = self::getDataGenerator()->create_user();
249 $user5 = self::getDataGenerator()->create_user();
250 $this->setUser($user1);
251 $this->assertEquals(array(), core_message_external::create_contacts(array($user3->id, $user4->id, $user5->id)));
252
253 // Blocking a contact.
254 $return = core_message_external::block_contacts(array($user2->id));
255 $return = external_api::clean_returnvalue(core_message_external::block_contacts_returns(), $return);
256 $this->assertEquals(array(), $return);
257
258 // Blocking a contact who is already a contact.
259 $return = core_message_external::block_contacts(array($user2->id));
260 $return = external_api::clean_returnvalue(core_message_external::block_contacts_returns(), $return);
261 $this->assertEquals(array(), $return);
262
263 // Blocking multiple contacts.
264 $return = core_message_external::block_contacts(array($user3->id, $user4->id));
265 $return = external_api::clean_returnvalue(core_message_external::block_contacts_returns(), $return);
266 $this->assertEquals(array(), $return);
267
268 // Blocking a non-existing user.
269 $return = core_message_external::block_contacts(array(99999));
270 $return = external_api::clean_returnvalue(core_message_external::block_contacts_returns(), $return);
271 $this->assertCount(1, $return);
272 $return = array_pop($return);
273 $this->assertEquals($return['warningcode'], 'contactnotblocked');
274 $this->assertEquals($return['itemid'], 99999);
275
276 // Blocking contacts with valid and invalid parameters.
277 $return = core_message_external::block_contacts(array($user5->id, 99999));
278 $return = external_api::clean_returnvalue(core_message_external::block_contacts_returns(), $return);
279 $this->assertCount(1, $return);
280 $return = array_pop($return);
281 $this->assertEquals($return['warningcode'], 'contactnotblocked');
282 $this->assertEquals($return['itemid'], 99999);
343ba16c
SL
283
284 // Try to block a contact of another user contact list, should throw an exception.
285 // All assertions must be added before this point.
286 $this->expectException('required_capability_exception');
287 core_message_external::block_contacts(array($user2->id), $user3->id);
d6731600
FM
288 }
289
290 /**
291 * Test unblock_contacts.
292 */
293 public function test_unblock_contacts() {
294 $this->resetAfterTest(true);
295
296 $user1 = self::getDataGenerator()->create_user();
297 $user2 = self::getDataGenerator()->create_user();
298 $user3 = self::getDataGenerator()->create_user();
299 $user4 = self::getDataGenerator()->create_user();
300 $user5 = self::getDataGenerator()->create_user();
301 $user6 = self::getDataGenerator()->create_user();
302 $this->setUser($user1);
303 $this->assertEquals(array(), core_message_external::create_contacts(
304 array($user3->id, $user4->id, $user5->id, $user6->id)));
305
306 // Removing a non-contact.
307 $return = core_message_external::unblock_contacts(array($user2->id));
308 $this->assertNull($return);
309
310 // Removing one contact.
311 $return = core_message_external::unblock_contacts(array($user3->id));
312 $this->assertNull($return);
313
314 // Removing multiple contacts.
315 $return = core_message_external::unblock_contacts(array($user4->id, $user5->id));
316 $this->assertNull($return);
317
318 // Removing contact from unexisting user.
319 $return = core_message_external::unblock_contacts(array(99999));
320 $this->assertNull($return);
321
322 // Removing mixed valid and invalid data.
323 $return = core_message_external::unblock_contacts(array($user6->id, 99999));
324 $this->assertNull($return);
325
343ba16c
SL
326 // Try to unblock a contact of another user contact list, should throw an exception.
327 // All assertions must be added before this point.
328 $this->expectException('required_capability_exception');
329 core_message_external::unblock_contacts(array($user2->id), $user3->id);
d6731600
FM
330 }
331
332 /**
333 * Test get_contacts.
334 */
335 public function test_get_contacts() {
336 $this->resetAfterTest(true);
337
338 $user1 = self::getDataGenerator()->create_user();
339 $user_stranger = self::getDataGenerator()->create_user();
340 $user_offline1 = self::getDataGenerator()->create_user();
341 $user_offline2 = self::getDataGenerator()->create_user();
342 $user_offline3 = self::getDataGenerator()->create_user();
343 $user_online = new stdClass();
344 $user_online->lastaccess = time();
345 $user_online = self::getDataGenerator()->create_user($user_online);
346 $user_blocked = self::getDataGenerator()->create_user();
0b074e88 347 $noreplyuser = core_user::get_user(core_user::NOREPLY_USER);
d6731600
FM
348
349 // Login as user1.
350 $this->setUser($user1);
351 $this->assertEquals(array(), core_message_external::create_contacts(
352 array($user_offline1->id, $user_offline2->id, $user_offline3->id, $user_online->id)));
353
354 // User_stranger sends a couple of messages to user1.
355 $this->send_message($user_stranger, $user1, 'Hello there!');
356 $this->send_message($user_stranger, $user1, 'How you goin?');
357 $this->send_message($user_stranger, $user1, 'Cya!');
0b074e88 358 $this->send_message($noreplyuser, $user1, 'I am not a real user');
d6731600
FM
359
360 // User_blocked sends a message to user1.
361 $this->send_message($user_blocked, $user1, 'Here, have some spam.');
362
363 // Retrieve the contacts of the user.
364 $this->setUser($user1);
365 $contacts = core_message_external::get_contacts();
366 $contacts = external_api::clean_returnvalue(core_message_external::get_contacts_returns(), $contacts);
367 $this->assertCount(3, $contacts['offline']);
368 $this->assertCount(1, $contacts['online']);
0b074e88 369 $this->assertCount(3, $contacts['strangers']);
d6731600
FM
370 core_message_external::block_contacts(array($user_blocked->id));
371 $contacts = core_message_external::get_contacts();
372 $contacts = external_api::clean_returnvalue(core_message_external::get_contacts_returns(), $contacts);
373 $this->assertCount(3, $contacts['offline']);
374 $this->assertCount(1, $contacts['online']);
0b074e88 375 $this->assertCount(2, $contacts['strangers']);
d6731600
FM
376
377 // Checking some of the fields returned.
378 $stranger = array_pop($contacts['strangers']);
01393790 379
0b074e88
JL
380 $this->assertEquals(core_user::NOREPLY_USER, $stranger['id']);
381 $this->assertEquals(1, $stranger['unread']);
01393790
JL
382
383 // Check that deleted users are not returned.
384 delete_user($user_offline1);
385 delete_user($user_stranger);
386 delete_user($user_online);
387 $contacts = core_message_external::get_contacts();
388 $contacts = external_api::clean_returnvalue(core_message_external::get_contacts_returns(), $contacts);
389 $this->assertCount(2, $contacts['offline']);
390 $this->assertCount(0, $contacts['online']);
391 $this->assertCount(1, $contacts['strangers']);
d6731600
FM
392 }
393
394 /**
395 * Test search_contacts.
52f3e060 396 * @expectedException moodle_exception
d6731600
FM
397 */
398 public function test_search_contacts() {
399 global $DB;
400 $this->resetAfterTest(true);
401
402 $course1 = $this->getDataGenerator()->create_course();
403 $course2 = $this->getDataGenerator()->create_course();
404
405 $user1 = new stdClass();
406 $user1->firstname = 'X';
407 $user1->lastname = 'X';
408 $user1 = $this->getDataGenerator()->create_user($user1);
409 $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
410 $this->getDataGenerator()->enrol_user($user1->id, $course2->id);
411
412 $user2 = new stdClass();
413 $user2->firstname = 'Eric';
414 $user2->lastname = 'Cartman';
415 $user2 = self::getDataGenerator()->create_user($user2);
416 $user3 = new stdClass();
417 $user3->firstname = 'Stan';
418 $user3->lastname = 'Marsh';
419 $user3 = self::getDataGenerator()->create_user($user3);
420 self::getDataGenerator()->enrol_user($user3->id, $course1->id);
421 $user4 = new stdClass();
422 $user4->firstname = 'Kyle';
423 $user4->lastname = 'Broflovski';
424 $user4 = self::getDataGenerator()->create_user($user4);
425 $user5 = new stdClass();
426 $user5->firstname = 'Kenny';
427 $user5->lastname = 'McCormick';
428 $user5 = self::getDataGenerator()->create_user($user5);
429 self::getDataGenerator()->enrol_user($user5->id, $course2->id);
430
d6731600 431 $this->setUser($user1);
2e2d1977 432
d6731600
FM
433 $results = core_message_external::search_contacts('r');
434 $results = external_api::clean_returnvalue(core_message_external::search_contacts_returns(), $results);
2e2d1977
AD
435 $this->assertCount(5, $results); // Users 2 through 5 + admin
436
d6731600
FM
437 $results = core_message_external::search_contacts('r', true);
438 $results = external_api::clean_returnvalue(core_message_external::search_contacts_returns(), $results);
439 $this->assertCount(2, $results);
2e2d1977 440
d6731600
FM
441 $results = core_message_external::search_contacts('Kyle', false);
442 $results = external_api::clean_returnvalue(core_message_external::search_contacts_returns(), $results);
443 $this->assertCount(1, $results);
444 $result = reset($results);
445 $this->assertEquals($user4->id, $result['id']);
2e2d1977 446
d6731600
FM
447 $results = core_message_external::search_contacts('y', false);
448 $results = external_api::clean_returnvalue(core_message_external::search_contacts_returns(), $results);
449 $this->assertCount(2, $results);
2e2d1977 450
d6731600
FM
451 $results = core_message_external::search_contacts('y', true);
452 $results = external_api::clean_returnvalue(core_message_external::search_contacts_returns(), $results);
453 $this->assertCount(1, $results);
454 $result = reset($results);
455 $this->assertEquals($user5->id, $result['id']);
456
457 // Empty query, will throw an exception.
d6731600
FM
458 $results = core_message_external::search_contacts('');
459 }
6ff4464b
JL
460
461 /**
462 * Test get_messages.
463 */
464 public function test_get_messages() {
ea21d637 465 global $CFG, $DB;
6ff4464b
JL
466 $this->resetAfterTest(true);
467
468 $this->preventResetByRollback();
469 // This mark the messages as read!.
470 $sink = $this->redirectMessages();
471
472 $user1 = self::getDataGenerator()->create_user();
473 $user2 = self::getDataGenerator()->create_user();
474 $user3 = self::getDataGenerator()->create_user();
475
476 $course = self::getDataGenerator()->create_course();
477
478 // Send a message from one user to another.
479 message_post_message($user1, $user2, 'some random text 1', FORMAT_MOODLE);
480 message_post_message($user1, $user3, 'some random text 2', FORMAT_MOODLE);
481 message_post_message($user2, $user3, 'some random text 3', FORMAT_MOODLE);
482 message_post_message($user3, $user2, 'some random text 4', FORMAT_MOODLE);
483 message_post_message($user3, $user1, 'some random text 5', FORMAT_MOODLE);
484
485 $this->setUser($user1);
486 // Get read conversations from user1 to user2.
487 $messages = core_message_external::get_messages($user2->id, $user1->id, 'conversations', true, true, 0, 0);
488 $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
489 $this->assertCount(1, $messages['messages']);
490
ea21d637
JL
491 // Delete the message.
492 $message = array_shift($messages['messages']);
883ce421 493 \core_message\api::delete_message($user1->id, $message['id']);
ea21d637
JL
494
495 $messages = core_message_external::get_messages($user2->id, $user1->id, 'conversations', true, true, 0, 0);
496 $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
497 $this->assertCount(0, $messages['messages']);
498
6ff4464b
JL
499 // Get unread conversations from user1 to user2.
500 $messages = core_message_external::get_messages($user2->id, $user1->id, 'conversations', false, true, 0, 0);
501 $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
502 $this->assertCount(0, $messages['messages']);
503
504 // Get read messages send from user1.
505 $messages = core_message_external::get_messages(0, $user1->id, 'conversations', true, true, 0, 0);
506 $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
ea21d637 507 $this->assertCount(1, $messages['messages']);
6ff4464b
JL
508
509 $this->setUser($user2);
510 // Get read conversations from any user to user2.
511 $messages = core_message_external::get_messages($user2->id, 0, 'conversations', true, true, 0, 0);
512 $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
513 $this->assertCount(2, $messages['messages']);
514
ea21d637
JL
515 // Conversations from user3 to user2.
516 $messages = core_message_external::get_messages($user2->id, $user3->id, 'conversations', true, true, 0, 0);
517 $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
518 $this->assertCount(1, $messages['messages']);
519
520 // Delete the message.
521 $message = array_shift($messages['messages']);
883ce421 522 \core_message\api::delete_message($user2->id, $message['id']);
ea21d637
JL
523
524 $messages = core_message_external::get_messages($user2->id, $user3->id, 'conversations', true, true, 0, 0);
525 $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
526 $this->assertCount(0, $messages['messages']);
527
6ff4464b
JL
528 $this->setUser($user3);
529 // Get read notifications received by user3.
530 $messages = core_message_external::get_messages($user3->id, 0, 'notifications', true, true, 0, 0);
531 $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
532 $this->assertCount(0, $messages['messages']);
533
534 // Now, create some notifications...
535 // We are creating fake notifications but based on real ones.
536
127ef540 537 // This one omits notification = 1.
cc350fd9
AD
538 $eventdata = new \core\message\message();
539 $eventdata->courseid = $course->id;
6ff4464b
JL
540 $eventdata->modulename = 'moodle';
541 $eventdata->component = 'enrol_paypal';
542 $eventdata->name = 'paypal_enrolment';
543 $eventdata->userfrom = get_admin();
544 $eventdata->userto = $user1;
545 $eventdata->subject = "Moodle: PayPal payment";
546 $eventdata->fullmessage = "Your PayPal payment is pending.";
547 $eventdata->fullmessageformat = FORMAT_PLAIN;
548 $eventdata->fullmessagehtml = '';
549 $eventdata->smallmessage = '';
550 message_send($eventdata);
551
cc350fd9 552 $message = new \core\message\message();
880fc15b 553 $message->courseid = $course->id;
6ff4464b
JL
554 $message->notification = 1;
555 $message->component = 'enrol_manual';
556 $message->name = 'expiry_notification';
557 $message->userfrom = $user2;
558 $message->userto = $user1;
559 $message->subject = 'Enrolment expired';
560 $message->fullmessage = 'Enrolment expired blah blah blah';
561 $message->fullmessageformat = FORMAT_MARKDOWN;
562 $message->fullmessagehtml = markdown_to_html($message->fullmessage);
563 $message->smallmessage = $message->subject;
564 $message->contexturlname = $course->fullname;
565 $message->contexturl = (string)new moodle_url('/course/view.php', array('id' => $course->id));
566 message_send($message);
567
568 $userfrom = core_user::get_noreply_user();
569 $userfrom->maildisplay = true;
0e8b5160
EM
570 $eventdata = new \core\message\message();
571 $eventdata->courseid = $course->id;
6ff4464b
JL
572 $eventdata->component = 'moodle';
573 $eventdata->name = 'badgecreatornotice';
574 $eventdata->userfrom = $userfrom;
575 $eventdata->userto = $user1;
576 $eventdata->notification = 1;
577 $eventdata->subject = 'New badge';
578 $eventdata->fullmessage = format_text_email($eventdata->subject, FORMAT_HTML);
579 $eventdata->fullmessageformat = FORMAT_PLAIN;
580 $eventdata->fullmessagehtml = $eventdata->subject;
581 $eventdata->smallmessage = $eventdata->subject;
582 message_send($eventdata);
583
cc350fd9
AD
584 $eventdata = new \core\message\message();
585 $eventdata->courseid = $course->id;
6ff4464b
JL
586 $eventdata->name = 'submission';
587 $eventdata->component = 'mod_feedback';
588 $eventdata->userfrom = $user1;
589 $eventdata->userto = $user2;
590 $eventdata->subject = 'Feedback submitted';
591 $eventdata->fullmessage = 'Feedback submitted from an user';
592 $eventdata->fullmessageformat = FORMAT_PLAIN;
593 $eventdata->fullmessagehtml = '<strong>Feedback submitted</strong>';
594 $eventdata->smallmessage = '';
595 message_send($eventdata);
596
597 $this->setUser($user1);
598 // Get read notifications from any user to user1.
599 $messages = core_message_external::get_messages($user1->id, 0, 'notifications', true, true, 0, 0);
600 $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
601 $this->assertCount(3, $messages['messages']);
602
603 // Get one read notifications from any user to user1.
604 $messages = core_message_external::get_messages($user1->id, 0, 'notifications', true, true, 0, 1);
605 $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
606 $this->assertCount(1, $messages['messages']);
607
608 // Get unread notifications from any user to user1.
609 $messages = core_message_external::get_messages($user1->id, 0, 'notifications', false, true, 0, 0);
610 $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
611 $this->assertCount(0, $messages['messages']);
612
613 // Get read both type of messages from any user to user1.
614 $messages = core_message_external::get_messages($user1->id, 0, 'both', true, true, 0, 0);
615 $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
616 $this->assertCount(4, $messages['messages']);
617
618 // Get read notifications from no-reply-user to user1.
619 $messages = core_message_external::get_messages($user1->id, $userfrom->id, 'notifications', true, true, 0, 0);
620 $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
621 $this->assertCount(1, $messages['messages']);
622
623 // Get notifications send by user1 to any user.
624 $messages = core_message_external::get_messages(0, $user1->id, 'notifications', true, true, 0, 0);
625 $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
626 $this->assertCount(1, $messages['messages']);
627
628 // Test warnings.
629 $CFG->messaging = 0;
630
631 $messages = core_message_external::get_messages(0, $user1->id, 'both', true, true, 0, 0);
632 $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
633 $this->assertCount(1, $messages['warnings']);
634
635 // Test exceptions.
636
637 // Messaging disabled.
638 try {
639 $messages = core_message_external::get_messages(0, $user1->id, 'conversations', true, true, 0, 0);
640 $this->fail('Exception expected due messaging disabled.');
641 } catch (moodle_exception $e) {
642 $this->assertEquals('disabled', $e->errorcode);
643 }
644
645 $CFG->messaging = 1;
646
647 // Invalid users.
648 try {
649 $messages = core_message_external::get_messages(0, 0, 'conversations', true, true, 0, 0);
650 $this->fail('Exception expected due invalid users.');
651 } catch (moodle_exception $e) {
652 $this->assertEquals('accessdenied', $e->errorcode);
653 }
654
655 // Invalid user ids.
656 try {
657 $messages = core_message_external::get_messages(2500, 0, 'conversations', true, true, 0, 0);
658 $this->fail('Exception expected due invalid users.');
659 } catch (moodle_exception $e) {
660 $this->assertEquals('invaliduser', $e->errorcode);
661 }
662
663 // Invalid users (permissions).
664 $this->setUser($user2);
665 try {
666 $messages = core_message_external::get_messages(0, $user1->id, 'conversations', true, true, 0, 0);
667 $this->fail('Exception expected due invalid user.');
668 } catch (moodle_exception $e) {
669 $this->assertEquals('accessdenied', $e->errorcode);
670 }
671
672 }
ff1f3739
JL
673
674 /**
675 * Test get_blocked_users.
676 */
677 public function test_get_blocked_users() {
678 $this->resetAfterTest(true);
679
680 $user1 = self::getDataGenerator()->create_user();
681 $userstranger = self::getDataGenerator()->create_user();
682 $useroffline1 = self::getDataGenerator()->create_user();
683 $useroffline2 = self::getDataGenerator()->create_user();
684 $userblocked = self::getDataGenerator()->create_user();
685
686 // Login as user1.
687 $this->setUser($user1);
688 $this->assertEquals(array(), core_message_external::create_contacts(
689 array($useroffline1->id, $useroffline2->id)));
690
691 // The userstranger sends a couple of messages to user1.
692 $this->send_message($userstranger, $user1, 'Hello there!');
693 $this->send_message($userstranger, $user1, 'How you goin?');
694
695 // The userblocked sends a message to user1.
696 // Note that this user is not blocked at this point.
697 $this->send_message($userblocked, $user1, 'Here, have some spam.');
698
699 // Retrieve the list of blocked users.
700 $this->setUser($user1);
701 $blockedusers = core_message_external::get_blocked_users($user1->id);
702 $blockedusers = external_api::clean_returnvalue(core_message_external::get_blocked_users_returns(), $blockedusers);
703 $this->assertCount(0, $blockedusers['users']);
704
705 // Block the $userblocked and retrieve again the list.
706 core_message_external::block_contacts(array($userblocked->id));
707 $blockedusers = core_message_external::get_blocked_users($user1->id);
708 $blockedusers = external_api::clean_returnvalue(core_message_external::get_blocked_users_returns(), $blockedusers);
709 $this->assertCount(1, $blockedusers['users']);
710
01393790
JL
711 // Remove the $userblocked and check that the list now is empty.
712 delete_user($userblocked);
713 $blockedusers = core_message_external::get_blocked_users($user1->id);
714 $blockedusers = external_api::clean_returnvalue(core_message_external::get_blocked_users_returns(), $blockedusers);
715 $this->assertCount(0, $blockedusers['users']);
716
ff1f3739
JL
717 }
718
b6795827
JL
719 /**
720 * Test mark_message_read.
721 */
722 public function test_mark_message_read() {
723 $this->resetAfterTest(true);
724
725 $user1 = self::getDataGenerator()->create_user();
726 $user2 = self::getDataGenerator()->create_user();
727 $user3 = self::getDataGenerator()->create_user();
728
729 // Login as user1.
730 $this->setUser($user1);
731 $this->assertEquals(array(), core_message_external::create_contacts(
732 array($user2->id, $user3->id)));
733
734 // The user2 sends a couple of messages to user1.
735 $this->send_message($user2, $user1, 'Hello there!');
736 $this->send_message($user2, $user1, 'How you goin?');
737 $this->send_message($user3, $user1, 'How you goin?');
738 $this->send_message($user3, $user2, 'How you goin?');
739
740 // Retrieve all messages sent by user2 (they are currently unread).
741 $lastmessages = message_get_messages($user1->id, $user2->id, 0, false);
742
743 $messageids = array();
744 foreach ($lastmessages as $m) {
745 $messageid = core_message_external::mark_message_read($m->id, time());
746 $messageids[] = external_api::clean_returnvalue(core_message_external::mark_message_read_returns(), $messageid);
747 }
748
749 // Retrieve all messages sent (they are currently read).
750 $lastmessages = message_get_messages($user1->id, $user2->id, 0, true);
751 $this->assertCount(2, $lastmessages);
752 $this->assertArrayHasKey($messageids[0]['messageid'], $lastmessages);
753 $this->assertArrayHasKey($messageids[1]['messageid'], $lastmessages);
754
755 // Retrieve all messages sent by any user (that are currently unread).
756 $lastmessages = message_get_messages($user1->id, 0, 0, false);
757 $this->assertCount(1, $lastmessages);
758
759 // Invalid message ids.
760 try {
883ce421 761 $messageid = core_message_external::mark_message_read(1337, time());
b6795827
JL
762 $this->fail('Exception expected due invalid messageid.');
763 } catch (dml_missing_record_exception $e) {
883ce421 764 $this->assertEquals('invalidrecordunknown', $e->errorcode);
b6795827
JL
765 }
766
767 // A message to a different user.
768 $lastmessages = message_get_messages($user2->id, $user3->id, 0, false);
769 $messageid = array_pop($lastmessages)->id;
770 try {
771 $messageid = core_message_external::mark_message_read($messageid, time());
772 $this->fail('Exception expected due invalid messageid.');
773 } catch (invalid_parameter_exception $e) {
774 $this->assertEquals('invalidparameter', $e->errorcode);
775 }
2b595d96
MN
776 }
777
778 /**
779 * Test mark_notification_read.
780 */
781 public function test_mark_notification_read() {
782 $this->resetAfterTest(true);
783
784 $user1 = self::getDataGenerator()->create_user();
785 $user2 = self::getDataGenerator()->create_user();
786 $user3 = self::getDataGenerator()->create_user();
787
788 // Login as user1.
789 $this->setUser($user1);
790 $this->assertEquals(array(), core_message_external::create_contacts(
791 array($user2->id, $user3->id)));
b6795827 792
2b595d96
MN
793 // The user2 sends a couple of notifications to user1.
794 $this->send_message($user2, $user1, 'Hello there!', 1);
795 $this->send_message($user2, $user1, 'How you goin?', 1);
796 $this->send_message($user3, $user1, 'How you goin?', 1);
797 $this->send_message($user3, $user2, 'How you goin?', 1);
798
799 // Retrieve all notifications sent by user2 (they are currently unread).
800 $lastnotifications = message_get_messages($user1->id, $user2->id, 1, false);
801
802 $notificationids = array();
803 foreach ($lastnotifications as $n) {
804 $notificationid = core_message_external::mark_notification_read($n->id, time());
805 $notificationids[] = external_api::clean_returnvalue(core_message_external::mark_notification_read_returns(),
806 $notificationid);
807 }
808
809 // Retrieve all notifications sent (they are currently read).
810 $lastnotifications = message_get_messages($user1->id, $user2->id, 1, true);
811 $this->assertCount(2, $lastnotifications);
812 $this->assertArrayHasKey($notificationids[1]['notificationid'], $lastnotifications);
813 $this->assertArrayHasKey($notificationids[0]['notificationid'], $lastnotifications);
814
815 // Retrieve all notifications sent by any user (that are currently unread).
816 $lastnotifications = message_get_messages($user1->id, 0, 1, false);
817 $this->assertCount(1, $lastnotifications);
818
819 // Invalid notification ids.
820 try {
821 $notificationid = core_message_external::mark_notification_read(1337, time());
822 $this->fail('Exception expected due invalid notificationid.');
823 } catch (dml_missing_record_exception $e) {
824 $this->assertEquals('invalidrecord', $e->errorcode);
825 }
826
827 // A notification to a different user.
828 $lastnotifications = message_get_messages($user2->id, $user3->id, 1, false);
829 $notificationid = array_pop($lastnotifications)->id;
830 try {
831 $notificationid = core_message_external::mark_notification_read($notificationid, time());
832 $this->fail('Exception expected due invalid notificationid.');
833 } catch (invalid_parameter_exception $e) {
834 $this->assertEquals('invalidparameter', $e->errorcode);
835 }
b6795827
JL
836 }
837
419b1128
JL
838 /**
839 * Test delete_message.
840 */
841 public function test_delete_message() {
842 global $DB;
843 $this->resetAfterTest(true);
844
845 $user1 = self::getDataGenerator()->create_user();
846 $user2 = self::getDataGenerator()->create_user();
847 $user3 = self::getDataGenerator()->create_user();
848 $user4 = self::getDataGenerator()->create_user();
849
850 // Login as user1.
851 $this->setUser($user1);
852 $this->assertEquals(array(), core_message_external::create_contacts(array($user2->id, $user3->id)));
853
854 // User user1 does not interchange messages with user3.
855 $m1to2 = message_post_message($user1, $user2, 'some random text 1', FORMAT_MOODLE);
856 $m2to3 = message_post_message($user2, $user3, 'some random text 3', FORMAT_MOODLE);
857 $m3to2 = message_post_message($user3, $user2, 'some random text 4', FORMAT_MOODLE);
858 $m3to4 = message_post_message($user3, $user4, 'some random text 4', FORMAT_MOODLE);
859
860 // Retrieve all messages sent by user2 (they are currently unread).
861 $lastmessages = message_get_messages($user1->id, $user2->id, 0, false);
862
863 // Delete a message not read, as a user from.
864 $result = core_message_external::delete_message($m1to2, $user1->id, false);
865 $result = external_api::clean_returnvalue(core_message_external::delete_message_returns(), $result);
866 $this->assertTrue($result['status']);
867 $this->assertCount(0, $result['warnings']);
883ce421
MN
868 $mua = $DB->get_record('message_user_actions', array('messageid' => $m1to2, 'userid' => $user1->id));
869 $this->assertEquals(\core_message\api::MESSAGE_ACTION_DELETED, $mua->action);
419b1128
JL
870
871 // Try to delete the same message again.
872 $result = core_message_external::delete_message($m1to2, $user1->id, false);
873 $result = external_api::clean_returnvalue(core_message_external::delete_message_returns(), $result);
874 $this->assertFalse($result['status']);
875
876 // Try to delete a message that does not belong to me.
877 try {
878 $messageid = core_message_external::delete_message($m2to3, $user3->id, false);
879 $this->fail('Exception expected due invalid messageid.');
880 } catch (moodle_exception $e) {
881 $this->assertEquals('You do not have permission to delete this message', $e->errorcode);
882 }
883
884 $this->setUser($user3);
885 // Delete a message not read, as a user to.
886 $result = core_message_external::delete_message($m2to3, $user3->id, false);
887 $result = external_api::clean_returnvalue(core_message_external::delete_message_returns(), $result);
888 $this->assertTrue($result['status']);
889 $this->assertCount(0, $result['warnings']);
883ce421
MN
890 $this->assertTrue($DB->record_exists('message_user_actions', array('messageid' => $m2to3, 'userid' => $user3->id,
891 'action' => \core_message\api::MESSAGE_ACTION_DELETED)));
419b1128
JL
892
893 // Delete a message read.
548936a6
MN
894 $message = $DB->get_record('messages', ['id' => $m3to2]);
895 \core_message\api::mark_message_as_read($user3->id, $message, time());
883ce421 896 $result = core_message_external::delete_message($m3to2, $user3->id);
419b1128
JL
897 $result = external_api::clean_returnvalue(core_message_external::delete_message_returns(), $result);
898 $this->assertTrue($result['status']);
899 $this->assertCount(0, $result['warnings']);
883ce421
MN
900 $this->assertTrue($DB->record_exists('message_user_actions', array('messageid' => $m3to2, 'userid' => $user3->id,
901 'action' => \core_message\api::MESSAGE_ACTION_DELETED)));
419b1128
JL
902
903 // Invalid message ids.
904 try {
905 $result = core_message_external::delete_message(-1, $user1->id);
906 $this->fail('Exception expected due invalid messageid.');
907 } catch (dml_missing_record_exception $e) {
883ce421 908 $this->assertEquals('invalidrecordunknown', $e->errorcode);
419b1128
JL
909 }
910
911 // Invalid user.
912 try {
913 $result = core_message_external::delete_message($m1to2, -1, false);
914 $this->fail('Exception expected due invalid user.');
915 } catch (moodle_exception $e) {
916 $this->assertEquals('invaliduser', $e->errorcode);
917 }
918
919 // Not active user.
920 delete_user($user2);
921 try {
922 $result = core_message_external::delete_message($m1to2, $user2->id, false);
923 $this->fail('Exception expected due invalid user.');
924 } catch (moodle_exception $e) {
925 $this->assertEquals('userdeleted', $e->errorcode);
926 }
927
928 // Now, as an admin, try to delete any message.
929 $this->setAdminUser();
930 $result = core_message_external::delete_message($m3to4, $user4->id, false);
931 $result = external_api::clean_returnvalue(core_message_external::delete_message_returns(), $result);
932 $this->assertTrue($result['status']);
933 $this->assertCount(0, $result['warnings']);
883ce421
MN
934 $this->assertTrue($DB->record_exists('message_user_actions', array('messageid' => $m3to4, 'userid' => $user4->id,
935 'action' => \core_message\api::MESSAGE_ACTION_DELETED)));
419b1128
JL
936
937 }
938
3274d5ca
RW
939 public function test_mark_all_notifications_as_read_invalid_user_exception() {
940 $this->resetAfterTest(true);
941
6aa01968
MN
942 $this->expectException('moodle_exception');
943 core_message_external::mark_all_notifications_as_read(-2132131, 0);
3274d5ca
RW
944 }
945
946 public function test_mark_all_notifications_as_read_access_denied_exception() {
947 $this->resetAfterTest(true);
948
949 $sender = $this->getDataGenerator()->create_user();
950 $user = $this->getDataGenerator()->create_user();
951
952 $this->setUser($user);
6aa01968
MN
953 $this->expectException('moodle_exception');
954 core_message_external::mark_all_notifications_as_read($sender->id, 0);
3274d5ca
RW
955 }
956
957 public function test_mark_all_notifications_as_read_missing_from_user_exception() {
958 $this->resetAfterTest(true);
959
960 $sender = $this->getDataGenerator()->create_user();
961
962 $this->setUser($sender);
6aa01968
MN
963 $this->expectException('moodle_exception');
964 core_message_external::mark_all_notifications_as_read($sender->id, 99999);
3274d5ca
RW
965 }
966
967 public function test_mark_all_notifications_as_read() {
7d69958e
RW
968 global $DB;
969
3274d5ca
RW
970 $this->resetAfterTest(true);
971
972 $sender1 = $this->getDataGenerator()->create_user();
973 $sender2 = $this->getDataGenerator()->create_user();
974 $sender3 = $this->getDataGenerator()->create_user();
975 $recipient = $this->getDataGenerator()->create_user();
976
977 $this->setUser($recipient);
978
6aa01968
MN
979 $this->send_message($sender1, $recipient, 'Notification', 1);
980 $this->send_message($sender1, $recipient, 'Notification', 1);
981 $this->send_message($sender2, $recipient, 'Notification', 1);
982 $this->send_message($sender2, $recipient, 'Notification', 1);
983 $this->send_message($sender3, $recipient, 'Notification', 1);
984 $this->send_message($sender3, $recipient, 'Notification', 1);
3274d5ca
RW
985
986 core_message_external::mark_all_notifications_as_read($recipient->id, $sender1->id);
883ce421
MN
987 $readnotifications = $DB->get_records_select('notifications', 'useridto = ? AND timeread IS NOT NULL', [$recipient->id]);
988 $unreadnotifications = $DB->get_records_select('notifications', 'useridto = ? AND timeread IS NULL', [$recipient->id]);
3274d5ca 989
7d69958e
RW
990 $this->assertCount(2, $readnotifications);
991 $this->assertCount(4, $unreadnotifications);
3274d5ca
RW
992
993 core_message_external::mark_all_notifications_as_read($recipient->id, 0);
883ce421
MN
994 $readnotifications = $DB->get_records_select('notifications', 'useridto = ? AND timeread IS NOT NULL', [$recipient->id]);
995 $unreadnotifications = $DB->get_records_select('notifications', 'useridto = ? AND timeread IS NULL', [$recipient->id]);
3274d5ca 996
7d69958e
RW
997 $this->assertCount(6, $readnotifications);
998 $this->assertCount(0, $unreadnotifications);
3274d5ca 999 }
e86f0cb4
JL
1000
1001 /**
1002 * Test get_user_notification_preferences
1003 */
1004 public function test_get_user_notification_preferences() {
1005 $this->resetAfterTest(true);
1006
1007 $user = self::getDataGenerator()->create_user();
1008 $this->setUser($user);
1009
1010 // Set a couple of preferences to test.
1011 set_user_preference('message_provider_mod_assign_assign_notification_loggedin', 'popup', $user);
1012 set_user_preference('message_provider_mod_assign_assign_notification_loggedoff', 'email', $user);
1013
1014 $prefs = core_message_external::get_user_notification_preferences();
1015 $prefs = external_api::clean_returnvalue(core_message_external::get_user_notification_preferences_returns(), $prefs);
1016 // Check processors.
46c5c883 1017 $this->assertGreaterThanOrEqual(2, count($prefs['preferences']['processors']));
e86f0cb4
JL
1018 $this->assertEquals($user->id, $prefs['preferences']['userid']);
1019
1020 // Check components.
46c5c883 1021 $this->assertGreaterThanOrEqual(8, count($prefs['preferences']['components']));
e86f0cb4
JL
1022
1023 // Check some preferences that we previously set.
1024 $found = 0;
1025 foreach ($prefs['preferences']['components'] as $component) {
1026 foreach ($component['notifications'] as $prefdata) {
1027 if ($prefdata['preferencekey'] != 'message_provider_mod_assign_assign_notification') {
1028 continue;
1029 }
1030 foreach ($prefdata['processors'] as $processor) {
1031 if ($processor['name'] == 'popup') {
1032 $this->assertTrue($processor['loggedin']['checked']);
1033 $found++;
1034 } else if ($processor['name'] == 'email') {
1035 $this->assertTrue($processor['loggedoff']['checked']);
1036 $found++;
1037 }
1038 }
1039 }
1040 }
1041 $this->assertEquals(2, $found);
1042 }
1043
1044 /**
1045 * Test get_user_notification_preferences permissions
1046 */
1047 public function test_get_user_notification_preferences_permissions() {
1048 $this->resetAfterTest(true);
1049
1050 $user = self::getDataGenerator()->create_user();
1051 $otheruser = self::getDataGenerator()->create_user();
1052 $this->setUser($user);
1053
1054 $this->expectException('moodle_exception');
1055 $prefs = core_message_external::get_user_notification_preferences($otheruser->id);
1056 }
6aa01968
MN
1057
1058 /**
1059 * Tests searching users in a course.
1060 */
1061 public function test_messagearea_search_users_in_course() {
1062 $this->resetAfterTest(true);
1063
1064 // Create some users.
1065 $user1 = new stdClass();
1066 $user1->firstname = 'User';
1067 $user1->lastname = 'One';
1068 $user1 = self::getDataGenerator()->create_user($user1);
1069
1070 // The person doing the search.
1071 $this->setUser($user1);
1072
1073 // Set the second user's status to online by setting their last access to now.
1074 $user2 = new stdClass();
1075 $user2->firstname = 'User';
1076 $user2->lastname = 'Two';
1077 $user2->lastaccess = time();
1078 $user2 = self::getDataGenerator()->create_user($user2);
1079
1080 // Block the second user.
1081 message_block_contact($user2->id, $user1->id);
1082
1083 $user3 = new stdClass();
1084 $user3->firstname = 'User';
1085 $user3->lastname = 'Three';
1086 $user3 = self::getDataGenerator()->create_user($user3);
1087
1088 // Create a course.
1089 $course1 = new stdClass();
1090 $course1->fullname = 'Course';
1091 $course1->shortname = 'One';
1092 $course1 = $this->getDataGenerator()->create_course();
1093
1094 // Enrol the user we are doing the search for and one user in the course.
1095 $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
1096 $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
1097
1098 // Perform a search.
1099 $result = core_message_external::data_for_messagearea_search_users_in_course($user1->id, $course1->id, 'User');
1100
1101 // We need to execute the return values cleaning process to simulate the web service.
1102 $result = external_api::clean_returnvalue(core_message_external::data_for_messagearea_search_users_in_course_returns(),
1103 $result);
1104
1105 // Check that we only retrieved a user that was enrolled, and that the user performing the search was not returned.
1106 $users = $result['contacts'];
1107 $this->assertCount(1, $users);
1108
1109 $user = $users[0];
1110 $this->assertEquals($user2->id, $user['userid']);
1111 $this->assertEquals(fullname($user2), $user['fullname']);
1112 $this->assertFalse($user['ismessaging']);
1113 $this->assertFalse($user['sentfromcurrentuser']);
1114 $this->assertNull($user['lastmessage']);
1115 $this->assertNull($user['messageid']);
cb805753 1116 $this->assertNull($user['isonline']);
6aa01968
MN
1117 $this->assertFalse($user['isread']);
1118 $this->assertTrue($user['isblocked']);
1119 $this->assertNull($user['unreadcount']);
1120 }
1121
1122 /**
1123 * Tests searching users in course as another user.
1124 */
1125 public function test_messagearea_search_users_in_course_as_other_user() {
1126 $this->resetAfterTest(true);
1127
1128 // The person doing the search for another user.
1129 $this->setAdminUser();
1130
1131 // Create some users.
1132 $user1 = new stdClass();
1133 $user1->firstname = 'User';
1134 $user1->lastname = 'One';
1135 $user1 = self::getDataGenerator()->create_user($user1);
1136
1137 $user2 = new stdClass();
1138 $user2->firstname = 'User';
1139 $user2->lastname = 'Two';
1140 $user2 = self::getDataGenerator()->create_user($user2);
1141
1142 $user3 = new stdClass();
1143 $user3->firstname = 'User';
1144 $user3->lastname = 'Three';
1145 $user3 = self::getDataGenerator()->create_user($user3);
1146
1147 // Create a course.
1148 $course1 = new stdClass();
1149 $course1->fullname = 'Course';
1150 $course1->shortname = 'One';
1151 $course1 = $this->getDataGenerator()->create_course();
1152
1153 // Enrol the user we are doing the search for and one user in the course.
1154 $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
1155 $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
1156
1157 // Perform a search.
1158 $result = core_message_external::data_for_messagearea_search_users_in_course($user1->id, $course1->id, 'User');
1159
1160 // We need to execute the return values cleaning process to simulate the web service server.
1161 $result = external_api::clean_returnvalue(core_message_external::data_for_messagearea_search_users_in_course_returns(),
1162 $result);
1163
1164 // Check that we got the user enrolled, and that the user we are performing the search on behalf of was not returned.
1165 $users = $result['contacts'];
1166 $this->assertCount(1, $users);
1167
1168 $user = $users[0];
1169 $this->assertEquals($user2->id, $user['userid']);
1170 $this->assertEquals(fullname($user2), $user['fullname']);
1171 $this->assertFalse($user['ismessaging']);
1172 $this->assertFalse($user['sentfromcurrentuser']);
1173 $this->assertNull($user['lastmessage']);
1174 $this->assertNull($user['messageid']);
1175 $this->assertFalse($user['isonline']);
1176 $this->assertFalse($user['isread']);
1177 $this->assertFalse($user['isblocked']);
1178 $this->assertNull($user['unreadcount']);
1179 }
1180
1181 /**
1182 * Tests searching users in course as another user without the proper capabilities.
1183 */
1184 public function test_messagearea_search_users_in_course_as_other_user_without_cap() {
1185 $this->resetAfterTest(true);
1186
1187 // Create some users.
1188 $user1 = self::getDataGenerator()->create_user();
1189 $user2 = self::getDataGenerator()->create_user();
1190
1191 // The person doing the search for another user.
1192 $this->setUser($user1);
1193
1194 // Create a course.
1195 $course = $this->getDataGenerator()->create_course();
1196
1197 // Ensure an exception is thrown.
1198 $this->expectException('moodle_exception');
1199 core_message_external::data_for_messagearea_search_users_in_course($user2->id, $course->id, 'User');
1200 }
1201
1202 /**
1203 * Tests searching users in course with messaging disabled.
1204 */
1205 public function test_messagearea_search_users_in_course_messaging_disabled() {
1206 global $CFG;
1207
1208 $this->resetAfterTest(true);
1209
1210 // Create some skeleton data just so we can call the WS..
1211 $user = self::getDataGenerator()->create_user();
1212 $course = $this->getDataGenerator()->create_course();
1213
1214 // The person doing the search for another user.
1215 $this->setUser($user);
1216
1217 // Disable messaging.
1218 $CFG->messaging = 0;
1219
1220 // Ensure an exception is thrown.
1221 $this->expectException('moodle_exception');
1222 core_message_external::data_for_messagearea_search_users_in_course($user->id, $course->id, 'User');
1223 }
1224
1225 /**
1226 * Tests searching users.
1227 */
1228 public function test_messagearea_search_users() {
1229 $this->resetAfterTest(true);
1230
1231 // Create some users.
1232 $user1 = new stdClass();
1233 $user1->firstname = 'User';
1234 $user1->lastname = 'One';
1235 $user1 = self::getDataGenerator()->create_user($user1);
1236
1237 // Set as the user performing the search.
1238 $this->setUser($user1);
1239
1240 $user2 = new stdClass();
1241 $user2->firstname = 'User search';
1242 $user2->lastname = 'Two';
1243 $user2 = self::getDataGenerator()->create_user($user2);
1244
1245 $user3 = new stdClass();
1246 $user3->firstname = 'User search';
1247 $user3->lastname = 'Three';
1248 $user3 = self::getDataGenerator()->create_user($user3);
1249
1250 $user4 = new stdClass();
1251 $user4->firstname = 'User';
1252 $user4->lastname = 'Four';
1253 $user4 = self::getDataGenerator()->create_user($user4);
1254
1255 $user5 = new stdClass();
1256 $user5->firstname = 'User search';
1257 $user5->lastname = 'Five';
1258 $user5 = self::getDataGenerator()->create_user($user5);
1259
1260 $user6 = new stdClass();
1261 $user6->firstname = 'User';
1262 $user6->lastname = 'Six';
1263 $user6 = self::getDataGenerator()->create_user($user6);
1264
1265 // Create some courses.
1266 $course1 = new stdClass();
1267 $course1->fullname = 'Course search';
1268 $course1->shortname = 'One';
1269 $course1 = $this->getDataGenerator()->create_course($course1);
1270
1271 $course2 = new stdClass();
1272 $course2->fullname = 'Course';
1273 $course2->shortname = 'Two';
1274 $course2 = $this->getDataGenerator()->create_course($course2);
1275
1276 $course3 = new stdClass();
1277 $course3->fullname = 'Course';
1278 $course3->shortname = 'Three search';
1279 $course3 = $this->getDataGenerator()->create_course($course3);
1280
87d4ab65
AG
1281 $course4 = new stdClass();
1282 $course4->fullname = 'Course Four';
1283 $course4->shortname = 'CF100';
1284 $course4 = $this->getDataGenerator()->create_course($course4);
1285
1286 $this->getDataGenerator()->enrol_user($user1->id, $course1->id, 'student');
1287 $this->getDataGenerator()->enrol_user($user1->id, $course2->id, 'student');
1288 $this->getDataGenerator()->enrol_user($user1->id, $course3->id, 'student');
1289
6aa01968
MN
1290 // Add some users as contacts.
1291 message_add_contact($user2->id, 0, $user1->id);
1292 message_add_contact($user3->id, 0, $user1->id);
1293 message_add_contact($user4->id, 0, $user1->id);
1294
1295 // Perform a search.
1296 $result = core_message_external::data_for_messagearea_search_users($user1->id, 'search');
1297
1298 // We need to execute the return values cleaning process to simulate the web service server.
1299 $result = external_api::clean_returnvalue(core_message_external::data_for_messagearea_search_users_returns(),
1300 $result);
1301
1302 // Confirm that we returns contacts, courses and non-contacts.
1303 $contacts = $result['contacts'];
1304 $courses = $result['courses'];
1305 $noncontacts = $result['noncontacts'];
1306
1307 // Check that we retrieved the correct contacts.
1308 $this->assertCount(2, $contacts);
1309 $this->assertEquals($user3->id, $contacts[0]['userid']);
1310 $this->assertEquals($user2->id, $contacts[1]['userid']);
1311
1312 // Check that we retrieved the correct courses.
1313 $this->assertCount(2, $courses);
1314 $this->assertEquals($course3->id, $courses[0]['id']);
1315 $this->assertEquals($course1->id, $courses[1]['id']);
1316
1317 // Check that we retrieved the correct non-contacts.
1318 $this->assertCount(1, $noncontacts);
1319 $this->assertEquals($user5->id, $noncontacts[0]['userid']);
1320 }
1321
1322 /**
1323 * Tests searching users as another user.
1324 */
1325 public function test_messagearea_search_users_as_other_user() {
1326 $this->resetAfterTest(true);
1327
1328 // The person doing the search.
1329 $this->setAdminUser();
1330
1331 // Create some users.
1332 $user1 = new stdClass();
1333 $user1->firstname = 'User';
1334 $user1->lastname = 'One';
1335 $user1 = self::getDataGenerator()->create_user($user1);
1336
1337 $user2 = new stdClass();
1338 $user2->firstname = 'User search';
1339 $user2->lastname = 'Two';
1340 $user2 = self::getDataGenerator()->create_user($user2);
1341
1342 $user3 = new stdClass();
1343 $user3->firstname = 'User search';
1344 $user3->lastname = 'Three';
1345 $user3 = self::getDataGenerator()->create_user($user3);
1346
1347 $user4 = new stdClass();
1348 $user4->firstname = 'User';
1349 $user4->lastname = 'Four';
1350 $user4 = self::getDataGenerator()->create_user($user4);
1351
1352 $user5 = new stdClass();
1353 $user5->firstname = 'User search';
1354 $user5->lastname = 'Five';
1355 $user5 = self::getDataGenerator()->create_user($user5);
1356
1357 $user6 = new stdClass();
1358 $user6->firstname = 'User';
1359 $user6->lastname = 'Six';
1360 $user6 = self::getDataGenerator()->create_user($user6);
1361
1362 // Create some courses.
1363 $course1 = new stdClass();
1364 $course1->fullname = 'Course search';
1365 $course1->shortname = 'One';
1366 $course1 = $this->getDataGenerator()->create_course($course1);
1367
1368 $course2 = new stdClass();
1369 $course2->fullname = 'Course';
1370 $course2->shortname = 'Two';
1371 $course2 = $this->getDataGenerator()->create_course($course2);
1372
1373 $course3 = new stdClass();
1374 $course3->fullname = 'Course';
1375 $course3->shortname = 'Three search';
1376 $course3 = $this->getDataGenerator()->create_course($course3);
1377
1378 // Add some users as contacts.
1379 message_add_contact($user2->id, 0, $user1->id);
1380 message_add_contact($user3->id, 0, $user1->id);
1381 message_add_contact($user4->id, 0, $user1->id);
1382
1383 // Perform a search.
1384 $result = core_message_external::data_for_messagearea_search_users($user1->id, 'search');
1385
1386 // We need to execute the return values cleaning process to simulate the web service server.
1387 $result = external_api::clean_returnvalue(core_message_external::data_for_messagearea_search_users_returns(),
1388 $result);
1389
1390 // Confirm that we returns contacts, courses and non-contacts.
1391 $contacts = $result['contacts'];
1392 $courses = $result['courses'];
1393 $noncontacts = $result['noncontacts'];
1394
1395 // Check that we retrieved the correct contacts.
1396 $this->assertCount(2, $contacts);
1397 $this->assertEquals($user3->id, $contacts[0]['userid']);
1398 $this->assertEquals($user2->id, $contacts[1]['userid']);
1399
1400 // Check that we retrieved the correct courses.
87d4ab65 1401 $this->assertCount(0, $courses);
6aa01968
MN
1402
1403 // Check that we retrieved the correct non-contacts.
1404 $this->assertCount(1, $noncontacts);
1405 $this->assertEquals($user5->id, $noncontacts[0]['userid']);
1406 }
1407
1408 /**
1409 * Tests searching users as another user without the proper capabilities.
1410 */
1411 public function test_messagearea_search_users_as_other_user_without_cap() {
1412 $this->resetAfterTest(true);
1413
1414 // Create some users.
1415 $user1 = self::getDataGenerator()->create_user();
1416 $user2 = self::getDataGenerator()->create_user();
1417
1418 // The person doing the search for another user.
1419 $this->setUser($user1);
1420
1421 // Ensure an exception is thrown.
1422 $this->expectException('moodle_exception');
1423 core_message_external::data_for_messagearea_search_users($user2->id, 'User');
1424 }
1425
1426 /**
1427 * Tests searching users with messaging disabled.
1428 */
1429 public function test_messagearea_search_users_messaging_disabled() {
1430 global $CFG;
1431
1432 $this->resetAfterTest(true);
1433
1434 // Create some skeleton data just so we can call the WS.
1435 $user = self::getDataGenerator()->create_user();
1436
1437 // The person doing the search.
1438 $this->setUser($user);
1439
1440 // Disable messaging.
1441 $CFG->messaging = 0;
1442
1443 // Ensure an exception is thrown.
1444 $this->expectException('moodle_exception');
1445 core_message_external::data_for_messagearea_search_users($user->id, 'User');
1446 }
1447
1448 /**
1449 * Tests searching messages.
1450 */
1451 public function test_messagearea_search_messages() {
1452 $this->resetAfterTest(true);
1453
1454 // Create some users.
1455 $user1 = self::getDataGenerator()->create_user();
1456 $user2 = self::getDataGenerator()->create_user();
1457
1458 // The person doing the search.
1459 $this->setUser($user1);
1460
1461 // Send some messages back and forth.
1462 $time = time();
1463 $this->send_message($user1, $user2, 'Yo!', 0, $time);
1464 $this->send_message($user2, $user1, 'Sup mang?', 0, $time + 1);
1465 $this->send_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 2);
1466 $this->send_message($user2, $user1, 'Word.', 0, $time + 3);
1467
1468 // Perform a search.
1469 $result = core_message_external::data_for_messagearea_search_messages($user1->id, 'o');
1470
1471 // We need to execute the return values cleaning process to simulate the web service server.
1472 $result = external_api::clean_returnvalue(core_message_external::data_for_messagearea_search_messages_returns(),
1473 $result);
1474
1475 // Confirm the data is correct.
1476 $messages = $result['contacts'];
1477 $this->assertCount(2, $messages);
1478
1479 $message1 = $messages[0];
1480 $message2 = $messages[1];
1481
1482 $this->assertEquals($user2->id, $message1['userid']);
1483 $this->assertEquals(fullname($user2), $message1['fullname']);
1484 $this->assertTrue($message1['ismessaging']);
1485 $this->assertFalse($message1['sentfromcurrentuser']);
1486 $this->assertEquals('Word.', $message1['lastmessage']);
1487 $this->assertNotEmpty($message1['messageid']);
cb805753 1488 $this->assertNull($message1['isonline']);
6aa01968
MN
1489 $this->assertFalse($message1['isread']);
1490 $this->assertFalse($message1['isblocked']);
1491 $this->assertNull($message1['unreadcount']);
1492
1493 $this->assertEquals($user2->id, $message2['userid']);
1494 $this->assertEquals(fullname($user2), $message2['fullname']);
1495 $this->assertTrue($message2['ismessaging']);
1496 $this->assertTrue($message2['sentfromcurrentuser']);
1497 $this->assertEquals('Yo!', $message2['lastmessage']);
1498 $this->assertNotEmpty($message2['messageid']);
cb805753 1499 $this->assertNull($message2['isonline']);
6aa01968
MN
1500 $this->assertTrue($message2['isread']);
1501 $this->assertFalse($message2['isblocked']);
1502 $this->assertNull($message2['unreadcount']);
1503 }
1504
1505 /**
1506 * Tests searching messages as another user.
1507 */
1508 public function test_messagearea_search_messages_as_other_user() {
1509 $this->resetAfterTest(true);
1510
1511 // The person doing the search.
1512 $this->setAdminUser();
1513
1514 // Create some users.
1515 $user1 = self::getDataGenerator()->create_user();
1516 $user2 = self::getDataGenerator()->create_user();
1517
1518 // Send some messages back and forth.
1519 $time = time();
1520 $this->send_message($user1, $user2, 'Yo!', 0, $time);
1521 $this->send_message($user2, $user1, 'Sup mang?', 0, $time + 1);
1522 $this->send_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 2);
1523 $this->send_message($user2, $user1, 'Word.', 0, $time + 3);
1524
1525 // Perform a search.
1526 $result = core_message_external::data_for_messagearea_search_messages($user1->id, 'o');
1527
1528 // We need to execute the return values cleaning process to simulate the web service server.
1529 $result = external_api::clean_returnvalue(core_message_external::data_for_messagearea_search_messages_returns(),
1530 $result);
1531
1532 // Confirm the data is correct.
1533 $messages = $result['contacts'];
1534 $this->assertCount(2, $messages);
1535
1536 $message1 = $messages[0];
1537 $message2 = $messages[1];
1538
1539 $this->assertEquals($user2->id, $message1['userid']);
1540 $this->assertEquals(fullname($user2), $message1['fullname']);
1541 $this->assertTrue($message1['ismessaging']);
1542 $this->assertFalse($message1['sentfromcurrentuser']);
1543 $this->assertEquals('Word.', $message1['lastmessage']);
1544 $this->assertNotEmpty($message1['messageid']);
1545 $this->assertFalse($message1['isonline']);
1546 $this->assertFalse($message1['isread']);
1547 $this->assertFalse($message1['isblocked']);
1548 $this->assertNull($message1['unreadcount']);
1549
1550 $this->assertEquals($user2->id, $message2['userid']);
1551 $this->assertEquals(fullname($user2), $message2['fullname']);
1552 $this->assertTrue($message2['ismessaging']);
1553 $this->assertTrue($message2['sentfromcurrentuser']);
1554 $this->assertEquals('Yo!', $message2['lastmessage']);
1555 $this->assertNotEmpty($message2['messageid']);
1556 $this->assertFalse($message2['isonline']);
1557 $this->assertTrue($message2['isread']);
1558 $this->assertFalse($message2['isblocked']);
1559 $this->assertNull($message2['unreadcount']);
1560 }
1561
1562 /**
1563 * Tests searching messages as another user without the proper capabilities.
1564 */
1565 public function test_messagearea_search_messages_as_other_user_without_cap() {
1566 $this->resetAfterTest(true);
1567
1568 // Create some users.
1569 $user1 = self::getDataGenerator()->create_user();
1570 $user2 = self::getDataGenerator()->create_user();
1571
1572 // The person doing the search for another user.
1573 $this->setUser($user1);
1574
1575 // Ensure an exception is thrown.
1576 $this->expectException('moodle_exception');
1577 core_message_external::data_for_messagearea_search_messages($user2->id, 'Search');
1578 }
1579
1580 /**
1581 * Tests searching messages with messaging disabled
1582 */
1583 public function test_messagearea_search_messages_messaging_disabled() {
1584 global $CFG;
1585
1586 $this->resetAfterTest(true);
1587
1588 // Create some skeleton data just so we can call the WS.
1589 $user = self::getDataGenerator()->create_user();
1590
1591 // The person doing the search .
1592 $this->setUser($user);
1593
1594 // Disable messaging.
1595 $CFG->messaging = 0;
1596
1597 // Ensure an exception is thrown.
1598 $this->expectException('moodle_exception');
1599 core_message_external::data_for_messagearea_search_messages($user->id, 'Search');
1600 }
1601
1602 /**
1603 * Tests retrieving conversations.
1604 */
1605 public function test_messagearea_conversations() {
1606 $this->resetAfterTest(true);
1607
1608 // Create some users.
1609 $user1 = self::getDataGenerator()->create_user();
1610 $user2 = self::getDataGenerator()->create_user();
1611 $user3 = self::getDataGenerator()->create_user();
1612 $user4 = self::getDataGenerator()->create_user();
1613
1614 // The person retrieving the conversations.
1615 $this->setUser($user1);
1616
1617 // Send some messages back and forth, have some different conversations with different users.
1618 $time = time();
1619 $this->send_message($user1, $user2, 'Yo!', 0, $time);
1620 $this->send_message($user2, $user1, 'Sup mang?', 0, $time + 1);
1621 $this->send_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 2);
4d146f1a 1622 $messageid1 = $this->send_message($user2, $user1, 'Word.', 0, $time + 3);
6aa01968
MN
1623
1624 $this->send_message($user1, $user3, 'Booyah', 0, $time + 4);
1625 $this->send_message($user3, $user1, 'Whaaat?', 0, $time + 5);
1626 $this->send_message($user1, $user3, 'Nothing.', 0, $time + 6);
4d146f1a 1627 $messageid2 = $this->send_message($user3, $user1, 'Cool.', 0, $time + 7);
6aa01968
MN
1628
1629 $this->send_message($user1, $user4, 'Hey mate, you see the new messaging UI in Moodle?', 0, $time + 8);
1630 $this->send_message($user4, $user1, 'Yah brah, it\'s pretty rad.', 0, $time + 9);
4d146f1a 1631 $messageid3 = $this->send_message($user1, $user4, 'Dope.', 0, $time + 10);
6aa01968
MN
1632
1633 // Retrieve the conversations.
1634 $result = core_message_external::data_for_messagearea_conversations($user1->id);
1635
1636 // We need to execute the return values cleaning process to simulate the web service server.
1637 $result = external_api::clean_returnvalue(core_message_external::data_for_messagearea_conversations_returns(),
1638 $result);
1639
1640 // Confirm the data is correct.
1641 $messages = $result['contacts'];
1642 $this->assertCount(3, $messages);
1643
1644 $message1 = $messages[0];
1645 $message2 = $messages[1];
1646 $message3 = $messages[2];
1647
1648 $this->assertEquals($user4->id, $message1['userid']);
1649 $this->assertTrue($message1['ismessaging']);
1650 $this->assertTrue($message1['sentfromcurrentuser']);
1651 $this->assertEquals('Dope.', $message1['lastmessage']);
4d146f1a 1652 $this->assertEquals($messageid3, $message1['messageid']);
cb805753 1653 $this->assertNull($message1['isonline']);
4d146f1a 1654 $this->assertFalse($message1['isread']);
6aa01968 1655 $this->assertFalse($message1['isblocked']);
4d146f1a 1656 $this->assertEquals(1, $message1['unreadcount']);
6aa01968
MN
1657
1658 $this->assertEquals($user3->id, $message2['userid']);
1659 $this->assertTrue($message2['ismessaging']);
1660 $this->assertFalse($message2['sentfromcurrentuser']);
1661 $this->assertEquals('Cool.', $message2['lastmessage']);
4d146f1a 1662 $this->assertEquals($messageid2, $message2['messageid']);
cb805753 1663 $this->assertNull($message2['isonline']);
6aa01968
MN
1664 $this->assertFalse($message2['isread']);
1665 $this->assertFalse($message2['isblocked']);
1666 $this->assertEquals(2, $message2['unreadcount']);
1667
1668 $this->assertEquals($user2->id, $message3['userid']);
1669 $this->assertTrue($message3['ismessaging']);
1670 $this->assertFalse($message3['sentfromcurrentuser']);
1671 $this->assertEquals('Word.', $message3['lastmessage']);
4d146f1a 1672 $this->assertEquals($messageid1, $message3['messageid']);
cb805753 1673 $this->assertNull($message3['isonline']);
6aa01968
MN
1674 $this->assertFalse($message3['isread']);
1675 $this->assertFalse($message3['isblocked']);
1676 $this->assertEquals(2, $message3['unreadcount']);
1677 }
1678
1679 /**
1680 * Tests retrieving conversations as another user.
1681 */
1682 public function test_messagearea_conversations_as_other_user() {
1683 $this->resetAfterTest(true);
1684
1685 // Set as admin.
1686 $this->setAdminUser();
1687
1688 // Create some users.
1689 $user1 = self::getDataGenerator()->create_user();
1690 $user2 = self::getDataGenerator()->create_user();
1691 $user3 = self::getDataGenerator()->create_user();
1692 $user4 = self::getDataGenerator()->create_user();
1693
1694 // Send some messages back and forth, have some different conversations with different users.
1695 $time = time();
1696 $this->send_message($user1, $user2, 'Yo!', 0, $time);
1697 $this->send_message($user2, $user1, 'Sup mang?', 0, $time + 1);
1698 $this->send_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 2);
4d146f1a 1699 $messageid1 = $this->send_message($user2, $user1, 'Word.', 0, $time + 3);
6aa01968
MN
1700
1701 $this->send_message($user1, $user3, 'Booyah', 0, $time + 4);
1702 $this->send_message($user3, $user1, 'Whaaat?', 0, $time + 5);
1703 $this->send_message($user1, $user3, 'Nothing.', 0, $time + 6);
4d146f1a 1704 $messageid2 = $this->send_message($user3, $user1, 'Cool.', 0, $time + 7);
6aa01968
MN
1705
1706 $this->send_message($user1, $user4, 'Hey mate, you see the new messaging UI in Moodle?', 0, $time + 8);
1707 $this->send_message($user4, $user1, 'Yah brah, it\'s pretty rad.', 0, $time + 9);
4d146f1a 1708 $messageid3 = $this->send_message($user1, $user4, 'Dope.', 0, $time + 10);
6aa01968
MN
1709
1710 // Retrieve the conversations.
1711 $result = core_message_external::data_for_messagearea_conversations($user1->id);
1712
1713 // We need to execute the return values cleaning process to simulate the web service server.
1714 $result = external_api::clean_returnvalue(core_message_external::data_for_messagearea_conversations_returns(),
1715 $result);
1716
1717 // Confirm the data is correct.
1718 $messages = $result['contacts'];
1719 $this->assertCount(3, $messages);
1720
1721 $message1 = $messages[0];
1722 $message2 = $messages[1];
1723 $message3 = $messages[2];
1724
1725 $this->assertEquals($user4->id, $message1['userid']);
1726 $this->assertTrue($message1['ismessaging']);
1727 $this->assertTrue($message1['sentfromcurrentuser']);
1728 $this->assertEquals('Dope.', $message1['lastmessage']);
4d146f1a 1729 $this->assertEquals($messageid3, $message1['messageid']);
6aa01968 1730 $this->assertFalse($message1['isonline']);
4d146f1a 1731 $this->assertFalse($message1['isread']);
6aa01968 1732 $this->assertFalse($message1['isblocked']);
4d146f1a 1733 $this->assertEquals(1, $message1['unreadcount']);
6aa01968
MN
1734
1735 $this->assertEquals($user3->id, $message2['userid']);
1736 $this->assertTrue($message2['ismessaging']);
1737 $this->assertFalse($message2['sentfromcurrentuser']);
1738 $this->assertEquals('Cool.', $message2['lastmessage']);
4d146f1a 1739 $this->assertEquals($messageid2, $message2['messageid']);
6aa01968
MN
1740 $this->assertFalse($message2['isonline']);
1741 $this->assertFalse($message2['isread']);
1742 $this->assertFalse($message2['isblocked']);
1743 $this->assertEquals(2, $message2['unreadcount']);
1744
1745 $this->assertEquals($user2->id, $message3['userid']);
1746 $this->assertTrue($message3['ismessaging']);
1747 $this->assertFalse($message3['sentfromcurrentuser']);
1748 $this->assertEquals('Word.', $message3['lastmessage']);
4d146f1a 1749 $this->assertEquals($messageid1, $message3['messageid']);
6aa01968
MN
1750 $this->assertFalse($message3['isonline']);
1751 $this->assertFalse($message3['isread']);
1752 $this->assertFalse($message3['isblocked']);
1753 $this->assertEquals(2, $message3['unreadcount']);
1754 }
1755
1756 /**
1757 * Tests retrieving conversations as another user without the proper capabilities.
1758 */
1759 public function test_messagearea_conversations_as_other_user_without_cap() {
1760 $this->resetAfterTest(true);
1761
1762 // Create some users.
1763 $user1 = self::getDataGenerator()->create_user();
1764 $user2 = self::getDataGenerator()->create_user();
1765
1766 // The person retrieving the conversations for another user.
1767 $this->setUser($user1);
1768
1769 // Ensure an exception is thrown.
1770 $this->expectException('moodle_exception');
1771 core_message_external::data_for_messagearea_conversations($user2->id);
1772 }
1773
1774 /**
1775 * Tests retrieving conversations with messaging disabled.
1776 */
1777 public function test_messagearea_conversations_messaging_disabled() {
1778 global $CFG;
1779
1780 $this->resetAfterTest(true);
1781
1782 // Create some skeleton data just so we can call the WS.
1783 $user = self::getDataGenerator()->create_user();
1784
1785 // The person retrieving the conversations.
1786 $this->setUser($user);
1787
1788 // Disable messaging.
1789 $CFG->messaging = 0;
1790
1791 // Ensure an exception is thrown.
1792 $this->expectException('moodle_exception');
1793 core_message_external::data_for_messagearea_conversations($user->id);
1794 }
1795
1796 /**
1797 * Tests retrieving contacts.
1798 */
1799 public function test_messagearea_contacts() {
1800 $this->resetAfterTest(true);
1801
1802 // Create some users.
1803 $user1 = self::getDataGenerator()->create_user();
1804
1805 // Set as the user.
1806 $this->setUser($user1);
1807
1808 $user2 = new stdClass();
1809 $user2->firstname = 'User';
1810 $user2->lastname = 'A';
1811 $user2 = self::getDataGenerator()->create_user($user2);
1812
1813 $user3 = new stdClass();
1814 $user3->firstname = 'User';
1815 $user3->lastname = 'B';
1816 $user3 = self::getDataGenerator()->create_user($user3);
1817
1818 $user4 = new stdClass();
1819 $user4->firstname = 'User';
1820 $user4->lastname = 'C';
1821 $user4 = self::getDataGenerator()->create_user($user4);
1822
1823 $user5 = new stdClass();
1824 $user5->firstname = 'User';
1825 $user5->lastname = 'D';
1826 $user5 = self::getDataGenerator()->create_user($user5);
1827
1828 // Add some users as contacts.
1829 message_add_contact($user2->id, 0, $user1->id);
1830 message_add_contact($user3->id, 0, $user1->id);
1831 message_add_contact($user4->id, 0, $user1->id);
1832
1833 // Retrieve the contacts.
1834 $result = core_message_external::data_for_messagearea_contacts($user1->id);
1835
1836 // We need to execute the return values cleaning process to simulate the web service server.
1837 $result = external_api::clean_returnvalue(core_message_external::data_for_messagearea_contacts_returns(),
1838 $result);
1839
1840 // Confirm the data is correct.
1841 $contacts = $result['contacts'];
1842 $this->assertCount(3, $contacts);
1843
1844 $contact1 = $contacts[0];
1845 $contact2 = $contacts[1];
1846 $contact3 = $contacts[2];
1847
1848 $this->assertEquals($user2->id, $contact1['userid']);
1849 $this->assertFalse($contact1['ismessaging']);
1850 $this->assertFalse($contact1['sentfromcurrentuser']);
1851 $this->assertNull($contact1['lastmessage']);
1852 $this->assertNull($contact1['messageid']);
cb805753 1853 $this->assertNull($contact1['isonline']);
6aa01968
MN
1854 $this->assertFalse($contact1['isread']);
1855 $this->assertFalse($contact1['isblocked']);
1856 $this->assertNull($contact1['unreadcount']);
1857
1858 $this->assertEquals($user3->id, $contact2['userid']);
1859 $this->assertFalse($contact2['ismessaging']);
1860 $this->assertFalse($contact2['sentfromcurrentuser']);
1861 $this->assertNull($contact2['lastmessage']);
1862 $this->assertNull($contact2['messageid']);
cb805753 1863 $this->assertNull($contact2['isonline']);
6aa01968
MN
1864 $this->assertFalse($contact2['isread']);
1865 $this->assertFalse($contact2['isblocked']);
1866 $this->assertNull($contact2['unreadcount']);
1867
1868 $this->assertEquals($user4->id, $contact3['userid']);
1869 $this->assertFalse($contact3['ismessaging']);
1870 $this->assertFalse($contact3['sentfromcurrentuser']);
1871 $this->assertNull($contact3['lastmessage']);
1872 $this->assertNull($contact3['messageid']);
cb805753 1873 $this->assertNull($contact3['isonline']);
6aa01968
MN
1874 $this->assertFalse($contact3['isread']);
1875 $this->assertFalse($contact3['isblocked']);
1876 $this->assertNull($contact3['unreadcount']);
1877 }
1878
1879 /**
1880 * Tests retrieving contacts as another user.
1881 */
1882 public function test_messagearea_contacts_as_other_user() {
1883 $this->resetAfterTest(true);
1884
1885 $this->setAdminUser();
1886
1887 // Create some users.
1888 $user1 = self::getDataGenerator()->create_user();
1889
1890 $user2 = new stdClass();
1891 $user2->firstname = 'User';
1892 $user2->lastname = 'A';
1893 $user2 = self::getDataGenerator()->create_user($user2);
1894
1895 $user3 = new stdClass();
1896 $user3->firstname = 'User';
1897 $user3->lastname = 'B';
1898 $user3 = self::getDataGenerator()->create_user($user3);
1899
1900 $user4 = new stdClass();
1901 $user4->firstname = 'User';
1902 $user4->lastname = 'C';
1903 $user4 = self::getDataGenerator()->create_user($user4);
1904
1905 $user5 = new stdClass();
1906 $user5->firstname = 'User';
1907 $user5->lastname = 'D';
1908 $user5 = self::getDataGenerator()->create_user($user5);
1909
1910 // Add some users as contacts.
1911 message_add_contact($user2->id, 0, $user1->id);
1912 message_add_contact($user3->id, 0, $user1->id);
1913 message_add_contact($user4->id, 0, $user1->id);
1914
1915 // Retrieve the contacts.
1916 $result = core_message_external::data_for_messagearea_contacts($user1->id);
1917
1918 // We need to execute the return values cleaning process to simulate the web service server.
1919 $result = external_api::clean_returnvalue(core_message_external::data_for_messagearea_contacts_returns(),
1920 $result);
1921
1922 // Confirm the data is correct.
1923 $contacts = $result['contacts'];
1924 $this->assertCount(3, $contacts);
1925
1926 $contact1 = $contacts[0];
1927 $contact2 = $contacts[1];
1928 $contact3 = $contacts[2];
1929
1930 $this->assertEquals($user2->id, $contact1['userid']);
1931 $this->assertFalse($contact1['ismessaging']);
1932 $this->assertFalse($contact1['sentfromcurrentuser']);
1933 $this->assertNull($contact1['lastmessage']);
1934 $this->assertNull($contact1['messageid']);
1935 $this->assertFalse($contact1['isonline']);
1936 $this->assertFalse($contact1['isread']);
1937 $this->assertFalse($contact1['isblocked']);
1938 $this->assertNull($contact1['unreadcount']);
1939
1940 $this->assertEquals($user3->id, $contact2['userid']);
1941 $this->assertFalse($contact2['ismessaging']);
1942 $this->assertFalse($contact2['sentfromcurrentuser']);
1943 $this->assertNull($contact2['lastmessage']);
1944 $this->assertNull($contact2['messageid']);
1945 $this->assertFalse($contact2['isonline']);
1946 $this->assertFalse($contact2['isread']);
1947 $this->assertFalse($contact2['isblocked']);
1948 $this->assertNull($contact2['unreadcount']);
1949
1950 $this->assertEquals($user4->id, $contact3['userid']);
1951 $this->assertFalse($contact3['ismessaging']);
1952 $this->assertFalse($contact3['sentfromcurrentuser']);
1953 $this->assertNull($contact3['lastmessage']);
1954 $this->assertNull($contact3['messageid']);
1955 $this->assertFalse($contact3['isonline']);
1956 $this->assertFalse($contact3['isread']);
1957 $this->assertFalse($contact3['isblocked']);
1958 $this->assertNull($contact3['unreadcount']);
1959 }
1960
1961 /**
1962 * Tests retrieving contacts as another user without the proper capabilities.
1963 */
1964 public function test_messagearea_contacts_as_other_user_without_cap() {
1965 $this->resetAfterTest(true);
1966
1967 // Create some users.
1968 $user1 = self::getDataGenerator()->create_user();
1969 $user2 = self::getDataGenerator()->create_user();
1970
1971 // The person retrieving the contacts for another user.
1972 $this->setUser($user1);
1973
1974 // Perform the WS call and ensure an exception is thrown.
1975 $this->expectException('moodle_exception');
1976 core_message_external::data_for_messagearea_contacts($user2->id);
1977 }
1978
1979 /**
1980 * Tests retrieving contacts with messaging disabled.
1981 */
1982 public function test_messagearea_contacts_messaging_disabled() {
1983 global $CFG;
1984
1985 $this->resetAfterTest(true);
1986
1987 // Create some skeleton data just so we can call the WS.
1988 $user = self::getDataGenerator()->create_user();
1989
1990 // The person retrieving the contacts.
1991 $this->setUser($user);
1992
1993 // Disable messaging.
1994 $CFG->messaging = 0;
1995
1996 // Perform the WS call and ensure we are shown that it is disabled.
1997 $this->expectException('moodle_exception');
1998 core_message_external::data_for_messagearea_contacts($user->id);
1999 }
2000
2001 /**
2002 * Tests retrieving messages.
2003 */
2004 public function test_messagearea_messages() {
2005 $this->resetAfterTest(true);
2006
2007 // Create some users.
2008 $user1 = self::getDataGenerator()->create_user();
2009 $user2 = self::getDataGenerator()->create_user();
2010
2011 // The person asking for the messages.
2012 $this->setUser($user1);
2013
2014 // Send some messages back and forth.
2015 $time = time();
2016 $this->send_message($user1, $user2, 'Yo!', 0, $time);
2017 $this->send_message($user2, $user1, 'Sup mang?', 0, $time + 1);
2018 $this->send_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 2);
2019 $this->send_message($user2, $user1, 'Word.', 0, $time + 3);
2020
2021 // Retrieve the messages.
2022 $result = core_message_external::data_for_messagearea_messages($user1->id, $user2->id);
2023
2024 // We need to execute the return values cleaning process to simulate the web service server.
2025 $result = external_api::clean_returnvalue(core_message_external::data_for_messagearea_messages_returns(),
2026 $result);
2027
2028 // Check the results are correct.
2029 $this->assertTrue($result['iscurrentuser']);
2030 $this->assertEquals($user1->id, $result['currentuserid']);
2031 $this->assertEquals($user2->id, $result['otheruserid']);
2032 $this->assertEquals(fullname($user2), $result['otheruserfullname']);
cb805753 2033 $this->assertNull($result['isonline']);
6aa01968
MN
2034
2035 // Confirm the message data is correct.
2036 $messages = $result['messages'];
2037 $this->assertCount(4, $messages);
2038
2039 $message1 = $messages[0];
2040 $message2 = $messages[1];
2041 $message3 = $messages[2];
2042 $message4 = $messages[3];
2043
2044 $this->assertEquals($user1->id, $message1['useridfrom']);
2045 $this->assertEquals($user2->id, $message1['useridto']);
2046 $this->assertTrue($message1['displayblocktime']);
2047 $this->assertContains('Yo!', $message1['text']);
2048
2049 $this->assertEquals($user2->id, $message2['useridfrom']);
2050 $this->assertEquals($user1->id, $message2['useridto']);
2051 $this->assertFalse($message2['displayblocktime']);
2052 $this->assertContains('Sup mang?', $message2['text']);
2053
2054 $this->assertEquals($user1->id, $message3['useridfrom']);
2055 $this->assertEquals($user2->id, $message3['useridto']);
2056 $this->assertFalse($message3['displayblocktime']);
2057 $this->assertContains('Writing PHPUnit tests!', $message3['text']);
2058
2059 $this->assertEquals($user2->id, $message4['useridfrom']);
2060 $this->assertEquals($user1->id, $message4['useridto']);
2061 $this->assertFalse($message4['displayblocktime']);
2062 $this->assertContains('Word.', $message4['text']);
2063 }
2064
fb1469d8
RW
2065 /**
2066 * Tests retrieving messages.
2067 */
ffd7798c 2068 public function test_messagearea_messages_timefrom() {
fb1469d8
RW
2069 $this->resetAfterTest(true);
2070
2071 // Create some users.
2072 $user1 = self::getDataGenerator()->create_user();
2073 $user2 = self::getDataGenerator()->create_user();
2074
2075 // The person asking for the messages.
2076 $this->setUser($user1);
2077
2078 // Send some messages back and forth.
2079 $time = time();
2080 $this->send_message($user1, $user2, 'Message 1', 0, $time - 4);
2081 $this->send_message($user2, $user1, 'Message 2', 0, $time - 3);
2082 $this->send_message($user1, $user2, 'Message 3', 0, $time - 2);
2083 $this->send_message($user2, $user1, 'Message 4', 0, $time - 1);
2084
ffd7798c 2085 // Retrieve the messages from $time - 3, which should be the 3 most recent messages.
fb1469d8
RW
2086 $result = core_message_external::data_for_messagearea_messages($user1->id, $user2->id, 0, 0, false, $time - 3);
2087
2088 // We need to execute the return values cleaning process to simulate the web service server.
2089 $result = external_api::clean_returnvalue(core_message_external::data_for_messagearea_messages_returns(),
2090 $result);
2091
2092 // Confirm the message data is correct. We shouldn't get 'Message 1' back.
2093 $messages = $result['messages'];
2094 $this->assertCount(3, $messages);
2095
2096 $message1 = $messages[0];
2097 $message2 = $messages[1];
2098 $message3 = $messages[2];
2099
2100 $this->assertContains('Message 2', $message1['text']);
2101 $this->assertContains('Message 3', $message2['text']);
2102 $this->assertContains('Message 4', $message3['text']);
2103 }
2104
6aa01968
MN
2105 /**
2106 * Tests retrieving messages as another user.
2107 */
2108 public function test_messagearea_messages_as_other_user() {
2109 $this->resetAfterTest(true);
2110
2111 // Set as admin.
2112 $this->setAdminUser();
2113
2114 // Create some users.
2115 $user1 = self::getDataGenerator()->create_user();
2116 $user2 = self::getDataGenerator()->create_user();
2117
2118 // Send some messages back and forth.
2119 $time = time();
2120 $this->send_message($user1, $user2, 'Yo!', 0, $time);
2121 $this->send_message($user2, $user1, 'Sup mang?', 0, $time + 1);
2122 $this->send_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 2);
2123 $this->send_message($user2, $user1, 'Word.', 0, $time + 3);
2124
2125 // Retrieve the messages.
2126 $result = core_message_external::data_for_messagearea_messages($user1->id, $user2->id);
2127
2128 // We need to execute the return values cleaning process to simulate the web service server.
2129 $result = external_api::clean_returnvalue(core_message_external::data_for_messagearea_messages_returns(),
2130 $result);
2131
2132 // Check the results are correct.
2133 $this->assertFalse($result['iscurrentuser']);
2134 $this->assertEquals($user1->id, $result['currentuserid']);
2135 $this->assertEquals($user2->id, $result['otheruserid']);
2136 $this->assertEquals(fullname($user2), $result['otheruserfullname']);
2137 $this->assertFalse($result['isonline']);
2138
2139 // Confirm the message data is correct.
2140 $messages = $result['messages'];
2141 $this->assertCount(4, $messages);
2142
2143 $message1 = $messages[0];
2144 $message2 = $messages[1];
2145 $message3 = $messages[2];
2146 $message4 = $messages[3];
2147
2148 $this->assertEquals($user1->id, $message1['useridfrom']);
2149 $this->assertEquals($user2->id, $message1['useridto']);
2150 $this->assertTrue($message1['displayblocktime']);
2151 $this->assertContains('Yo!', $message1['text']);
2152
2153 $this->assertEquals($user2->id, $message2['useridfrom']);
2154 $this->assertEquals($user1->id, $message2['useridto']);
2155 $this->assertFalse($message2['displayblocktime']);
2156 $this->assertContains('Sup mang?', $message2['text']);
2157
2158 $this->assertEquals($user1->id, $message3['useridfrom']);
2159 $this->assertEquals($user2->id, $message3['useridto']);
2160 $this->assertFalse($message3['displayblocktime']);
2161 $this->assertContains('Writing PHPUnit tests!', $message3['text']);
2162
2163 $this->assertEquals($user2->id, $message4['useridfrom']);
2164 $this->assertEquals($user1->id, $message4['useridto']);
2165 $this->assertFalse($message4['displayblocktime']);
2166 $this->assertContains('Word.', $message4['text']);
2167 }
2168
2169 /**
2170 * Tests retrieving messages as another user without the proper capabilities.
2171 */
2172 public function test_messagearea_messages_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 $user3 = self::getDataGenerator()->create_user();
2179
2180 // The person asking for the messages for another user.
2181 $this->setUser($user1);
2182
2183 // Ensure an exception is thrown.
2184 $this->expectException('moodle_exception');
2185 core_message_external::data_for_messagearea_messages($user2->id, $user3->id);
2186 }
2187
2188 /**
2189 * Tests retrieving messages with messaging disabled.
2190 */
2191 public function test_messagearea_messages_messaging_disabled() {
2192 global $CFG;
2193
2194 $this->resetAfterTest(true);
2195
2196 // Create some skeleton data just so we can call the WS.
2197 $user1 = self::getDataGenerator()->create_user();
2198 $user2 = self::getDataGenerator()->create_user();
2199
2200 // The person asking for the messages for another user.
2201 $this->setUser($user1);
2202
2203 // Disable messaging.
2204 $CFG->messaging = 0;
2205
2206 // Ensure an exception is thrown.
2207 $this->expectException('moodle_exception');
2208 core_message_external::data_for_messagearea_messages($user1->id, $user2->id);
2209 }
2210
2211 /**
2212 * Tests retrieving most recent message.
2213 */
2214 public function test_messagearea_get_most_recent_message() {
2215 $this->resetAfterTest(true);
2216
2217 // Create some users.
2218 $user1 = self::getDataGenerator()->create_user();
2219 $user2 = self::getDataGenerator()->create_user();
2220
2221 // The person doing the search.
2222 $this->setUser($user1);
2223
2224 // Send some messages back and forth.
2225 $time = time();
2226 $this->send_message($user1, $user2, 'Yo!', 0, $time);
2227 $this->send_message($user2, $user1, 'Sup mang?', 0, $time + 1);
2228 $this->send_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 2);
2229 $this->send_message($user2, $user1, 'Word.', 0, $time + 3);
2230
2231 // Get the most recent message.
2232 $result = core_message_external::data_for_messagearea_get_most_recent_message($user1->id, $user2->id);
2233
2234 // We need to execute the return values cleaning process to simulate the web service server.
2235 $result = external_api::clean_returnvalue(core_message_external::data_for_messagearea_get_most_recent_message_returns(),
2236 $result);
2237
2238 // Check the results are correct.
2239 $this->assertEquals($user2->id, $result['useridfrom']);
2240 $this->assertEquals($user1->id, $result['useridto']);
2241 $this->assertContains('Word.', $result['text']);
2242 }
2243
2244 /**
2245 * Tests retrieving most recent message as another user.
2246 */
2247 public function test_messagearea_get_most_recent_message_as_other_user() {
2248 $this->resetAfterTest(true);
2249
2250 // The person doing the search.
2251 $this->setAdminUser();
2252
2253 // Create some users.
2254 $user1 = self::getDataGenerator()->create_user();
2255 $user2 = self::getDataGenerator()->create_user();
2256
2257 // Send some messages back and forth.
2258 $time = time();
2259 $this->send_message($user1, $user2, 'Yo!', 0, $time);
2260 $this->send_message($user2, $user1, 'Sup mang?', 0, $time + 1);
2261 $this->send_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 2);
2262 $this->send_message($user2, $user1, 'Word.', 0, $time + 3);
2263
2264 // Get the most recent message.
2265 $result = core_message_external::data_for_messagearea_get_most_recent_message($user1->id, $user2->id);
2266
2267 // We need to execute the return values cleaning process to simulate the web service server.
2268 $result = external_api::clean_returnvalue(core_message_external::data_for_messagearea_get_most_recent_message_returns(),
2269 $result);
2270
2271 // Check the results are correct.
2272 $this->assertEquals($user2->id, $result['useridfrom']);
2273 $this->assertEquals($user1->id, $result['useridto']);
2274 $this->assertContains('Word.', $result['text']);
2275 }
2276
2277 /**
2278 * Tests retrieving most recent message as another user without the proper capabilities.
2279 */
2280 public function test_messagearea_get_most_recent_message_as_other_user_without_cap() {
2281 $this->resetAfterTest(true);
2282
2283 // Create some users.
2284 $user1 = self::getDataGenerator()->create_user();
2285 $user2 = self::getDataGenerator()->create_user();
2286 $user3 = self::getDataGenerator()->create_user();
2287
2288 // The person asking for the most recent message for another user.
2289 $this->setUser($user1);
2290
2291 // Ensure an exception is thrown.
2292 $this->expectException('moodle_exception');
2293 core_message_external::data_for_messagearea_get_most_recent_message($user2->id, $user3->id);
2294 }
2295
2296 /**
2297 * Tests retrieving most recent message with messaging disabled.
2298 */
2299 public function test_messagearea_get_most_recent_message_messaging_disabled() {
2300 global $CFG;
2301
2302 $this->resetAfterTest(true);
2303
2304 // Create some skeleton data just so we can call the WS.
2305 $user1 = self::getDataGenerator()->create_user();
2306 $user2 = self::getDataGenerator()->create_user();
2307
2308 // The person asking for the most recent message.
2309 $this->setUser($user1);
2310
2311 // Disable messaging.
2312 $CFG->messaging = 0;
2313
2314 // Ensure an exception is thrown.
2315 $this->expectException('moodle_exception');
2316 core_message_external::data_for_messagearea_get_most_recent_message($user1->id, $user2->id);
2317 }
2318
2319 /**
2320 * Tests retrieving a user's profile.
2321 */
2322 public function test_messagearea_get_profile() {
2323 $this->resetAfterTest(true);
2324
2325 // Create some users.
2326 $user1 = self::getDataGenerator()->create_user();
2327 $user2 = self::getDataGenerator()->create_user();
2328
2329 // The person asking for the profile information.
2330 $this->setUser($user1);
2331
2332 // Get the profile.
2333 $result = core_message_external::data_for_messagearea_get_profile($user1->id, $user2->id);
2334
2335 // We need to execute the return values cleaning process to simulate the web service server.
2336 $result = external_api::clean_returnvalue(core_message_external::data_for_messagearea_get_profile_returns(),
2337 $result);
2338
2339 $this->assertEquals($user2->id, $result['userid']);
2340 $this->assertEmpty($result['email']);
2341 $this->assertEmpty($result['country']);
2342 $this->assertEmpty($result['city']);
2343 $this->assertEquals(fullname($user2), $result['fullname']);
cb805753 2344 $this->assertNull($result['isonline']);
6aa01968
MN
2345 $this->assertFalse($result['isblocked']);
2346 $this->assertFalse($result['iscontact']);
2347 }
2348
2349 /**
2350 * Tests retrieving a user's profile as another user.
2351 */
2352 public function test_messagearea_profile_as_other_user() {
2353 $this->resetAfterTest(true);
2354
2355 // The person asking for the profile information.
2356 $this->setAdminUser();
2357
2358 // Create some users.
2359 $user1 = self::getDataGenerator()->create_user();
2360
2361 $user2 = new stdClass();
2362 $user2->country = 'AU';
2363 $user2->city = 'Perth';
2364 $user2 = self::getDataGenerator()->create_user($user2);
2365
2366 // Get the profile.
2367 $result = core_message_external::data_for_messagearea_get_profile($user1->id, $user2->id);
2368
2369 // We need to execute the return values cleaning process to simulate the web service server.
2370 $result = external_api::clean_returnvalue(core_message_external::data_for_messagearea_get_profile_returns(),
2371 $result);
2372
2373 $this->assertEquals($user2->id, $result['userid']);
2374 $this->assertEquals($user2->email, $result['email']);
2375 $this->assertEquals(get_string($user2->country, 'countries'), $result['country']);
2376 $this->assertEquals($user2->city, $result['city']);
2377 $this->assertEquals(fullname($user2), $result['fullname']);
2378 $this->assertFalse($result['isonline']);
2379 $this->assertFalse($result['isblocked']);
2380 $this->assertFalse($result['iscontact']);
2381 }
2382
2383 /**
2384 * Tests retrieving a user's profile as another user without the proper capabilities.
2385 */
2386 public function test_messagearea_profile_as_other_user_without_cap() {
2387 $this->resetAfterTest(true);
2388
2389 // Create some users.
2390 $user1 = self::getDataGenerator()->create_user();
2391 $user2 = self::getDataGenerator()->create_user();
2392 $user3 = self::getDataGenerator()->create_user();
2393
2394 // The person asking for the profile information for another user.
2395 $this->setUser($user1);
2396
2397 // Ensure an exception is thrown.
2398 $this->expectException('moodle_exception');
2399 core_message_external::data_for_messagearea_get_profile($user2->id, $user3->id);
2400 }
2401
2402 /**
2403 * Tests retrieving a user's profile with messaging disabled.
2404 */
2405 public function test_messagearea_profile_messaging_disabled() {
2406 global $CFG;
2407
2408 $this->resetAfterTest(true);
2409
2410 // Create some skeleton data just so we can call the WS.
2411 $user1 = self::getDataGenerator()->create_user();
2412 $user2 = self::getDataGenerator()->create_user();
2413
2414 // The person asking for the profile information.
2415 $this->setUser($user1);
2416
2417 // Disable messaging.
2418 $CFG->messaging = 0;
2419
2420 // Ensure an exception is thrown.
2421 $this->expectException('moodle_exception');
2422 core_message_external::data_for_messagearea_get_profile($user1->id, $user2->id);
2423 }
2424
2425 /**
2426 * Test marking all message as read with an invalid user.
2427 */
2428 public function test_mark_all_messages_as_read_invalid_user_exception() {
2429 $this->resetAfterTest(true);
2430
2431 $this->expectException('moodle_exception');
2432 core_message_external::mark_all_messages_as_read(-2132131, 0);
2433 }
2434
2435 /**
2436 * Test marking all message as read without proper access.
2437 */
2438 public function test_mark_all_messages_as_read_access_denied_exception() {
2439 $this->resetAfterTest(true);
2440
2441 $sender = $this->getDataGenerator()->create_user();
2442 $user = $this->getDataGenerator()->create_user();
2443
2444 $this->setUser($user);
2445 $this->expectException('moodle_exception');
2446 core_message_external::mark_all_messages_as_read($sender->id, 0);
2447 }
2448
2449 /**
2450 * Test marking all message as read with missing from user.
2451 */
2452 public function test_mark_all_messages_as_read_missing_from_user_exception() {
2453 $this->resetAfterTest(true);
2454
2455 $sender = $this->getDataGenerator()->create_user();
2456
2457 $this->setUser($sender);
2458 $this->expectException('moodle_exception');
2459 core_message_external::mark_all_messages_as_read($sender->id, 99999);
2460 }
2461
2462 /**
2463 * Test marking all message as read.
2464 */
2465 public function test_mark_all_messages_as_read() {
2466 global $DB;
2467
2468 $this->resetAfterTest(true);
2469
2470 $sender1 = $this->getDataGenerator()->create_user();
2471 $sender2 = $this->getDataGenerator()->create_user();
2472 $sender3 = $this->getDataGenerator()->create_user();
2473 $recipient = $this->getDataGenerator()->create_user();
2474
2475 $this->setUser($recipient);
2476
2477 $this->send_message($sender1, $recipient, 'Message');
2478 $this->send_message($sender1, $recipient, 'Message');
2479 $this->send_message($sender2, $recipient, 'Message');
2480 $this->send_message($sender2, $recipient, 'Message');
2481 $this->send_message($sender3, $recipient, 'Message');
2482 $this->send_message($sender3, $recipient, 'Message');
2483
2484 core_message_external::mark_all_messages_as_read($recipient->id, $sender1->id);
883ce421 2485 $this->assertEquals(2, $DB->count_records('message_user_actions'));
6aa01968
MN
2486
2487 core_message_external::mark_all_messages_as_read($recipient->id, 0);
883ce421 2488 $this->assertEquals(6, $DB->count_records('message_user_actions'));
6aa01968
MN
2489 }
2490
2491 /**
2492 * Test getting unread conversation count.
2493 */
2494 public function test_get_unread_conversations_count() {
2495 $this->resetAfterTest(true);
2496
2497 // Create some users.
2498 $user1 = self::getDataGenerator()->create_user();
2499 $user2 = self::getDataGenerator()->create_user();
2500 $user3 = self::getDataGenerator()->create_user();
2501 $user4 = self::getDataGenerator()->create_user();
2502
2503 // The person wanting the conversation count.
2504 $this->setUser($user1);
2505
2506 // Send some messages back and forth, have some different conversations with different users.
2507 $this->send_message($user1, $user2, 'Yo!');
2508 $this->send_message($user2, $user1, 'Sup mang?');
2509 $this->send_message($user1, $user2, 'Writing PHPUnit tests!');
2510 $this->send_message($user2, $user1, 'Word.');
2511
2512 $this->send_message($user1, $user3, 'Booyah');
2513 $this->send_message($user3, $user1, 'Whaaat?');
2514 $this->send_message($user1, $user3, 'Nothing.');
2515 $this->send_message($user3, $user1, 'Cool.');
2516
2517 $this->send_message($user1, $user4, 'Hey mate, you see the new messaging UI in Moodle?');
2518 $this->send_message($user4, $user1, 'Yah brah, it\'s pretty rad.');
2519 $this->send_message($user1, $user4, 'Dope.');
2520
2521 // Get the unread conversation count.
2522 $result = core_message_external::get_unread_conversations_count($user1->id);
2523
2524 // We need to execute the return values cleaning process to simulate the web service server.
2525 $result = external_api::clean_returnvalue(core_message_external::get_unread_conversations_count_returns(),
2526 $result);
2527
2528 $this->assertEquals(3, $result);
2529 }
2530
2531 /**
2532 * Test getting unread conversation count as other user.
2533 */
2534 public function test_get_unread_conversations_count_as_other_user() {
2535 $this->resetAfterTest(true);
2536
2537 // The person wanting the conversation count.
2538 $this->setAdminUser();
2539
2540 // Create some users.
2541 $user1 = self::getDataGenerator()->create_user();
2542 $user2 = self::getDataGenerator()->create_user();
2543 $user3 = self::getDataGenerator()->create_user();
2544 $user4 = self::getDataGenerator()->create_user();
2545
2546 // Send some messages back and forth, have some different conversations with different users.
2547 $this->send_message($user1, $user2, 'Yo!');
2548 $this->send_message($user2, $user1, 'Sup mang?');
2549 $this->send_message($user1, $user2, 'Writing PHPUnit tests!');
2550 $this->send_message($user2, $user1, 'Word.');
2551
2552 $this->send_message($user1, $user3, 'Booyah');
2553 $this->send_message($user3, $user1, 'Whaaat?');
2554 $this->send_message($user1, $user3, 'Nothing.');
2555 $this->send_message($user3, $user1, 'Cool.');
2556
2557 $this->send_message($user1, $user4, 'Hey mate, you see the new messaging UI in Moodle?');
2558 $this->send_message($user4, $user1, 'Yah brah, it\'s pretty rad.');
2559 $this->send_message($user1, $user4, 'Dope.');
2560
2561 // Get the unread conversation count.
2562 $result = core_message_external::get_unread_conversations_count($user1->id);
2563
2564 // We need to execute the return values cleaning process to simulate the web service server.
2565 $result = external_api::clean_returnvalue(core_message_external::get_unread_conversations_count_returns(),
2566 $result);
2567
2568 $this->assertEquals(3, $result);
2569 }
2570
2571 /**
2572 * Test getting unread conversation count as other user without proper capability.
2573 */
2574 public function test_get_unread_conversations_count_as_other_user_without_cap() {
2575 $this->resetAfterTest(true);
2576
2577 // Create some users.
2578 $user1 = self::getDataGenerator()->create_user();
2579 $user2 = self::getDataGenerator()->create_user();
2580
2581 // The person wanting the conversation count.
2582 $this->setUser($user1);
2583
2584 // Ensure an exception is thrown.
2585 $this->expectException('moodle_exception');
2586 core_message_external::get_unread_conversations_count($user2->id);
2587 }
2588
2589 /**
2590 * Test deleting conversation.
2591 */
2592 public function test_delete_conversation() {
2593 global $DB;
2594
2595 $this->resetAfterTest(true);
2596
2597 // Create some users.
2598 $user1 = self::getDataGenerator()->create_user();
2599 $user2 = self::getDataGenerator()->create_user();
2600
2601 // The person wanting to delete the conversation.
2602 $this->setUser($user1);
2603
2604 // Send some messages back and forth.
2605 $time = time();
883ce421
MN
2606 $m1id = $this->send_message($user1, $user2, 'Yo!', 0, $time);
2607 $m2id = $this->send_message($user2, $user1, 'Sup mang?', 0, $time + 1);
2608 $m3id = $this->send_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 2);
2609 $m4id = $this->send_message($user2, $user1, 'Word.', 0, $time + 3);
6aa01968
MN
2610
2611 // Delete the conversation.
2612 core_message_external::delete_conversation($user1->id, $user2->id);
2613
883ce421
MN
2614 $muas = $DB->get_records('message_user_actions', array(), 'timecreated ASC');
2615 $this->assertCount(4, $muas);
2616 // Sort by id.
2617 ksort($muas);
6aa01968 2618
883ce421
MN
2619 $mua1 = array_shift($muas);
2620 $mua2 = array_shift($muas);
2621 $mua3 = array_shift($muas);
2622 $mua4 = array_shift($muas);
6aa01968 2623
883ce421
MN
2624 $this->assertEquals($user1->id, $mua1->userid);
2625 $this->assertEquals($m1id, $mua1->messageid);
2626 $this->assertEquals(\core_message\api::MESSAGE_ACTION_DELETED, $mua1->action);
6aa01968 2627
883ce421
MN
2628 $this->assertEquals($user1->id, $mua2->userid);
2629 $this->assertEquals($m2id, $mua2->messageid);
2630 $this->assertEquals(\core_message\api::MESSAGE_ACTION_DELETED, $mua2->action);
6aa01968 2631
883ce421
MN
2632 $this->assertEquals($user1->id, $mua3->userid);
2633 $this->assertEquals($m3id, $mua3->messageid);
2634 $this->assertEquals(\core_message\api::MESSAGE_ACTION_DELETED, $mua3->action);
6aa01968 2635
883ce421
MN
2636 $this->assertEquals($user1->id, $mua4->userid);
2637 $this->assertEquals($m4id, $mua4->messageid);
2638 $this->assertEquals(\core_message\api::MESSAGE_ACTION_DELETED, $mua4->action);
6aa01968
MN
2639 }
2640
2641 /**
2642 * Test deleting conversation as other user.
2643 */
2644 public function test_delete_conversation_as_other_user() {
2645 global $DB;
2646
2647 $this->resetAfterTest(true);
2648
2649 $this->setAdminUser();
2650
2651 // Create some users.
2652 $user1 = self::getDataGenerator()->create_user();
2653 $user2 = self::getDataGenerator()->create_user();
2654
2655 // Send some messages back and forth.
2656 $time = time();
883ce421
MN
2657 $m1id = $this->send_message($user1, $user2, 'Yo!', 0, $time);
2658 $m2id = $this->send_message($user2, $user1, 'Sup mang?', 0, $time + 1);
2659 $m3id = $this->send_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 2);
2660 $m4id = $this->send_message($user2, $user1, 'Word.', 0, $time + 3);
6aa01968
MN
2661
2662 // Delete the conversation.
2663 core_message_external::delete_conversation($user1->id, $user2->id);
2664
883ce421
MN
2665 $muas = $DB->get_records('message_user_actions', array(), 'timecreated ASC');
2666 $this->assertCount(4, $muas);
2667 // Sort by id.
2668 ksort($muas);
6aa01968 2669
883ce421
MN
2670 $mua1 = array_shift($muas);
2671 $mua2 = array_shift($muas);
2672 $mua3 = array_shift($muas);
2673 $mua4 = array_shift($muas);
6aa01968 2674
883ce421
MN
2675 $this->assertEquals($user1->id, $mua1->userid);
2676 $this->assertEquals($m1id, $mua1->messageid);
2677 $this->assertEquals(\core_message\api::MESSAGE_ACTION_DELETED, $mua1->action);
6aa01968 2678
883ce421
MN
2679 $this->assertEquals($user1->id, $mua2->userid);
2680 $this->assertEquals($m2id, $mua2->messageid);
2681 $this->assertEquals(\core_message\api::MESSAGE_ACTION_DELETED, $mua2->action);
6aa01968 2682
883ce421
MN
2683 $this->assertEquals($user1->id, $mua3->userid);
2684 $this->assertEquals($m3id, $mua3->messageid);
2685 $this->assertEquals(\core_message\api::MESSAGE_ACTION_DELETED, $mua3->action);
6aa01968 2686
883ce421
MN
2687 $this->assertEquals($user1->id, $mua4->userid);
2688 $this->assertEquals($m4id, $mua4->messageid);
2689 $this->assertEquals(\core_message\api::MESSAGE_ACTION_DELETED, $mua4->action);
6aa01968
MN
2690 }
2691
2692 /**
2693 * Test deleting conversation as other user without proper capability.
2694 */
2695 public function test_delete_conversation_as_other_user_without_cap() {
2696 $this->resetAfterTest(true);
2697
2698 // Create some users.
2699 $user1 = self::getDataGenerator()->create_user();
2700 $user2 = self::getDataGenerator()->create_user();
2701 $user3 = self::getDataGenerator()->create_user();
2702
2703 // The person wanting to delete the conversation.
2704 $this->setUser($user3);
2705
2706 // Ensure an exception is thrown.
2707 $this->expectException('moodle_exception');
2708 core_message_external::delete_conversation($user1->id, $user2->id);
2709 }
2710
2711 /**
2712 * Test deleting conversation with messaging disabled.
2713 */
2714 public function test_delete_conversation_messaging_disabled() {
2715 global $CFG;
2716
2717 $this->resetAfterTest(true);
2718
2719 // Create some users.
2720 $user1 = self::getDataGenerator()->create_user();
2721 $user2 = self::getDataGenerator()->create_user();
2722
2723 // The person wanting to delete the conversation.
2724 $this->setUser($user1);
2725
2726 // Disable messaging.
2727 $CFG->messaging = 0;
2728
2729 // Ensure an exception is thrown.
2730 $this->expectException('moodle_exception');
2731 core_message_external::delete_conversation($user1->id, $user2->id);
2732 }
2733
2734 /**
2735 * Test get message processor.
2736 */
2737 public function test_get_message_processor() {
2738 $this->resetAfterTest(true);
2739
2740 // Create a user.
2741 $user1 = self::getDataGenerator()->create_user();
2742
2743 // Set you as the user.
2744 $this->setUser($user1);
2745
2746 // Get the message processors.
2747 $result = core_message_external::get_message_processor($user1->id, 'popup');
2748
2749 // We need to execute the return values cleaning process to simulate the web service server.
2750 $result = external_api::clean_returnvalue(core_message_external::get_message_processor_returns(), $result);
2751
2752 $this->assertNotEmpty($result['systemconfigured']);
2753 $this->assertNotEmpty($result['userconfigured']);
2754 }
2521afd2
JL
2755
2756 /**
2757 * Test get_user_notification_preferences
2758 */
2759 public function test_get_user_message_preferences() {
2760 $this->resetAfterTest(true);
2761
2762 $user = self::getDataGenerator()->create_user();
2763 $this->setUser($user);
2764
2765 // Set a couple of preferences to test.
2766 set_user_preference('message_provider_moodle_instantmessage_loggedin', 'email', $user);
2767 set_user_preference('message_provider_moodle_instantmessage_loggedoff', 'email', $user);
2768 set_user_preference('message_blocknoncontacts', 1, $user);
2769
2770 $prefs = core_message_external::get_user_message_preferences();
2771 $prefs = external_api::clean_returnvalue(core_message_external::get_user_message_preferences_returns(), $prefs);
2772 $this->assertEquals($user->id, $prefs['preferences']['userid']);
2773
2774 // Check components.
2775 $this->assertCount(1, $prefs['preferences']['components']);
2776 $this->assertTrue($prefs['blocknoncontacts']);
2777
2778 // Check some preferences that we previously set.
2779 $found = false;
2780 foreach ($prefs['preferences']['components'] as $component) {
2781 foreach ($component['notifications'] as $prefdata) {
2782 if ($prefdata['preferencekey'] != 'message_provider_moodle_instantmessage') {
2783 continue;
2784 }
2785 foreach ($prefdata['processors'] as $processor) {
2786 if ($processor['name'] == 'email') {
2787 $this->assertTrue($processor['loggedin']['checked']);
2788 $this->assertTrue($processor['loggedoff']['checked']);
2789 $found = true;
2790 }
2791 }
2792 }
2793 }
2794 $this->assertTrue($found);
2795 }
2796
2797 /**
2798 * Test get_user_message_preferences permissions
2799 */
2800 public function test_get_user_message_preferences_permissions() {
2801 $this->resetAfterTest(true);
2802
2803 $user = self::getDataGenerator()->create_user();
2804 $otheruser = self::getDataGenerator()->create_user();
2805 $this->setUser($user);
2806
2807 $this->expectException('moodle_exception');
2808 $prefs = core_message_external::get_user_message_preferences($otheruser->id);
2809 }
d6731600 2810}