weekly release 3.2dev
[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
1175 // Add some users as contacts.
1176 message_add_contact($user2->id, 0, $user1->id);
1177 message_add_contact($user3->id, 0, $user1->id);
1178 message_add_contact($user4->id, 0, $user1->id);
1179
1180 // Perform a search.
1181 $result = core_message_external::data_for_messagearea_search_users($user1->id, 'search');
1182
1183 // We need to execute the return values cleaning process to simulate the web service server.
1184 $result = external_api::clean_returnvalue(core_message_external::data_for_messagearea_search_users_returns(),
1185 $result);
1186
1187 // Confirm that we returns contacts, courses and non-contacts.
1188 $contacts = $result['contacts'];
1189 $courses = $result['courses'];
1190 $noncontacts = $result['noncontacts'];
1191
1192 // Check that we retrieved the correct contacts.
1193 $this->assertCount(2, $contacts);
1194 $this->assertEquals($user3->id, $contacts[0]['userid']);
1195 $this->assertEquals($user2->id, $contacts[1]['userid']);
1196
1197 // Check that we retrieved the correct courses.
1198 $this->assertCount(2, $courses);
1199 $this->assertEquals($course3->id, $courses[0]['id']);
1200 $this->assertEquals($course1->id, $courses[1]['id']);
1201
1202 // Check that we retrieved the correct non-contacts.
1203 $this->assertCount(1, $noncontacts);
1204 $this->assertEquals($user5->id, $noncontacts[0]['userid']);
1205 }
1206
1207 /**
1208 * Tests searching users as another user.
1209 */
1210 public function test_messagearea_search_users_as_other_user() {
1211 $this->resetAfterTest(true);
1212
1213 // The person doing the search.
1214 $this->setAdminUser();
1215
1216 // Create some users.
1217 $user1 = new stdClass();
1218 $user1->firstname = 'User';
1219 $user1->lastname = 'One';
1220 $user1 = self::getDataGenerator()->create_user($user1);
1221
1222 $user2 = new stdClass();
1223 $user2->firstname = 'User search';
1224 $user2->lastname = 'Two';
1225 $user2 = self::getDataGenerator()->create_user($user2);
1226
1227 $user3 = new stdClass();
1228 $user3->firstname = 'User search';
1229 $user3->lastname = 'Three';
1230 $user3 = self::getDataGenerator()->create_user($user3);
1231
1232 $user4 = new stdClass();
1233 $user4->firstname = 'User';
1234 $user4->lastname = 'Four';
1235 $user4 = self::getDataGenerator()->create_user($user4);
1236
1237 $user5 = new stdClass();
1238 $user5->firstname = 'User search';
1239 $user5->lastname = 'Five';
1240 $user5 = self::getDataGenerator()->create_user($user5);
1241
1242 $user6 = new stdClass();
1243 $user6->firstname = 'User';
1244 $user6->lastname = 'Six';
1245 $user6 = self::getDataGenerator()->create_user($user6);
1246
1247 // Create some courses.
1248 $course1 = new stdClass();
1249 $course1->fullname = 'Course search';
1250 $course1->shortname = 'One';
1251 $course1 = $this->getDataGenerator()->create_course($course1);
1252
1253 $course2 = new stdClass();
1254 $course2->fullname = 'Course';
1255 $course2->shortname = 'Two';
1256 $course2 = $this->getDataGenerator()->create_course($course2);
1257
1258 $course3 = new stdClass();
1259 $course3->fullname = 'Course';
1260 $course3->shortname = 'Three search';
1261 $course3 = $this->getDataGenerator()->create_course($course3);
1262
1263 // Add some users as contacts.
1264 message_add_contact($user2->id, 0, $user1->id);
1265 message_add_contact($user3->id, 0, $user1->id);
1266 message_add_contact($user4->id, 0, $user1->id);
1267
1268 // Perform a search.
1269 $result = core_message_external::data_for_messagearea_search_users($user1->id, 'search');
1270
1271 // We need to execute the return values cleaning process to simulate the web service server.
1272 $result = external_api::clean_returnvalue(core_message_external::data_for_messagearea_search_users_returns(),
1273 $result);
1274
1275 // Confirm that we returns contacts, courses and non-contacts.
1276 $contacts = $result['contacts'];
1277 $courses = $result['courses'];
1278 $noncontacts = $result['noncontacts'];
1279
1280 // Check that we retrieved the correct contacts.
1281 $this->assertCount(2, $contacts);
1282 $this->assertEquals($user3->id, $contacts[0]['userid']);
1283 $this->assertEquals($user2->id, $contacts[1]['userid']);
1284
1285 // Check that we retrieved the correct courses.
1286 $this->assertCount(2, $courses);
1287 $this->assertEquals($course3->id, $courses[0]['id']);
1288 $this->assertEquals($course1->id, $courses[1]['id']);
1289
1290 // Check that we retrieved the correct non-contacts.
1291 $this->assertCount(1, $noncontacts);
1292 $this->assertEquals($user5->id, $noncontacts[0]['userid']);
1293 }
1294
1295 /**
1296 * Tests searching users as another user without the proper capabilities.
1297 */
1298 public function test_messagearea_search_users_as_other_user_without_cap() {
1299 $this->resetAfterTest(true);
1300
1301 // Create some users.
1302 $user1 = self::getDataGenerator()->create_user();
1303 $user2 = self::getDataGenerator()->create_user();
1304
1305 // The person doing the search for another user.
1306 $this->setUser($user1);
1307
1308 // Ensure an exception is thrown.
1309 $this->expectException('moodle_exception');
1310 core_message_external::data_for_messagearea_search_users($user2->id, 'User');
1311 }
1312
1313 /**
1314 * Tests searching users with messaging disabled.
1315 */
1316 public function test_messagearea_search_users_messaging_disabled() {
1317 global $CFG;
1318
1319 $this->resetAfterTest(true);
1320
1321 // Create some skeleton data just so we can call the WS.
1322 $user = self::getDataGenerator()->create_user();
1323
1324 // The person doing the search.
1325 $this->setUser($user);
1326
1327 // Disable messaging.
1328 $CFG->messaging = 0;
1329
1330 // Ensure an exception is thrown.
1331 $this->expectException('moodle_exception');
1332 core_message_external::data_for_messagearea_search_users($user->id, 'User');
1333 }
1334
1335 /**
1336 * Tests searching messages.
1337 */
1338 public function test_messagearea_search_messages() {
1339 $this->resetAfterTest(true);
1340
1341 // Create some users.
1342 $user1 = self::getDataGenerator()->create_user();
1343 $user2 = self::getDataGenerator()->create_user();
1344
1345 // The person doing the search.
1346 $this->setUser($user1);
1347
1348 // Send some messages back and forth.
1349 $time = time();
1350 $this->send_message($user1, $user2, 'Yo!', 0, $time);
1351 $this->send_message($user2, $user1, 'Sup mang?', 0, $time + 1);
1352 $this->send_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 2);
1353 $this->send_message($user2, $user1, 'Word.', 0, $time + 3);
1354
1355 // Perform a search.
1356 $result = core_message_external::data_for_messagearea_search_messages($user1->id, 'o');
1357
1358 // We need to execute the return values cleaning process to simulate the web service server.
1359 $result = external_api::clean_returnvalue(core_message_external::data_for_messagearea_search_messages_returns(),
1360 $result);
1361
1362 // Confirm the data is correct.
1363 $messages = $result['contacts'];
1364 $this->assertCount(2, $messages);
1365
1366 $message1 = $messages[0];
1367 $message2 = $messages[1];
1368
1369 $this->assertEquals($user2->id, $message1['userid']);
1370 $this->assertEquals(fullname($user2), $message1['fullname']);
1371 $this->assertTrue($message1['ismessaging']);
1372 $this->assertFalse($message1['sentfromcurrentuser']);
1373 $this->assertEquals('Word.', $message1['lastmessage']);
1374 $this->assertNotEmpty($message1['messageid']);
1375 $this->assertFalse($message1['isonline']);
1376 $this->assertFalse($message1['isread']);
1377 $this->assertFalse($message1['isblocked']);
1378 $this->assertNull($message1['unreadcount']);
1379
1380 $this->assertEquals($user2->id, $message2['userid']);
1381 $this->assertEquals(fullname($user2), $message2['fullname']);
1382 $this->assertTrue($message2['ismessaging']);
1383 $this->assertTrue($message2['sentfromcurrentuser']);
1384 $this->assertEquals('Yo!', $message2['lastmessage']);
1385 $this->assertNotEmpty($message2['messageid']);
1386 $this->assertFalse($message2['isonline']);
1387 $this->assertTrue($message2['isread']);
1388 $this->assertFalse($message2['isblocked']);
1389 $this->assertNull($message2['unreadcount']);
1390 }
1391
1392 /**
1393 * Tests searching messages as another user.
1394 */
1395 public function test_messagearea_search_messages_as_other_user() {
1396 $this->resetAfterTest(true);
1397
1398 // The person doing the search.
1399 $this->setAdminUser();
1400
1401 // Create some users.
1402 $user1 = self::getDataGenerator()->create_user();
1403 $user2 = self::getDataGenerator()->create_user();
1404
1405 // Send some messages back and forth.
1406 $time = time();
1407 $this->send_message($user1, $user2, 'Yo!', 0, $time);
1408 $this->send_message($user2, $user1, 'Sup mang?', 0, $time + 1);
1409 $this->send_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 2);
1410 $this->send_message($user2, $user1, 'Word.', 0, $time + 3);
1411
1412 // Perform a search.
1413 $result = core_message_external::data_for_messagearea_search_messages($user1->id, 'o');
1414
1415 // We need to execute the return values cleaning process to simulate the web service server.
1416 $result = external_api::clean_returnvalue(core_message_external::data_for_messagearea_search_messages_returns(),
1417 $result);
1418
1419 // Confirm the data is correct.
1420 $messages = $result['contacts'];
1421 $this->assertCount(2, $messages);
1422
1423 $message1 = $messages[0];
1424 $message2 = $messages[1];
1425
1426 $this->assertEquals($user2->id, $message1['userid']);
1427 $this->assertEquals(fullname($user2), $message1['fullname']);
1428 $this->assertTrue($message1['ismessaging']);
1429 $this->assertFalse($message1['sentfromcurrentuser']);
1430 $this->assertEquals('Word.', $message1['lastmessage']);
1431 $this->assertNotEmpty($message1['messageid']);
1432 $this->assertFalse($message1['isonline']);
1433 $this->assertFalse($message1['isread']);
1434 $this->assertFalse($message1['isblocked']);
1435 $this->assertNull($message1['unreadcount']);
1436
1437 $this->assertEquals($user2->id, $message2['userid']);
1438 $this->assertEquals(fullname($user2), $message2['fullname']);
1439 $this->assertTrue($message2['ismessaging']);
1440 $this->assertTrue($message2['sentfromcurrentuser']);
1441 $this->assertEquals('Yo!', $message2['lastmessage']);
1442 $this->assertNotEmpty($message2['messageid']);
1443 $this->assertFalse($message2['isonline']);
1444 $this->assertTrue($message2['isread']);
1445 $this->assertFalse($message2['isblocked']);
1446 $this->assertNull($message2['unreadcount']);
1447 }
1448
1449 /**
1450 * Tests searching messages as another user without the proper capabilities.
1451 */
1452 public function test_messagearea_search_messages_as_other_user_without_cap() {
1453 $this->resetAfterTest(true);
1454
1455 // Create some users.
1456 $user1 = self::getDataGenerator()->create_user();
1457 $user2 = self::getDataGenerator()->create_user();
1458
1459 // The person doing the search for another user.
1460 $this->setUser($user1);
1461
1462 // Ensure an exception is thrown.
1463 $this->expectException('moodle_exception');
1464 core_message_external::data_for_messagearea_search_messages($user2->id, 'Search');
1465 }
1466
1467 /**
1468 * Tests searching messages with messaging disabled
1469 */
1470 public function test_messagearea_search_messages_messaging_disabled() {
1471 global $CFG;
1472
1473 $this->resetAfterTest(true);
1474
1475 // Create some skeleton data just so we can call the WS.
1476 $user = self::getDataGenerator()->create_user();
1477
1478 // The person doing the search .
1479 $this->setUser($user);
1480
1481 // Disable messaging.
1482 $CFG->messaging = 0;
1483
1484 // Ensure an exception is thrown.
1485 $this->expectException('moodle_exception');
1486 core_message_external::data_for_messagearea_search_messages($user->id, 'Search');
1487 }
1488
1489 /**
1490 * Tests retrieving conversations.
1491 */
1492 public function test_messagearea_conversations() {
1493 $this->resetAfterTest(true);
1494
1495 // Create some users.
1496 $user1 = self::getDataGenerator()->create_user();
1497 $user2 = self::getDataGenerator()->create_user();
1498 $user3 = self::getDataGenerator()->create_user();
1499 $user4 = self::getDataGenerator()->create_user();
1500
1501 // The person retrieving the conversations.
1502 $this->setUser($user1);
1503
1504 // Send some messages back and forth, have some different conversations with different users.
1505 $time = time();
1506 $this->send_message($user1, $user2, 'Yo!', 0, $time);
1507 $this->send_message($user2, $user1, 'Sup mang?', 0, $time + 1);
1508 $this->send_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 2);
1509 $this->send_message($user2, $user1, 'Word.', 0, $time + 3);
1510
1511 $this->send_message($user1, $user3, 'Booyah', 0, $time + 4);
1512 $this->send_message($user3, $user1, 'Whaaat?', 0, $time + 5);
1513 $this->send_message($user1, $user3, 'Nothing.', 0, $time + 6);
1514 $this->send_message($user3, $user1, 'Cool.', 0, $time + 7);
1515
1516 $this->send_message($user1, $user4, 'Hey mate, you see the new messaging UI in Moodle?', 0, $time + 8);
1517 $this->send_message($user4, $user1, 'Yah brah, it\'s pretty rad.', 0, $time + 9);
1518 $this->send_message($user1, $user4, 'Dope.', 0, $time + 10);
1519
1520 // Retrieve the conversations.
1521 $result = core_message_external::data_for_messagearea_conversations($user1->id);
1522
1523 // We need to execute the return values cleaning process to simulate the web service server.
1524 $result = external_api::clean_returnvalue(core_message_external::data_for_messagearea_conversations_returns(),
1525 $result);
1526
1527 // Confirm the data is correct.
1528 $messages = $result['contacts'];
1529 $this->assertCount(3, $messages);
1530
1531 $message1 = $messages[0];
1532 $message2 = $messages[1];
1533 $message3 = $messages[2];
1534
1535 $this->assertEquals($user4->id, $message1['userid']);
1536 $this->assertTrue($message1['ismessaging']);
1537 $this->assertTrue($message1['sentfromcurrentuser']);
1538 $this->assertEquals('Dope.', $message1['lastmessage']);
1539 $this->assertNull($message1['messageid']);
1540 $this->assertFalse($message1['isonline']);
1541 $this->assertTrue($message1['isread']);
1542 $this->assertFalse($message1['isblocked']);
1543 $this->assertEquals(0, $message1['unreadcount']);
1544
1545 $this->assertEquals($user3->id, $message2['userid']);
1546 $this->assertTrue($message2['ismessaging']);
1547 $this->assertFalse($message2['sentfromcurrentuser']);
1548 $this->assertEquals('Cool.', $message2['lastmessage']);
1549 $this->assertNull($message2['messageid']);
1550 $this->assertFalse($message2['isonline']);
1551 $this->assertFalse($message2['isread']);
1552 $this->assertFalse($message2['isblocked']);
1553 $this->assertEquals(2, $message2['unreadcount']);
1554
1555 $this->assertEquals($user2->id, $message3['userid']);
1556 $this->assertTrue($message3['ismessaging']);
1557 $this->assertFalse($message3['sentfromcurrentuser']);
1558 $this->assertEquals('Word.', $message3['lastmessage']);
1559 $this->assertNull($message3['messageid']);
1560 $this->assertFalse($message3['isonline']);
1561 $this->assertFalse($message3['isread']);
1562 $this->assertFalse($message3['isblocked']);
1563 $this->assertEquals(2, $message3['unreadcount']);
1564 }
1565
1566 /**
1567 * Tests retrieving conversations as another user.
1568 */
1569 public function test_messagearea_conversations_as_other_user() {
1570 $this->resetAfterTest(true);
1571
1572 // Set as admin.
1573 $this->setAdminUser();
1574
1575 // Create some users.
1576 $user1 = self::getDataGenerator()->create_user();
1577 $user2 = self::getDataGenerator()->create_user();
1578 $user3 = self::getDataGenerator()->create_user();
1579 $user4 = self::getDataGenerator()->create_user();
1580
1581 // Send some messages back and forth, have some different conversations with different users.
1582 $time = time();
1583 $this->send_message($user1, $user2, 'Yo!', 0, $time);
1584 $this->send_message($user2, $user1, 'Sup mang?', 0, $time + 1);
1585 $this->send_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 2);
1586 $this->send_message($user2, $user1, 'Word.', 0, $time + 3);
1587
1588 $this->send_message($user1, $user3, 'Booyah', 0, $time + 4);
1589 $this->send_message($user3, $user1, 'Whaaat?', 0, $time + 5);
1590 $this->send_message($user1, $user3, 'Nothing.', 0, $time + 6);
1591 $this->send_message($user3, $user1, 'Cool.', 0, $time + 7);
1592
1593 $this->send_message($user1, $user4, 'Hey mate, you see the new messaging UI in Moodle?', 0, $time + 8);
1594 $this->send_message($user4, $user1, 'Yah brah, it\'s pretty rad.', 0, $time + 9);
1595 $this->send_message($user1, $user4, 'Dope.', 0, $time + 10);
1596
1597 // Retrieve the conversations.
1598 $result = core_message_external::data_for_messagearea_conversations($user1->id);
1599
1600 // We need to execute the return values cleaning process to simulate the web service server.
1601 $result = external_api::clean_returnvalue(core_message_external::data_for_messagearea_conversations_returns(),
1602 $result);
1603
1604 // Confirm the data is correct.
1605 $messages = $result['contacts'];
1606 $this->assertCount(3, $messages);
1607
1608 $message1 = $messages[0];
1609 $message2 = $messages[1];
1610 $message3 = $messages[2];
1611
1612 $this->assertEquals($user4->id, $message1['userid']);
1613 $this->assertTrue($message1['ismessaging']);
1614 $this->assertTrue($message1['sentfromcurrentuser']);
1615 $this->assertEquals('Dope.', $message1['lastmessage']);
1616 $this->assertNull($message1['messageid']);
1617 $this->assertFalse($message1['isonline']);
1618 $this->assertTrue($message1['isread']);
1619 $this->assertFalse($message1['isblocked']);
1620 $this->assertEquals(0, $message1['unreadcount']);
1621
1622 $this->assertEquals($user3->id, $message2['userid']);
1623 $this->assertTrue($message2['ismessaging']);
1624 $this->assertFalse($message2['sentfromcurrentuser']);
1625 $this->assertEquals('Cool.', $message2['lastmessage']);
1626 $this->assertNull($message2['messageid']);
1627 $this->assertFalse($message2['isonline']);
1628 $this->assertFalse($message2['isread']);
1629 $this->assertFalse($message2['isblocked']);
1630 $this->assertEquals(2, $message2['unreadcount']);
1631
1632 $this->assertEquals($user2->id, $message3['userid']);
1633 $this->assertTrue($message3['ismessaging']);
1634 $this->assertFalse($message3['sentfromcurrentuser']);
1635 $this->assertEquals('Word.', $message3['lastmessage']);
1636 $this->assertNull($message3['messageid']);
1637 $this->assertFalse($message3['isonline']);
1638 $this->assertFalse($message3['isread']);
1639 $this->assertFalse($message3['isblocked']);
1640 $this->assertEquals(2, $message3['unreadcount']);
1641 }
1642
1643 /**
1644 * Tests retrieving conversations as another user without the proper capabilities.
1645 */
1646 public function test_messagearea_conversations_as_other_user_without_cap() {
1647 $this->resetAfterTest(true);
1648
1649 // Create some users.
1650 $user1 = self::getDataGenerator()->create_user();
1651 $user2 = self::getDataGenerator()->create_user();
1652
1653 // The person retrieving the conversations for another user.
1654 $this->setUser($user1);
1655
1656 // Ensure an exception is thrown.
1657 $this->expectException('moodle_exception');
1658 core_message_external::data_for_messagearea_conversations($user2->id);
1659 }
1660
1661 /**
1662 * Tests retrieving conversations with messaging disabled.
1663 */
1664 public function test_messagearea_conversations_messaging_disabled() {
1665 global $CFG;
1666
1667 $this->resetAfterTest(true);
1668
1669 // Create some skeleton data just so we can call the WS.
1670 $user = self::getDataGenerator()->create_user();
1671
1672 // The person retrieving the conversations.
1673 $this->setUser($user);
1674
1675 // Disable messaging.
1676 $CFG->messaging = 0;
1677
1678 // Ensure an exception is thrown.
1679 $this->expectException('moodle_exception');
1680 core_message_external::data_for_messagearea_conversations($user->id);
1681 }
1682
1683 /**
1684 * Tests retrieving contacts.
1685 */
1686 public function test_messagearea_contacts() {
1687 $this->resetAfterTest(true);
1688
1689 // Create some users.
1690 $user1 = self::getDataGenerator()->create_user();
1691
1692 // Set as the user.
1693 $this->setUser($user1);
1694
1695 $user2 = new stdClass();
1696 $user2->firstname = 'User';
1697 $user2->lastname = 'A';
1698 $user2 = self::getDataGenerator()->create_user($user2);
1699
1700 $user3 = new stdClass();
1701 $user3->firstname = 'User';
1702 $user3->lastname = 'B';
1703 $user3 = self::getDataGenerator()->create_user($user3);
1704
1705 $user4 = new stdClass();
1706 $user4->firstname = 'User';
1707 $user4->lastname = 'C';
1708 $user4 = self::getDataGenerator()->create_user($user4);
1709
1710 $user5 = new stdClass();
1711 $user5->firstname = 'User';
1712 $user5->lastname = 'D';
1713 $user5 = self::getDataGenerator()->create_user($user5);
1714
1715 // Add some users as contacts.
1716 message_add_contact($user2->id, 0, $user1->id);
1717 message_add_contact($user3->id, 0, $user1->id);
1718 message_add_contact($user4->id, 0, $user1->id);
1719
1720 // Retrieve the contacts.
1721 $result = core_message_external::data_for_messagearea_contacts($user1->id);
1722
1723 // We need to execute the return values cleaning process to simulate the web service server.
1724 $result = external_api::clean_returnvalue(core_message_external::data_for_messagearea_contacts_returns(),
1725 $result);
1726
1727 // Confirm the data is correct.
1728 $contacts = $result['contacts'];
1729 $this->assertCount(3, $contacts);
1730
1731 $contact1 = $contacts[0];
1732 $contact2 = $contacts[1];
1733 $contact3 = $contacts[2];
1734
1735 $this->assertEquals($user2->id, $contact1['userid']);
1736 $this->assertFalse($contact1['ismessaging']);
1737 $this->assertFalse($contact1['sentfromcurrentuser']);
1738 $this->assertNull($contact1['lastmessage']);
1739 $this->assertNull($contact1['messageid']);
1740 $this->assertFalse($contact1['isonline']);
1741 $this->assertFalse($contact1['isread']);
1742 $this->assertFalse($contact1['isblocked']);
1743 $this->assertNull($contact1['unreadcount']);
1744
1745 $this->assertEquals($user3->id, $contact2['userid']);
1746 $this->assertFalse($contact2['ismessaging']);
1747 $this->assertFalse($contact2['sentfromcurrentuser']);
1748 $this->assertNull($contact2['lastmessage']);
1749 $this->assertNull($contact2['messageid']);
1750 $this->assertFalse($contact2['isonline']);
1751 $this->assertFalse($contact2['isread']);
1752 $this->assertFalse($contact2['isblocked']);
1753 $this->assertNull($contact2['unreadcount']);
1754
1755 $this->assertEquals($user4->id, $contact3['userid']);
1756 $this->assertFalse($contact3['ismessaging']);
1757 $this->assertFalse($contact3['sentfromcurrentuser']);
1758 $this->assertNull($contact3['lastmessage']);
1759 $this->assertNull($contact3['messageid']);
1760 $this->assertFalse($contact3['isonline']);
1761 $this->assertFalse($contact3['isread']);
1762 $this->assertFalse($contact3['isblocked']);
1763 $this->assertNull($contact3['unreadcount']);
1764 }
1765
1766 /**
1767 * Tests retrieving contacts as another user.
1768 */
1769 public function test_messagearea_contacts_as_other_user() {
1770 $this->resetAfterTest(true);
1771
1772 $this->setAdminUser();
1773
1774 // Create some users.
1775 $user1 = self::getDataGenerator()->create_user();
1776
1777 $user2 = new stdClass();
1778 $user2->firstname = 'User';
1779 $user2->lastname = 'A';
1780 $user2 = self::getDataGenerator()->create_user($user2);
1781
1782 $user3 = new stdClass();
1783 $user3->firstname = 'User';
1784 $user3->lastname = 'B';
1785 $user3 = self::getDataGenerator()->create_user($user3);
1786
1787 $user4 = new stdClass();
1788 $user4->firstname = 'User';
1789 $user4->lastname = 'C';
1790 $user4 = self::getDataGenerator()->create_user($user4);
1791
1792 $user5 = new stdClass();
1793 $user5->firstname = 'User';
1794 $user5->lastname = 'D';
1795 $user5 = self::getDataGenerator()->create_user($user5);
1796
1797 // Add some users as contacts.
1798 message_add_contact($user2->id, 0, $user1->id);
1799 message_add_contact($user3->id, 0, $user1->id);
1800 message_add_contact($user4->id, 0, $user1->id);
1801
1802 // Retrieve the contacts.
1803 $result = core_message_external::data_for_messagearea_contacts($user1->id);
1804
1805 // We need to execute the return values cleaning process to simulate the web service server.
1806 $result = external_api::clean_returnvalue(core_message_external::data_for_messagearea_contacts_returns(),
1807 $result);
1808
1809 // Confirm the data is correct.
1810 $contacts = $result['contacts'];
1811 $this->assertCount(3, $contacts);
1812
1813 $contact1 = $contacts[0];
1814 $contact2 = $contacts[1];
1815 $contact3 = $contacts[2];
1816
1817 $this->assertEquals($user2->id, $contact1['userid']);
1818 $this->assertFalse($contact1['ismessaging']);
1819 $this->assertFalse($contact1['sentfromcurrentuser']);
1820 $this->assertNull($contact1['lastmessage']);
1821 $this->assertNull($contact1['messageid']);
1822 $this->assertFalse($contact1['isonline']);
1823 $this->assertFalse($contact1['isread']);
1824 $this->assertFalse($contact1['isblocked']);
1825 $this->assertNull($contact1['unreadcount']);
1826
1827 $this->assertEquals($user3->id, $contact2['userid']);
1828 $this->assertFalse($contact2['ismessaging']);
1829 $this->assertFalse($contact2['sentfromcurrentuser']);
1830 $this->assertNull($contact2['lastmessage']);
1831 $this->assertNull($contact2['messageid']);
1832 $this->assertFalse($contact2['isonline']);
1833 $this->assertFalse($contact2['isread']);
1834 $this->assertFalse($contact2['isblocked']);
1835 $this->assertNull($contact2['unreadcount']);
1836
1837 $this->assertEquals($user4->id, $contact3['userid']);
1838 $this->assertFalse($contact3['ismessaging']);
1839 $this->assertFalse($contact3['sentfromcurrentuser']);
1840 $this->assertNull($contact3['lastmessage']);
1841 $this->assertNull($contact3['messageid']);
1842 $this->assertFalse($contact3['isonline']);
1843 $this->assertFalse($contact3['isread']);
1844 $this->assertFalse($contact3['isblocked']);
1845 $this->assertNull($contact3['unreadcount']);
1846 }
1847
1848 /**
1849 * Tests retrieving contacts as another user without the proper capabilities.
1850 */
1851 public function test_messagearea_contacts_as_other_user_without_cap() {
1852 $this->resetAfterTest(true);
1853
1854 // Create some users.
1855 $user1 = self::getDataGenerator()->create_user();
1856 $user2 = self::getDataGenerator()->create_user();
1857
1858 // The person retrieving the contacts for another user.
1859 $this->setUser($user1);
1860
1861 // Perform the WS call and ensure an exception is thrown.
1862 $this->expectException('moodle_exception');
1863 core_message_external::data_for_messagearea_contacts($user2->id);
1864 }
1865
1866 /**
1867 * Tests retrieving contacts with messaging disabled.
1868 */
1869 public function test_messagearea_contacts_messaging_disabled() {
1870 global $CFG;
1871
1872 $this->resetAfterTest(true);
1873
1874 // Create some skeleton data just so we can call the WS.
1875 $user = self::getDataGenerator()->create_user();
1876
1877 // The person retrieving the contacts.
1878 $this->setUser($user);
1879
1880 // Disable messaging.
1881 $CFG->messaging = 0;
1882
1883 // Perform the WS call and ensure we are shown that it is disabled.
1884 $this->expectException('moodle_exception');
1885 core_message_external::data_for_messagearea_contacts($user->id);
1886 }
1887
1888 /**
1889 * Tests retrieving messages.
1890 */
1891 public function test_messagearea_messages() {
1892 $this->resetAfterTest(true);
1893
1894 // Create some users.
1895 $user1 = self::getDataGenerator()->create_user();
1896 $user2 = self::getDataGenerator()->create_user();
1897
1898 // The person asking for the messages.
1899 $this->setUser($user1);
1900
1901 // Send some messages back and forth.
1902 $time = time();
1903 $this->send_message($user1, $user2, 'Yo!', 0, $time);
1904 $this->send_message($user2, $user1, 'Sup mang?', 0, $time + 1);
1905 $this->send_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 2);
1906 $this->send_message($user2, $user1, 'Word.', 0, $time + 3);
1907
1908 // Retrieve the messages.
1909 $result = core_message_external::data_for_messagearea_messages($user1->id, $user2->id);
1910
1911 // We need to execute the return values cleaning process to simulate the web service server.
1912 $result = external_api::clean_returnvalue(core_message_external::data_for_messagearea_messages_returns(),
1913 $result);
1914
1915 // Check the results are correct.
1916 $this->assertTrue($result['iscurrentuser']);
1917 $this->assertEquals($user1->id, $result['currentuserid']);
1918 $this->assertEquals($user2->id, $result['otheruserid']);
1919 $this->assertEquals(fullname($user2), $result['otheruserfullname']);
1920 $this->assertFalse($result['isonline']);
1921
1922 // Confirm the message data is correct.
1923 $messages = $result['messages'];
1924 $this->assertCount(4, $messages);
1925
1926 $message1 = $messages[0];
1927 $message2 = $messages[1];
1928 $message3 = $messages[2];
1929 $message4 = $messages[3];
1930
1931 $this->assertEquals($user1->id, $message1['useridfrom']);
1932 $this->assertEquals($user2->id, $message1['useridto']);
1933 $this->assertTrue($message1['displayblocktime']);
1934 $this->assertContains('Yo!', $message1['text']);
1935
1936 $this->assertEquals($user2->id, $message2['useridfrom']);
1937 $this->assertEquals($user1->id, $message2['useridto']);
1938 $this->assertFalse($message2['displayblocktime']);
1939 $this->assertContains('Sup mang?', $message2['text']);
1940
1941 $this->assertEquals($user1->id, $message3['useridfrom']);
1942 $this->assertEquals($user2->id, $message3['useridto']);
1943 $this->assertFalse($message3['displayblocktime']);
1944 $this->assertContains('Writing PHPUnit tests!', $message3['text']);
1945
1946 $this->assertEquals($user2->id, $message4['useridfrom']);
1947 $this->assertEquals($user1->id, $message4['useridto']);
1948 $this->assertFalse($message4['displayblocktime']);
1949 $this->assertContains('Word.', $message4['text']);
1950 }
1951
1952 /**
1953 * Tests retrieving messages as another user.
1954 */
1955 public function test_messagearea_messages_as_other_user() {
1956 $this->resetAfterTest(true);
1957
1958 // Set as admin.
1959 $this->setAdminUser();
1960
1961 // Create some users.
1962 $user1 = self::getDataGenerator()->create_user();
1963 $user2 = self::getDataGenerator()->create_user();
1964
1965 // Send some messages back and forth.
1966 $time = time();
1967 $this->send_message($user1, $user2, 'Yo!', 0, $time);
1968 $this->send_message($user2, $user1, 'Sup mang?', 0, $time + 1);
1969 $this->send_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 2);
1970 $this->send_message($user2, $user1, 'Word.', 0, $time + 3);
1971
1972 // Retrieve the messages.
1973 $result = core_message_external::data_for_messagearea_messages($user1->id, $user2->id);
1974
1975 // We need to execute the return values cleaning process to simulate the web service server.
1976 $result = external_api::clean_returnvalue(core_message_external::data_for_messagearea_messages_returns(),
1977 $result);
1978
1979 // Check the results are correct.
1980 $this->assertFalse($result['iscurrentuser']);
1981 $this->assertEquals($user1->id, $result['currentuserid']);
1982 $this->assertEquals($user2->id, $result['otheruserid']);
1983 $this->assertEquals(fullname($user2), $result['otheruserfullname']);
1984 $this->assertFalse($result['isonline']);
1985
1986 // Confirm the message data is correct.
1987 $messages = $result['messages'];
1988 $this->assertCount(4, $messages);
1989
1990 $message1 = $messages[0];
1991 $message2 = $messages[1];
1992 $message3 = $messages[2];
1993 $message4 = $messages[3];
1994
1995 $this->assertEquals($user1->id, $message1['useridfrom']);
1996 $this->assertEquals($user2->id, $message1['useridto']);
1997 $this->assertTrue($message1['displayblocktime']);
1998 $this->assertContains('Yo!', $message1['text']);
1999
2000 $this->assertEquals($user2->id, $message2['useridfrom']);
2001 $this->assertEquals($user1->id, $message2['useridto']);
2002 $this->assertFalse($message2['displayblocktime']);
2003 $this->assertContains('Sup mang?', $message2['text']);
2004
2005 $this->assertEquals($user1->id, $message3['useridfrom']);
2006 $this->assertEquals($user2->id, $message3['useridto']);
2007 $this->assertFalse($message3['displayblocktime']);
2008 $this->assertContains('Writing PHPUnit tests!', $message3['text']);
2009
2010 $this->assertEquals($user2->id, $message4['useridfrom']);
2011 $this->assertEquals($user1->id, $message4['useridto']);
2012 $this->assertFalse($message4['displayblocktime']);
2013 $this->assertContains('Word.', $message4['text']);
2014 }
2015
2016 /**
2017 * Tests retrieving messages as another user without the proper capabilities.
2018 */
2019 public function test_messagearea_messages_as_other_user_without_cap() {
2020 $this->resetAfterTest(true);
2021
2022 // Create some users.
2023 $user1 = self::getDataGenerator()->create_user();
2024 $user2 = self::getDataGenerator()->create_user();
2025 $user3 = self::getDataGenerator()->create_user();
2026
2027 // The person asking for the messages for another user.
2028 $this->setUser($user1);
2029
2030 // Ensure an exception is thrown.
2031 $this->expectException('moodle_exception');
2032 core_message_external::data_for_messagearea_messages($user2->id, $user3->id);
2033 }
2034
2035 /**
2036 * Tests retrieving messages with messaging disabled.
2037 */
2038 public function test_messagearea_messages_messaging_disabled() {
2039 global $CFG;
2040
2041 $this->resetAfterTest(true);
2042
2043 // Create some skeleton data just so we can call the WS.
2044 $user1 = self::getDataGenerator()->create_user();
2045 $user2 = self::getDataGenerator()->create_user();
2046
2047 // The person asking for the messages for another user.
2048 $this->setUser($user1);
2049
2050 // Disable messaging.
2051 $CFG->messaging = 0;
2052
2053 // Ensure an exception is thrown.
2054 $this->expectException('moodle_exception');
2055 core_message_external::data_for_messagearea_messages($user1->id, $user2->id);
2056 }
2057
2058 /**
2059 * Tests retrieving most recent message.
2060 */
2061 public function test_messagearea_get_most_recent_message() {
2062 $this->resetAfterTest(true);
2063
2064 // Create some users.
2065 $user1 = self::getDataGenerator()->create_user();
2066 $user2 = self::getDataGenerator()->create_user();
2067
2068 // The person doing the search.
2069 $this->setUser($user1);
2070
2071 // Send some messages back and forth.
2072 $time = time();
2073 $this->send_message($user1, $user2, 'Yo!', 0, $time);
2074 $this->send_message($user2, $user1, 'Sup mang?', 0, $time + 1);
2075 $this->send_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 2);
2076 $this->send_message($user2, $user1, 'Word.', 0, $time + 3);
2077
2078 // Get the most recent message.
2079 $result = core_message_external::data_for_messagearea_get_most_recent_message($user1->id, $user2->id);
2080
2081 // We need to execute the return values cleaning process to simulate the web service server.
2082 $result = external_api::clean_returnvalue(core_message_external::data_for_messagearea_get_most_recent_message_returns(),
2083 $result);
2084
2085 // Check the results are correct.
2086 $this->assertEquals($user2->id, $result['useridfrom']);
2087 $this->assertEquals($user1->id, $result['useridto']);
2088 $this->assertContains('Word.', $result['text']);
2089 }
2090
2091 /**
2092 * Tests retrieving most recent message as another user.
2093 */
2094 public function test_messagearea_get_most_recent_message_as_other_user() {
2095 $this->resetAfterTest(true);
2096
2097 // The person doing the search.
2098 $this->setAdminUser();
2099
2100 // Create some users.
2101 $user1 = self::getDataGenerator()->create_user();
2102 $user2 = self::getDataGenerator()->create_user();
2103
2104 // Send some messages back and forth.
2105 $time = time();
2106 $this->send_message($user1, $user2, 'Yo!', 0, $time);
2107 $this->send_message($user2, $user1, 'Sup mang?', 0, $time + 1);
2108 $this->send_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 2);
2109 $this->send_message($user2, $user1, 'Word.', 0, $time + 3);
2110
2111 // Get the most recent message.
2112 $result = core_message_external::data_for_messagearea_get_most_recent_message($user1->id, $user2->id);
2113
2114 // We need to execute the return values cleaning process to simulate the web service server.
2115 $result = external_api::clean_returnvalue(core_message_external::data_for_messagearea_get_most_recent_message_returns(),
2116 $result);
2117
2118 // Check the results are correct.
2119 $this->assertEquals($user2->id, $result['useridfrom']);
2120 $this->assertEquals($user1->id, $result['useridto']);
2121 $this->assertContains('Word.', $result['text']);
2122 }
2123
2124 /**
2125 * Tests retrieving most recent message as another user without the proper capabilities.
2126 */
2127 public function test_messagearea_get_most_recent_message_as_other_user_without_cap() {
2128 $this->resetAfterTest(true);
2129
2130 // Create some users.
2131 $user1 = self::getDataGenerator()->create_user();
2132 $user2 = self::getDataGenerator()->create_user();
2133 $user3 = self::getDataGenerator()->create_user();
2134
2135 // The person asking for the most recent message for another user.
2136 $this->setUser($user1);
2137
2138 // Ensure an exception is thrown.
2139 $this->expectException('moodle_exception');
2140 core_message_external::data_for_messagearea_get_most_recent_message($user2->id, $user3->id);
2141 }
2142
2143 /**
2144 * Tests retrieving most recent message with messaging disabled.
2145 */
2146 public function test_messagearea_get_most_recent_message_messaging_disabled() {
2147 global $CFG;
2148
2149 $this->resetAfterTest(true);
2150
2151 // Create some skeleton data just so we can call the WS.
2152 $user1 = self::getDataGenerator()->create_user();
2153 $user2 = self::getDataGenerator()->create_user();
2154
2155 // The person asking for the most recent message.
2156 $this->setUser($user1);
2157
2158 // Disable messaging.
2159 $CFG->messaging = 0;
2160
2161 // Ensure an exception is thrown.
2162 $this->expectException('moodle_exception');
2163 core_message_external::data_for_messagearea_get_most_recent_message($user1->id, $user2->id);
2164 }
2165
2166 /**
2167 * Tests retrieving a user's profile.
2168 */
2169 public function test_messagearea_get_profile() {
2170 $this->resetAfterTest(true);
2171
2172 // Create some users.
2173 $user1 = self::getDataGenerator()->create_user();
2174 $user2 = self::getDataGenerator()->create_user();
2175
2176 // The person asking for the profile information.
2177 $this->setUser($user1);
2178
2179 // Get the profile.
2180 $result = core_message_external::data_for_messagearea_get_profile($user1->id, $user2->id);
2181
2182 // We need to execute the return values cleaning process to simulate the web service server.
2183 $result = external_api::clean_returnvalue(core_message_external::data_for_messagearea_get_profile_returns(),
2184 $result);
2185
2186 $this->assertEquals($user2->id, $result['userid']);
2187 $this->assertEmpty($result['email']);
2188 $this->assertEmpty($result['country']);
2189 $this->assertEmpty($result['city']);
2190 $this->assertEquals(fullname($user2), $result['fullname']);
2191 $this->assertFalse($result['isonline']);
2192 $this->assertFalse($result['isblocked']);
2193 $this->assertFalse($result['iscontact']);
2194 }
2195
2196 /**
2197 * Tests retrieving a user's profile as another user.
2198 */
2199 public function test_messagearea_profile_as_other_user() {
2200 $this->resetAfterTest(true);
2201
2202 // The person asking for the profile information.
2203 $this->setAdminUser();
2204
2205 // Create some users.
2206 $user1 = self::getDataGenerator()->create_user();
2207
2208 $user2 = new stdClass();
2209 $user2->country = 'AU';
2210 $user2->city = 'Perth';
2211 $user2 = self::getDataGenerator()->create_user($user2);
2212
2213 // Get the profile.
2214 $result = core_message_external::data_for_messagearea_get_profile($user1->id, $user2->id);
2215
2216 // We need to execute the return values cleaning process to simulate the web service server.
2217 $result = external_api::clean_returnvalue(core_message_external::data_for_messagearea_get_profile_returns(),
2218 $result);
2219
2220 $this->assertEquals($user2->id, $result['userid']);
2221 $this->assertEquals($user2->email, $result['email']);
2222 $this->assertEquals(get_string($user2->country, 'countries'), $result['country']);
2223 $this->assertEquals($user2->city, $result['city']);
2224 $this->assertEquals(fullname($user2), $result['fullname']);
2225 $this->assertFalse($result['isonline']);
2226 $this->assertFalse($result['isblocked']);
2227 $this->assertFalse($result['iscontact']);
2228 }
2229
2230 /**
2231 * Tests retrieving a user's profile as another user without the proper capabilities.
2232 */
2233 public function test_messagearea_profile_as_other_user_without_cap() {
2234 $this->resetAfterTest(true);
2235
2236 // Create some users.
2237 $user1 = self::getDataGenerator()->create_user();
2238 $user2 = self::getDataGenerator()->create_user();
2239 $user3 = self::getDataGenerator()->create_user();
2240
2241 // The person asking for the profile information for another user.
2242 $this->setUser($user1);
2243
2244 // Ensure an exception is thrown.
2245 $this->expectException('moodle_exception');
2246 core_message_external::data_for_messagearea_get_profile($user2->id, $user3->id);
2247 }
2248
2249 /**
2250 * Tests retrieving a user's profile with messaging disabled.
2251 */
2252 public function test_messagearea_profile_messaging_disabled() {
2253 global $CFG;
2254
2255 $this->resetAfterTest(true);
2256
2257 // Create some skeleton data just so we can call the WS.
2258 $user1 = self::getDataGenerator()->create_user();
2259 $user2 = self::getDataGenerator()->create_user();
2260
2261 // The person asking for the profile information.
2262 $this->setUser($user1);
2263
2264 // Disable messaging.
2265 $CFG->messaging = 0;
2266
2267 // Ensure an exception is thrown.
2268 $this->expectException('moodle_exception');
2269 core_message_external::data_for_messagearea_get_profile($user1->id, $user2->id);
2270 }
2271
2272 /**
2273 * Test marking all message as read with an invalid user.
2274 */
2275 public function test_mark_all_messages_as_read_invalid_user_exception() {
2276 $this->resetAfterTest(true);
2277
2278 $this->expectException('moodle_exception');
2279 core_message_external::mark_all_messages_as_read(-2132131, 0);
2280 }
2281
2282 /**
2283 * Test marking all message as read without proper access.
2284 */
2285 public function test_mark_all_messages_as_read_access_denied_exception() {
2286 $this->resetAfterTest(true);
2287
2288 $sender = $this->getDataGenerator()->create_user();
2289 $user = $this->getDataGenerator()->create_user();
2290
2291 $this->setUser($user);
2292 $this->expectException('moodle_exception');
2293 core_message_external::mark_all_messages_as_read($sender->id, 0);
2294 }
2295
2296 /**
2297 * Test marking all message as read with missing from user.
2298 */
2299 public function test_mark_all_messages_as_read_missing_from_user_exception() {
2300 $this->resetAfterTest(true);
2301
2302 $sender = $this->getDataGenerator()->create_user();
2303
2304 $this->setUser($sender);
2305 $this->expectException('moodle_exception');
2306 core_message_external::mark_all_messages_as_read($sender->id, 99999);
2307 }
2308
2309 /**
2310 * Test marking all message as read.
2311 */
2312 public function test_mark_all_messages_as_read() {
2313 global $DB;
2314
2315 $this->resetAfterTest(true);
2316
2317 $sender1 = $this->getDataGenerator()->create_user();
2318 $sender2 = $this->getDataGenerator()->create_user();
2319 $sender3 = $this->getDataGenerator()->create_user();
2320 $recipient = $this->getDataGenerator()->create_user();
2321
2322 $this->setUser($recipient);
2323
2324 $this->send_message($sender1, $recipient, 'Message');
2325 $this->send_message($sender1, $recipient, 'Message');
2326 $this->send_message($sender2, $recipient, 'Message');
2327 $this->send_message($sender2, $recipient, 'Message');
2328 $this->send_message($sender3, $recipient, 'Message');
2329 $this->send_message($sender3, $recipient, 'Message');
2330
2331 core_message_external::mark_all_messages_as_read($recipient->id, $sender1->id);
2332 $readnotifications = $DB->get_records('message_read', array('useridto' => $recipient->id));
2333 $unreadnotifications = $DB->get_records('message', array('useridto' => $recipient->id));
2334
2335 $this->assertCount(2, $readnotifications);
2336 $this->assertCount(4, $unreadnotifications);
2337
2338 core_message_external::mark_all_messages_as_read($recipient->id, 0);
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(6, $readnotifications);
2343 $this->assertCount(0, $unreadnotifications);
2344 }
2345
2346 /**
2347 * Test getting unread conversation count.
2348 */
2349 public function test_get_unread_conversations_count() {
2350 $this->resetAfterTest(true);
2351
2352 // Create some users.
2353 $user1 = self::getDataGenerator()->create_user();
2354 $user2 = self::getDataGenerator()->create_user();
2355 $user3 = self::getDataGenerator()->create_user();
2356 $user4 = self::getDataGenerator()->create_user();
2357
2358 // The person wanting the conversation count.
2359 $this->setUser($user1);
2360
2361 // Send some messages back and forth, have some different conversations with different users.
2362 $this->send_message($user1, $user2, 'Yo!');
2363 $this->send_message($user2, $user1, 'Sup mang?');
2364 $this->send_message($user1, $user2, 'Writing PHPUnit tests!');
2365 $this->send_message($user2, $user1, 'Word.');
2366
2367 $this->send_message($user1, $user3, 'Booyah');
2368 $this->send_message($user3, $user1, 'Whaaat?');
2369 $this->send_message($user1, $user3, 'Nothing.');
2370 $this->send_message($user3, $user1, 'Cool.');
2371
2372 $this->send_message($user1, $user4, 'Hey mate, you see the new messaging UI in Moodle?');
2373 $this->send_message($user4, $user1, 'Yah brah, it\'s pretty rad.');
2374 $this->send_message($user1, $user4, 'Dope.');
2375
2376 // Get the unread conversation count.
2377 $result = core_message_external::get_unread_conversations_count($user1->id);
2378
2379 // We need to execute the return values cleaning process to simulate the web service server.
2380 $result = external_api::clean_returnvalue(core_message_external::get_unread_conversations_count_returns(),
2381 $result);
2382
2383 $this->assertEquals(3, $result);
2384 }
2385
2386 /**
2387 * Test getting unread conversation count as other user.
2388 */
2389 public function test_get_unread_conversations_count_as_other_user() {
2390 $this->resetAfterTest(true);
2391
2392 // The person wanting the conversation count.
2393 $this->setAdminUser();
2394
2395 // Create some users.
2396 $user1 = self::getDataGenerator()->create_user();
2397 $user2 = self::getDataGenerator()->create_user();
2398 $user3 = self::getDataGenerator()->create_user();
2399 $user4 = self::getDataGenerator()->create_user();
2400
2401 // Send some messages back and forth, have some different conversations with different users.
2402 $this->send_message($user1, $user2, 'Yo!');
2403 $this->send_message($user2, $user1, 'Sup mang?');
2404 $this->send_message($user1, $user2, 'Writing PHPUnit tests!');
2405 $this->send_message($user2, $user1, 'Word.');
2406
2407 $this->send_message($user1, $user3, 'Booyah');
2408 $this->send_message($user3, $user1, 'Whaaat?');
2409 $this->send_message($user1, $user3, 'Nothing.');
2410 $this->send_message($user3, $user1, 'Cool.');
2411
2412 $this->send_message($user1, $user4, 'Hey mate, you see the new messaging UI in Moodle?');
2413 $this->send_message($user4, $user1, 'Yah brah, it\'s pretty rad.');
2414 $this->send_message($user1, $user4, 'Dope.');
2415
2416 // Get the unread conversation count.
2417 $result = core_message_external::get_unread_conversations_count($user1->id);
2418
2419 // We need to execute the return values cleaning process to simulate the web service server.
2420 $result = external_api::clean_returnvalue(core_message_external::get_unread_conversations_count_returns(),
2421 $result);
2422
2423 $this->assertEquals(3, $result);
2424 }
2425
2426 /**
2427 * Test getting unread conversation count as other user without proper capability.
2428 */
2429 public function test_get_unread_conversations_count_as_other_user_without_cap() {
2430 $this->resetAfterTest(true);
2431
2432 // Create some users.
2433 $user1 = self::getDataGenerator()->create_user();
2434 $user2 = self::getDataGenerator()->create_user();
2435
2436 // The person wanting the conversation count.
2437 $this->setUser($user1);
2438
2439 // Ensure an exception is thrown.
2440 $this->expectException('moodle_exception');
2441 core_message_external::get_unread_conversations_count($user2->id);
2442 }
2443
2444 /**
2445 * Test deleting conversation.
2446 */
2447 public function test_delete_conversation() {
2448 global $DB;
2449
2450 $this->resetAfterTest(true);
2451
2452 // Create some users.
2453 $user1 = self::getDataGenerator()->create_user();
2454 $user2 = self::getDataGenerator()->create_user();
2455
2456 // The person wanting to delete the conversation.
2457 $this->setUser($user1);
2458
2459 // Send some messages back and forth.
2460 $time = time();
2461 $this->send_message($user1, $user2, 'Yo!', 0, $time);
2462 $this->send_message($user2, $user1, 'Sup mang?', 0, $time + 1);
2463 $this->send_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 2);
2464 $this->send_message($user2, $user1, 'Word.', 0, $time + 3);
2465
2466 // Delete the conversation.
2467 core_message_external::delete_conversation($user1->id, $user2->id);
2468
2469 $messages = $DB->get_records('message', array(), 'timecreated ASC');
2470 $this->assertCount(4, $messages);
2471
2472 $message1 = array_shift($messages);
2473 $message2 = array_shift($messages);
2474 $message3 = array_shift($messages);
2475 $message4 = array_shift($messages);
2476
2477 $this->assertNotEmpty($message1->timeuserfromdeleted);
2478 $this->assertEmpty($message1->timeusertodeleted);
2479
2480 $this->assertEmpty($message2->timeuserfromdeleted);
2481 $this->assertNotEmpty($message2->timeusertodeleted);
2482
2483 $this->assertNotEmpty($message3->timeuserfromdeleted);
2484 $this->assertEmpty($message3->timeusertodeleted);
2485
2486 $this->assertEmpty($message4->timeuserfromdeleted);
2487 $this->assertNotEmpty($message4->timeusertodeleted);
2488 }
2489
2490 /**
2491 * Test deleting conversation as other user.
2492 */
2493 public function test_delete_conversation_as_other_user() {
2494 global $DB;
2495
2496 $this->resetAfterTest(true);
2497
2498 $this->setAdminUser();
2499
2500 // Create some users.
2501 $user1 = self::getDataGenerator()->create_user();
2502 $user2 = self::getDataGenerator()->create_user();
2503
2504 // Send some messages back and forth.
2505 $time = time();
2506 $this->send_message($user1, $user2, 'Yo!', 0, $time);
2507 $this->send_message($user2, $user1, 'Sup mang?', 0, $time + 1);
2508 $this->send_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 2);
2509 $this->send_message($user2, $user1, 'Word.', 0, $time + 3);
2510
2511 // Delete the conversation.
2512 core_message_external::delete_conversation($user1->id, $user2->id);
2513
2514 $messages = $DB->get_records('message', array(), 'timecreated ASC');
2515 $this->assertCount(4, $messages);
2516
2517 $message1 = array_shift($messages);
2518 $message2 = array_shift($messages);
2519 $message3 = array_shift($messages);
2520 $message4 = array_shift($messages);
2521
2522 $this->assertNotEmpty($message1->timeuserfromdeleted);
2523 $this->assertEmpty($message1->timeusertodeleted);
2524
2525 $this->assertEmpty($message2->timeuserfromdeleted);
2526 $this->assertNotEmpty($message2->timeusertodeleted);
2527
2528 $this->assertNotEmpty($message3->timeuserfromdeleted);
2529 $this->assertEmpty($message3->timeusertodeleted);
2530
2531 $this->assertEmpty($message4->timeuserfromdeleted);
2532 $this->assertNotEmpty($message4->timeusertodeleted);
2533 }
2534
2535 /**
2536 * Test deleting conversation as other user without proper capability.
2537 */
2538 public function test_delete_conversation_as_other_user_without_cap() {
2539 $this->resetAfterTest(true);
2540
2541 // Create some users.
2542 $user1 = self::getDataGenerator()->create_user();
2543 $user2 = self::getDataGenerator()->create_user();
2544 $user3 = self::getDataGenerator()->create_user();
2545
2546 // The person wanting to delete the conversation.
2547 $this->setUser($user3);
2548
2549 // Ensure an exception is thrown.
2550 $this->expectException('moodle_exception');
2551 core_message_external::delete_conversation($user1->id, $user2->id);
2552 }
2553
2554 /**
2555 * Test deleting conversation with messaging disabled.
2556 */
2557 public function test_delete_conversation_messaging_disabled() {
2558 global $CFG;
2559
2560 $this->resetAfterTest(true);
2561
2562 // Create some users.
2563 $user1 = self::getDataGenerator()->create_user();
2564 $user2 = self::getDataGenerator()->create_user();
2565
2566 // The person wanting to delete the conversation.
2567 $this->setUser($user1);
2568
2569 // Disable messaging.
2570 $CFG->messaging = 0;
2571
2572 // Ensure an exception is thrown.
2573 $this->expectException('moodle_exception');
2574 core_message_external::delete_conversation($user1->id, $user2->id);
2575 }
2576
2577 /**
2578 * Test get message processor.
2579 */
2580 public function test_get_message_processor() {
2581 $this->resetAfterTest(true);
2582
2583 // Create a user.
2584 $user1 = self::getDataGenerator()->create_user();
2585
2586 // Set you as the user.
2587 $this->setUser($user1);
2588
2589 // Get the message processors.
2590 $result = core_message_external::get_message_processor($user1->id, 'popup');
2591
2592 // We need to execute the return values cleaning process to simulate the web service server.
2593 $result = external_api::clean_returnvalue(core_message_external::get_message_processor_returns(), $result);
2594
2595 $this->assertNotEmpty($result['systemconfigured']);
2596 $this->assertNotEmpty($result['userconfigured']);
2597 }
d6731600 2598}