MDL-36941 core: alter events to use new table structure
[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);
29c3b0b4 76 $this->assertEquals(message_count_unread_messages($recipient), 3);
79f6c36c
MN
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
bb2924d0
DM
336 // No conversations yet.
337 $this->assertEquals([], \core_message\api::get_conversations($user1->id));
338
1f64514d
MN
339 // Send some messages back and forth, have some different conversations with different users.
340 $time = 1;
341 $this->send_fake_message($user1, $user2, 'Yo!', 0, $time + 1);
342 $this->send_fake_message($user2, $user1, 'Sup mang?', 0, $time + 2);
343 $this->send_fake_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 3);
4d146f1a 344 $messageid1 = $this->send_fake_message($user2, $user1, 'Word.', 0, $time + 4);
1f64514d
MN
345
346 $this->send_fake_message($user1, $user3, 'Booyah', 0, $time + 5);
347 $this->send_fake_message($user3, $user1, 'Whaaat?', 0, $time + 6);
348 $this->send_fake_message($user1, $user3, 'Nothing.', 0, $time + 7);
4d146f1a 349 $messageid2 = $this->send_fake_message($user3, $user1, 'Cool.', 0, $time + 8);
1f64514d
MN
350
351 $this->send_fake_message($user1, $user4, 'Hey mate, you see the new messaging UI in Moodle?', 0, $time + 9);
352 $this->send_fake_message($user4, $user1, 'Yah brah, it\'s pretty rad.', 0, $time + 10);
4d146f1a 353 $messageid3 = $this->send_fake_message($user1, $user4, 'Dope.', 0, $time + 11);
1f64514d
MN
354
355 // Retrieve the conversations.
356 $conversations = \core_message\api::get_conversations($user1->id);
357
358 // Confirm the data is correct.
359 $this->assertEquals(3, count($conversations));
360
361 $message1 = array_shift($conversations);
362 $message2 = array_shift($conversations);
363 $message3 = array_shift($conversations);
364
365 $this->assertEquals($user4->id, $message1->userid);
366 $this->assertEquals($user1->id, $message1->useridfrom);
367 $this->assertTrue($message1->ismessaging);
368 $this->assertEquals('Dope.', $message1->lastmessage);
4d146f1a 369 $this->assertEquals($messageid3, $message1->messageid);
cb805753 370 $this->assertNull($message1->isonline);
4d146f1a 371 $this->assertFalse($message1->isread);
1f64514d 372 $this->assertFalse($message1->isblocked);
4d146f1a 373 $this->assertEquals(1, $message1->unreadcount);
1f64514d
MN
374
375 $this->assertEquals($user3->id, $message2->userid);
376 $this->assertEquals($user3->id, $message2->useridfrom);
377 $this->assertTrue($message2->ismessaging);
378 $this->assertEquals('Cool.', $message2->lastmessage);
4d146f1a 379 $this->assertEquals($messageid2, $message2->messageid);
cb805753 380 $this->assertNull($message2->isonline);
1f64514d
MN
381 $this->assertFalse($message2->isread);
382 $this->assertFalse($message2->isblocked);
383 $this->assertEquals(2, $message2->unreadcount);
384
385 $this->assertEquals($user2->id, $message3->userid);
386 $this->assertEquals($user2->id, $message3->useridfrom);
387 $this->assertTrue($message3->ismessaging);
388 $this->assertEquals('Word.', $message3->lastmessage);
4d146f1a 389 $this->assertEquals($messageid1, $message3->messageid);
cb805753 390 $this->assertNull($message3->isonline);
1f64514d
MN
391 $this->assertFalse($message3->isread);
392 $this->assertFalse($message3->isblocked);
393 $this->assertEquals(2, $message3->unreadcount);
394 }
395
4d146f1a
RW
396 /**
397 * Tests retrieving conversations with a limit and offset to ensure pagination works correctly.
398 */
399 public function test_get_conversations_limit_offset() {
400 // Create some users.
401 $user1 = self::getDataGenerator()->create_user();
402 $user2 = self::getDataGenerator()->create_user();
403 $user3 = self::getDataGenerator()->create_user();
404 $user4 = self::getDataGenerator()->create_user();
405
406 // The person doing the search.
407 $this->setUser($user1);
408
409 // Send some messages back and forth, have some different conversations with different users.
410 $time = 1;
411 $this->send_fake_message($user1, $user2, 'Yo!', 0, $time + 1);
412 $this->send_fake_message($user2, $user1, 'Sup mang?', 0, $time + 2);
413 $this->send_fake_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 3);
414 $messageid1 = $this->send_fake_message($user2, $user1, 'Word.', 0, $time + 4);
415
416 $this->send_fake_message($user1, $user3, 'Booyah', 0, $time + 5);
417 $this->send_fake_message($user3, $user1, 'Whaaat?', 0, $time + 6);
418 $this->send_fake_message($user1, $user3, 'Nothing.', 0, $time + 7);
419 $messageid2 = $this->send_fake_message($user3, $user1, 'Cool.', 0, $time + 8);
420
421 $this->send_fake_message($user1, $user4, 'Hey mate, you see the new messaging UI in Moodle?', 0, $time + 9);
422 $this->send_fake_message($user4, $user1, 'Yah brah, it\'s pretty rad.', 0, $time + 10);
423 $messageid3 = $this->send_fake_message($user1, $user4, 'Dope.', 0, $time + 11);
424
425 // Retrieve the conversations.
426 $conversations = \core_message\api::get_conversations($user1->id, 1, 1);
427
428 // We should only have one conversation because of the limit.
429 $this->assertCount(1, $conversations);
430
431 $conversation = array_shift($conversations);
432
433 $this->assertEquals($user3->id, $conversation->userid);
434 $this->assertEquals($user3->id, $conversation->useridfrom);
435 $this->assertTrue($conversation->ismessaging);
436 $this->assertEquals('Cool.', $conversation->lastmessage);
437 $this->assertEquals($messageid2, $conversation->messageid);
438 $this->assertNull($conversation->isonline);
439 $this->assertFalse($conversation->isread);
440 $this->assertFalse($conversation->isblocked);
441 $this->assertEquals(2, $conversation->unreadcount);
442
443 // Retrieve the next conversation.
444 $conversations = \core_message\api::get_conversations($user1->id, 2, 1);
445
446 // We should only have one conversation because of the limit.
447 $this->assertCount(1, $conversations);
448
449 $conversation = array_shift($conversations);
450
451 $this->assertEquals($user2->id, $conversation->userid);
452 $this->assertEquals($user2->id, $conversation->useridfrom);
453 $this->assertTrue($conversation->ismessaging);
454 $this->assertEquals('Word.', $conversation->lastmessage);
455 $this->assertEquals($messageid1, $conversation->messageid);
456 $this->assertNull($conversation->isonline);
457 $this->assertFalse($conversation->isread);
458 $this->assertFalse($conversation->isblocked);
459 $this->assertEquals(2, $conversation->unreadcount);
460
461 // Ask for an offset that doesn't exist.
462 $conversations = \core_message\api::get_conversations($user1->id, 4, 1);
463
464 // We should not get any conversations back.
465 $this->assertCount(0, $conversations);
466 }
467
f512355b
MN
468 /**
469 * Tests retrieving conversations when a conversation contains a deleted user.
470 */
471 public function test_get_conversations_with_deleted_user() {
472 // Create some users.
473 $user1 = self::getDataGenerator()->create_user();
474 $user2 = self::getDataGenerator()->create_user();
475 $user3 = self::getDataGenerator()->create_user();
476
477 // Send some messages back and forth, have some different conversations with different users.
478 $time = 1;
479 $this->send_fake_message($user1, $user2, 'Yo!', 0, $time + 1);
480 $this->send_fake_message($user2, $user1, 'Sup mang?', 0, $time + 2);
481 $this->send_fake_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 3);
482 $this->send_fake_message($user2, $user1, 'Word.', 0, $time + 4);
483
484 $this->send_fake_message($user1, $user3, 'Booyah', 0, $time + 5);
485 $this->send_fake_message($user3, $user1, 'Whaaat?', 0, $time + 6);
486 $this->send_fake_message($user1, $user3, 'Nothing.', 0, $time + 7);
487 $this->send_fake_message($user3, $user1, 'Cool.', 0, $time + 8);
488
489 // Delete the second user.
490 delete_user($user2);
491
492 // Retrieve the conversations.
493 $conversations = \core_message\api::get_conversations($user1->id);
494
495 // We should only have one conversation because the other user was deleted.
496 $this->assertCount(1, $conversations);
497
498 // Confirm the conversation is from the non-deleted user.
499 $conversation = reset($conversations);
500 $this->assertEquals($user3->id, $conversation->userid);
501 }
502
e12d9f49
RW
503 /**
504 * The data provider for get_conversations_mixed.
505 *
506 * This provides sets of data to for testing.
507 * @return array
508 */
509 public function get_conversations_mixed_provider() {
510 return array(
4d146f1a
RW
511 'Test that conversations with messages contacts is correctly ordered.' => array(
512 'users' => array(
513 'user1',
514 'user2',
515 'user3',
516 ),
517 'contacts' => array(
518 ),
519 'messages' => array(
520 array(
521 'from' => 'user1',
522 'to' => 'user2',
523 'state' => 'unread',
524 'subject' => 'S1',
525 ),
526 array(
527 'from' => 'user2',
528 'to' => 'user1',
529 'state' => 'unread',
530 'subject' => 'S2',
531 ),
532 array(
533 'from' => 'user1',
534 'to' => 'user2',
535 'state' => 'unread',
536 'timecreated' => 0,
537 'subject' => 'S3',
538 ),
539 array(
540 'from' => 'user1',
541 'to' => 'user3',
542 'state' => 'read',
543 'timemodifier' => 1,
544 'subject' => 'S4',
545 ),
546 array(
547 'from' => 'user3',
548 'to' => 'user1',
549 'state' => 'read',
550 'timemodifier' => 1,
551 'subject' => 'S5',
552 ),
553 array(
554 'from' => 'user1',
555 'to' => 'user3',
556 'state' => 'read',
557 'timecreated' => 0,
558 'subject' => 'S6',
559 ),
560 ),
561 'expectations' => array(
562 'user1' => array(
563 // User1 has conversed most recently with user3. The most recent message is M5.
564 array(
565 'messageposition' => 0,
566 'with' => 'user3',
567 'subject' => 'S5',
e12d9f49 568 'unreadcount' => 0,
4d146f1a
RW
569 ),
570 // User1 has also conversed with user2. The most recent message is S2.
571 array(
572 'messageposition' => 1,
573 'with' => 'user2',
574 'subject' => 'S2',
e12d9f49 575 'unreadcount' => 1,
4d146f1a
RW
576 ),
577 ),
578 'user2' => array(
579 // User2 has only conversed with user1. Their most recent shared message was S2.
580 array(
581 'messageposition' => 0,
582 'with' => 'user1',
583 'subject' => 'S2',
e12d9f49 584 'unreadcount' => 2,
4d146f1a
RW
585 ),
586 ),
587 'user3' => array(
588 // User3 has only conversed with user1. Their most recent shared message was S5.
589 array(
590 'messageposition' => 0,
591 'with' => 'user1',
592 'subject' => 'S5',
e12d9f49 593 'unreadcount' => 0,
4d146f1a
RW
594 ),
595 ),
596 ),
597 ),
598 'Test that users with contacts and messages to self work as expected' => array(
599 'users' => array(
600 'user1',
601 'user2',
602 'user3',
603 ),
604 'contacts' => array(
605 'user1' => array(
606 'user2' => 0,
607 'user3' => 0,
608 ),
609 'user2' => array(
610 'user3' => 0,
611 ),
612 ),
613 'messages' => array(
614 array(
615 'from' => 'user1',
616 'to' => 'user1',
617 'state' => 'unread',
618 'subject' => 'S1',
619 ),
620 array(
621 'from' => 'user1',
622 'to' => 'user1',
623 'state' => 'unread',
624 'subject' => 'S2',
625 ),
626 ),
627 'expectations' => array(
628 'user1' => array(
629 // User1 has conversed most recently with user1. The most recent message is S2.
630 array(
631 'messageposition' => 0,
632 'with' => 'user1',
633 'subject' => 'S2',
e12d9f49 634 'unreadcount' => 2,
4d146f1a
RW
635 ),
636 ),
637 ),
638 ),
639 'Test conversations with a single user, where some messages are read and some are not.' => array(
640 'users' => array(
641 'user1',
642 'user2',
643 ),
644 'contacts' => array(
645 ),
646 'messages' => array(
647 array(
648 'from' => 'user1',
649 'to' => 'user2',
650 'state' => 'read',
651 'subject' => 'S1',
652 ),
653 array(
654 'from' => 'user2',
655 'to' => 'user1',
656 'state' => 'read',
657 'subject' => 'S2',
658 ),
659 array(
660 'from' => 'user1',
661 'to' => 'user2',
662 'state' => 'unread',
663 'timemodifier' => 1,
664 'subject' => 'S3',
665 ),
666 array(
667 'from' => 'user1',
668 'to' => 'user2',
669 'state' => 'unread',
670 'timemodifier' => 1,
671 'subject' => 'S4',
672 ),
673 ),
674 'expectations' => array(
675 // The most recent message between user1 and user2 was S4.
676 'user1' => array(
677 array(
678 'messageposition' => 0,
679 'with' => 'user2',
680 'subject' => 'S4',
e12d9f49 681 'unreadcount' => 0,
4d146f1a
RW
682 ),
683 ),
684 'user2' => array(
685 // The most recent message between user1 and user2 was S4.
686 array(
687 'messageposition' => 0,
688 'with' => 'user1',
689 'subject' => 'S4',
e12d9f49 690 'unreadcount' => 2,
4d146f1a
RW
691 ),
692 ),
693 ),
694 ),
695 'Test conversations with a single user, where some messages are read and some are not, and messages ' .
696 'are out of order' => array(
697 // This can happen through a combination of factors including multi-master DB replication with messages
698 // read somehow (e.g. API).
699 'users' => array(
700 'user1',
701 'user2',
702 ),
703 'contacts' => array(
704 ),
705 'messages' => array(
706 array(
707 'from' => 'user1',
708 'to' => 'user2',
709 'state' => 'read',
710 'subject' => 'S1',
711 'timemodifier' => 1,
712 ),
713 array(
714 'from' => 'user2',
715 'to' => 'user1',
716 'state' => 'read',
717 'subject' => 'S2',
718 'timemodifier' => 2,
719 ),
720 array(
721 'from' => 'user1',
722 'to' => 'user2',
723 'state' => 'unread',
724 'subject' => 'S3',
725 ),
726 array(
727 'from' => 'user1',
728 'to' => 'user2',
729 'state' => 'unread',
730 'subject' => 'S4',
731 ),
732 ),
733 'expectations' => array(
734 // The most recent message between user1 and user2 was S2, even though later IDs have not been read.
735 'user1' => array(
736 array(
737 'messageposition' => 0,
738 'with' => 'user2',
739 'subject' => 'S2',
e12d9f49 740 'unreadcount' => 0,
4d146f1a
RW
741 ),
742 ),
743 'user2' => array(
744 array(
745 'messageposition' => 0,
746 'with' => 'user1',
747 'subject' => 'S2',
e12d9f49
RW
748 'unreadcount' => 2
749 ),
750 ),
751 ),
752 ),
753 'Test unread message count is correct for both users' => array(
754 'users' => array(
755 'user1',
756 'user2',
757 ),
758 'contacts' => array(
759 ),
760 'messages' => array(
761 array(
762 'from' => 'user1',
763 'to' => 'user2',
764 'state' => 'read',
765 'subject' => 'S1',
766 'timemodifier' => 1,
767 ),
768 array(
769 'from' => 'user2',
770 'to' => 'user1',
771 'state' => 'read',
772 'subject' => 'S2',
773 'timemodifier' => 2,
774 ),
775 array(
776 'from' => 'user1',
777 'to' => 'user2',
778 'state' => 'read',
779 'subject' => 'S3',
780 'timemodifier' => 3,
781 ),
782 array(
783 'from' => 'user1',
784 'to' => 'user2',
785 'state' => 'read',
786 'subject' => 'S4',
787 'timemodifier' => 4,
788 ),
789 array(
790 'from' => 'user1',
791 'to' => 'user2',
792 'state' => 'unread',
793 'subject' => 'S5',
794 'timemodifier' => 5,
795 ),
796 array(
797 'from' => 'user2',
798 'to' => 'user1',
799 'state' => 'unread',
800 'subject' => 'S6',
801 'timemodifier' => 6,
802 ),
803 array(
804 'from' => 'user1',
805 'to' => 'user2',
806 'state' => 'unread',
807 'subject' => 'S7',
808 'timemodifier' => 7,
809 ),
810 array(
811 'from' => 'user1',
812 'to' => 'user2',
813 'state' => 'unread',
814 'subject' => 'S8',
815 'timemodifier' => 8,
816 ),
817 ),
818 'expectations' => array(
819 // The most recent message between user1 and user2 was S2, even though later IDs have not been read.
820 'user1' => array(
821 array(
822 'messageposition' => 0,
823 'with' => 'user2',
824 'subject' => 'S8',
825 'unreadcount' => 1,
826 ),
827 ),
828 'user2' => array(
829 array(
830 'messageposition' => 0,
831 'with' => 'user1',
832 'subject' => 'S8',
833 'unreadcount' => 3,
4d146f1a
RW
834 ),
835 ),
836 ),
837 ),
838 );
839 }
840
841 /**
842 * Test get_conversations with a mixture of messages.
843 *
844 * @dataProvider get_conversations_mixed_provider
845 * @param array $usersdata The list of users to create for this test.
846 * @param array $messagesdata The list of messages to create.
847 * @param array $expectations The list of expected outcomes.
848 */
849 public function test_get_conversations_mixed($usersdata, $contacts, $messagesdata, $expectations) {
850 global $DB;
851
852 // Create all of the users.
853 $users = array();
854 foreach ($usersdata as $username) {
855 $users[$username] = $this->getDataGenerator()->create_user(array('username' => $username));
856 }
857
858 foreach ($contacts as $username => $contact) {
859 foreach ($contact as $contactname => $blocked) {
860 $record = new stdClass();
861 $record->userid = $users[$username]->id;
862 $record->contactid = $users[$contactname]->id;
863 $record->blocked = $blocked;
864 $record->id = $DB->insert_record('message_contacts', $record);
865 }
866 }
867
868 $defaulttimecreated = time();
869 foreach ($messagesdata as $messagedata) {
870 $from = $users[$messagedata['from']];
871 $to = $users[$messagedata['to']];
872 $subject = $messagedata['subject'];
873
874 if (isset($messagedata['state']) && $messagedata['state'] == 'unread') {
875 $table = 'message';
876 $messageid = $this->send_fake_message($from, $to, $subject);
877 } else {
878 // If there is no state, or the state is not 'unread', assume the message is read.
879 $table = 'message_read';
880 $messageid = message_post_message($from, $to, $subject, FORMAT_PLAIN);
881 }
882
883 $updatemessage = new stdClass();
884 $updatemessage->id = $messageid;
885 if (isset($messagedata['timecreated'])) {
886 $updatemessage->timecreated = $messagedata['timecreated'];
887 } else if (isset($messagedata['timemodifier'])) {
888 $updatemessage->timecreated = $defaulttimecreated + $messagedata['timemodifier'];
889 } else {
890 $updatemessage->timecreated = $defaulttimecreated;
891 }
e12d9f49 892
4d146f1a
RW
893 $DB->update_record($table, $updatemessage);
894 }
895
896 foreach ($expectations as $username => $data) {
897 // Get the recent conversations for the specified user.
898 $user = $users[$username];
899 $conversations = array_values(\core_message\api::get_conversations($user->id));
900 foreach ($data as $expectation) {
901 $otheruser = $users[$expectation['with']];
902 $conversation = $conversations[$expectation['messageposition']];
903 $this->assertEquals($otheruser->id, $conversation->userid);
904 $this->assertEquals($expectation['subject'], $conversation->lastmessage);
e12d9f49 905 $this->assertEquals($expectation['unreadcount'], $conversation->unreadcount);
4d146f1a
RW
906 }
907 }
908 }
909
1f64514d
MN
910 /**
911 * Tests retrieving contacts.
912 */
913 public function test_get_contacts() {
914 // Create some users.
915 $user1 = self::getDataGenerator()->create_user();
916
917 // Set as the user.
918 $this->setUser($user1);
919
920 $user2 = new stdClass();
921 $user2->firstname = 'User';
922 $user2->lastname = 'A';
923 $user2 = self::getDataGenerator()->create_user($user2);
924
925 $user3 = new stdClass();
926 $user3->firstname = 'User';
927 $user3->lastname = 'B';
928 $user3 = self::getDataGenerator()->create_user($user3);
929
930 $user4 = new stdClass();
931 $user4->firstname = 'User';
932 $user4->lastname = 'C';
933 $user4 = self::getDataGenerator()->create_user($user4);
934
935 $user5 = new stdClass();
936 $user5->firstname = 'User';
937 $user5->lastname = 'D';
938 $user5 = self::getDataGenerator()->create_user($user5);
939
940 // Add some users as contacts.
941 message_add_contact($user2->id, 0, $user1->id);
942 message_add_contact($user3->id, 0, $user1->id);
943 message_add_contact($user4->id, 0, $user1->id);
944
945 // Retrieve the contacts.
946 $contacts = \core_message\api::get_contacts($user1->id);
947
948 // Confirm the data is correct.
949 $this->assertEquals(3, count($contacts));
950
951 $contact1 = $contacts[0];
952 $contact2 = $contacts[1];
953 $contact3 = $contacts[2];
954
955 $this->assertEquals($user2->id, $contact1->userid);
956 $this->assertEmpty($contact1->useridfrom);
957 $this->assertFalse($contact1->ismessaging);
958 $this->assertNull($contact1->lastmessage);
959 $this->assertNull($contact1->messageid);
cb805753 960 $this->assertNull($contact1->isonline);
1f64514d
MN
961 $this->assertFalse($contact1->isread);
962 $this->assertFalse($contact1->isblocked);
963 $this->assertNull($contact1->unreadcount);
964
965 $this->assertEquals($user3->id, $contact2->userid);
966 $this->assertEmpty($contact2->useridfrom);
967 $this->assertFalse($contact2->ismessaging);
968 $this->assertNull($contact2->lastmessage);
969 $this->assertNull($contact2->messageid);
cb805753 970 $this->assertNull($contact2->isonline);
1f64514d
MN
971 $this->assertFalse($contact2->isread);
972 $this->assertFalse($contact2->isblocked);
973 $this->assertNull($contact2->unreadcount);
974
975 $this->assertEquals($user4->id, $contact3->userid);
976 $this->assertEmpty($contact3->useridfrom);
977 $this->assertFalse($contact3->ismessaging);
978 $this->assertNull($contact3->lastmessage);
979 $this->assertNull($contact3->messageid);
cb805753 980 $this->assertNull($contact3->isonline);
1f64514d
MN
981 $this->assertFalse($contact3->isread);
982 $this->assertFalse($contact3->isblocked);
983 $this->assertNull($contact3->unreadcount);
984 }
985
986 /**
987 * Tests retrieving messages.
988 */
989 public function test_get_messages() {
990 // Create some users.
991 $user1 = self::getDataGenerator()->create_user();
992 $user2 = self::getDataGenerator()->create_user();
993
994 // The person doing the search.
995 $this->setUser($user1);
996
997 // Send some messages back and forth.
998 $time = 1;
999 $this->send_fake_message($user1, $user2, 'Yo!', 0, $time + 1);
1000 $this->send_fake_message($user2, $user1, 'Sup mang?', 0, $time + 2);
1001 $this->send_fake_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 3);
1002 $this->send_fake_message($user2, $user1, 'Word.', 0, $time + 4);
1003
1004 // Retrieve the messages.
1005 $messages = \core_message\api::get_messages($user1->id, $user2->id);
1006
1007 // Confirm the message data is correct.
1008 $this->assertEquals(4, count($messages));
1009
1010 $message1 = $messages[0];
1011 $message2 = $messages[1];
1012 $message3 = $messages[2];
1013 $message4 = $messages[3];
1014
1015 $this->assertEquals($user1->id, $message1->useridfrom);
1016 $this->assertEquals($user2->id, $message1->useridto);
1017 $this->assertTrue($message1->displayblocktime);
1018 $this->assertContains('Yo!', $message1->text);
1019
1020 $this->assertEquals($user2->id, $message2->useridfrom);
1021 $this->assertEquals($user1->id, $message2->useridto);
1022 $this->assertFalse($message2->displayblocktime);
1023 $this->assertContains('Sup mang?', $message2->text);
1024
1025 $this->assertEquals($user1->id, $message3->useridfrom);
1026 $this->assertEquals($user2->id, $message3->useridto);
1027 $this->assertFalse($message3->displayblocktime);
1028 $this->assertContains('Writing PHPUnit tests!', $message3->text);
1029
1030 $this->assertEquals($user2->id, $message4->useridfrom);
1031 $this->assertEquals($user1->id, $message4->useridto);
1032 $this->assertFalse($message4->displayblocktime);
1033 $this->assertContains('Word.', $message4->text);
1034 }
1035
1036 /**
1037 * Tests retrieving most recent message.
1038 */
1039 public function test_get_most_recent_message() {
1040 // Create some users.
1041 $user1 = self::getDataGenerator()->create_user();
1042 $user2 = self::getDataGenerator()->create_user();
1043
1044 // The person doing the search.
1045 $this->setUser($user1);
1046
1047 // Send some messages back and forth.
1048 $time = 1;
1049 $this->send_fake_message($user1, $user2, 'Yo!', 0, $time + 1);
1050 $this->send_fake_message($user2, $user1, 'Sup mang?', 0, $time + 2);
1051 $this->send_fake_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 3);
1052 $this->send_fake_message($user2, $user1, 'Word.', 0, $time + 4);
1053
1054 // Retrieve the most recent messages.
1055 $message = \core_message\api::get_most_recent_message($user1->id, $user2->id);
1056
1057 // Check the results are correct.
1058 $this->assertEquals($user2->id, $message->useridfrom);
1059 $this->assertEquals($user1->id, $message->useridto);
1060 $this->assertContains('Word.', $message->text);
1061 }
1062
1063 /**
1064 * Tests retrieving a user's profile.
1065 */
1066 public function test_get_profile() {
1067 // Create some users.
1068 $user1 = self::getDataGenerator()->create_user();
1069
1070 $user2 = new stdClass();
1071 $user2->country = 'AU';
1072 $user2->city = 'Perth';
1073 $user2 = self::getDataGenerator()->create_user($user2);
1074
1075 // The person doing the search.
1076 $this->setUser($user1);
1077
1078 // Get the profile.
1079 $profile = \core_message\api::get_profile($user1->id, $user2->id);
1080
1081 $this->assertEquals($user2->id, $profile->userid);
1082 $this->assertEmpty($profile->email);
1083 $this->assertEmpty($profile->country);
1084 $this->assertEmpty($profile->city);
1085 $this->assertEquals(fullname($user2), $profile->fullname);
cb805753 1086 $this->assertNull($profile->isonline);
1f64514d
MN
1087 $this->assertFalse($profile->isblocked);
1088 $this->assertFalse($profile->iscontact);
1089 }
1090
1091 /**
1092 * Tests retrieving a user's profile.
1093 */
1094 public function test_get_profile_as_admin() {
1095 // The person doing the search.
1096 $this->setAdminUser();
1097
1098 // Create some users.
1099 $user1 = self::getDataGenerator()->create_user();
1100
1101 $user2 = new stdClass();
1102 $user2->country = 'AU';
1103 $user2->city = 'Perth';
1104 $user2 = self::getDataGenerator()->create_user($user2);
1105
1106 // Get the profile.
1107 $profile = \core_message\api::get_profile($user1->id, $user2->id);
1108
1109 $this->assertEquals($user2->id, $profile->userid);
1110 $this->assertEquals($user2->email, $profile->email);
1111 $this->assertEquals($user2->country, $profile->country);
1112 $this->assertEquals($user2->city, $profile->city);
1113 $this->assertEquals(fullname($user2), $profile->fullname);
1114 $this->assertFalse($profile->isonline);
1115 $this->assertFalse($profile->isblocked);
1116 $this->assertFalse($profile->iscontact);
1117 }
1118
1119 /**
1120 * Tests checking if a user can delete a conversation.
1121 */
1122 public function test_can_delete_conversation() {
1123 // Set as the admin.
1124 $this->setAdminUser();
1125
1126 // Create some users.
1127 $user1 = self::getDataGenerator()->create_user();
1128 $user2 = self::getDataGenerator()->create_user();
1129
1130 // The admin can do anything.
1131 $this->assertTrue(\core_message\api::can_delete_conversation($user1->id));
1132
1133 // Set as the user 1.
1134 $this->setUser($user1);
1135
1136 // They can delete their own messages.
1137 $this->assertTrue(\core_message\api::can_delete_conversation($user1->id));
1138
1139 // They can't delete someone elses.
1140 $this->assertFalse(\core_message\api::can_delete_conversation($user2->id));
1141 }
1142
1143 /**
1144 * Tests deleting a conversation.
1145 */
1146 public function test_delete_conversation() {
1147 global $DB;
1148
1149 // Create some users.
1150 $user1 = self::getDataGenerator()->create_user();
1151 $user2 = self::getDataGenerator()->create_user();
1152
1153 // The person doing the search.
1154 $this->setUser($user1);
1155
1156 // Send some messages back and forth.
1157 $time = 1;
1158 $this->send_fake_message($user1, $user2, 'Yo!', 0, $time + 1);
1159 $this->send_fake_message($user2, $user1, 'Sup mang?', 0, $time + 2);
1160 $this->send_fake_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 3);
1161 $this->send_fake_message($user2, $user1, 'Word.', 0, $time + 4);
1162
1163 // Delete the conversation as user 1.
1164 \core_message\api::delete_conversation($user1->id, $user2->id);
1165
1166 $messages = $DB->get_records('message', array(), 'timecreated ASC');
1167 $this->assertCount(4, $messages);
1168
1169 $message1 = array_shift($messages);
1170 $message2 = array_shift($messages);
1171 $message3 = array_shift($messages);
1172 $message4 = array_shift($messages);
1173
1174 $this->assertNotEmpty($message1->timeuserfromdeleted);
1175 $this->assertEmpty($message1->timeusertodeleted);
1176
1177 $this->assertEmpty($message2->timeuserfromdeleted);
1178 $this->assertNotEmpty($message2->timeusertodeleted);
1179
1180 $this->assertNotEmpty($message3->timeuserfromdeleted);
1181 $this->assertEmpty($message3->timeusertodeleted);
1182
1183 $this->assertEmpty($message4->timeuserfromdeleted);
1184 $this->assertNotEmpty($message4->timeusertodeleted);
1185
1186 }
1187
1188 /**
1189 * Tests counting unread conversations.
1190 */
1191 public function test_count_unread_conversations() {
1192 $this->resetAfterTest(true);
1193
1194 // Create some users.
1195 $user1 = self::getDataGenerator()->create_user();
1196 $user2 = self::getDataGenerator()->create_user();
1197 $user3 = self::getDataGenerator()->create_user();
1198 $user4 = self::getDataGenerator()->create_user();
1199
1200 // The person wanting the conversation count.
1201 $this->setUser($user1);
1202
1203 // Send some messages back and forth, have some different conversations with different users.
1204 $this->send_fake_message($user1, $user2, 'Yo!');
1205 $this->send_fake_message($user2, $user1, 'Sup mang?');
1206 $this->send_fake_message($user1, $user2, 'Writing PHPUnit tests!');
1207 $this->send_fake_message($user2, $user1, 'Word.');
1208
1209 $this->send_fake_message($user1, $user3, 'Booyah');
1210 $this->send_fake_message($user3, $user1, 'Whaaat?');
1211 $this->send_fake_message($user1, $user3, 'Nothing.');
1212 $this->send_fake_message($user3, $user1, 'Cool.');
1213
1214 $this->send_fake_message($user1, $user4, 'Hey mate, you see the new messaging UI in Moodle?');
1215 $this->send_fake_message($user4, $user1, 'Yah brah, it\'s pretty rad.');
1216 $this->send_fake_message($user1, $user4, 'Dope.');
1217
1218 // Check the amount for the current user.
1219 $this->assertEquals(3, core_message\api::count_unread_conversations());
1220
1221 // Check the amount for the second user.
1222 $this->assertEquals(1, core_message\api::count_unread_conversations($user2));
1223 }
1224
1225 /**
1226 * Tests deleting a conversation.
1227 */
1228 public function test_get_all_message_preferences() {
1229 $user = self::getDataGenerator()->create_user();
1230 $this->setUser($user);
1231
1232 // Set a couple of preferences to test.
1233 set_user_preference('message_provider_mod_assign_assign_notification_loggedin', 'popup', $user);
1234 set_user_preference('message_provider_mod_assign_assign_notification_loggedoff', 'email', $user);
1235
1236 $processors = get_message_processors();
1237 $providers = message_get_providers_for_user($user->id);
1238 $prefs = \core_message\api::get_all_message_preferences($processors, $providers, $user);
1239
1240 $this->assertEquals(1, $prefs->mod_assign_assign_notification_loggedin['popup']);
1241 $this->assertEquals(1, $prefs->mod_assign_assign_notification_loggedoff['email']);
1242 }
1243
1244 /**
1245 * Tests the user can post a message.
1246 */
1247 public function test_can_post_message() {
1248 // Create some users.
1249 $user1 = self::getDataGenerator()->create_user();
1250 $user2 = self::getDataGenerator()->create_user();
1251
1252 // Set as the user 1.
1253 $this->setUser($user1);
1254
1255 // They can post to someone else.
1256 $this->assertTrue(\core_message\api::can_post_message($user2));
1257 }
1258
1259 /**
1260 * Tests the user can't post a message without proper capability.
1261 */
1262 public function test_can_post_message_without_cap() {
1263 global $DB;
1264
1265 // Create some users.
1266 $user1 = self::getDataGenerator()->create_user();
1267 $user2 = self::getDataGenerator()->create_user();
1268
1269 // Set as the user 1.
1270 $this->setUser($user1);
1271
1272 // Remove the capability to send a message.
1273 $roleids = $DB->get_records_menu('role', null, '', 'shortname, id');
1274 unassign_capability('moodle/site:sendmessage', $roleids['user'],
1275 context_system::instance());
1276
1277 // Check that we can not post a message without the capability.
1278 $this->assertFalse(\core_message\api::can_post_message($user2));
1279 }
1280
1281 /**
1282 * Tests the user can't post a message if they are not a contact and the user
1283 * has requested messages only from contacts.
1284 */
1285 public function test_can_post_message_when_not_contact() {
1286 // Create some users.
1287 $user1 = self::getDataGenerator()->create_user();
1288 $user2 = self::getDataGenerator()->create_user();
1289
1290 // Set as the first user.
1291 $this->setUser($user1);
1292
1293 // Set the second user's preference to not receive messages from non-contacts.
1294 set_user_preference('message_blocknoncontacts', 1, $user2->id);
1295
1296 // Check that we can not send user 2 a message.
1297 $this->assertFalse(\core_message\api::can_post_message($user2));
1298 }
1299
1300 /**
1301 * Tests the user can't post a message if they are blocked.
1302 */
1303 public function test_can_post_message_when_blocked() {
1304 // Create some users.
1305 $user1 = self::getDataGenerator()->create_user();
1306 $user2 = self::getDataGenerator()->create_user();
1307
1308 // Set the user.
1309 $this->setUser($user1);
1310
1311 // Block the second user.
1312 message_block_contact($user2->id);
1313
1314 // Check that the second user can no longer send the first user a message.
1315 $this->assertFalse(\core_message\api::can_post_message($user1, $user2));
1316 }
1317
1318 /**
1319 * Tests that when blocking messages from non-contacts is enabled that
1320 * non-contacts trying to send a message return false.
1321 */
1322 public function test_is_user_non_contact_blocked() {
1323 // Create some users.
1324 $user1 = self::getDataGenerator()->create_user();
1325 $user2 = self::getDataGenerator()->create_user();
1326
1327 // Set as the first user.
1328 $this->setUser($user1);
1329
1330 // User hasn't sent their preference to block non-contacts, so should return false.
1331 $this->assertFalse(\core_message\api::is_user_non_contact_blocked($user2));
1332
1333 // Set the second user's preference to not receive messages from non-contacts.
1334 set_user_preference('message_blocknoncontacts', 1, $user2->id);
1335
1336 // Check that the return result is now true.
1337 $this->assertTrue(\core_message\api::is_user_non_contact_blocked($user2));
1338
1339 // Add the first user as a contact for the second user.
1340 message_add_contact($user1->id, 0, $user2->id);
1341
1342 // Check that the return result is now false.
1343 $this->assertFalse(\core_message\api::is_user_non_contact_blocked($user2));
1344 }
1345
1346 /**
1347 * Tests that we return true when a user is blocked, or false
1348 * if they are not blocked.
1349 */
1350 public function test_is_user_blocked() {
1351 // Create some users.
1352 $user1 = self::getDataGenerator()->create_user();
1353 $user2 = self::getDataGenerator()->create_user();
1354
1355 // Set the user.
1356 $this->setUser($user1);
1357
1358 // User shouldn't be blocked.
26dca05d 1359 $this->assertFalse(\core_message\api::is_user_blocked($user1->id, $user2->id));
1f64514d
MN
1360
1361 // Block the user.
1362 message_block_contact($user2->id);
1363
1364 // User should be blocked.
26dca05d 1365 $this->assertTrue(\core_message\api::is_user_blocked($user1->id, $user2->id));
727145bf
AN
1366
1367 // Unblock the user.
1368 message_unblock_contact($user2->id);
1369 $this->assertFalse(\core_message\api::is_user_blocked($user1->id, $user2->id));
1f64514d
MN
1370 }
1371
1372 /**
1373 * Tests that the admin is not blocked even if someone has chosen to block them.
1374 */
1375 public function test_is_user_blocked_as_admin() {
1376 // Create a user.
1377 $user1 = self::getDataGenerator()->create_user();
1378
1379 // Set the user.
1380 $this->setUser($user1);
1381
1382 // Block the admin user.
1383 message_block_contact(2);
1384
1385 // Now change to the admin user.
1386 $this->setAdminUser();
1387
1388 // As the admin you should still be able to send messages to the user.
26dca05d 1389 $this->assertFalse(\core_message\api::is_user_blocked($user1->id));
1f64514d 1390 }
20ab51fd
AA
1391
1392 /*
1393 * Tes get_message_processor api.
1394 */
1395 public function test_get_message_processor() {
55b2a33c 1396 $processors = get_message_processors(true);
20ab51fd
AA
1397 if (empty($processors)) {
1398 $this->markTestSkipped("No message processors found");
1399 }
1400
33683bc8
MG
1401 $name = key($processors);
1402 $processor = current($processors);
20ab51fd
AA
1403 $testprocessor = \core_message\api::get_message_processor($name);
1404 $this->assertEquals($processor->name, $testprocessor->name);
1405 $this->assertEquals($processor->enabled, $testprocessor->enabled);
1406 $this->assertEquals($processor->available, $testprocessor->available);
1407 $this->assertEquals($processor->configured, $testprocessor->configured);
1408
1409 // Disable processor and test.
1410 \core_message\api::update_processor_status($testprocessor, 0);
1411 $testprocessor = \core_message\api::get_message_processor($name, true);
1412 $this->assertEmpty($testprocessor);
1413 $testprocessor = \core_message\api::get_message_processor($name);
1414 $this->assertEquals($processor->name, $testprocessor->name);
1415 $this->assertEquals(0, $testprocessor->enabled);
1416
1417 // Enable again and test.
1418 \core_message\api::update_processor_status($testprocessor, 1);
1419 $testprocessor = \core_message\api::get_message_processor($name, true);
1420 $this->assertEquals($processor->name, $testprocessor->name);
1421 $this->assertEquals(1, $testprocessor->enabled);
1422 $testprocessor = \core_message\api::get_message_processor($name);
1423 $this->assertEquals($processor->name, $testprocessor->name);
1424 $this->assertEquals(1, $testprocessor->enabled);
1425 }
1426
1427 /**
1428 * Test method update_processor_status.
1429 */
1430 public function test_update_processor_status() {
1431 $processors = get_message_processors();
1432 if (empty($processors)) {
1433 $this->markTestSkipped("No message processors found");
1434 }
33683bc8
MG
1435 $name = key($processors);
1436 $testprocessor = current($processors);
20ab51fd
AA
1437
1438 // Enable.
1439 \core_message\api::update_processor_status($testprocessor, 1);
1440 $testprocessor = \core_message\api::get_message_processor($name);
1441 $this->assertEquals(1, $testprocessor->enabled);
1442
1443 // Disable.
1444 \core_message\api::update_processor_status($testprocessor, 0);
1445 $testprocessor = \core_message\api::get_message_processor($name);
1446 $this->assertEquals(0, $testprocessor->enabled);
1447
1448 // Enable again.
1449 \core_message\api::update_processor_status($testprocessor, 1);
1450 $testprocessor = \core_message\api::get_message_processor($name);
1451 $this->assertEquals(1, $testprocessor->enabled);
1452 }
1453
1454 /**
1455 * Test method is_user_enabled.
1456 */
1457 public function is_user_enabled() {
1458 $processors = get_message_processors();
1459 if (empty($processors)) {
1460 $this->markTestSkipped("No message processors found");
1461 }
33683bc8
MG
1462 $name = key($processors);
1463 $testprocessor = current($processors);
20ab51fd
AA
1464
1465 // Enable.
1466 \core_message\api::update_processor_status($testprocessor, 1);
1467 $status = \core_message\api::is_processor_enabled($name);
1468 $this->assertEquals(1, $status);
1469
1470 // Disable.
1471 \core_message\api::update_processor_status($testprocessor, 0);
1472 $status = \core_message\api::is_processor_enabled($name);
1473 $this->assertEquals(0, $status);
1474
1475 // Enable again.
1476 \core_message\api::update_processor_status($testprocessor, 1);
1477 $status = \core_message\api::is_processor_enabled($name);
1478 $this->assertEquals(1, $status);
1479 }
fb1469d8
RW
1480
1481 /**
1482 * Test retrieving messages by providing a minimum timecreated value.
1483 */
ffd7798c 1484 public function test_get_messages_time_from_only() {
fb1469d8
RW
1485 // Create some users.
1486 $user1 = self::getDataGenerator()->create_user();
1487 $user2 = self::getDataGenerator()->create_user();
1488
1489 // The person doing the search.
1490 $this->setUser($user1);
1491
1492 // Send some messages back and forth.
1493 $time = 1;
1494 $this->send_fake_message($user1, $user2, 'Message 1', 0, $time + 1);
1495 $this->send_fake_message($user2, $user1, 'Message 2', 0, $time + 2);
1496 $this->send_fake_message($user1, $user2, 'Message 3', 0, $time + 3);
1497 $this->send_fake_message($user2, $user1, 'Message 4', 0, $time + 4);
1498
ffd7798c 1499 // Retrieve the messages from $time, which should be all of them.
fb1469d8
RW
1500 $messages = \core_message\api::get_messages($user1->id, $user2->id, 0, 0, 'timecreated ASC', $time);
1501
1502 // Confirm the message data is correct.
1503 $this->assertEquals(4, count($messages));
1504
1505 $message1 = $messages[0];
1506 $message2 = $messages[1];
1507 $message3 = $messages[2];
1508 $message4 = $messages[3];
1509
1510 $this->assertContains('Message 1', $message1->text);
1511 $this->assertContains('Message 2', $message2->text);
1512 $this->assertContains('Message 3', $message3->text);
1513 $this->assertContains('Message 4', $message4->text);
1514
ffd7798c 1515 // Retrieve the messages from $time + 3, which should only be the 2 last messages.
fb1469d8
RW
1516 $messages = \core_message\api::get_messages($user1->id, $user2->id, 0, 0, 'timecreated ASC', $time + 3);
1517
1518 // Confirm the message data is correct.
1519 $this->assertEquals(2, count($messages));
1520
1521 $message1 = $messages[0];
1522 $message2 = $messages[1];
1523
1524 $this->assertContains('Message 3', $message1->text);
1525 $this->assertContains('Message 4', $message2->text);
1526 }
1527
1528 /**
1529 * Test retrieving messages by providing a maximum timecreated value.
1530 */
ffd7798c 1531 public function test_get_messages_time_to_only() {
fb1469d8
RW
1532 // Create some users.
1533 $user1 = self::getDataGenerator()->create_user();
1534 $user2 = self::getDataGenerator()->create_user();
1535
1536 // The person doing the search.
1537 $this->setUser($user1);
1538
1539 // Send some messages back and forth.
1540 $time = 1;
1541 $this->send_fake_message($user1, $user2, 'Message 1', 0, $time + 1);
1542 $this->send_fake_message($user2, $user1, 'Message 2', 0, $time + 2);
1543 $this->send_fake_message($user1, $user2, 'Message 3', 0, $time + 3);
1544 $this->send_fake_message($user2, $user1, 'Message 4', 0, $time + 4);
1545
ffd7798c 1546 // Retrieve the messages up until $time + 4, which should be all of them.
fb1469d8
RW
1547 $messages = \core_message\api::get_messages($user1->id, $user2->id, 0, 0, 'timecreated ASC', 0, $time + 4);
1548
1549 // Confirm the message data is correct.
1550 $this->assertEquals(4, count($messages));
1551
1552 $message1 = $messages[0];
1553 $message2 = $messages[1];
1554 $message3 = $messages[2];
1555 $message4 = $messages[3];
1556
1557 $this->assertContains('Message 1', $message1->text);
1558 $this->assertContains('Message 2', $message2->text);
1559 $this->assertContains('Message 3', $message3->text);
1560 $this->assertContains('Message 4', $message4->text);
1561
ffd7798c 1562 // Retrieve the messages up until $time + 2, which should be the first two.
fb1469d8
RW
1563 $messages = \core_message\api::get_messages($user1->id, $user2->id, 0, 0, 'timecreated ASC', 0, $time + 2);
1564
1565 // Confirm the message data is correct.
1566 $this->assertEquals(2, count($messages));
1567
1568 $message1 = $messages[0];
1569 $message2 = $messages[1];
1570
1571 $this->assertContains('Message 1', $message1->text);
1572 $this->assertContains('Message 2', $message2->text);
1573 }
1574
1575 /**
1576 * Test retrieving messages by providing a minimum and maximum timecreated value.
1577 */
ffd7798c 1578 public function test_get_messages_time_from_and_to() {
fb1469d8
RW
1579 // Create some users.
1580 $user1 = self::getDataGenerator()->create_user();
1581 $user2 = self::getDataGenerator()->create_user();
1582
1583 // The person doing the search.
1584 $this->setUser($user1);
1585
1586 // Send some messages back and forth.
1587 $time = 1;
1588 $this->send_fake_message($user1, $user2, 'Message 1', 0, $time + 1);
1589 $this->send_fake_message($user2, $user1, 'Message 2', 0, $time + 2);
1590 $this->send_fake_message($user1, $user2, 'Message 3', 0, $time + 3);
1591 $this->send_fake_message($user2, $user1, 'Message 4', 0, $time + 4);
1592
ffd7798c 1593 // Retrieve the messages from $time + 2 up until $time + 3, which should be 2nd and 3rd message.
fb1469d8
RW
1594 $messages = \core_message\api::get_messages($user1->id, $user2->id, 0, 0, 'timecreated ASC', $time + 2, $time + 3);
1595
1596 // Confirm the message data is correct.
1597 $this->assertEquals(2, count($messages));
1598
1599 $message1 = $messages[0];
1600 $message2 = $messages[1];
1601
1602 $this->assertContains('Message 2', $message1->text);
1603 $this->assertContains('Message 3', $message2->text);
1604 }
5cdebf2d 1605}