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