MDL-55942 core_message: moved added functionality from message/lib.php
[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
42 public function test_message_mark_all_read_for_user_touser() {
43 $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
44 $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
45
46 $this->send_fake_unread_popup_notification($sender, $recipient);
47 $this->send_fake_unread_popup_notification($sender, $recipient);
48 $this->send_fake_unread_popup_notification($sender, $recipient);
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
57 public function test_message_mark_all_read_for_user_touser_with_fromuser() {
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
62 $this->send_fake_unread_popup_notification($sender1, $recipient);
63 $this->send_fake_unread_popup_notification($sender1, $recipient);
64 $this->send_fake_unread_popup_notification($sender1, $recipient);
65 $this->send_fake_message($sender1, $recipient);
66 $this->send_fake_message($sender1, $recipient);
67 $this->send_fake_message($sender1, $recipient);
68 $this->send_fake_unread_popup_notification($sender2, $recipient);
69 $this->send_fake_unread_popup_notification($sender2, $recipient);
70 $this->send_fake_unread_popup_notification($sender2, $recipient);
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
79 public function test_message_mark_all_read_for_user_touser_with_type() {
80 $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
81 $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
82
83 $this->send_fake_unread_popup_notification($sender, $recipient);
84 $this->send_fake_unread_popup_notification($sender, $recipient);
85 $this->send_fake_unread_popup_notification($sender, $recipient);
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
97 /**
98 * Test that the get_popup_notifications function will return only read notifications if requested.
99 */
100 public function test_message_get_popup_notifications_read_only() {
101 $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
102 $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
103
104 $this->send_fake_read_popup_notification($sender, $recipient, 'Message 1', 2);
105 $this->send_fake_read_popup_notification($sender, $recipient, 'Message 2', 4);
106
107 $notifications = \core_message\api::get_popup_notifications($recipient->id, MESSAGE_READ);
108
109 $this->assertEquals($notifications[0]->fullmessage, 'Message 2');
110 $this->assertEquals($notifications[1]->fullmessage, 'Message 1');
111
112 // Check if we request read and unread but there are only read messages, it should
113 // still return those correctly.
114 $notifications = \core_message\api::get_popup_notifications($recipient->id, '');
115
116 $this->assertEquals($notifications[0]->fullmessage, 'Message 2');
117 $this->assertEquals($notifications[1]->fullmessage, 'Message 1');
118 }
119
120 /**
121 * Test that the get_popup_notifications function will return only unread notifications if requested.
122 */
123 public function test_message_get_popup_notifications_unread_only() {
124 $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
125 $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
126
127 $this->send_fake_unread_popup_notification($sender, $recipient, 'Message 1', 2);
128 $this->send_fake_unread_popup_notification($sender, $recipient, 'Message 2', 4);
129
130 $notifications = \core_message\api::get_popup_notifications($recipient->id, MESSAGE_UNREAD);
131
132 $this->assertEquals($notifications[0]->fullmessage, 'Message 2');
133 $this->assertEquals($notifications[1]->fullmessage, 'Message 1');
134
135 // Check if we request read and unread but there are only read messages, it should
136 // still return those correctly.
137 $notifications = \core_message\api::get_popup_notifications($recipient->id, '');
138
139 $this->assertEquals($notifications[0]->fullmessage, 'Message 2');
140 $this->assertEquals($notifications[1]->fullmessage, 'Message 1');
141 }
142
143 /**
144 * Test that the get_popup_notifications function will return the correct notifications when both
145 * read and unread notifications are included.
146 */
147 public function test_message_get_popup_notifications_mixed() {
148 $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
149 $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
150
151 $this->send_fake_read_popup_notification($sender, $recipient, 'Message 1', 1);
152 $this->send_fake_unread_popup_notification($sender, $recipient, 'Message 2', 2);
153 $this->send_fake_read_popup_notification($sender, $recipient, 'Message 3', 3, 1);
154 $this->send_fake_read_popup_notification($sender, $recipient, 'Message 4', 3, 2);
155 $this->send_fake_unread_popup_notification($sender, $recipient, 'Message 5', 4);
156
157 $notifications = \core_message\api::get_popup_notifications($recipient->id);
158
159 $this->assertEquals($notifications[0]->fullmessage, 'Message 5');
160 $this->assertEquals($notifications[1]->fullmessage, 'Message 4');
161 $this->assertEquals($notifications[2]->fullmessage, 'Message 3');
162 $this->assertEquals($notifications[3]->fullmessage, 'Message 2');
163 $this->assertEquals($notifications[4]->fullmessage, 'Message 1');
164
165 $notifications = \core_message\api::get_popup_notifications($recipient->id, MESSAGE_READ);
166
167 $this->assertEquals($notifications[0]->fullmessage, 'Message 4');
168 $this->assertEquals($notifications[1]->fullmessage, 'Message 3');
169 $this->assertEquals($notifications[2]->fullmessage, 'Message 1');
170
171 $notifications = \core_message\api::get_popup_notifications($recipient->id, MESSAGE_UNREAD);
172
173 $this->assertEquals($notifications[0]->fullmessage, 'Message 5');
174 $this->assertEquals($notifications[1]->fullmessage, 'Message 2');
175 }
176
177 /**
178 * Test that the get_popup_notifications function works correctly with limiting and offsetting
179 * the result set if requested.
180 */
181 public function test_message_get_popup_notifications_all_with_limit_and_offset() {
182 $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
183 $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
184
185 $this->send_fake_read_popup_notification($sender, $recipient, 'Message 1', 1);
186 $this->send_fake_unread_popup_notification($sender, $recipient, 'Message 2', 2);
187 $this->send_fake_read_popup_notification($sender, $recipient, 'Message 3', 3, 1);
188 $this->send_fake_read_popup_notification($sender, $recipient, 'Message 4', 3, 2);
189 $this->send_fake_unread_popup_notification($sender, $recipient, 'Message 5', 4);
190 $this->send_fake_unread_popup_notification($sender, $recipient, 'Message 6', 5);
191
192 $notifications = \core_message\api::get_popup_notifications($recipient->id, '', false, false, 'DESC', 2, 0);
193
194 $this->assertEquals($notifications[0]->fullmessage, 'Message 6');
195 $this->assertEquals($notifications[1]->fullmessage, 'Message 5');
196
197 $notifications = \core_message\api::get_popup_notifications($recipient->id, '', false, false, 'DESC', 2, 2);
198
199 $this->assertEquals($notifications[0]->fullmessage, 'Message 4');
200 $this->assertEquals($notifications[1]->fullmessage, 'Message 3');
201
202 $notifications = \core_message\api::get_popup_notifications($recipient->id, '', false, false, 'DESC', 0, 3);
203
204 $this->assertEquals($notifications[0]->fullmessage, 'Message 3');
205 $this->assertEquals($notifications[1]->fullmessage, 'Message 2');
206 $this->assertEquals($notifications[2]->fullmessage, 'Message 1');
207 }
208
209 /**
210 * Test that the get_popup_notifications function returns embedded user details for the
211 * sender if requested.
212 */
213 public function test_message_get_popup_notifications_embed_sender() {
214 $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
215 $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
216
217 $this->send_fake_read_popup_notification($sender, $recipient, 'Message 1', 1);
218 $this->send_fake_unread_popup_notification($sender, $recipient, 'Message 2', 2);
219
220 $notifications = \core_message\api::get_popup_notifications($recipient->id, '', false, true, 'DESC');
221
222 $func = function($type) {
223 return function($notification) use ($type) {
224 $user = new stdClass();
225 $user = username_load_fields_from_object($user, $notification, $type);
226 return $user;
227 };
228 };
229 $senders = array_map($func('userfrom'), $notifications);
230 $recipients = array_map($func('userto'), $notifications);
231
232 $this->assertEquals($senders[0]->firstname, 'Test1');
233 $this->assertEquals($senders[0]->lastname, 'User1');
234 $this->assertEquals($senders[1]->firstname, 'Test1');
235 $this->assertEquals($senders[1]->lastname, 'User1');
236
237 // Make sure we didn't get recipient details when they weren't requested.
238 $this->assertEmpty($recipients[0]->firstname);
239 $this->assertEmpty($recipients[0]->lastname);
240 $this->assertEmpty($recipients[1]->firstname);
241 $this->assertEmpty($recipients[1]->lastname);
242 }
243
244 /**
245 * Test that the get_popup_notifications function returns embedded user details for the
246 * recipient if requested.
247 */
248 public function test_message_get_popup_notifications_embed_recipient() {
249 $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
250 $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
251
252 $this->send_fake_read_popup_notification($sender, $recipient, 'Message 1', 1);
253 $this->send_fake_unread_popup_notification($sender, $recipient, 'Message 2', 2);
254
255 $notifications = \core_message\api::get_popup_notifications($recipient->id, '', true, false, 'DESC');
256
257 $func = function($type) {
258 return function($notification) use ($type) {
259 $user = new stdClass();
260 $user = username_load_fields_from_object($user, $notification, $type);
261 return $user;
262 };
263 };
264 $senders = array_map($func('userfrom'), $notifications);
265 $recipients = array_map($func('userto'), $notifications);
266
267 $this->assertEquals($recipients[0]->firstname, 'Test2');
268 $this->assertEquals($recipients[0]->lastname, 'User2');
269 $this->assertEquals($recipients[1]->firstname, 'Test2');
270 $this->assertEquals($recipients[1]->lastname, 'User2');
271
272 // Make sure we didn't get sender details when they weren't requested.
273 $this->assertEmpty($senders[0]->firstname);
274 $this->assertEmpty($senders[0]->lastname);
275 $this->assertEmpty($senders[1]->firstname);
276 $this->assertEmpty($senders[1]->lastname);
277 }
278
279 /**
280 * Test that the get_popup_notifications function returns embedded all user details.
281 */
282 public function test_message_get_popup_notifications_embed_both() {
283 $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
284 $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
285
286 $this->send_fake_read_popup_notification($sender, $recipient, 'Message 1', 1);
287 $this->send_fake_unread_popup_notification($sender, $recipient, 'Message 2', 2);
288
289 $notifications = \core_message\api::get_popup_notifications($recipient->id, '', true, true, 'DESC');
290
291 $func = function($type) {
292 return function($notification) use ($type) {
293 $user = new stdClass();
294 $user = username_load_fields_from_object($user, $notification, $type);
295 return $user;
296 };
297 };
298 $senders = array_map($func('userfrom'), $notifications);
299 $recipients = array_map($func('userto'), $notifications);
300
301 $this->assertEquals($recipients[0]->firstname, 'Test2');
302 $this->assertEquals($recipients[0]->lastname, 'User2');
303 $this->assertEquals($recipients[1]->firstname, 'Test2');
304 $this->assertEquals($recipients[1]->lastname, 'User2');
305
306 // Make sure we didn't get sender details when they weren't requested.
307 $this->assertEquals($senders[0]->firstname, 'Test1');
308 $this->assertEquals($senders[0]->lastname, 'User1');
309 $this->assertEquals($senders[1]->firstname, 'Test1');
310 $this->assertEquals($senders[1]->lastname, 'User1');
311 }
312
313 /**
314 * Test count_unread_popup_notifications.
315 */
316 public function test_message_count_unread_popup_notifications() {
317 $sender1 = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
318 $sender2 = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
319 $recipient1 = $this->getDataGenerator()->create_user(array('firstname' => 'Test3', 'lastname' => 'User3'));
320 $recipient2 = $this->getDataGenerator()->create_user(array('firstname' => 'Test4', 'lastname' => 'User4'));
321
322 $this->send_fake_unread_popup_notification($sender1, $recipient1);
323 $this->send_fake_unread_popup_notification($sender1, $recipient1);
324 $this->send_fake_unread_popup_notification($sender2, $recipient1);
325 $this->send_fake_unread_popup_notification($sender1, $recipient2);
326 $this->send_fake_unread_popup_notification($sender2, $recipient2);
327 $this->send_fake_unread_popup_notification($sender2, $recipient2);
328 $this->send_fake_unread_popup_notification($sender2, $recipient2);
329 $this->send_fake_unread_popup_notification($sender2, $recipient2);
330
331 $this->assertEquals(\core_message\api::count_unread_popup_notifications($recipient1->id), 3);
332 $this->assertEquals(\core_message\api::count_unread_popup_notifications($recipient2->id), 5);
333 }
334}