MDL-47162 core_message: debug whenever courseid is missing
[moodle.git] / message / tests / events_test.php
CommitLineData
0d9bdf61
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 * Events tests.
19 *
20 * @package core_message
21 * @category test
22 * @copyright 2014 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
28class core_message_events_testcase extends advanced_testcase {
29
30 /**
31 * Test set up.
32 *
33 * This is executed before running any test in this file.
34 */
35 public function setUp() {
36 $this->resetAfterTest();
37 }
38
39 /**
40 * Test the message contact added event.
41 */
42 public function test_message_contact_added() {
43 // Set this user as the admin.
44 $this->setAdminUser();
45
46 // Create a user to add to the admin's contact list.
47 $user = $this->getDataGenerator()->create_user();
48
49 // Trigger and capture the event when adding a contact.
50 $sink = $this->redirectEvents();
51 message_add_contact($user->id);
52 $events = $sink->get_events();
53 $event = reset($events);
54
55 // Check that the event data is valid.
56 $this->assertInstanceOf('\core\event\message_contact_added', $event);
57 $this->assertEquals(context_user::instance(2), $event->get_context());
58 $expected = array(SITEID, 'message', 'add contact', 'index.php?user1=' . $user->id .
59 '&amp;user2=2', $user->id);
60 $this->assertEventLegacyLogData($expected, $event);
b63f7732
AA
61 $url = new moodle_url('/message/index.php', array('user1' => $event->userid, 'user2' => $event->relateduserid));
62 $this->assertEquals($url, $event->get_url());
0d9bdf61 63 }
5edf9f3b
MN
64
65 /**
66 * Test the message contact removed event.
67 */
68 public function test_message_contact_removed() {
69 // Set this user as the admin.
70 $this->setAdminUser();
71
72 // Create a user to add to the admin's contact list.
73 $user = $this->getDataGenerator()->create_user();
74
75 // Add the user to the admin's contact list.
76 message_add_contact($user->id);
77
78 // Trigger and capture the event when adding a contact.
79 $sink = $this->redirectEvents();
80 message_remove_contact($user->id);
81 $events = $sink->get_events();
82 $event = reset($events);
83
84 // Check that the event data is valid.
85 $this->assertInstanceOf('\core\event\message_contact_removed', $event);
86 $this->assertEquals(context_user::instance(2), $event->get_context());
87 $expected = array(SITEID, 'message', 'remove contact', 'index.php?user1=' . $user->id .
88 '&amp;user2=2', $user->id);
89 $this->assertEventLegacyLogData($expected, $event);
b63f7732
AA
90 $url = new moodle_url('/message/index.php', array('user1' => $event->userid, 'user2' => $event->relateduserid));
91 $this->assertEquals($url, $event->get_url());
5edf9f3b 92 }
3f3d9521
MN
93
94 /**
95 * Test the message contact blocked event.
96 */
97 public function test_message_contact_blocked() {
98 // Set this user as the admin.
99 $this->setAdminUser();
100
101 // Create a user to add to the admin's contact list.
102 $user = $this->getDataGenerator()->create_user();
2236a565 103 $user2 = $this->getDataGenerator()->create_user();
3f3d9521
MN
104
105 // Add the user to the admin's contact list.
106 message_add_contact($user->id);
107
108 // Trigger and capture the event when blocking a contact.
109 $sink = $this->redirectEvents();
110 message_block_contact($user->id);
111 $events = $sink->get_events();
112 $event = reset($events);
113
114 // Check that the event data is valid.
115 $this->assertInstanceOf('\core\event\message_contact_blocked', $event);
116 $this->assertEquals(context_user::instance(2), $event->get_context());
117 $expected = array(SITEID, 'message', 'block contact', 'index.php?user1=' . $user->id . '&amp;user2=2', $user->id);
118 $this->assertEventLegacyLogData($expected, $event);
b63f7732
AA
119 $url = new moodle_url('/message/index.php', array('user1' => $event->userid, 'user2' => $event->relateduserid));
120 $this->assertEquals($url, $event->get_url());
2236a565 121
e8c5a98b
JP
122 // Make sure that the contact blocked event is not triggered again.
123 $sink->clear();
124 message_block_contact($user->id);
125 $events = $sink->get_events();
126 $event = reset($events);
127 $this->assertEmpty($event);
128 // Make sure that we still have 1 blocked user.
5b0769db 129 $this->assertEquals(1, \core_message\api::count_blocked_users());
e8c5a98b 130
2236a565
FM
131 // Now blocking a user that is not a contact.
132 $sink->clear();
133 message_block_contact($user2->id);
134 $events = $sink->get_events();
135 $event = reset($events);
136
137 // Check that the event data is valid.
138 $this->assertInstanceOf('\core\event\message_contact_blocked', $event);
139 $this->assertEquals(context_user::instance(2), $event->get_context());
140 $expected = array(SITEID, 'message', 'block contact', 'index.php?user1=' . $user2->id . '&amp;user2=2', $user2->id);
141 $this->assertEventLegacyLogData($expected, $event);
142 $url = new moodle_url('/message/index.php', array('user1' => $event->userid, 'user2' => $event->relateduserid));
143 $this->assertEquals($url, $event->get_url());
3f3d9521 144 }
cd609365
MN
145
146 /**
147 * Test the message contact unblocked event.
148 */
149 public function test_message_contact_unblocked() {
150 // Set this user as the admin.
151 $this->setAdminUser();
152
153 // Create a user to add to the admin's contact list.
154 $user = $this->getDataGenerator()->create_user();
155
156 // Add the user to the admin's contact list.
157 message_add_contact($user->id);
158
e8c5a98b
JP
159 // Block the user.
160 message_block_contact($user->id);
161 // Make sure that we have 1 blocked user.
5b0769db 162 $this->assertEquals(1, \core_message\api::count_blocked_users());
e8c5a98b 163
cd609365
MN
164 // Trigger and capture the event when unblocking a contact.
165 $sink = $this->redirectEvents();
166 message_unblock_contact($user->id);
167 $events = $sink->get_events();
168 $event = reset($events);
169
170 // Check that the event data is valid.
171 $this->assertInstanceOf('\core\event\message_contact_unblocked', $event);
172 $this->assertEquals(context_user::instance(2), $event->get_context());
173 $expected = array(SITEID, 'message', 'unblock contact', 'index.php?user1=' . $user->id . '&amp;user2=2', $user->id);
174 $this->assertEventLegacyLogData($expected, $event);
b63f7732
AA
175 $url = new moodle_url('/message/index.php', array('user1' => $event->userid, 'user2' => $event->relateduserid));
176 $this->assertEquals($url, $event->get_url());
e8c5a98b
JP
177
178 // Make sure that we have no blocked users.
5b0769db 179 $this->assertEmpty(\core_message\api::count_blocked_users());
e8c5a98b
JP
180
181 // Make sure that the contact unblocked event is not triggered again.
182 $sink->clear();
183 message_unblock_contact($user->id);
184 $events = $sink->get_events();
185 $event = reset($events);
186 $this->assertEmpty($event);
187
188 // Make sure that we still have no blocked users.
5b0769db 189 $this->assertEmpty(\core_message\api::count_blocked_users());
cd609365 190 }
ab1bc5d2
MN
191
192 /**
193 * Test the message sent event.
194 *
195 * We can not use the message_send() function in the unit test to check that the event was fired as there is a
196 * conditional check to ensure a fake message is sent during unit tests when calling that particular function.
197 */
198 public function test_message_sent() {
199 $event = \core\event\message_sent::create(array(
200 'userid' => 1,
201 'context' => context_system::instance(),
202 'relateduserid' => 2,
203 'other' => array(
cc350fd9 204 'messageid' => 3,
a29bcf78 205 'courseid' => 4
ab1bc5d2
MN
206 )
207 ));
208
209 // Trigger and capturing the event.
210 $sink = $this->redirectEvents();
211 $event->trigger();
212 $events = $sink->get_events();
213 $event = reset($events);
214
215 // Check that the event data is valid.
216 $this->assertInstanceOf('\core\event\message_sent', $event);
217 $this->assertEquals(context_system::instance(), $event->get_context());
218 $expected = array(SITEID, 'message', 'write', 'index.php?user=1&id=2&history=1#m3', 1);
219 $this->assertEventLegacyLogData($expected, $event);
b63f7732
AA
220 $url = new moodle_url('/message/index.php', array('user1' => $event->userid, 'user2' => $event->relateduserid));
221 $this->assertEquals($url, $event->get_url());
a29bcf78
EL
222 $this->assertEquals(3, $event->other['messageid']);
223 $this->assertEquals(4, $event->other['courseid']);
ab1bc5d2 224 }
5b091909 225
a29bcf78
EL
226 public function test_mesage_sent_without_other_courseid() {
227
228 // Creating a message_sent event without other[courseid] leads to exception.
229 $this->expectException('coding_exception');
230 $this->expectExceptionMessage('The \'courseid\' value must be set in other');
231
232 $event = \core\event\message_sent::create(array(
233 'userid' => 1,
234 'context' => context_system::instance(),
235 'relateduserid' => 2,
236 'other' => array(
237 'messageid' => 3,
238 )
239 ));
240 }
241
242 public function test_mesage_sent_via_create_from_ids() {
243 // Containing courseid.
244 $event = \core\event\message_sent::create_from_ids(1, 2, 3, 4);
245
246 // Trigger and capturing the event.
247 $sink = $this->redirectEvents();
248 $event->trigger();
249 $events = $sink->get_events();
250 $event = reset($events);
251
252 // Check that the event data is valid.
253 $this->assertInstanceOf('\core\event\message_sent', $event);
254 $this->assertEquals(context_system::instance(), $event->get_context());
255 $expected = array(SITEID, 'message', 'write', 'index.php?user=1&id=2&history=1#m3', 1);
256 $this->assertEventLegacyLogData($expected, $event);
257 $url = new moodle_url('/message/index.php', array('user1' => $event->userid, 'user2' => $event->relateduserid));
258 $this->assertEquals($url, $event->get_url());
259 $this->assertEquals(3, $event->other['messageid']);
260 $this->assertEquals(4, $event->other['courseid']);
261 }
262
263 public function test_mesage_sent_via_create_from_ids_without_other_courseid() {
264
265 // Creating a message_sent event without courseid leads to debugging + SITEID.
266 // TODO: MDL-55449 Ensure this leads to exception instead of debugging in Moodle 3.6.
267 $event = \core\event\message_sent::create_from_ids(1, 2, 3);
268
269 // Trigger and capturing the event.
270 $sink = $this->redirectEvents();
271 $event->trigger();
272 $events = $sink->get_events();
273 $event = reset($events);
274
275 $this->assertDebuggingCalled();
276 $this->assertEquals(SITEID, $event->other['courseid']);
277 }
278
279
280
281
5b091909 282 /**
f3d98189 283 * Test the message viewed event.
5b091909 284 */
f3d98189 285 public function test_message_viewed() {
5b091909
MN
286 global $DB;
287
288 // Create a message to mark as read.
289 $message = new stdClass();
290 $message->useridfrom = '1';
291 $message->useridto = '2';
292 $message->subject = 'Subject';
293 $message->message = 'Message';
294 $message->id = $DB->insert_record('message', $message);
295
296 // Trigger and capture the event.
297 $sink = $this->redirectEvents();
298 message_mark_message_read($message, time());
299 $events = $sink->get_events();
300 $event = reset($events);
301
302 // Check that the event data is valid.
f3d98189 303 $this->assertInstanceOf('\core\event\message_viewed', $event);
5b091909 304 $this->assertEquals(context_user::instance(2), $event->get_context());
b63f7732
AA
305 $url = new moodle_url('/message/index.php', array('user1' => $event->userid, 'user2' => $event->relateduserid));
306 $this->assertEquals($url, $event->get_url());
5b091909 307 }
6fff51d6
MN
308
309 /**
310 * Test the message deleted event.
311 */
312 public function test_message_deleted() {
313 global $DB;
314
315 // Create a message.
316 $message = new stdClass();
317 $message->useridfrom = '1';
318 $message->useridto = '2';
319 $message->subject = 'Subject';
320 $message->message = 'Message';
321 $message->timeuserfromdeleted = 0;
322 $message->timeusertodeleted = 0;
323 $message->id = $DB->insert_record('message', $message);
324
325 // Trigger and capture the event.
326 $sink = $this->redirectEvents();
327 message_delete_message($message, $message->useridfrom);
328 $events = $sink->get_events();
329 $event = reset($events);
330
331 // Check that the event data is valid.
332 $this->assertInstanceOf('\core\event\message_deleted', $event);
333 $this->assertEquals($message->useridfrom, $event->userid); // The user who deleted it.
334 $this->assertEquals($message->useridto, $event->relateduserid);
335 $this->assertEquals('message', $event->other['messagetable']);
336 $this->assertEquals($message->id, $event->other['messageid']);
337 $this->assertEquals($message->useridfrom, $event->other['useridfrom']);
338 $this->assertEquals($message->useridto, $event->other['useridto']);
339
340 // Create a read message.
341 $message = new stdClass();
342 $message->useridfrom = '2';
343 $message->useridto = '1';
344 $message->subject = 'Subject';
345 $message->message = 'Message';
346 $message->timeuserfromdeleted = 0;
347 $message->timeusertodeleted = 0;
348 $message->timeread = time();
349 $message->id = $DB->insert_record('message_read', $message);
350
351 // Trigger and capture the event.
352 $sink = $this->redirectEvents();
353 message_delete_message($message, $message->useridto);
354 $events = $sink->get_events();
355 $event = reset($events);
356
357 // Check that the event data is valid.
358 $this->assertInstanceOf('\core\event\message_deleted', $event);
359 $this->assertEquals($message->useridto, $event->userid);
360 $this->assertEquals($message->useridfrom, $event->relateduserid);
361 $this->assertEquals('message_read', $event->other['messagetable']);
362 $this->assertEquals($message->id, $event->other['messageid']);
363 $this->assertEquals($message->useridfrom, $event->other['useridfrom']);
364 $this->assertEquals($message->useridto, $event->other['useridto']);
365 }
1f64514d
MN
366
367 /**
368 * Test the message deleted event is fired when deleting a conversation.
369 */
370 public function test_message_deleted_whole_conversation() {
371 global $DB;
372
373 // Create a message.
374 $message = new stdClass();
375 $message->useridfrom = '1';
376 $message->useridto = '2';
377 $message->subject = 'Subject';
378 $message->message = 'Message';
379 $message->timeuserfromdeleted = 0;
380 $message->timeusertodeleted = 0;
381 $message->timecreated = 1;
382
ebc746dc 383 $messages = [];
1f64514d 384 // Send this a few times.
ebc746dc 385 $messages[] = $DB->insert_record('message', $message);
1f64514d
MN
386
387 $message->timecreated++;
ebc746dc 388 $messages[] = $DB->insert_record('message', $message);
1f64514d
MN
389
390 $message->timecreated++;
ebc746dc 391 $messages[] = $DB->insert_record('message', $message);
1f64514d
MN
392
393 $message->timecreated++;
ebc746dc 394 $messages[] = $DB->insert_record('message', $message);
1f64514d
MN
395
396 // Create a read message.
397 $message->timeread = time();
398
399 // Send this a few times.
400 $message->timecreated++;
ebc746dc 401 $messages[] = $DB->insert_record('message_read', $message);
1f64514d
MN
402
403 $message->timecreated++;
ebc746dc 404 $messages[] = $DB->insert_record('message_read', $message);
1f64514d
MN
405
406 $message->timecreated++;
ebc746dc 407 $messages[] = $DB->insert_record('message_read', $message);
1f64514d
MN
408
409 $message->timecreated++;
ebc746dc 410 $messages[] = $DB->insert_record('message_read', $message);
1f64514d
MN
411
412 // Trigger and capture the event.
413 $sink = $this->redirectEvents();
414 \core_message\api::delete_conversation(1, 2);
415 $events = $sink->get_events();
416
417 // Check that there were the correct number of events triggered.
418 $this->assertEquals(8, count($events));
419
420 // Check that the event data is valid.
ebc746dc 421 $i = 0;
1f64514d 422 foreach ($events as $event) {
ebc746dc 423 $table = ($i > 3) ? 'message_read' : 'message';
1f64514d
MN
424
425 $this->assertInstanceOf('\core\event\message_deleted', $event);
426 $this->assertEquals($message->useridfrom, $event->userid);
427 $this->assertEquals($message->useridto, $event->relateduserid);
428 $this->assertEquals($table, $event->other['messagetable']);
ebc746dc 429 $this->assertEquals($messages[$i], $event->other['messageid']);
1f64514d
MN
430 $this->assertEquals($message->useridfrom, $event->other['useridfrom']);
431 $this->assertEquals($message->useridto, $event->other['useridto']);
432
433 $i++;
434 }
435 }
0d9bdf61 436}