1 |
efrain |
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 mod_chat
|
|
|
21 |
* @copyright 2013 Frédéric Massart
|
|
|
22 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
23 |
*/
|
|
|
24 |
|
|
|
25 |
namespace mod_chat\event;
|
|
|
26 |
|
|
|
27 |
defined('MOODLE_INTERNAL') || die();
|
|
|
28 |
|
|
|
29 |
global $CFG;
|
|
|
30 |
require_once($CFG->dirroot . '/mod/chat/lib.php');
|
|
|
31 |
|
|
|
32 |
/**
|
|
|
33 |
* Events tests class.
|
|
|
34 |
*
|
|
|
35 |
* @package mod_chat
|
|
|
36 |
* @copyright 2013 Frédéric Massart
|
|
|
37 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
38 |
*/
|
|
|
39 |
class events_test extends \advanced_testcase {
|
|
|
40 |
|
|
|
41 |
/**
|
|
|
42 |
* Setup testcase.
|
|
|
43 |
*/
|
|
|
44 |
public function setUp(): void {
|
|
|
45 |
// Chat module is disabled by default, enable it for testing.
|
|
|
46 |
$manager = \core_plugin_manager::resolve_plugininfo_class('mod');
|
|
|
47 |
$manager::enable_plugin('chat', 1);
|
|
|
48 |
}
|
|
|
49 |
|
|
|
50 |
public function test_message_sent() {
|
|
|
51 |
global $DB;
|
|
|
52 |
$this->resetAfterTest();
|
|
|
53 |
|
|
|
54 |
$this->setAdminUser();
|
|
|
55 |
$course = $this->getDataGenerator()->create_course();
|
|
|
56 |
$user1 = $this->getDataGenerator()->create_user();
|
|
|
57 |
$user2 = $this->getDataGenerator()->create_user();
|
|
|
58 |
$chat = $this->getDataGenerator()->create_module('chat', array('course' => $course->id));
|
|
|
59 |
$cm = $DB->get_record('course_modules', array('id' => $chat->cmid));
|
|
|
60 |
|
|
|
61 |
// Logging in first user to the chat.
|
|
|
62 |
$this->setUser($user1->id);
|
|
|
63 |
$sid1 = chat_login_user($chat->id, 'ajax', 0, $course);
|
|
|
64 |
|
|
|
65 |
// Logging in second user to the chat.
|
|
|
66 |
$this->setUser($user2->id);
|
|
|
67 |
$sid2 = chat_login_user($chat->id, 'ajax', 0, $course);
|
|
|
68 |
|
|
|
69 |
// Getting the chatuser record.
|
|
|
70 |
$chatuser1 = $DB->get_record('chat_users', array('sid' => $sid1));
|
|
|
71 |
$chatuser2 = $DB->get_record('chat_users', array('sid' => $sid2));
|
|
|
72 |
|
|
|
73 |
$sink = $this->redirectEvents();
|
|
|
74 |
|
|
|
75 |
// Send a messaging from the first user. We pass the CM to chat_send_chatmessage() this time.
|
|
|
76 |
// This ensures that the event triggered when sending a message is filled with the correct information.
|
|
|
77 |
$this->setUser($user1->id);
|
|
|
78 |
$messageid = chat_send_chatmessage($chatuser1, 'Hello!', false, $cm);
|
|
|
79 |
$events = $sink->get_events();
|
|
|
80 |
$this->assertCount(1, $events);
|
|
|
81 |
$event = reset($events);
|
|
|
82 |
$this->assertInstanceOf('\mod_chat\event\message_sent', $event);
|
|
|
83 |
$this->assertEquals($messageid, $event->objectid);
|
|
|
84 |
$this->assertEquals($user1->id, $event->relateduserid);
|
|
|
85 |
$this->assertEquals($user1->id, $event->userid);
|
|
|
86 |
|
|
|
87 |
// Send a messaging from the first user. We DO NOT pass the CM to chat_send_chatmessage() this time.
|
|
|
88 |
// This ensures that the event triggered when sending a message is filled with the correct information.
|
|
|
89 |
$sink->clear();
|
|
|
90 |
$this->setUser($user2->id);
|
|
|
91 |
$messageid = chat_send_chatmessage($chatuser2, 'Hello!');
|
|
|
92 |
$events = $sink->get_events();
|
|
|
93 |
$this->assertCount(1, $events);
|
|
|
94 |
$event = reset($events);
|
|
|
95 |
$this->assertInstanceOf('\mod_chat\event\message_sent', $event);
|
|
|
96 |
$this->assertEquals($messageid, $event->objectid);
|
|
|
97 |
$this->assertEquals($user2->id, $event->relateduserid);
|
|
|
98 |
$this->assertEquals($user2->id, $event->userid);
|
|
|
99 |
|
|
|
100 |
// Sending a message from the system should not trigger any event.
|
|
|
101 |
$sink->clear();
|
|
|
102 |
$this->setAdminUser();
|
|
|
103 |
chat_send_chatmessage($chatuser1, 'enter', true);
|
|
|
104 |
$this->assertEquals(0, $sink->count());
|
|
|
105 |
|
|
|
106 |
$sink->close();
|
|
|
107 |
}
|
|
|
108 |
|
|
|
109 |
public function test_sessions_viewed() {
|
|
|
110 |
global $USER;
|
|
|
111 |
$this->resetAfterTest();
|
|
|
112 |
|
|
|
113 |
// Not much can be tested here as the event is only triggered on a page load,
|
|
|
114 |
// let's just check that the event contains the expected basic information.
|
|
|
115 |
$this->setAdminUser();
|
|
|
116 |
$course = $this->getDataGenerator()->create_course();
|
|
|
117 |
$chat = $this->getDataGenerator()->create_module('chat', array('course' => $course->id));
|
|
|
118 |
|
|
|
119 |
$params = array(
|
|
|
120 |
'context' => \context_module::instance($chat->cmid),
|
|
|
121 |
'objectid' => $chat->id,
|
|
|
122 |
'other' => array(
|
|
|
123 |
'start' => 1234,
|
|
|
124 |
'end' => 5678
|
|
|
125 |
)
|
|
|
126 |
);
|
|
|
127 |
$event = \mod_chat\event\sessions_viewed::create($params);
|
|
|
128 |
$event->add_record_snapshot('chat', $chat);
|
|
|
129 |
$sink = $this->redirectEvents();
|
|
|
130 |
$event->trigger();
|
|
|
131 |
$events = $sink->get_events();
|
|
|
132 |
$event = reset($events);
|
|
|
133 |
$this->assertInstanceOf('\mod_chat\event\sessions_viewed', $event);
|
|
|
134 |
$this->assertEquals($USER->id, $event->userid);
|
|
|
135 |
$this->assertEquals(\context_module::instance($chat->cmid), $event->get_context());
|
|
|
136 |
$this->assertEquals(1234, $event->other['start']);
|
|
|
137 |
$this->assertEquals(5678, $event->other['end']);
|
|
|
138 |
$this->assertEquals($chat->id, $event->objectid);
|
|
|
139 |
$this->assertEquals($chat, $event->get_record_snapshot('chat', $chat->id));
|
|
|
140 |
}
|
|
|
141 |
|
|
|
142 |
public function test_course_module_instance_list_viewed() {
|
|
|
143 |
global $USER;
|
|
|
144 |
$this->resetAfterTest();
|
|
|
145 |
|
|
|
146 |
// Not much can be tested here as the event is only triggered on a page load,
|
|
|
147 |
// let's just check that the event contains the expected basic information.
|
|
|
148 |
$this->setAdminUser();
|
|
|
149 |
$course = $this->getDataGenerator()->create_course();
|
|
|
150 |
|
|
|
151 |
$params = array(
|
|
|
152 |
'context' => \context_course::instance($course->id)
|
|
|
153 |
);
|
|
|
154 |
$event = \mod_chat\event\course_module_instance_list_viewed::create($params);
|
|
|
155 |
$sink = $this->redirectEvents();
|
|
|
156 |
$event->trigger();
|
|
|
157 |
$events = $sink->get_events();
|
|
|
158 |
$event = reset($events);
|
|
|
159 |
$this->assertInstanceOf('\mod_chat\event\course_module_instance_list_viewed', $event);
|
|
|
160 |
$this->assertEquals($USER->id, $event->userid);
|
|
|
161 |
$this->assertEquals(\context_course::instance($course->id), $event->get_context());
|
|
|
162 |
}
|
|
|
163 |
|
|
|
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);
|
|
|
171 |
|
|
|
172 |
$params = array(
|
|
|
173 |
'objectid' => $chat->id,
|
|
|
174 |
'context' => $context
|
|
|
175 |
);
|
|
|
176 |
$event = \mod_chat\event\course_module_viewed::create($params);
|
|
|
177 |
$event->add_record_snapshot('chat', $chat);
|
|
|
178 |
$event->trigger();
|
|
|
179 |
|
|
|
180 |
$url = new \moodle_url('/mod/chat/view.php', array('id' => $cm->id));
|
|
|
181 |
$this->assertEquals($url, $event->get_url());
|
|
|
182 |
$event->get_name();
|
|
|
183 |
}
|
|
|
184 |
}
|