MDL-67296 core_h5p: minor changes
[moodle.git] / message / tests / events_test.php
blob8960bed680b4f987ff9a06f3f498e7666ed9b375
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 global $USER;
57 // Set this user as the admin.
58 $this->setAdminUser();
60 // Create a user to add to the admin's contact list.
61 $user = $this->getDataGenerator()->create_user();
63 // Trigger and capture the event when adding a contact.
64 $sink = $this->redirectEvents();
65 \core_message\api::add_contact($USER->id, $user->id);
66 $events = $sink->get_events();
67 $event = reset($events);
69 // Check that the event data is valid.
70 $this->assertInstanceOf('\core\event\message_contact_added', $event);
71 $this->assertEquals(context_user::instance(2), $event->get_context());
72 $expected = array(SITEID, 'message', 'add contact', 'index.php?user1=' . $user->id .
73 '&amp;user2=2', $user->id);
74 $this->assertEventLegacyLogData($expected, $event);
75 $url = new moodle_url('/message/index.php', array('user1' => $event->userid, 'user2' => $event->relateduserid));
76 $this->assertEquals($url, $event->get_url());
79 /**
80 * Test the message contact removed event.
82 public function test_message_contact_removed() {
83 global $USER;
85 // Set this user as the admin.
86 $this->setAdminUser();
88 // Create a user to add to the admin's contact list.
89 $user = $this->getDataGenerator()->create_user();
91 // Add the user to the admin's contact list.
92 \core_message\api::add_contact($USER->id, $user->id);
94 // Trigger and capture the event when adding a contact.
95 $sink = $this->redirectEvents();
96 \core_message\api::remove_contact($USER->id, $user->id);
97 $events = $sink->get_events();
98 $event = reset($events);
100 // Check that the event data is valid.
101 $this->assertInstanceOf('\core\event\message_contact_removed', $event);
102 $this->assertEquals(context_user::instance(2), $event->get_context());
103 $expected = array(SITEID, 'message', 'remove contact', 'index.php?user1=' . $user->id .
104 '&amp;user2=2', $user->id);
105 $this->assertEventLegacyLogData($expected, $event);
106 $url = new moodle_url('/message/index.php', array('user1' => $event->userid, 'user2' => $event->relateduserid));
107 $this->assertEquals($url, $event->get_url());
111 * Test the message user blocked event.
113 public function test_message_user_blocked() {
114 global $USER;
116 // Set this user as the admin.
117 $this->setAdminUser();
119 // Create a user to add to the admin's contact list.
120 $user = $this->getDataGenerator()->create_user();
122 // Add the user to the admin's contact list.
123 \core_message\api::add_contact($USER->id, $user->id);
125 // Trigger and capture the event when blocking a contact.
126 $sink = $this->redirectEvents();
127 \core_message\api::block_user($USER->id, $user->id);
128 $events = $sink->get_events();
129 $event = reset($events);
131 // Check that the event data is valid.
132 $this->assertInstanceOf('\core\event\message_user_blocked', $event);
133 $this->assertEquals(context_user::instance(2), $event->get_context());
137 * Test the message user unblocked event.
139 public function test_message_user_unblocked() {
140 global $USER;
142 // Set this user as the admin.
143 $this->setAdminUser();
145 // Create a user to add to the admin's contact list.
146 $user = $this->getDataGenerator()->create_user();
148 // Add the user to the admin's contact list.
149 \core_message\api::add_contact($USER->id, $user->id);
151 // Block the user.
152 \core_message\api::block_user($USER->id, $user->id);
153 // Make sure that we have 1 blocked user.
154 $this->assertEquals(1, \core_message\api::count_blocked_users());
156 // Trigger and capture the event when unblocking a contact.
157 $sink = $this->redirectEvents();
158 \core_message\api::unblock_user($USER->id, $user->id);
159 $events = $sink->get_events();
160 $event = reset($events);
162 // Check that the event data is valid.
163 $this->assertInstanceOf('\core\event\message_user_unblocked', $event);
164 $this->assertEquals(context_user::instance(2), $event->get_context());
166 // Make sure that we have no blocked users.
167 $this->assertEmpty(\core_message\api::count_blocked_users());
171 * Test the message sent event.
173 * We can not use the message_send() function in the unit test to check that the event was fired as there is a
174 * conditional check to ensure a fake message is sent during unit tests when calling that particular function.
176 public function test_message_sent() {
177 $event = \core\event\message_sent::create(array(
178 'objectid' => 3,
179 'userid' => 1,
180 'context' => context_system::instance(),
181 'relateduserid' => 2,
182 'other' => array(
183 'courseid' => 4
187 // Trigger and capturing the event.
188 $sink = $this->redirectEvents();
189 $event->trigger();
190 $events = $sink->get_events();
191 $event = reset($events);
193 // Check that the event data is valid.
194 $this->assertInstanceOf('\core\event\message_sent', $event);
195 $this->assertEquals(context_system::instance(), $event->get_context());
196 $expected = array(SITEID, 'message', 'write', 'index.php?user=1&id=2&history=1#m3', 1);
197 $this->assertEventLegacyLogData($expected, $event);
198 $url = new moodle_url('/message/index.php', array('user1' => $event->userid, 'user2' => $event->relateduserid));
199 $this->assertEquals($url, $event->get_url());
200 $this->assertEquals(3, $event->objectid);
201 $this->assertEquals(4, $event->other['courseid']);
204 public function test_mesage_sent_without_other_courseid() {
206 // Creating a message_sent event without other[courseid] leads to exception.
207 $this->expectException('coding_exception');
208 $this->expectExceptionMessage('The \'courseid\' value must be set in other');
210 $event = \core\event\message_sent::create(array(
211 'userid' => 1,
212 'context' => context_system::instance(),
213 'relateduserid' => 2,
214 'other' => array(
215 'messageid' => 3,
220 public function test_mesage_sent_via_create_from_ids() {
221 // Containing courseid.
222 $event = \core\event\message_sent::create_from_ids(1, 2, 3, 4);
224 // Trigger and capturing the event.
225 $sink = $this->redirectEvents();
226 $event->trigger();
227 $events = $sink->get_events();
228 $event = reset($events);
230 // Check that the event data is valid.
231 $this->assertInstanceOf('\core\event\message_sent', $event);
232 $this->assertEquals(context_system::instance(), $event->get_context());
233 $expected = array(SITEID, 'message', 'write', 'index.php?user=1&id=2&history=1#m3', 1);
234 $this->assertEventLegacyLogData($expected, $event);
235 $url = new moodle_url('/message/index.php', array('user1' => $event->userid, 'user2' => $event->relateduserid));
236 $this->assertEquals($url, $event->get_url());
237 $this->assertEquals(3, $event->objectid);
238 $this->assertEquals(4, $event->other['courseid']);
242 * Test the group message sent event.
244 * We can't test events in any testing of the message_send() function as there is a conditional PHPUNIT check in message_send,
245 * resulting in fake messages being generated and captured under test. As a result, none of the events code, nor message
246 * processor code is called during testing.
248 public function test_group_message_sent() {
249 $event = \core\event\group_message_sent::create([
250 'objectid' => 3,
251 'userid' => 1,
252 'context' => context_system::instance(),
253 'other' => [
254 'courseid' => 4,
255 'conversationid' => 54
259 // Trigger and capture the event.
260 $sink = $this->redirectEvents();
261 $event->trigger();
262 $events = $sink->get_events();
263 $event = reset($events);
265 // Check that the event data is valid.
266 $this->assertInstanceOf('\core\event\group_message_sent', $event);
267 $this->assertEquals(context_system::instance(), $event->get_context());
268 $url = new moodle_url('/message/index.php');
269 $this->assertEquals($url, $event->get_url());
270 $this->assertEquals(3, $event->objectid);
271 $this->assertEquals(4, $event->other['courseid']);
272 $this->assertEquals(54, $event->other['conversationid']);
276 * Test the group message sent event when created without a courseid.
278 public function test_group_message_sent_without_other_courseid() {
279 // Creating a message_sent event without other[courseid] leads to exception.
280 $this->expectException('coding_exception');
281 $this->expectExceptionMessage('The \'courseid\' value must be set in other');
283 $event = \core\event\group_message_sent::create([
284 'userid' => 1,
285 'objectid' => 3,
286 'context' => context_system::instance(),
287 'relateduserid' => 2,
288 'other' => [
289 'conversationid' => 34
295 * Test the group message sent event when created without a conversationid.
297 public function test_group_message_sent_without_other_conversationid() {
298 // Creating a message_sent event without other[courseid] leads to exception.
299 $this->expectException('coding_exception');
300 $this->expectExceptionMessage('The \'conversationid\' value must be set in other');
302 $event = \core\event\group_message_sent::create([
303 'userid' => 1,
304 'objectid' => 3,
305 'context' => context_system::instance(),
306 'relateduserid' => 2,
307 'other' => [
308 'courseid' => 44,
314 * Test the group message sent event using the create_from_ids() method.
316 public function test_group_message_sent_via_create_from_ids() {
317 // Fields are: userfromid, conversationid, messageid, courseid.
318 $event = \core\event\group_message_sent::create_from_ids(1, 2, 3, 4);
320 // Trigger and capture the event.
321 $sink = $this->redirectEvents();
322 $event->trigger();
323 $events = $sink->get_events();
324 $event = reset($events);
326 // Check that the event data is valid.
327 $this->assertInstanceOf('\core\event\group_message_sent', $event);
328 $this->assertEquals(context_system::instance(), $event->get_context());
329 $this->assertEquals(new moodle_url('/message/index.php'), $event->get_url());
330 $this->assertEquals(1, $event->userid);
331 $this->assertEquals(2, $event->other['conversationid']);
332 $this->assertEquals(3, $event->objectid);
333 $this->assertEquals(4, $event->other['courseid']);
337 * Test the message viewed event.
339 public function test_message_viewed() {
340 global $DB;
342 // Create users to send messages between.
343 $user1 = $this->getDataGenerator()->create_user();
344 $user2 = $this->getDataGenerator()->create_user();
346 $messageid = $this->send_fake_message($user1, $user2);
348 // Trigger and capture the event.
349 $sink = $this->redirectEvents();
350 $message = $DB->get_record('messages', ['id' => $messageid]);
351 \core_message\api::mark_message_as_read($user1->id, $message);
352 $events = $sink->get_events();
353 $event = reset($events);
355 // Get the usage action.
356 $mua = $DB->get_record('message_user_actions', ['userid' => $user1->id, 'messageid' => $messageid,
357 'action' => \core_message\api::MESSAGE_ACTION_READ]);
359 // Check that the event data is valid.
360 $this->assertInstanceOf('\core\event\message_viewed', $event);
361 $this->assertEquals(context_user::instance($user1->id), $event->get_context());
362 $this->assertEquals($mua->id, $event->objectid);
363 $this->assertEquals($messageid, $event->other['messageid']);
364 $url = new moodle_url('/message/index.php', array('user1' => $event->userid, 'user2' => $event->relateduserid));
365 $this->assertEquals($url, $event->get_url());
369 * Test the message deleted event.
371 public function test_message_deleted() {
372 global $DB, $USER;
374 $this->setAdminUser();
376 // Create users to send messages between.
377 $user1 = $this->getDataGenerator()->create_user();
378 $user2 = $this->getDataGenerator()->create_user();
380 $messageid = $this->send_fake_message($user1, $user2);
382 // Trigger and capture the event.
383 $sink = $this->redirectEvents();
384 \core_message\api::delete_message($user1->id, $messageid);
385 $events = $sink->get_events();
386 $event = reset($events);
388 // Get the usage action.
389 $mua = $DB->get_record('message_user_actions', ['userid' => $user1->id, 'messageid' => $messageid,
390 'action' => \core_message\api::MESSAGE_ACTION_DELETED]);
392 // Check that the event data is valid.
393 $this->assertInstanceOf('\core\event\message_deleted', $event);
394 $this->assertEquals($USER->id, $event->userid); // The user who deleted it.
395 $this->assertEquals($user1->id, $event->relateduserid);
396 $this->assertEquals($mua->id, $event->objectid);
397 $this->assertEquals($messageid, $event->other['messageid']);
399 $this->setUser($user1);
401 // Create a read message.
402 $messageid = $this->send_fake_message($user1, $user2);
403 $m = $DB->get_record('messages', ['id' => $messageid]);
404 \core_message\api::mark_message_as_read($user2->id, $m);
406 // Trigger and capture the event.
407 $sink = $this->redirectEvents();
408 \core_message\api::delete_message($user2->id, $messageid);
409 $events = $sink->get_events();
410 $event = reset($events);
412 // Get the usage action.
413 $mua = $DB->get_record('message_user_actions', ['userid' => $user2->id, 'messageid' => $messageid,
414 'action' => \core_message\api::MESSAGE_ACTION_DELETED]);
416 // Check that the event data is valid.
417 $this->assertInstanceOf('\core\event\message_deleted', $event);
418 $this->assertEquals($user1->id, $event->userid);
419 $this->assertEquals($user2->id, $event->relateduserid);
420 $this->assertEquals($mua->id, $event->objectid);
421 $this->assertEquals($messageid, $event->other['messageid']);
425 * Test the message deleted event is fired when deleting a conversation.
427 public function test_message_deleted_whole_conversation() {
428 global $DB;
430 // Create some users.
431 $user1 = self::getDataGenerator()->create_user();
432 $user2 = self::getDataGenerator()->create_user();
434 // The person doing the deletion.
435 $this->setUser($user1);
437 // Send some messages back and forth.
438 $time = 1;
439 $messages = [];
440 $messages[] = $this->send_fake_message($user1, $user2, 'Yo!', 0, $time + 1);
441 $messages[] = $this->send_fake_message($user2, $user1, 'Sup mang?', 0, $time + 2);
442 $messages[] = $this->send_fake_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 3);
443 $messages[] = $this->send_fake_message($user2, $user1, 'Word.', 0, $time + 4);
444 $messages[] = $this->send_fake_message($user1, $user2, 'You doing much?', 0, $time + 5);
445 $messages[] = $this->send_fake_message($user2, $user1, 'Nah', 0, $time + 6);
446 $messages[] = $this->send_fake_message($user1, $user2, 'You nubz0r!', 0, $time + 7);
447 $messages[] = $this->send_fake_message($user2, $user1, 'Ouch.', 0, $time + 8);
449 // Mark the last 4 messages as read.
450 $m5 = $DB->get_record('messages', ['id' => $messages[4]]);
451 $m6 = $DB->get_record('messages', ['id' => $messages[5]]);
452 $m7 = $DB->get_record('messages', ['id' => $messages[6]]);
453 $m8 = $DB->get_record('messages', ['id' => $messages[7]]);
454 \core_message\api::mark_message_as_read($user2->id, $m5);
455 \core_message\api::mark_message_as_read($user1->id, $m6);
456 \core_message\api::mark_message_as_read($user2->id, $m7);
457 \core_message\api::mark_message_as_read($user1->id, $m8);
459 // Trigger and capture the event.
460 $sink = $this->redirectEvents();
461 \core_message\api::delete_conversation($user1->id, $user2->id);
462 $this->assertDebuggingCalled();
463 $events = $sink->get_events();
465 // Get the user actions for the messages deleted by that user.
466 $muas = $DB->get_records('message_user_actions', ['userid' => $user1->id,
467 'action' => \core_message\api::MESSAGE_ACTION_DELETED], 'timecreated ASC');
468 $this->assertCount(8, $muas);
470 // Create a list we can use for testing.
471 $muatest = [];
472 foreach ($muas as $mua) {
473 $muatest[$mua->messageid] = $mua;
476 // Check that there were the correct number of events triggered.
477 $this->assertEquals(8, count($events));
479 // Check that the event data is valid.
480 $i = 1;
481 foreach ($events as $event) {
482 $messageid = $messages[$i - 1];
484 $this->assertInstanceOf('\core\event\message_deleted', $event);
486 $this->assertEquals($muatest[$messageid]->id, $event->objectid);
487 $this->assertEquals($user1->id, $event->userid);
488 $this->assertEquals($user1->id, $event->relateduserid);
489 $this->assertEquals($messageid, $event->other['messageid']);
491 $i++;
496 * Test the notification sent event.
498 public function test_notification_sent() {
499 // Create a course.
500 $course = $this->getDataGenerator()->create_course();
502 // Create users to send notification between.
503 $user1 = $this->getDataGenerator()->create_user();
504 $user2 = $this->getDataGenerator()->create_user();
506 // Send a notification.
507 $notificationid = $this->send_fake_message($user1, $user2, 'Hello world!', 1);
509 // Containing courseid.
510 $event = \core\event\notification_sent::create_from_ids($user1->id, $user2->id, $notificationid, $course->id);
512 // Trigger and capturing the event.
513 $sink = $this->redirectEvents();
514 $event->trigger();
515 $events = $sink->get_events();
516 $event = reset($events);
518 // Check that the event data is valid.
519 $this->assertInstanceOf('\core\event\notification_sent', $event);
520 $this->assertEquals($notificationid, $event->objectid);
521 $this->assertEquals($user1->id, $event->userid);
522 $this->assertEquals($user2->id, $event->relateduserid);
523 $this->assertEquals(context_system::instance(), $event->get_context());
524 $this->assertEquals($course->id, $event->other['courseid']);
525 $url = new moodle_url('/message/output/popup/notifications.php', array('notificationid' => $event->objectid));
526 $this->assertEquals($url, $event->get_url());
530 * Test the notification sent event when null passed as course.
532 public function test_notification_sent_with_null_course() {
533 $event = \core\event\notification_sent::create_from_ids(1, 1, 1, null);
535 // Trigger and capture the event.
536 $sink = $this->redirectEvents();
537 $event->trigger();
538 $events = $sink->get_events();
539 $event = reset($events);
541 // Check that the event data is valid.
542 $this->assertInstanceOf('\core\event\notification_sent', $event);
543 $this->assertEquals(SITEID, $event->other['courseid']);
547 * Test the notification viewed event.
549 public function test_notification_viewed() {
550 global $DB;
552 // Create users to send notifications between.
553 $user1 = $this->getDataGenerator()->create_user();
554 $user2 = $this->getDataGenerator()->create_user();
556 // Send a notification.
557 $notificationid = $this->send_fake_message($user1, $user2, 'Hello world!', 1);
559 // Trigger and capture the event.
560 $sink = $this->redirectEvents();
561 $notification = $DB->get_record('notifications', ['id' => $notificationid]);
562 \core_message\api::mark_notification_as_read($notification);
563 $events = $sink->get_events();
564 $event = reset($events);
566 // Check that the event data is valid.
567 $this->assertInstanceOf('\core\event\notification_viewed', $event);
568 $this->assertEquals($notificationid, $event->objectid);
569 $this->assertEquals($user2->id, $event->userid);
570 $this->assertEquals($user1->id, $event->relateduserid);
571 $this->assertEquals(context_user::instance($user2->id), $event->get_context());
572 $url = new moodle_url('/message/output/popup/notifications.php', array('notificationid' => $event->objectid));
573 $this->assertEquals($url, $event->get_url());