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() {
68 // Generate user data.
69 $user = $this->getDataGenerator()->create_user();
72 $sink = $this->redirectEvents();
73 choice_user_submit_response(3, $this->choice
, $user->id
, $this->course
, $this->cm
);
74 $events = $sink->get_events();
77 $this->assertCount(1, $events);
78 $this->assertInstanceOf('\mod_choice\event\answer_submitted', $events[0]);
79 $this->assertEquals($user->id
, $events[0]->userid
);
80 $this->assertEquals(context_module
::instance($this->choice
->id
), $events[0]->get_context());
81 $this->assertEquals(1, $events[0]->other
['choiceid']);
82 $this->assertEquals(3, $events[0]->other
['optionid']);
83 $expected = array($this->course
->id
, "choice", "choose", 'view.php?id=' . $this->cm
->id
, $this->choice
->id
, $this->cm
->id
);
84 $this->assertEventLegacyLogData($expected, $events[0]);
85 $this->assertEventContextNotUsed($events[0]);
90 * Test custom validations.
92 public function test_answer_submitted_other_exception() {
93 // Generate user data.
94 $user = $this->getDataGenerator()->create_user();
97 $eventdata['context'] = $this->context
;
98 $eventdata['objectid'] = 2;
99 $eventdata['userid'] = $user->id
;
100 $eventdata['courseid'] = $this->course
->id
;
101 $eventdata['other'] = array();
103 // Make sure content identifier is always set.
104 $this->setExpectedException('coding_exception');
105 $event = \mod_choice\event\answer_submitted
::create($eventdata);
107 $this->assertEventContextNotUsed($event);
111 * Test to ensure that event data is being stored correctly.
113 public function test_answer_updated() {
114 // Generate user data.
115 $user = $this->getDataGenerator()->create_user();
117 // Create the first answer.
118 choice_user_submit_response(2, $this->choice
, $user->id
, $this->course
, $this->cm
);
121 $sink = $this->redirectEvents();
122 // Now choose a different answer.
123 choice_user_submit_response(3, $this->choice
, $user->id
, $this->course
, $this->cm
);
125 $events = $sink->get_events();
128 $this->assertCount(1, $events);
129 $this->assertInstanceOf('\mod_choice\event\answer_updated', $events[0]);
130 $this->assertEquals($user->id
, $events[0]->userid
);
131 $this->assertEquals(context_module
::instance($this->choice
->id
), $events[0]->get_context());
132 $this->assertEquals(1, $events[0]->other
['choiceid']);
133 $this->assertEquals(3, $events[0]->other
['optionid']);
134 $expected = array($this->course
->id
, "choice", "choose again", 'view.php?id=' . $this->cm
->id
,
135 $this->choice
->id
, $this->cm
->id
);
136 $this->assertEventLegacyLogData($expected, $events[0]);
137 $this->assertEventContextNotUsed($events[0]);
142 * Test custom validations
143 * for answer_updated event.
145 public function test_answer_updated_other_exception() {
146 // Generate user data.
147 $user = $this->getDataGenerator()->create_user();
149 $eventdata = array();
150 $eventdata['context'] = $this->context
;
151 $eventdata['objectid'] = 2;
152 $eventdata['userid'] = $user->id
;
153 $eventdata['courseid'] = $this->course
->id
;
154 $eventdata['other'] = array();
156 // Make sure content identifier is always set.
157 $this->setExpectedException('coding_exception');
158 $event = \mod_choice\event\answer_updated
::create($eventdata);
160 $this->assertEventContextNotUsed($event);
164 * Test to ensure that event data is being stored correctly.
166 public function test_report_viewed() {
169 $this->resetAfterTest();
171 // Generate user data.
172 $this->setAdminUser();
174 $eventdata = array();
175 $eventdata['objectid'] = $this->choice
->id
;
176 $eventdata['context'] = $this->context
;
177 $eventdata['courseid'] = $this->course
->id
;
178 $eventdata['other']['content'] = 'choicereportcontentviewed';
180 // This is fired in a page view so we can't run this through a function.
181 $event = \mod_choice\event\report_viewed
::create($eventdata);
184 $sink = $this->redirectEvents();
186 $event = $sink->get_events();
189 $this->assertCount(1, $event);
190 $this->assertInstanceOf('\mod_choice\event\report_viewed', $event[0]);
191 $this->assertEquals($USER->id
, $event[0]->userid
);
192 $this->assertEquals(context_module
::instance($this->choice
->id
), $event[0]->get_context());
193 $expected = array($this->course
->id
, "choice", "report", 'report.php?id=' . $this->context
->instanceid
,
194 $this->choice
->id
, $this->context
->instanceid
);
195 $this->assertEventLegacyLogData($expected, $event[0]);
196 $this->assertEventContextNotUsed($event[0]);
201 * Test to ensure that event data is being stored correctly.
203 public function test_course_module_viewed() {
206 // Generate user data.
207 $this->setAdminUser();
209 $eventdata = array();
210 $eventdata['objectid'] = $this->choice
->id
;
211 $eventdata['context'] = $this->context
;
212 $eventdata['courseid'] = $this->course
->id
;
213 $eventdata['other']['content'] = 'pageresourceview';
215 // This is fired in a page view so we can't run this through a function.
216 $event = \mod_choice\event\course_module_viewed
::create($eventdata);
219 $sink = $this->redirectEvents();
221 $event = $sink->get_events();
224 $this->assertCount(1, $event);
225 $this->assertInstanceOf('\mod_choice\event\course_module_viewed', $event[0]);
226 $this->assertEquals($USER->id
, $event[0]->userid
);
227 $this->assertEquals(context_module
::instance($this->choice
->id
), $event[0]->get_context());
228 $expected = array($this->course
->id
, "choice", "view", 'view.php?id=' . $this->context
->instanceid
,
229 $this->choice
->id
, $this->context
->instanceid
);
230 $this->assertEventLegacyLogData($expected, $event[0]);
231 $this->assertEventContextNotUsed($event[0]);
236 * Test to ensure that event data is being stored correctly.
238 public function test_course_module_instance_list_viewed_viewed() {
241 // Not much can be tested here as the event is only triggered on a page load,
242 // let's just check that the event contains the expected basic information.
243 $this->setAdminUser();
245 $params = array('context' => context_course
::instance($this->course
->id
));
246 $event = \mod_choice\event\course_module_instance_list_viewed
::create($params);
247 $sink = $this->redirectEvents();
249 $events = $sink->get_events();
250 $event = reset($events);
251 $this->assertInstanceOf('\mod_choice\event\course_module_instance_list_viewed', $event);
252 $this->assertEquals($USER->id
, $event->userid
);
253 $this->assertEquals(context_course
::instance($this->course
->id
), $event->get_context());
254 $expected = array($this->course
->id
, 'choice', 'view all', 'index.php?id=' . $this->course
->id
, '');
255 $this->assertEventLegacyLogData($expected, $event);
256 $this->assertEventContextNotUsed($event);