MDL-62781 question/privacy: fix tests with CodeRunner is installed
[moodle.git] / message / tests / events_test.php
blob9c9c4158edbcf4227cb0bf1e744d8d1993461bd7
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.
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/>.
17 /**
18 * Events tests.
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
26 defined('MOODLE_INTERNAL') || die();
28 global $CFG;
30 require_once($CFG->dirroot . '/message/tests/messagelib_test.php');
32 /**
33 * Class containing the tests for message related events.
35 * @package core_message
36 * @category test
37 * @copyright 2014 Mark Nelson <markn@moodle.com>
38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40 class core_message_events_testcase extends core_message_messagelib_testcase {
42 /**
43 * Test set up.
45 * This is executed before running any test in this file.
47 public function setUp() {
48 $this->resetAfterTest();
51 /**
52 * Test the message contact added event.
54 public function test_message_contact_added() {
55 // Set this user as the admin.
56 $this->setAdminUser();
58 // Create a user to add to the admin's contact list.
59 $user = $this->getDataGenerator()->create_user();
61 // Trigger and capture the event when adding a contact.
62 $sink = $this->redirectEvents();
63 message_add_contact($user->id);
64 $events = $sink->get_events();
65 $event = reset($events);
67 // Check that the event data is valid.
68 $this->assertInstanceOf('\core\event\message_contact_added', $event);
69 $this->assertEquals(context_user::instance(2), $event->get_context());
70 $expected = array(SITEID, 'message', 'add contact', 'index.php?user1=' . $user->id .
71 '&amp;user2=2', $user->id);
72 $this->assertEventLegacyLogData($expected, $event);
73 $url = new moodle_url('/message/index.php', array('user1' => $event->userid, 'user2' => $event->relateduserid));
74 $this->assertEquals($url, $event->get_url());
77 /**
78 * Test the message contact removed event.
80 public function test_message_contact_removed() {
81 // Set this user as the admin.
82 $this->setAdminUser();
84 // Create a user to add to the admin's contact list.
85 $user = $this->getDataGenerator()->create_user();
87 // Add the user to the admin's contact list.
88 message_add_contact($user->id);
90 // Trigger and capture the event when adding a contact.
91 $sink = $this->redirectEvents();
92 message_remove_contact($user->id);
93 $events = $sink->get_events();
94 $event = reset($events);
96 // Check that the event data is valid.
97 $this->assertInstanceOf('\core\event\message_contact_removed', $event);
98 $this->assertEquals(context_user::instance(2), $event->get_context());
99 $expected = array(SITEID, 'message', 'remove contact', 'index.php?user1=' . $user->id .
100 '&amp;user2=2', $user->id);
101 $this->assertEventLegacyLogData($expected, $event);
102 $url = new moodle_url('/message/index.php', array('user1' => $event->userid, 'user2' => $event->relateduserid));
103 $this->assertEquals($url, $event->get_url());
107 * Test the message contact blocked event.
109 public function test_message_contact_blocked() {
110 // Set this user as the admin.
111 $this->setAdminUser();
113 // Create a user to add to the admin's contact list.
114 $user = $this->getDataGenerator()->create_user();
115 $user2 = $this->getDataGenerator()->create_user();
117 // Add the user to the admin's contact list.
118 message_add_contact($user->id);
120 // Trigger and capture the event when blocking a contact.
121 $sink = $this->redirectEvents();
122 message_block_contact($user->id);
123 $events = $sink->get_events();
124 $event = reset($events);
126 // Check that the event data is valid.
127 $this->assertInstanceOf('\core\event\message_contact_blocked', $event);
128 $this->assertEquals(context_user::instance(2), $event->get_context());
129 $expected = array(SITEID, 'message', 'block contact', 'index.php?user1=' . $user->id . '&amp;user2=2', $user->id);
130 $this->assertEventLegacyLogData($expected, $event);
131 $url = new moodle_url('/message/index.php', array('user1' => $event->userid, 'user2' => $event->relateduserid));
132 $this->assertEquals($url, $event->get_url());
134 // Make sure that the contact blocked event is not triggered again.
135 $sink->clear();
136 message_block_contact($user->id);
137 $events = $sink->get_events();
138 $event = reset($events);
139 $this->assertEmpty($event);
140 // Make sure that we still have 1 blocked user.
141 $this->assertEquals(1, \core_message\api::count_blocked_users());
143 // Now blocking a user that is not a contact.
144 $sink->clear();
145 message_block_contact($user2->id);
146 $events = $sink->get_events();
147 $event = reset($events);
149 // Check that the event data is valid.
150 $this->assertInstanceOf('\core\event\message_contact_blocked', $event);
151 $this->assertEquals(context_user::instance(2), $event->get_context());
152 $expected = array(SITEID, 'message', 'block contact', 'index.php?user1=' . $user2->id . '&amp;user2=2', $user2->id);
153 $this->assertEventLegacyLogData($expected, $event);
154 $url = new moodle_url('/message/index.php', array('user1' => $event->userid, 'user2' => $event->relateduserid));
155 $this->assertEquals($url, $event->get_url());
159 * Test the message contact unblocked event.
161 public function test_message_contact_unblocked() {
162 // Set this user as the admin.
163 $this->setAdminUser();
165 // Create a user to add to the admin's contact list.
166 $user = $this->getDataGenerator()->create_user();
168 // Add the user to the admin's contact list.
169 message_add_contact($user->id);
171 // Block the user.
172 message_block_contact($user->id);
173 // Make sure that we have 1 blocked user.
174 $this->assertEquals(1, \core_message\api::count_blocked_users());
176 // Trigger and capture the event when unblocking a contact.
177 $sink = $this->redirectEvents();
178 message_unblock_contact($user->id);
179 $events = $sink->get_events();
180 $event = reset($events);
182 // Check that the event data is valid.
183 $this->assertInstanceOf('\core\event\message_contact_unblocked', $event);
184 $this->assertEquals(context_user::instance(2), $event->get_context());
185 $expected = array(SITEID, 'message', 'unblock contact', 'index.php?user1=' . $user->id . '&amp;user2=2', $user->id);
186 $this->assertEventLegacyLogData($expected, $event);
187 $url = new moodle_url('/message/index.php', array('user1' => $event->userid, 'user2' => $event->relateduserid));
188 $this->assertEquals($url, $event->get_url());
190 // Make sure that we have no blocked users.
191 $this->assertEmpty(\core_message\api::count_blocked_users());
193 // Make sure that the contact unblocked event is not triggered again.
194 $sink->clear();
195 message_unblock_contact($user->id);
196 $events = $sink->get_events();
197 $event = reset($events);
198 $this->assertEmpty($event);
200 // Make sure that we still have no blocked users.
201 $this->assertEmpty(\core_message\api::count_blocked_users());
205 * Test the message sent event.
207 * We can not use the message_send() function in the unit test to check that the event was fired as there is a
208 * conditional check to ensure a fake message is sent during unit tests when calling that particular function.
210 public function test_message_sent() {
211 $event = \core\event\message_sent::create(array(
212 'objectid' => 3,
213 'userid' => 1,
214 'context' => context_system::instance(),
215 'relateduserid' => 2,
216 'other' => array(
217 'courseid' => 4
221 // Trigger and capturing the event.
222 $sink = $this->redirectEvents();
223 $event->trigger();
224 $events = $sink->get_events();
225 $event = reset($events);
227 // Check that the event data is valid.
228 $this->assertInstanceOf('\core\event\message_sent', $event);
229 $this->assertEquals(context_system::instance(), $event->get_context());
230 $expected = array(SITEID, 'message', 'write', 'index.php?user=1&id=2&history=1#m3', 1);
231 $this->assertEventLegacyLogData($expected, $event);
232 $url = new moodle_url('/message/index.php', array('user1' => $event->userid, 'user2' => $event->relateduserid));
233 $this->assertEquals($url, $event->get_url());
234 $this->assertEquals(3, $event->objectid);
235 $this->assertEquals(4, $event->other['courseid']);
238 public function test_mesage_sent_without_other_courseid() {
240 // Creating a message_sent event without other[courseid] leads to exception.
241 $this->expectException('coding_exception');
242 $this->expectExceptionMessage('The \'courseid\' value must be set in other');
244 $event = \core\event\message_sent::create(array(
245 'userid' => 1,
246 'context' => context_system::instance(),
247 'relateduserid' => 2,
248 'other' => array(
249 'messageid' => 3,
254 public function test_mesage_sent_via_create_from_ids() {
255 // Containing courseid.
256 $event = \core\event\message_sent::create_from_ids(1, 2, 3, 4);
258 // Trigger and capturing the event.
259 $sink = $this->redirectEvents();
260 $event->trigger();
261 $events = $sink->get_events();
262 $event = reset($events);
264 // Check that the event data is valid.
265 $this->assertInstanceOf('\core\event\message_sent', $event);
266 $this->assertEquals(context_system::instance(), $event->get_context());
267 $expected = array(SITEID, 'message', 'write', 'index.php?user=1&id=2&history=1#m3', 1);
268 $this->assertEventLegacyLogData($expected, $event);
269 $url = new moodle_url('/message/index.php', array('user1' => $event->userid, 'user2' => $event->relateduserid));
270 $this->assertEquals($url, $event->get_url());
271 $this->assertEquals(3, $event->objectid);
272 $this->assertEquals(4, $event->other['courseid']);
275 public function test_mesage_sent_via_create_from_ids_without_other_courseid() {
277 // Creating a message_sent event without courseid leads to debugging + SITEID.
278 // TODO: MDL-55449 Ensure this leads to exception instead of debugging in Moodle 3.6.
279 $event = \core\event\message_sent::create_from_ids(1, 2, 3);
281 // Trigger and capturing the event.
282 $sink = $this->redirectEvents();
283 $event->trigger();
284 $events = $sink->get_events();
285 $event = reset($events);
287 $this->assertDebuggingCalled();
288 $this->assertEquals(SITEID, $event->other['courseid']);
292 * Test the message viewed event.
294 public function test_message_viewed() {
295 global $DB;
297 // Create users to send messages between.
298 $user1 = $this->getDataGenerator()->create_user();
299 $user2 = $this->getDataGenerator()->create_user();
301 $messageid = $this->send_fake_message($user1, $user2);
303 // Trigger and capture the event.
304 $sink = $this->redirectEvents();
305 $message = $DB->get_record('messages', ['id' => $messageid]);
306 \core_message\api::mark_message_as_read($user1->id, $message);
307 $events = $sink->get_events();
308 $event = reset($events);
310 // Get the usage action.
311 $mua = $DB->get_record('message_user_actions', ['userid' => $user1->id, 'messageid' => $messageid,
312 'action' => \core_message\api::MESSAGE_ACTION_READ]);
314 // Check that the event data is valid.
315 $this->assertInstanceOf('\core\event\message_viewed', $event);
316 $this->assertEquals(context_user::instance($user1->id), $event->get_context());
317 $this->assertEquals($mua->id, $event->objectid);
318 $this->assertEquals($messageid, $event->other['messageid']);
319 $url = new moodle_url('/message/index.php', array('user1' => $event->userid, 'user2' => $event->relateduserid));
320 $this->assertEquals($url, $event->get_url());
324 * Test the message deleted event.
326 public function test_message_deleted() {
327 global $DB;
329 // Create users to send messages between.
330 $user1 = $this->getDataGenerator()->create_user();
331 $user2 = $this->getDataGenerator()->create_user();
333 $messageid = $this->send_fake_message($user1, $user2);
335 // Trigger and capture the event.
336 $sink = $this->redirectEvents();
337 \core_message\api::delete_message($user1->id, $messageid);
338 $events = $sink->get_events();
339 $event = reset($events);
341 // Get the usage action.
342 $mua = $DB->get_record('message_user_actions', ['userid' => $user1->id, 'messageid' => $messageid,
343 'action' => \core_message\api::MESSAGE_ACTION_DELETED]);
345 // Check that the event data is valid.
346 $this->assertInstanceOf('\core\event\message_deleted', $event);
347 $this->assertEquals($user1->id, $event->userid); // The user who deleted it.
348 $this->assertEquals($user2->id, $event->relateduserid);
349 $this->assertEquals($mua->id, $event->objectid);
350 $this->assertEquals($messageid, $event->other['messageid']);
351 $this->assertEquals($user1->id, $event->other['useridfrom']);
352 $this->assertEquals($user2->id, $event->other['useridto']);
354 // Create a read message.
355 $messageid = $this->send_fake_message($user1, $user2);
356 $m = $DB->get_record('messages', ['id' => $messageid]);
357 \core_message\api::mark_message_as_read($user2->id, $m);
359 // Trigger and capture the event.
360 $sink = $this->redirectEvents();
361 \core_message\api::delete_message($user2->id, $messageid);
362 $events = $sink->get_events();
363 $event = reset($events);
365 // Get the usage action.
366 $mua = $DB->get_record('message_user_actions', ['userid' => $user2->id, 'messageid' => $messageid,
367 'action' => \core_message\api::MESSAGE_ACTION_DELETED]);
369 // Check that the event data is valid.
370 $this->assertInstanceOf('\core\event\message_deleted', $event);
371 $this->assertEquals($user2->id, $event->userid);
372 $this->assertEquals($user1->id, $event->relateduserid);
373 $this->assertEquals($mua->id, $event->objectid);
374 $this->assertEquals($messageid, $event->other['messageid']);
375 $this->assertEquals($user1->id, $event->other['useridfrom']);
376 $this->assertEquals($user2->id, $event->other['useridto']);
380 * Test the message deleted event is fired when deleting a conversation.
382 public function test_message_deleted_whole_conversation() {
383 global $DB;
385 // Create some users.
386 $user1 = self::getDataGenerator()->create_user();
387 $user2 = self::getDataGenerator()->create_user();
389 // The person doing the search.
390 $this->setUser($user1);
392 // Send some messages back and forth.
393 $time = 1;
394 $messages = [];
395 $messages[] = $this->send_fake_message($user1, $user2, 'Yo!', 0, $time + 1);
396 $messages[] = $this->send_fake_message($user2, $user1, 'Sup mang?', 0, $time + 2);
397 $messages[] = $this->send_fake_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 3);
398 $messages[] = $this->send_fake_message($user2, $user1, 'Word.', 0, $time + 4);
399 $messages[] = $this->send_fake_message($user1, $user2, 'You doing much?', 0, $time + 5);
400 $messages[] = $this->send_fake_message($user2, $user1, 'Nah', 0, $time + 6);
401 $messages[] = $this->send_fake_message($user1, $user2, 'You nubz0r!', 0, $time + 7);
402 $messages[] = $this->send_fake_message($user2, $user1, 'Ouch.', 0, $time + 8);
404 // Mark the last 4 messages as read.
405 $m5 = $DB->get_record('messages', ['id' => $messages[4]]);
406 $m6 = $DB->get_record('messages', ['id' => $messages[5]]);
407 $m7 = $DB->get_record('messages', ['id' => $messages[6]]);
408 $m8 = $DB->get_record('messages', ['id' => $messages[7]]);
409 \core_message\api::mark_message_as_read($user2->id, $m5);
410 \core_message\api::mark_message_as_read($user1->id, $m6);
411 \core_message\api::mark_message_as_read($user2->id, $m7);
412 \core_message\api::mark_message_as_read($user1->id, $m8);
414 // Trigger and capture the event.
415 $sink = $this->redirectEvents();
416 \core_message\api::delete_conversation($user1->id, $user2->id);
417 $events = $sink->get_events();
419 // Get the user actions for the messages deleted by that user.
420 $muas = $DB->get_records('message_user_actions', ['userid' => $user1->id,
421 'action' => \core_message\api::MESSAGE_ACTION_DELETED], 'timecreated ASC');
422 $this->assertCount(8, $muas);
424 // Create a list we can use for testing.
425 $muatest = [];
426 foreach ($muas as $mua) {
427 $muatest[$mua->messageid] = $mua;
430 // Check that there were the correct number of events triggered.
431 $this->assertEquals(8, count($events));
433 // Check that the event data is valid.
434 $i = 1;
435 foreach ($events as $event) {
436 $useridfromid = ($i % 2 == 0) ? $user2->id : $user1->id;
437 $useridtoid = ($i % 2 == 0) ? $user1->id : $user2->id;
438 $messageid = $messages[$i - 1];
440 $this->assertInstanceOf('\core\event\message_deleted', $event);
442 $this->assertEquals($muatest[$messageid]->id, $event->objectid);
443 $this->assertEquals($user1->id, $event->userid);
444 $this->assertEquals($user2->id, $event->relateduserid);
445 $this->assertEquals($messageid, $event->other['messageid']);
446 $this->assertEquals($useridfromid, $event->other['useridfrom']);
447 $this->assertEquals($useridtoid, $event->other['useridto']);
449 $i++;
454 * Test the notification sent event.
456 public function test_notification_sent() {
457 // Create a course.
458 $course = $this->getDataGenerator()->create_course();
460 // Create users to send notification between.
461 $user1 = $this->getDataGenerator()->create_user();
462 $user2 = $this->getDataGenerator()->create_user();
464 // Send a notification.
465 $notificationid = $this->send_fake_message($user1, $user2, 'Hello world!', 1);
467 // Containing courseid.
468 $event = \core\event\notification_sent::create_from_ids($user1->id, $user2->id, $notificationid, $course->id);
470 // Trigger and capturing the event.
471 $sink = $this->redirectEvents();
472 $event->trigger();
473 $events = $sink->get_events();
474 $event = reset($events);
476 // Check that the event data is valid.
477 $this->assertInstanceOf('\core\event\notification_sent', $event);
478 $this->assertEquals($notificationid, $event->objectid);
479 $this->assertEquals($user1->id, $event->userid);
480 $this->assertEquals($user2->id, $event->relateduserid);
481 $this->assertEquals(context_system::instance(), $event->get_context());
482 $this->assertEquals($course->id, $event->other['courseid']);
483 $url = new moodle_url('/message/output/popup/notifications.php', array('notificationid' => $event->objectid));
484 $this->assertEquals($url, $event->get_url());
488 * Test the notification sent event when null passed as course.
490 public function test_notification_sent_with_null_course() {
491 $event = \core\event\notification_sent::create_from_ids(1, 1, 1, null);
493 // Trigger and capture the event.
494 $sink = $this->redirectEvents();
495 $event->trigger();
496 $events = $sink->get_events();
497 $event = reset($events);
499 // Check that the event data is valid.
500 $this->assertInstanceOf('\core\event\notification_sent', $event);
501 $this->assertEquals(SITEID, $event->other['courseid']);
505 * Test the notification viewed event.
507 public function test_notification_viewed() {
508 global $DB;
510 // Create users to send notifications between.
511 $user1 = $this->getDataGenerator()->create_user();
512 $user2 = $this->getDataGenerator()->create_user();
514 // Send a notification.
515 $notificationid = $this->send_fake_message($user1, $user2, 'Hello world!', 1);
517 // Trigger and capture the event.
518 $sink = $this->redirectEvents();
519 $notification = $DB->get_record('notifications', ['id' => $notificationid]);
520 \core_message\api::mark_notification_as_read($notification);
521 $events = $sink->get_events();
522 $event = reset($events);
524 // Check that the event data is valid.
525 $this->assertInstanceOf('\core\event\notification_viewed', $event);
526 $this->assertEquals($notificationid, $event->objectid);
527 $this->assertEquals($user2->id, $event->userid);
528 $this->assertEquals($user1->id, $event->relateduserid);
529 $this->assertEquals(context_user::instance($user2->id), $event->get_context());
530 $url = new moodle_url('/message/output/popup/notifications.php', array('notificationid' => $event->objectid));
531 $this->assertEquals($url, $event->get_url());