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