MDL-57370 message: remove joins from get_conversations
[moodle.git] / message / tests / api_test.php
CommitLineData
79f6c36c
MN
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 * Test message API.
19 *
20 * @package core_message
21 * @category test
22 * @copyright 2016 Mark Nelson <markn@moodle.com>
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 . '/message/tests/messagelib_test.php');
31
32/**
33 * Test message API.
34 *
35 * @package core_message
36 * @category test
37 * @copyright 2016 Mark Nelson <markn@moodle.com>
38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39 */
40class core_message_api_testcase extends core_message_messagelib_testcase {
41
1f64514d 42 public function test_mark_all_read_for_user_touser() {
79f6c36c
MN
43 $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
44 $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
45
7d69958e
RW
46 $this->send_fake_message($sender, $recipient, 'Notification', 1);
47 $this->send_fake_message($sender, $recipient, 'Notification', 1);
48 $this->send_fake_message($sender, $recipient, 'Notification', 1);
79f6c36c
MN
49 $this->send_fake_message($sender, $recipient);
50 $this->send_fake_message($sender, $recipient);
51 $this->send_fake_message($sender, $recipient);
52
53 \core_message\api::mark_all_read_for_user($recipient->id);
54 $this->assertEquals(message_count_unread_messages($recipient), 0);
55 }
56
1f64514d 57 public function test_mark_all_read_for_user_touser_with_fromuser() {
79f6c36c
MN
58 $sender1 = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
59 $sender2 = $this->getDataGenerator()->create_user(array('firstname' => 'Test3', 'lastname' => 'User3'));
60 $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
61
7d69958e
RW
62 $this->send_fake_message($sender1, $recipient, 'Notification', 1);
63 $this->send_fake_message($sender1, $recipient, 'Notification', 1);
64 $this->send_fake_message($sender1, $recipient, 'Notification', 1);
79f6c36c
MN
65 $this->send_fake_message($sender1, $recipient);
66 $this->send_fake_message($sender1, $recipient);
67 $this->send_fake_message($sender1, $recipient);
7d69958e
RW
68 $this->send_fake_message($sender2, $recipient, 'Notification', 1);
69 $this->send_fake_message($sender2, $recipient, 'Notification', 1);
70 $this->send_fake_message($sender2, $recipient, 'Notification', 1);
79f6c36c
MN
71 $this->send_fake_message($sender2, $recipient);
72 $this->send_fake_message($sender2, $recipient);
73 $this->send_fake_message($sender2, $recipient);
74
75 \core_message\api::mark_all_read_for_user($recipient->id, $sender1->id);
76 $this->assertEquals(message_count_unread_messages($recipient), 6);
77 }
78
1f64514d 79 public function test_mark_all_read_for_user_touser_with_type() {
79f6c36c
MN
80 $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
81 $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
82
7d69958e
RW
83 $this->send_fake_message($sender, $recipient, 'Notification', 1);
84 $this->send_fake_message($sender, $recipient, 'Notification', 1);
85 $this->send_fake_message($sender, $recipient, 'Notification', 1);
79f6c36c
MN
86 $this->send_fake_message($sender, $recipient);
87 $this->send_fake_message($sender, $recipient);
88 $this->send_fake_message($sender, $recipient);
89
90 \core_message\api::mark_all_read_for_user($recipient->id, 0, MESSAGE_TYPE_NOTIFICATION);
91 $this->assertEquals(message_count_unread_messages($recipient), 3);
92
93 \core_message\api::mark_all_read_for_user($recipient->id, 0, MESSAGE_TYPE_MESSAGE);
94 $this->assertEquals(message_count_unread_messages($recipient), 0);
95 }
96
5b0769db
MN
97 /**
98 * Test count_blocked_users.
5b0769db 99 */
1f64514d 100 public function test_count_blocked_users() {
5b0769db
MN
101 // Set this user as the admin.
102 $this->setAdminUser();
103
104 // Create users to add to the admin's contact list.
105 $user1 = $this->getDataGenerator()->create_user();
106 $user2 = $this->getDataGenerator()->create_user();
107
108 $this->assertEquals(0, \core_message\api::count_blocked_users());
109
110 // Add 1 blocked and 1 normal contact to admin's contact list.
111 message_add_contact($user1->id);
112 message_add_contact($user2->id, 1);
113
114 $this->assertEquals(0, \core_message\api::count_blocked_users($user2));
115 $this->assertEquals(1, \core_message\api::count_blocked_users());
116 }
1f64514d
MN
117
118 /**
119 * Tests searching users in a course.
120 */
121 public function test_search_users_in_course() {
122 // Create some users.
123 $user1 = new stdClass();
124 $user1->firstname = 'User';
125 $user1->lastname = 'One';
126 $user1 = self::getDataGenerator()->create_user($user1);
127
128 // The person doing the search.
129 $this->setUser($user1);
130
131 // Second user is going to have their last access set to now, so they are online.
132 $user2 = new stdClass();
133 $user2->firstname = 'User';
134 $user2->lastname = 'Two';
135 $user2->lastaccess = time();
136 $user2 = self::getDataGenerator()->create_user($user2);
137
138 // Block the second user.
139 message_block_contact($user2->id, $user1->id);
140
141 $user3 = new stdClass();
142 $user3->firstname = 'User';
143 $user3->lastname = 'Three';
144 $user3 = self::getDataGenerator()->create_user($user3);
145
146 // Create a course.
147 $course1 = new stdClass();
148 $course1->fullname = 'Course';
149 $course1->shortname = 'One';
150 $course1 = $this->getDataGenerator()->create_course($course1);
151
152 // Enrol the searcher and one user in the course.
153 $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
154 $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
155
156 // Perform a search.
157 $results = \core_message\api::search_users_in_course($user1->id, $course1->id, 'User');
158
159 $this->assertEquals(1, count($results));
160
161 $user = $results[0];
162 $this->assertEquals($user2->id, $user->userid);
163 $this->assertEquals(fullname($user2), $user->fullname);
164 $this->assertFalse($user->ismessaging);
165 $this->assertNull($user->lastmessage);
166 $this->assertNull($user->messageid);
cb805753 167 $this->assertNull($user->isonline);
1f64514d
MN
168 $this->assertFalse($user->isread);
169 $this->assertTrue($user->isblocked);
170 $this->assertNull($user->unreadcount);
171 }
172
173 /**
174 * Tests searching users.
175 */
176 public function test_search_users() {
09a05b79
AN
177 global $DB;
178
1f64514d
MN
179 // Create some users.
180 $user1 = new stdClass();
181 $user1->firstname = 'User';
182 $user1->lastname = 'One';
183 $user1 = self::getDataGenerator()->create_user($user1);
184
185 // Set as the user performing the search.
186 $this->setUser($user1);
187
188 $user2 = new stdClass();
189 $user2->firstname = 'User search';
190 $user2->lastname = 'Two';
191 $user2 = self::getDataGenerator()->create_user($user2);
192
193 $user3 = new stdClass();
194 $user3->firstname = 'User search';
195 $user3->lastname = 'Three';
196 $user3 = self::getDataGenerator()->create_user($user3);
197
198 $user4 = new stdClass();
199 $user4->firstname = 'User';
200 $user4->lastname = 'Four';
201 $user4 = self::getDataGenerator()->create_user($user4);
202
203 $user5 = new stdClass();
204 $user5->firstname = 'User search';
205 $user5->lastname = 'Five';
206 $user5 = self::getDataGenerator()->create_user($user5);
207
208 $user6 = new stdClass();
209 $user6->firstname = 'User';
210 $user6->lastname = 'Six';
211 $user6 = self::getDataGenerator()->create_user($user6);
212
213 // Create some courses.
214 $course1 = new stdClass();
215 $course1->fullname = 'Course search';
216 $course1->shortname = 'One';
217 $course1 = $this->getDataGenerator()->create_course($course1);
218
219 $course2 = new stdClass();
220 $course2->fullname = 'Course';
221 $course2->shortname = 'Two';
222 $course2 = $this->getDataGenerator()->create_course($course2);
223
224 $course3 = new stdClass();
225 $course3->fullname = 'Course';
226 $course3->shortname = 'Three search';
227 $course3 = $this->getDataGenerator()->create_course($course3);
228
87d4ab65
AG
229 $course4 = new stdClass();
230 $course4->fullname = 'Course Four';
231 $course4->shortname = 'CF100';
232 $course4 = $this->getDataGenerator()->create_course($course4);
233
09a05b79
AN
234 $course5 = new stdClass();
235 $course5->fullname = 'Course';
236 $course5->shortname = 'Five search';
237 $course5 = $this->getDataGenerator()->create_course($course5);
238
239 $role = $DB->get_record('role', ['shortname' => 'student']);
240 $this->getDataGenerator()->enrol_user($user1->id, $course1->id, $role->id);
241 $this->getDataGenerator()->enrol_user($user1->id, $course2->id, $role->id);
242 $this->getDataGenerator()->enrol_user($user1->id, $course3->id, $role->id);
243 $this->getDataGenerator()->enrol_user($user1->id, $course5->id, $role->id);
87d4ab65 244
1f64514d
MN
245 // Add some users as contacts.
246 message_add_contact($user2->id, 0, $user1->id);
247 message_add_contact($user3->id, 0, $user1->id);
248 message_add_contact($user4->id, 0, $user1->id);
249
09a05b79
AN
250 // Remove the viewparticipants capability from one of the courses.
251 $course5context = context_course::instance($course5->id);
252 assign_capability('moodle/course:viewparticipants', CAP_PROHIBIT, $role->id, $course5context->id);
253 $course5context->mark_dirty();
254
1f64514d
MN
255 // Perform a search.
256 list($contacts, $courses, $noncontacts) = \core_message\api::search_users($user1->id, 'search');
257
258 // Check that we retrieved the correct contacts.
259 $this->assertEquals(2, count($contacts));
260 $this->assertEquals($user3->id, $contacts[0]->userid);
261 $this->assertEquals($user2->id, $contacts[1]->userid);
262
263 // Check that we retrieved the correct courses.
264 $this->assertEquals(2, count($courses));
265 $this->assertEquals($course3->id, $courses[0]->id);
266 $this->assertEquals($course1->id, $courses[1]->id);
267
268 // Check that we retrieved the correct non-contacts.
269 $this->assertEquals(1, count($noncontacts));
270 $this->assertEquals($user5->id, $noncontacts[0]->userid);
271 }
272
273 /**
274 * Tests searching messages.
275 */
276 public function test_search_messages() {
277 // Create some users.
278 $user1 = self::getDataGenerator()->create_user();
279 $user2 = self::getDataGenerator()->create_user();
280
281 // The person doing the search.
282 $this->setUser($user1);
283
284 // Send some messages back and forth.
285 $time = 1;
286 $this->send_fake_message($user1, $user2, 'Yo!', 0, $time);
287 $this->send_fake_message($user2, $user1, 'Sup mang?', 0, $time + 1);
288 $this->send_fake_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 2);
289 $this->send_fake_message($user2, $user1, 'Word.', 0, $time + 3);
290
291 // Perform a search.
292 $messages = \core_message\api::search_messages($user1->id, 'o');
293
294 // Confirm the data is correct.
295 $this->assertEquals(2, count($messages));
296
297 $message1 = $messages[0];
298 $message2 = $messages[1];
299
300 $this->assertEquals($user2->id, $message1->userid);
301 $this->assertEquals($user2->id, $message1->useridfrom);
302 $this->assertEquals(fullname($user2), $message1->fullname);
303 $this->assertTrue($message1->ismessaging);
304 $this->assertEquals('Word.', $message1->lastmessage);
305 $this->assertNotEmpty($message1->messageid);
cb805753 306 $this->assertNull($message1->isonline);
1f64514d
MN
307 $this->assertFalse($message1->isread);
308 $this->assertFalse($message1->isblocked);
309 $this->assertNull($message1->unreadcount);
310
311 $this->assertEquals($user2->id, $message2->userid);
312 $this->assertEquals($user1->id, $message2->useridfrom);
313 $this->assertEquals(fullname($user2), $message2->fullname);
314 $this->assertTrue($message2->ismessaging);
315 $this->assertEquals('Yo!', $message2->lastmessage);
316 $this->assertNotEmpty($message2->messageid);
cb805753 317 $this->assertNull($message2->isonline);
1f64514d
MN
318 $this->assertTrue($message2->isread);
319 $this->assertFalse($message2->isblocked);
320 $this->assertNull($message2->unreadcount);
321 }
322
323 /**
324 * Tests retrieving conversations.
325 */
326 public function test_get_conversations() {
327 // Create some users.
328 $user1 = self::getDataGenerator()->create_user();
329 $user2 = self::getDataGenerator()->create_user();
330 $user3 = self::getDataGenerator()->create_user();
331 $user4 = self::getDataGenerator()->create_user();
332
333 // The person doing the search.
334 $this->setUser($user1);
335
336 // Send some messages back and forth, have some different conversations with different users.
337 $time = 1;
338 $this->send_fake_message($user1, $user2, 'Yo!', 0, $time + 1);
339 $this->send_fake_message($user2, $user1, 'Sup mang?', 0, $time + 2);
340 $this->send_fake_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 3);
341 $this->send_fake_message($user2, $user1, 'Word.', 0, $time + 4);
342
343 $this->send_fake_message($user1, $user3, 'Booyah', 0, $time + 5);
344 $this->send_fake_message($user3, $user1, 'Whaaat?', 0, $time + 6);
345 $this->send_fake_message($user1, $user3, 'Nothing.', 0, $time + 7);
346 $this->send_fake_message($user3, $user1, 'Cool.', 0, $time + 8);
347
348 $this->send_fake_message($user1, $user4, 'Hey mate, you see the new messaging UI in Moodle?', 0, $time + 9);
349 $this->send_fake_message($user4, $user1, 'Yah brah, it\'s pretty rad.', 0, $time + 10);
350 $this->send_fake_message($user1, $user4, 'Dope.', 0, $time + 11);
351
352 // Retrieve the conversations.
353 $conversations = \core_message\api::get_conversations($user1->id);
354
355 // Confirm the data is correct.
356 $this->assertEquals(3, count($conversations));
357
358 $message1 = array_shift($conversations);
359 $message2 = array_shift($conversations);
360 $message3 = array_shift($conversations);
361
362 $this->assertEquals($user4->id, $message1->userid);
363 $this->assertEquals($user1->id, $message1->useridfrom);
364 $this->assertTrue($message1->ismessaging);
365 $this->assertEquals('Dope.', $message1->lastmessage);
366 $this->assertNull($message1->messageid);
cb805753 367 $this->assertNull($message1->isonline);
1f64514d
MN
368 $this->assertTrue($message1->isread);
369 $this->assertFalse($message1->isblocked);
370 $this->assertEquals(0, $message1->unreadcount);
371
372 $this->assertEquals($user3->id, $message2->userid);
373 $this->assertEquals($user3->id, $message2->useridfrom);
374 $this->assertTrue($message2->ismessaging);
375 $this->assertEquals('Cool.', $message2->lastmessage);
376 $this->assertNull($message2->messageid);
cb805753 377 $this->assertNull($message2->isonline);
1f64514d
MN
378 $this->assertFalse($message2->isread);
379 $this->assertFalse($message2->isblocked);
380 $this->assertEquals(2, $message2->unreadcount);
381
382 $this->assertEquals($user2->id, $message3->userid);
383 $this->assertEquals($user2->id, $message3->useridfrom);
384 $this->assertTrue($message3->ismessaging);
385 $this->assertEquals('Word.', $message3->lastmessage);
386 $this->assertNull($message3->messageid);
cb805753 387 $this->assertNull($message3->isonline);
1f64514d
MN
388 $this->assertFalse($message3->isread);
389 $this->assertFalse($message3->isblocked);
390 $this->assertEquals(2, $message3->unreadcount);
391 }
392
393 /**
394 * Tests retrieving contacts.
395 */
396 public function test_get_contacts() {
397 // Create some users.
398 $user1 = self::getDataGenerator()->create_user();
399
400 // Set as the user.
401 $this->setUser($user1);
402
403 $user2 = new stdClass();
404 $user2->firstname = 'User';
405 $user2->lastname = 'A';
406 $user2 = self::getDataGenerator()->create_user($user2);
407
408 $user3 = new stdClass();
409 $user3->firstname = 'User';
410 $user3->lastname = 'B';
411 $user3 = self::getDataGenerator()->create_user($user3);
412
413 $user4 = new stdClass();
414 $user4->firstname = 'User';
415 $user4->lastname = 'C';
416 $user4 = self::getDataGenerator()->create_user($user4);
417
418 $user5 = new stdClass();
419 $user5->firstname = 'User';
420 $user5->lastname = 'D';
421 $user5 = self::getDataGenerator()->create_user($user5);
422
423 // Add some users as contacts.
424 message_add_contact($user2->id, 0, $user1->id);
425 message_add_contact($user3->id, 0, $user1->id);
426 message_add_contact($user4->id, 0, $user1->id);
427
428 // Retrieve the contacts.
429 $contacts = \core_message\api::get_contacts($user1->id);
430
431 // Confirm the data is correct.
432 $this->assertEquals(3, count($contacts));
433
434 $contact1 = $contacts[0];
435 $contact2 = $contacts[1];
436 $contact3 = $contacts[2];
437
438 $this->assertEquals($user2->id, $contact1->userid);
439 $this->assertEmpty($contact1->useridfrom);
440 $this->assertFalse($contact1->ismessaging);
441 $this->assertNull($contact1->lastmessage);
442 $this->assertNull($contact1->messageid);
cb805753 443 $this->assertNull($contact1->isonline);
1f64514d
MN
444 $this->assertFalse($contact1->isread);
445 $this->assertFalse($contact1->isblocked);
446 $this->assertNull($contact1->unreadcount);
447
448 $this->assertEquals($user3->id, $contact2->userid);
449 $this->assertEmpty($contact2->useridfrom);
450 $this->assertFalse($contact2->ismessaging);
451 $this->assertNull($contact2->lastmessage);
452 $this->assertNull($contact2->messageid);
cb805753 453 $this->assertNull($contact2->isonline);
1f64514d
MN
454 $this->assertFalse($contact2->isread);
455 $this->assertFalse($contact2->isblocked);
456 $this->assertNull($contact2->unreadcount);
457
458 $this->assertEquals($user4->id, $contact3->userid);
459 $this->assertEmpty($contact3->useridfrom);
460 $this->assertFalse($contact3->ismessaging);
461 $this->assertNull($contact3->lastmessage);
462 $this->assertNull($contact3->messageid);
cb805753 463 $this->assertNull($contact3->isonline);
1f64514d
MN
464 $this->assertFalse($contact3->isread);
465 $this->assertFalse($contact3->isblocked);
466 $this->assertNull($contact3->unreadcount);
467 }
468
469 /**
470 * Tests retrieving messages.
471 */
472 public function test_get_messages() {
473 // Create some users.
474 $user1 = self::getDataGenerator()->create_user();
475 $user2 = self::getDataGenerator()->create_user();
476
477 // The person doing the search.
478 $this->setUser($user1);
479
480 // Send some messages back and forth.
481 $time = 1;
482 $this->send_fake_message($user1, $user2, 'Yo!', 0, $time + 1);
483 $this->send_fake_message($user2, $user1, 'Sup mang?', 0, $time + 2);
484 $this->send_fake_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 3);
485 $this->send_fake_message($user2, $user1, 'Word.', 0, $time + 4);
486
487 // Retrieve the messages.
488 $messages = \core_message\api::get_messages($user1->id, $user2->id);
489
490 // Confirm the message data is correct.
491 $this->assertEquals(4, count($messages));
492
493 $message1 = $messages[0];
494 $message2 = $messages[1];
495 $message3 = $messages[2];
496 $message4 = $messages[3];
497
498 $this->assertEquals($user1->id, $message1->useridfrom);
499 $this->assertEquals($user2->id, $message1->useridto);
500 $this->assertTrue($message1->displayblocktime);
501 $this->assertContains('Yo!', $message1->text);
502
503 $this->assertEquals($user2->id, $message2->useridfrom);
504 $this->assertEquals($user1->id, $message2->useridto);
505 $this->assertFalse($message2->displayblocktime);
506 $this->assertContains('Sup mang?', $message2->text);
507
508 $this->assertEquals($user1->id, $message3->useridfrom);
509 $this->assertEquals($user2->id, $message3->useridto);
510 $this->assertFalse($message3->displayblocktime);
511 $this->assertContains('Writing PHPUnit tests!', $message3->text);
512
513 $this->assertEquals($user2->id, $message4->useridfrom);
514 $this->assertEquals($user1->id, $message4->useridto);
515 $this->assertFalse($message4->displayblocktime);
516 $this->assertContains('Word.', $message4->text);
517 }
518
519 /**
520 * Tests retrieving most recent message.
521 */
522 public function test_get_most_recent_message() {
523 // Create some users.
524 $user1 = self::getDataGenerator()->create_user();
525 $user2 = self::getDataGenerator()->create_user();
526
527 // The person doing the search.
528 $this->setUser($user1);
529
530 // Send some messages back and forth.
531 $time = 1;
532 $this->send_fake_message($user1, $user2, 'Yo!', 0, $time + 1);
533 $this->send_fake_message($user2, $user1, 'Sup mang?', 0, $time + 2);
534 $this->send_fake_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 3);
535 $this->send_fake_message($user2, $user1, 'Word.', 0, $time + 4);
536
537 // Retrieve the most recent messages.
538 $message = \core_message\api::get_most_recent_message($user1->id, $user2->id);
539
540 // Check the results are correct.
541 $this->assertEquals($user2->id, $message->useridfrom);
542 $this->assertEquals($user1->id, $message->useridto);
543 $this->assertContains('Word.', $message->text);
544 }
545
546 /**
547 * Tests retrieving a user's profile.
548 */
549 public function test_get_profile() {
550 // Create some users.
551 $user1 = self::getDataGenerator()->create_user();
552
553 $user2 = new stdClass();
554 $user2->country = 'AU';
555 $user2->city = 'Perth';
556 $user2 = self::getDataGenerator()->create_user($user2);
557
558 // The person doing the search.
559 $this->setUser($user1);
560
561 // Get the profile.
562 $profile = \core_message\api::get_profile($user1->id, $user2->id);
563
564 $this->assertEquals($user2->id, $profile->userid);
565 $this->assertEmpty($profile->email);
566 $this->assertEmpty($profile->country);
567 $this->assertEmpty($profile->city);
568 $this->assertEquals(fullname($user2), $profile->fullname);
cb805753 569 $this->assertNull($profile->isonline);
1f64514d
MN
570 $this->assertFalse($profile->isblocked);
571 $this->assertFalse($profile->iscontact);
572 }
573
574 /**
575 * Tests retrieving a user's profile.
576 */
577 public function test_get_profile_as_admin() {
578 // The person doing the search.
579 $this->setAdminUser();
580
581 // Create some users.
582 $user1 = self::getDataGenerator()->create_user();
583
584 $user2 = new stdClass();
585 $user2->country = 'AU';
586 $user2->city = 'Perth';
587 $user2 = self::getDataGenerator()->create_user($user2);
588
589 // Get the profile.
590 $profile = \core_message\api::get_profile($user1->id, $user2->id);
591
592 $this->assertEquals($user2->id, $profile->userid);
593 $this->assertEquals($user2->email, $profile->email);
594 $this->assertEquals($user2->country, $profile->country);
595 $this->assertEquals($user2->city, $profile->city);
596 $this->assertEquals(fullname($user2), $profile->fullname);
597 $this->assertFalse($profile->isonline);
598 $this->assertFalse($profile->isblocked);
599 $this->assertFalse($profile->iscontact);
600 }
601
602 /**
603 * Tests checking if a user can delete a conversation.
604 */
605 public function test_can_delete_conversation() {
606 // Set as the admin.
607 $this->setAdminUser();
608
609 // Create some users.
610 $user1 = self::getDataGenerator()->create_user();
611 $user2 = self::getDataGenerator()->create_user();
612
613 // The admin can do anything.
614 $this->assertTrue(\core_message\api::can_delete_conversation($user1->id));
615
616 // Set as the user 1.
617 $this->setUser($user1);
618
619 // They can delete their own messages.
620 $this->assertTrue(\core_message\api::can_delete_conversation($user1->id));
621
622 // They can't delete someone elses.
623 $this->assertFalse(\core_message\api::can_delete_conversation($user2->id));
624 }
625
626 /**
627 * Tests deleting a conversation.
628 */
629 public function test_delete_conversation() {
630 global $DB;
631
632 // Create some users.
633 $user1 = self::getDataGenerator()->create_user();
634 $user2 = self::getDataGenerator()->create_user();
635
636 // The person doing the search.
637 $this->setUser($user1);
638
639 // Send some messages back and forth.
640 $time = 1;
641 $this->send_fake_message($user1, $user2, 'Yo!', 0, $time + 1);
642 $this->send_fake_message($user2, $user1, 'Sup mang?', 0, $time + 2);
643 $this->send_fake_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 3);
644 $this->send_fake_message($user2, $user1, 'Word.', 0, $time + 4);
645
646 // Delete the conversation as user 1.
647 \core_message\api::delete_conversation($user1->id, $user2->id);
648
649 $messages = $DB->get_records('message', array(), 'timecreated ASC');
650 $this->assertCount(4, $messages);
651
652 $message1 = array_shift($messages);
653 $message2 = array_shift($messages);
654 $message3 = array_shift($messages);
655 $message4 = array_shift($messages);
656
657 $this->assertNotEmpty($message1->timeuserfromdeleted);
658 $this->assertEmpty($message1->timeusertodeleted);
659
660 $this->assertEmpty($message2->timeuserfromdeleted);
661 $this->assertNotEmpty($message2->timeusertodeleted);
662
663 $this->assertNotEmpty($message3->timeuserfromdeleted);
664 $this->assertEmpty($message3->timeusertodeleted);
665
666 $this->assertEmpty($message4->timeuserfromdeleted);
667 $this->assertNotEmpty($message4->timeusertodeleted);
668
669 }
670
671 /**
672 * Tests counting unread conversations.
673 */
674 public function test_count_unread_conversations() {
675 $this->resetAfterTest(true);
676
677 // Create some users.
678 $user1 = self::getDataGenerator()->create_user();
679 $user2 = self::getDataGenerator()->create_user();
680 $user3 = self::getDataGenerator()->create_user();
681 $user4 = self::getDataGenerator()->create_user();
682
683 // The person wanting the conversation count.
684 $this->setUser($user1);
685
686 // Send some messages back and forth, have some different conversations with different users.
687 $this->send_fake_message($user1, $user2, 'Yo!');
688 $this->send_fake_message($user2, $user1, 'Sup mang?');
689 $this->send_fake_message($user1, $user2, 'Writing PHPUnit tests!');
690 $this->send_fake_message($user2, $user1, 'Word.');
691
692 $this->send_fake_message($user1, $user3, 'Booyah');
693 $this->send_fake_message($user3, $user1, 'Whaaat?');
694 $this->send_fake_message($user1, $user3, 'Nothing.');
695 $this->send_fake_message($user3, $user1, 'Cool.');
696
697 $this->send_fake_message($user1, $user4, 'Hey mate, you see the new messaging UI in Moodle?');
698 $this->send_fake_message($user4, $user1, 'Yah brah, it\'s pretty rad.');
699 $this->send_fake_message($user1, $user4, 'Dope.');
700
701 // Check the amount for the current user.
702 $this->assertEquals(3, core_message\api::count_unread_conversations());
703
704 // Check the amount for the second user.
705 $this->assertEquals(1, core_message\api::count_unread_conversations($user2));
706 }
707
708 /**
709 * Tests deleting a conversation.
710 */
711 public function test_get_all_message_preferences() {
712 $user = self::getDataGenerator()->create_user();
713 $this->setUser($user);
714
715 // Set a couple of preferences to test.
716 set_user_preference('message_provider_mod_assign_assign_notification_loggedin', 'popup', $user);
717 set_user_preference('message_provider_mod_assign_assign_notification_loggedoff', 'email', $user);
718
719 $processors = get_message_processors();
720 $providers = message_get_providers_for_user($user->id);
721 $prefs = \core_message\api::get_all_message_preferences($processors, $providers, $user);
722
723 $this->assertEquals(1, $prefs->mod_assign_assign_notification_loggedin['popup']);
724 $this->assertEquals(1, $prefs->mod_assign_assign_notification_loggedoff['email']);
725 }
726
727 /**
728 * Tests the user can post a message.
729 */
730 public function test_can_post_message() {
731 // Create some users.
732 $user1 = self::getDataGenerator()->create_user();
733 $user2 = self::getDataGenerator()->create_user();
734
735 // Set as the user 1.
736 $this->setUser($user1);
737
738 // They can post to someone else.
739 $this->assertTrue(\core_message\api::can_post_message($user2));
740 }
741
742 /**
743 * Tests the user can't post a message without proper capability.
744 */
745 public function test_can_post_message_without_cap() {
746 global $DB;
747
748 // Create some users.
749 $user1 = self::getDataGenerator()->create_user();
750 $user2 = self::getDataGenerator()->create_user();
751
752 // Set as the user 1.
753 $this->setUser($user1);
754
755 // Remove the capability to send a message.
756 $roleids = $DB->get_records_menu('role', null, '', 'shortname, id');
757 unassign_capability('moodle/site:sendmessage', $roleids['user'],
758 context_system::instance());
759
760 // Check that we can not post a message without the capability.
761 $this->assertFalse(\core_message\api::can_post_message($user2));
762 }
763
764 /**
765 * Tests the user can't post a message if they are not a contact and the user
766 * has requested messages only from contacts.
767 */
768 public function test_can_post_message_when_not_contact() {
769 // Create some users.
770 $user1 = self::getDataGenerator()->create_user();
771 $user2 = self::getDataGenerator()->create_user();
772
773 // Set as the first user.
774 $this->setUser($user1);
775
776 // Set the second user's preference to not receive messages from non-contacts.
777 set_user_preference('message_blocknoncontacts', 1, $user2->id);
778
779 // Check that we can not send user 2 a message.
780 $this->assertFalse(\core_message\api::can_post_message($user2));
781 }
782
783 /**
784 * Tests the user can't post a message if they are blocked.
785 */
786 public function test_can_post_message_when_blocked() {
787 // Create some users.
788 $user1 = self::getDataGenerator()->create_user();
789 $user2 = self::getDataGenerator()->create_user();
790
791 // Set the user.
792 $this->setUser($user1);
793
794 // Block the second user.
795 message_block_contact($user2->id);
796
797 // Check that the second user can no longer send the first user a message.
798 $this->assertFalse(\core_message\api::can_post_message($user1, $user2));
799 }
800
801 /**
802 * Tests that when blocking messages from non-contacts is enabled that
803 * non-contacts trying to send a message return false.
804 */
805 public function test_is_user_non_contact_blocked() {
806 // Create some users.
807 $user1 = self::getDataGenerator()->create_user();
808 $user2 = self::getDataGenerator()->create_user();
809
810 // Set as the first user.
811 $this->setUser($user1);
812
813 // User hasn't sent their preference to block non-contacts, so should return false.
814 $this->assertFalse(\core_message\api::is_user_non_contact_blocked($user2));
815
816 // Set the second user's preference to not receive messages from non-contacts.
817 set_user_preference('message_blocknoncontacts', 1, $user2->id);
818
819 // Check that the return result is now true.
820 $this->assertTrue(\core_message\api::is_user_non_contact_blocked($user2));
821
822 // Add the first user as a contact for the second user.
823 message_add_contact($user1->id, 0, $user2->id);
824
825 // Check that the return result is now false.
826 $this->assertFalse(\core_message\api::is_user_non_contact_blocked($user2));
827 }
828
829 /**
830 * Tests that we return true when a user is blocked, or false
831 * if they are not blocked.
832 */
833 public function test_is_user_blocked() {
834 // Create some users.
835 $user1 = self::getDataGenerator()->create_user();
836 $user2 = self::getDataGenerator()->create_user();
837
838 // Set the user.
839 $this->setUser($user1);
840
841 // User shouldn't be blocked.
26dca05d 842 $this->assertFalse(\core_message\api::is_user_blocked($user1->id, $user2->id));
1f64514d
MN
843
844 // Block the user.
845 message_block_contact($user2->id);
846
847 // User should be blocked.
26dca05d 848 $this->assertTrue(\core_message\api::is_user_blocked($user1->id, $user2->id));
727145bf
AN
849
850 // Unblock the user.
851 message_unblock_contact($user2->id);
852 $this->assertFalse(\core_message\api::is_user_blocked($user1->id, $user2->id));
1f64514d
MN
853 }
854
855 /**
856 * Tests that the admin is not blocked even if someone has chosen to block them.
857 */
858 public function test_is_user_blocked_as_admin() {
859 // Create a user.
860 $user1 = self::getDataGenerator()->create_user();
861
862 // Set the user.
863 $this->setUser($user1);
864
865 // Block the admin user.
866 message_block_contact(2);
867
868 // Now change to the admin user.
869 $this->setAdminUser();
870
871 // As the admin you should still be able to send messages to the user.
26dca05d 872 $this->assertFalse(\core_message\api::is_user_blocked($user1->id));
1f64514d 873 }
20ab51fd
AA
874
875 /*
876 * Tes get_message_processor api.
877 */
878 public function test_get_message_processor() {
55b2a33c 879 $processors = get_message_processors(true);
20ab51fd
AA
880 if (empty($processors)) {
881 $this->markTestSkipped("No message processors found");
882 }
883
884 list($name, $processor) = each($processors);
885 $testprocessor = \core_message\api::get_message_processor($name);
886 $this->assertEquals($processor->name, $testprocessor->name);
887 $this->assertEquals($processor->enabled, $testprocessor->enabled);
888 $this->assertEquals($processor->available, $testprocessor->available);
889 $this->assertEquals($processor->configured, $testprocessor->configured);
890
891 // Disable processor and test.
892 \core_message\api::update_processor_status($testprocessor, 0);
893 $testprocessor = \core_message\api::get_message_processor($name, true);
894 $this->assertEmpty($testprocessor);
895 $testprocessor = \core_message\api::get_message_processor($name);
896 $this->assertEquals($processor->name, $testprocessor->name);
897 $this->assertEquals(0, $testprocessor->enabled);
898
899 // Enable again and test.
900 \core_message\api::update_processor_status($testprocessor, 1);
901 $testprocessor = \core_message\api::get_message_processor($name, true);
902 $this->assertEquals($processor->name, $testprocessor->name);
903 $this->assertEquals(1, $testprocessor->enabled);
904 $testprocessor = \core_message\api::get_message_processor($name);
905 $this->assertEquals($processor->name, $testprocessor->name);
906 $this->assertEquals(1, $testprocessor->enabled);
907 }
908
909 /**
910 * Test method update_processor_status.
911 */
912 public function test_update_processor_status() {
913 $processors = get_message_processors();
914 if (empty($processors)) {
915 $this->markTestSkipped("No message processors found");
916 }
917 list($name, $testprocessor) = each($processors);
918
919 // Enable.
920 \core_message\api::update_processor_status($testprocessor, 1);
921 $testprocessor = \core_message\api::get_message_processor($name);
922 $this->assertEquals(1, $testprocessor->enabled);
923
924 // Disable.
925 \core_message\api::update_processor_status($testprocessor, 0);
926 $testprocessor = \core_message\api::get_message_processor($name);
927 $this->assertEquals(0, $testprocessor->enabled);
928
929 // Enable again.
930 \core_message\api::update_processor_status($testprocessor, 1);
931 $testprocessor = \core_message\api::get_message_processor($name);
932 $this->assertEquals(1, $testprocessor->enabled);
933 }
934
935 /**
936 * Test method is_user_enabled.
937 */
938 public function is_user_enabled() {
939 $processors = get_message_processors();
940 if (empty($processors)) {
941 $this->markTestSkipped("No message processors found");
942 }
943 list($name, $testprocessor) = each($processors);
944
945 // Enable.
946 \core_message\api::update_processor_status($testprocessor, 1);
947 $status = \core_message\api::is_processor_enabled($name);
948 $this->assertEquals(1, $status);
949
950 // Disable.
951 \core_message\api::update_processor_status($testprocessor, 0);
952 $status = \core_message\api::is_processor_enabled($name);
953 $this->assertEquals(0, $status);
954
955 // Enable again.
956 \core_message\api::update_processor_status($testprocessor, 1);
957 $status = \core_message\api::is_processor_enabled($name);
958 $this->assertEquals(1, $status);
959 }
fb1469d8
RW
960
961 /**
962 * Test retrieving messages by providing a minimum timecreated value.
963 */
ffd7798c 964 public function test_get_messages_time_from_only() {
fb1469d8
RW
965 // Create some users.
966 $user1 = self::getDataGenerator()->create_user();
967 $user2 = self::getDataGenerator()->create_user();
968
969 // The person doing the search.
970 $this->setUser($user1);
971
972 // Send some messages back and forth.
973 $time = 1;
974 $this->send_fake_message($user1, $user2, 'Message 1', 0, $time + 1);
975 $this->send_fake_message($user2, $user1, 'Message 2', 0, $time + 2);
976 $this->send_fake_message($user1, $user2, 'Message 3', 0, $time + 3);
977 $this->send_fake_message($user2, $user1, 'Message 4', 0, $time + 4);
978
ffd7798c 979 // Retrieve the messages from $time, which should be all of them.
fb1469d8
RW
980 $messages = \core_message\api::get_messages($user1->id, $user2->id, 0, 0, 'timecreated ASC', $time);
981
982 // Confirm the message data is correct.
983 $this->assertEquals(4, count($messages));
984
985 $message1 = $messages[0];
986 $message2 = $messages[1];
987 $message3 = $messages[2];
988 $message4 = $messages[3];
989
990 $this->assertContains('Message 1', $message1->text);
991 $this->assertContains('Message 2', $message2->text);
992 $this->assertContains('Message 3', $message3->text);
993 $this->assertContains('Message 4', $message4->text);
994
ffd7798c 995 // Retrieve the messages from $time + 3, which should only be the 2 last messages.
fb1469d8
RW
996 $messages = \core_message\api::get_messages($user1->id, $user2->id, 0, 0, 'timecreated ASC', $time + 3);
997
998 // Confirm the message data is correct.
999 $this->assertEquals(2, count($messages));
1000
1001 $message1 = $messages[0];
1002 $message2 = $messages[1];
1003
1004 $this->assertContains('Message 3', $message1->text);
1005 $this->assertContains('Message 4', $message2->text);
1006 }
1007
1008 /**
1009 * Test retrieving messages by providing a maximum timecreated value.
1010 */
ffd7798c 1011 public function test_get_messages_time_to_only() {
fb1469d8
RW
1012 // Create some users.
1013 $user1 = self::getDataGenerator()->create_user();
1014 $user2 = self::getDataGenerator()->create_user();
1015
1016 // The person doing the search.
1017 $this->setUser($user1);
1018
1019 // Send some messages back and forth.
1020 $time = 1;
1021 $this->send_fake_message($user1, $user2, 'Message 1', 0, $time + 1);
1022 $this->send_fake_message($user2, $user1, 'Message 2', 0, $time + 2);
1023 $this->send_fake_message($user1, $user2, 'Message 3', 0, $time + 3);
1024 $this->send_fake_message($user2, $user1, 'Message 4', 0, $time + 4);
1025
ffd7798c 1026 // Retrieve the messages up until $time + 4, which should be all of them.
fb1469d8
RW
1027 $messages = \core_message\api::get_messages($user1->id, $user2->id, 0, 0, 'timecreated ASC', 0, $time + 4);
1028
1029 // Confirm the message data is correct.
1030 $this->assertEquals(4, count($messages));
1031
1032 $message1 = $messages[0];
1033 $message2 = $messages[1];
1034 $message3 = $messages[2];
1035 $message4 = $messages[3];
1036
1037 $this->assertContains('Message 1', $message1->text);
1038 $this->assertContains('Message 2', $message2->text);
1039 $this->assertContains('Message 3', $message3->text);
1040 $this->assertContains('Message 4', $message4->text);
1041
ffd7798c 1042 // Retrieve the messages up until $time + 2, which should be the first two.
fb1469d8
RW
1043 $messages = \core_message\api::get_messages($user1->id, $user2->id, 0, 0, 'timecreated ASC', 0, $time + 2);
1044
1045 // Confirm the message data is correct.
1046 $this->assertEquals(2, count($messages));
1047
1048 $message1 = $messages[0];
1049 $message2 = $messages[1];
1050
1051 $this->assertContains('Message 1', $message1->text);
1052 $this->assertContains('Message 2', $message2->text);
1053 }
1054
1055 /**
1056 * Test retrieving messages by providing a minimum and maximum timecreated value.
1057 */
ffd7798c 1058 public function test_get_messages_time_from_and_to() {
fb1469d8
RW
1059 // Create some users.
1060 $user1 = self::getDataGenerator()->create_user();
1061 $user2 = self::getDataGenerator()->create_user();
1062
1063 // The person doing the search.
1064 $this->setUser($user1);
1065
1066 // Send some messages back and forth.
1067 $time = 1;
1068 $this->send_fake_message($user1, $user2, 'Message 1', 0, $time + 1);
1069 $this->send_fake_message($user2, $user1, 'Message 2', 0, $time + 2);
1070 $this->send_fake_message($user1, $user2, 'Message 3', 0, $time + 3);
1071 $this->send_fake_message($user2, $user1, 'Message 4', 0, $time + 4);
1072
ffd7798c 1073 // Retrieve the messages from $time + 2 up until $time + 3, which should be 2nd and 3rd message.
fb1469d8
RW
1074 $messages = \core_message\api::get_messages($user1->id, $user2->id, 0, 0, 'timecreated ASC', $time + 2, $time + 3);
1075
1076 // Confirm the message data is correct.
1077 $this->assertEquals(2, count($messages));
1078
1079 $message1 = $messages[0];
1080 $message2 = $messages[1];
1081
1082 $this->assertContains('Message 2', $message1->text);
1083 $this->assertContains('Message 3', $message2->text);
1084 }
5cdebf2d 1085}