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