2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
21 * @copyright 2013 Frédéric Massart
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') ||
die();
28 require_once($CFG->dirroot
. '/mod/chat/lib.php');
34 * @copyright 2013 Frédéric Massart
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37 class mod_chat_events_testcase
extends advanced_testcase
{
39 public function test_message_sent() {
41 $this->resetAfterTest();
43 $this->setAdminUser();
44 $course = $this->getDataGenerator()->create_course();
45 $user1 = $this->getDataGenerator()->create_user();
46 $user2 = $this->getDataGenerator()->create_user();
47 $chat = $this->getDataGenerator()->create_module('chat', array('course' => $course->id
));
48 $cm = $DB->get_record('course_modules', array('id' => $chat->cmid
));
50 // Logging in first user to the chat.
51 $this->setUser($user1->id
);
52 $sid1 = chat_login_user($chat->id
, 'ajax', 0, $course);
54 // Logging in second user to the chat.
55 $this->setUser($user2->id
);
56 $sid2 = chat_login_user($chat->id
, 'ajax', 0, $course);
58 // Getting the chatuser record.
59 $chatuser1 = $DB->get_record('chat_users', array('sid' => $sid1));
60 $chatuser2 = $DB->get_record('chat_users', array('sid' => $sid2));
62 $sink = $this->redirectEvents();
64 // Send a messaging from the first user. We pass the CM to chat_send_chatmessage() this time.
65 // This ensures that the event triggered when sending a message is filled with the correct information.
66 $this->setUser($user1->id
);
67 $messageid = chat_send_chatmessage($chatuser1, 'Hello!', false, $cm);
68 $events = $sink->get_events();
69 $this->assertCount(1, $events);
70 $event = reset($events);
71 $this->assertInstanceOf('\mod_chat\event\message_sent', $event);
72 $this->assertEquals($messageid, $event->objectid
);
73 $this->assertEquals($user1->id
, $event->relateduserid
);
74 $this->assertEquals($user1->id
, $event->userid
);
75 $expected = array($course->id
, 'chat', 'talk', "view.php?id=$cm->id", $chat->id
, $cm->id
, $user1->id
);
76 $this->assertEventLegacyLogData($expected, $event);
78 // Send a messaging from the first user. We DO NOT pass the CM to chat_send_chatmessage() this time.
79 // This ensures that the event triggered when sending a message is filled with the correct information.
81 $this->setUser($user2->id
);
82 $messageid = chat_send_chatmessage($chatuser2, 'Hello!');
83 $events = $sink->get_events();
84 $this->assertCount(1, $events);
85 $event = reset($events);
86 $this->assertInstanceOf('\mod_chat\event\message_sent', $event);
87 $this->assertEquals($messageid, $event->objectid
);
88 $this->assertEquals($user2->id
, $event->relateduserid
);
89 $this->assertEquals($user2->id
, $event->userid
);
90 $expected = array($course->id
, 'chat', 'talk', "view.php?id=$cm->id", $chat->id
, $cm->id
, $user2->id
);
91 $this->assertEventLegacyLogData($expected, $event);
92 $this->assertEventContextNotUsed($event);
94 // Sending a message from the system should not trigger any event.
96 $this->setAdminUser();
97 chat_send_chatmessage($chatuser1, 'enter', true);
98 $this->assertEquals(0, $sink->count());
103 public function test_sessions_viewed() {
105 $this->resetAfterTest();
107 // Not much can be tested here as the event is only triggered on a page load,
108 // let's just check that the event contains the expected basic information.
109 $this->setAdminUser();
110 $course = $this->getDataGenerator()->create_course();
111 $chat = $this->getDataGenerator()->create_module('chat', array('course' => $course->id
));
114 'context' => context_module
::instance($chat->cmid
),
115 'objectid' => $chat->id
,
121 $event = \mod_chat\event\sessions_viewed
::create($params);
122 $event->add_record_snapshot('chat', $chat);
123 $sink = $this->redirectEvents();
125 $events = $sink->get_events();
126 $event = reset($events);
127 $this->assertInstanceOf('\mod_chat\event\sessions_viewed', $event);
128 $this->assertEquals($USER->id
, $event->userid
);
129 $this->assertEquals(context_module
::instance($chat->cmid
), $event->get_context());
130 $this->assertEquals(1234, $event->other
['start']);
131 $this->assertEquals(5678, $event->other
['end']);
132 $this->assertEquals($chat->id
, $event->objectid
);
133 $this->assertEquals($chat, $event->get_record_snapshot('chat', $chat->id
));
134 $expected = array($course->id
, 'chat', 'report', "report.php?id=$chat->cmid", $chat->id
, $chat->cmid
);
135 $this->assertEventLegacyLogData($expected, $event);
136 $this->assertEventContextNotUsed($event);
139 public function test_course_module_instance_list_viewed() {
141 $this->resetAfterTest();
143 // Not much can be tested here as the event is only triggered on a page load,
144 // let's just check that the event contains the expected basic information.
145 $this->setAdminUser();
146 $course = $this->getDataGenerator()->create_course();
149 'context' => context_course
::instance($course->id
)
151 $event = \mod_chat\event\course_module_instance_list_viewed
::create($params);
152 $sink = $this->redirectEvents();
154 $events = $sink->get_events();
155 $event = reset($events);
156 $this->assertInstanceOf('\mod_chat\event\course_module_instance_list_viewed', $event);
157 $this->assertEquals($USER->id
, $event->userid
);
158 $this->assertEquals(context_course
::instance($course->id
), $event->get_context());
159 $expected = array($course->id
, 'chat', 'view all', "index.php?id=$course->id", '');
160 $this->assertEventLegacyLogData($expected, $event);
161 $this->assertEventContextNotUsed($event);
164 public function test_course_module_viewed() {
165 $this->resetAfterTest();
166 $this->setAdminUser();
167 $course = $this->getDataGenerator()->create_course();
168 $chat = $this->getDataGenerator()->create_module('chat', array('course' => $course->id
));
169 $cm = get_coursemodule_from_instance('chat', $chat->id
);
170 $context = context_module
::instance($cm->id
);
173 'objectid' => $chat->id
,
174 'context' => $context
176 $event = \mod_chat\event\course_module_viewed
::create($params);
177 $event->add_record_snapshot('chat', $chat);
180 $expected = array($course->id
, 'chat', 'view', "view.php?id=$cm->id", $chat->id
, $cm->id
);
181 $this->assertEventLegacyLogData($expected, $event);
182 $this->assertEventContextNotUsed($event);
183 $url = new moodle_url('/mod/chat/view.php', array('id' => $cm->id
));
184 $this->assertEquals($url, $event->get_url());