MDL-51569 mod_choice: Fix use of hardcoded ids
[moodle.git] / mod / choice / tests / events_test.php
blob71bb5998647e58a11b1b9f1fd347e6b7061324ee
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 mod_choice
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();
27 global $CFG;
28 require_once($CFG->dirroot . '/mod/choice/lib.php');
30 /**
31 * Events tests class.
33 * @package mod_choice
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 */
39 protected $choice;
41 /** @var course_object */
42 protected $course;
44 /** @var cm_object Course module object. */
45 protected $cm;
47 /** @var context_object */
48 protected $context;
50 /**
51 * Setup often used objects for the following tests.
53 protected function setup() {
54 global $DB;
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);
64 /**
65 * Test to ensure that event data is being stored correctly.
67 public function test_answer_submitted() {
68 global $DB;
69 // Generate user data.
70 $user = $this->getDataGenerator()->create_user();
72 $optionids = array_keys($DB->get_records('choice_options', array('choiceid' => $this->choice->id)));
73 // Redirect event.
74 $sink = $this->redirectEvents();
75 choice_user_submit_response($optionids[3], $this->choice, $user->id, $this->course, $this->cm);
76 $events = $sink->get_events();
78 // Data checking.
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]);
88 $sink->close();
91 /**
92 * Test to ensure that multiple choice data is being stored correctly.
94 public function test_answer_submitted_multiple() {
95 global $DB;
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]);
109 // Redirect event.
110 $sink = $this->redirectEvents();
111 choice_user_submit_response($submittedoptionids, $choice, $user->id, $this->course, $cm);
112 $events = $sink->get_events();
114 // Data checking.
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]);
124 $sink->close();
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);
144 $event->trigger();
145 $this->assertEventContextNotUsed($event);
149 * Test to ensure that event data is being stored correctly.
151 public function test_answer_updated() {
152 global $DB;
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);
161 // Redirect event.
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();
168 // Data checking.
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]);
179 $sink->close();
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);
200 $event->trigger();
201 $this->assertEventContextNotUsed($event);
205 * Test to ensure that event data is being stored correctly.
207 public function test_report_viewed() {
208 global $USER;
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);
224 // Redirect event.
225 $sink = $this->redirectEvents();
226 $event->trigger();
227 $event = $sink->get_events();
229 // Data checking.
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]);
238 $sink->close();
242 * Test to ensure that event data is being stored correctly.
244 public function test_course_module_viewed() {
245 global $USER;
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);
259 // Redirect event.
260 $sink = $this->redirectEvents();
261 $event->trigger();
262 $event = $sink->get_events();
264 // Data checking.
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]);
273 $sink->close();
277 * Test to ensure that event data is being stored correctly.
279 public function test_course_module_instance_list_viewed_viewed() {
280 global $USER;
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();
289 $event->trigger();
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);