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 Adrian Greeve <adrian@moodle.com>
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/choice/lib.php');
34 * @copyright 2013 Adrian Greeve <adrian@moodle.com>
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37 class mod_choice_events_testcase
extends advanced_testcase
{
38 /** @var choice_object */
41 /** @var course_object */
44 /** @var cm_object Course module object. */
47 /** @var context_object */
51 * Setup often used objects for the following tests.
53 protected function setup() {
56 $this->resetAfterTest();
58 $this->course
= $this->getDataGenerator()->create_course();
59 $this->choice
= $this->getDataGenerator()->create_module('choice', array('course' => $this->course
->id
));
60 $this->cm
= $DB->get_record('course_modules', array('id' => $this->choice
->cmid
));
61 $this->context
= context_module
::instance($this->choice
->cmid
);
65 * Test to ensure that event data is being stored correctly.
67 public function test_answer_submitted() {
69 // Generate user data.
70 $user = $this->getDataGenerator()->create_user();
72 $optionids = array_keys($DB->get_records('choice_options', array('choiceid' => $this->choice
->id
)));
74 $sink = $this->redirectEvents();
75 choice_user_submit_response($optionids[3], $this->choice
, $user->id
, $this->course
, $this->cm
);
76 $events = $sink->get_events();
79 $this->assertCount(1, $events);
80 $this->assertInstanceOf('\mod_choice\event\answer_submitted', $events[0]);
81 $this->assertEquals($user->id
, $events[0]->userid
);
82 $this->assertEquals(context_module
::instance($this->choice
->cmid
), $events[0]->get_context());
83 $this->assertEquals($this->choice
->id
, $events[0]->other
['choiceid']);
84 $this->assertEquals(array($optionids[3]), $events[0]->other
['optionid']);
85 $expected = array($this->course
->id
, "choice", "choose", 'view.php?id=' . $this->cm
->id
, $this->choice
->id
, $this->cm
->id
);
86 $this->assertEventLegacyLogData($expected, $events[0]);
87 $this->assertEventContextNotUsed($events[0]);
92 * Test to ensure that multiple choice data is being stored correctly.
94 public function test_answer_submitted_multiple() {
97 // Generate user data.
98 $user = $this->getDataGenerator()->create_user();
100 // Create multiple choice.
101 $choice = $this->getDataGenerator()->create_module('choice', array('course' => $this->course
->id
,
102 'allowmultiple' => 1));
103 $cm = $DB->get_record('course_modules', array('id' => $choice->cmid
));
104 $context = context_module
::instance($choice->cmid
);
106 $optionids = array_keys($DB->get_records('choice_options', array('choiceid' => $choice->id
)));
107 $submittedoptionids = array($optionids[1], $optionids[3]);
110 $sink = $this->redirectEvents();
111 choice_user_submit_response($submittedoptionids, $choice, $user->id
, $this->course
, $cm);
112 $events = $sink->get_events();
115 $this->assertCount(1, $events);
116 $this->assertInstanceOf('\mod_choice\event\answer_submitted', $events[0]);
117 $this->assertEquals($user->id
, $events[0]->userid
);
118 $this->assertEquals(context_module
::instance($choice->cmid
), $events[0]->get_context());
119 $this->assertEquals($choice->id
, $events[0]->other
['choiceid']);
120 $this->assertEquals($submittedoptionids, $events[0]->other
['optionid']);
121 $expected = array($this->course
->id
, "choice", "choose", 'view.php?id=' . $cm->id
, $choice->id
, $cm->id
);
122 $this->assertEventLegacyLogData($expected, $events[0]);
123 $this->assertEventContextNotUsed($events[0]);
128 * Test custom validations.
130 public function test_answer_submitted_other_exception() {
131 // Generate user data.
132 $user = $this->getDataGenerator()->create_user();
134 $eventdata = array();
135 $eventdata['context'] = $this->context
;
136 $eventdata['objectid'] = 2;
137 $eventdata['userid'] = $user->id
;
138 $eventdata['courseid'] = $this->course
->id
;
139 $eventdata['other'] = array();
141 // Make sure content identifier is always set.
142 $this->setExpectedException('coding_exception');
143 $event = \mod_choice\event\answer_submitted
::create($eventdata);
145 $this->assertEventContextNotUsed($event);
149 * Test to ensure that event data is being stored correctly.
151 public function test_answer_updated() {
153 // Generate user data.
154 $user = $this->getDataGenerator()->create_user();
156 $optionids = array_keys($DB->get_records('choice_options', array('choiceid' => $this->choice
->id
)));
158 // Create the first answer.
159 choice_user_submit_response($optionids[2], $this->choice
, $user->id
, $this->course
, $this->cm
);
162 $sink = $this->redirectEvents();
163 // Now choose a different answer.
164 choice_user_submit_response($optionids[3], $this->choice
, $user->id
, $this->course
, $this->cm
);
166 $events = $sink->get_events();
169 $this->assertCount(1, $events);
170 $this->assertInstanceOf('\mod_choice\event\answer_updated', $events[0]);
171 $this->assertEquals($user->id
, $events[0]->userid
);
172 $this->assertEquals(context_module
::instance($this->choice
->cmid
), $events[0]->get_context());
173 $this->assertEquals($this->choice
->id
, $events[0]->other
['choiceid']);
174 $this->assertEquals($optionids[3], $events[0]->other
['optionid']);
175 $expected = array($this->course
->id
, "choice", "choose again", 'view.php?id=' . $this->cm
->id
,
176 $this->choice
->id
, $this->cm
->id
);
177 $this->assertEventLegacyLogData($expected, $events[0]);
178 $this->assertEventContextNotUsed($events[0]);
183 * Test custom validations
184 * for answer_updated event.
186 public function test_answer_updated_other_exception() {
187 // Generate user data.
188 $user = $this->getDataGenerator()->create_user();
190 $eventdata = array();
191 $eventdata['context'] = $this->context
;
192 $eventdata['objectid'] = 2;
193 $eventdata['userid'] = $user->id
;
194 $eventdata['courseid'] = $this->course
->id
;
195 $eventdata['other'] = array();
197 // Make sure content identifier is always set.
198 $this->setExpectedException('coding_exception');
199 $event = \mod_choice\event\answer_updated
::create($eventdata);
201 $this->assertEventContextNotUsed($event);
205 * Test to ensure that event data is being stored correctly.
207 public function test_report_viewed() {
210 $this->resetAfterTest();
212 // Generate user data.
213 $this->setAdminUser();
215 $eventdata = array();
216 $eventdata['objectid'] = $this->choice
->id
;
217 $eventdata['context'] = $this->context
;
218 $eventdata['courseid'] = $this->course
->id
;
219 $eventdata['other']['content'] = 'choicereportcontentviewed';
221 // This is fired in a page view so we can't run this through a function.
222 $event = \mod_choice\event\report_viewed
::create($eventdata);
225 $sink = $this->redirectEvents();
227 $event = $sink->get_events();
230 $this->assertCount(1, $event);
231 $this->assertInstanceOf('\mod_choice\event\report_viewed', $event[0]);
232 $this->assertEquals($USER->id
, $event[0]->userid
);
233 $this->assertEquals(context_module
::instance($this->choice
->cmid
), $event[0]->get_context());
234 $expected = array($this->course
->id
, "choice", "report", 'report.php?id=' . $this->context
->instanceid
,
235 $this->choice
->id
, $this->context
->instanceid
);
236 $this->assertEventLegacyLogData($expected, $event[0]);
237 $this->assertEventContextNotUsed($event[0]);
242 * Test to ensure that event data is being stored correctly.
244 public function test_course_module_viewed() {
247 // Generate user data.
248 $this->setAdminUser();
250 $eventdata = array();
251 $eventdata['objectid'] = $this->choice
->id
;
252 $eventdata['context'] = $this->context
;
253 $eventdata['courseid'] = $this->course
->id
;
254 $eventdata['other']['content'] = 'pageresourceview';
256 // This is fired in a page view so we can't run this through a function.
257 $event = \mod_choice\event\course_module_viewed
::create($eventdata);
260 $sink = $this->redirectEvents();
262 $event = $sink->get_events();
265 $this->assertCount(1, $event);
266 $this->assertInstanceOf('\mod_choice\event\course_module_viewed', $event[0]);
267 $this->assertEquals($USER->id
, $event[0]->userid
);
268 $this->assertEquals(context_module
::instance($this->choice
->cmid
), $event[0]->get_context());
269 $expected = array($this->course
->id
, "choice", "view", 'view.php?id=' . $this->context
->instanceid
,
270 $this->choice
->id
, $this->context
->instanceid
);
271 $this->assertEventLegacyLogData($expected, $event[0]);
272 $this->assertEventContextNotUsed($event[0]);
277 * Test to ensure that event data is being stored correctly.
279 public function test_course_module_instance_list_viewed_viewed() {
282 // Not much can be tested here as the event is only triggered on a page load,
283 // let's just check that the event contains the expected basic information.
284 $this->setAdminUser();
286 $params = array('context' => context_course
::instance($this->course
->id
));
287 $event = \mod_choice\event\course_module_instance_list_viewed
::create($params);
288 $sink = $this->redirectEvents();
290 $events = $sink->get_events();
291 $event = reset($events);
292 $this->assertInstanceOf('\mod_choice\event\course_module_instance_list_viewed', $event);
293 $this->assertEquals($USER->id
, $event->userid
);
294 $this->assertEquals(context_course
::instance($this->course
->id
), $event->get_context());
295 $expected = array($this->course
->id
, 'choice', 'view all', 'index.php?id=' . $this->course
->id
, '');
296 $this->assertEventLegacyLogData($expected, $event);
297 $this->assertEventContextNotUsed($event);