Merge branch 'MDL-45740_master' of git://github.com/markn86/moodle
[moodle.git] / mod / choice / tests / events_test.php
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/>.
17 /**
18  * Events tests.
19  *
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
23  */
25 defined('MOODLE_INTERNAL') || die();
27 global $CFG;
28 require_once($CFG->dirroot . '/mod/choice/lib.php');
30 /**
31  * Events tests class.
32  *
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
36  */
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.
52      */
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);
62     }
64     /**
65      * Test to ensure that event data is being stored correctly.
66      */
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();
89     }
91     /**
92      * Test to ensure that multiple choice data is being stored correctly.
93      */
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();
125     }
127     /**
128      * Test custom validations.
129      */
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);
146     }
148     /**
149      * Test to ensure that event data is being stored correctly.
150      */
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();
180     }
182     /**
183      * Test custom validations
184      * for answer_updated event.
185      */
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);
202     }
204     /**
205      * Test to ensure that event data is being stored correctly.
206      */
207     public function test_answer_deleted() {
208         global $DB, $USER;
209         // Generate user data.
210         $user = $this->getDataGenerator()->create_user();
212         $optionids = array_keys($DB->get_records('choice_options', array('choiceid' => $this->choice->id)));
214         // Create the first answer.
215         choice_user_submit_response($optionids[2], $this->choice, $user->id, $this->course, $this->cm);
216         // Get the users response.
217         $answer = $DB->get_record('choice_answers', array('userid' => $user->id, 'choiceid' => $this->choice->id),
218                 '*', $strictness = IGNORE_MULTIPLE);
220         // Redirect event.
221         $sink = $this->redirectEvents();
222         // Now delete the answer.
223         choice_delete_responses(array($answer->id), $this->choice, $this->cm, $this->course);
225         // Get our event event.
226         $events = $sink->get_events();
227         $event = reset($events);
229         // Data checking.
230         $this->assertInstanceOf('\mod_choice\event\answer_deleted', $event);
231         $this->assertEquals($USER->id, $event->userid);
232         $this->assertEquals($user->id, $event->relateduserid);
233         $this->assertEquals(context_module::instance($this->choice->cmid), $event->get_context());
234         $this->assertEquals($this->choice->id, $event->other['choiceid']);
235         $this->assertEquals($answer->optionid, $event->other['optionid']);
236         $this->assertEventContextNotUsed($event);
237         $sink->close();
238     }
240     /**
241      * Test to ensure that event data is being stored correctly.
242      */
243     public function test_report_viewed() {
244         global $USER;
246         $this->resetAfterTest();
248         // Generate user data.
249         $this->setAdminUser();
251         $eventdata = array();
252         $eventdata['objectid'] = $this->choice->id;
253         $eventdata['context'] = $this->context;
254         $eventdata['courseid'] = $this->course->id;
255         $eventdata['other']['content'] = 'choicereportcontentviewed';
257         // This is fired in a page view so we can't run this through a function.
258         $event = \mod_choice\event\report_viewed::create($eventdata);
260         // Redirect event.
261         $sink = $this->redirectEvents();
262         $event->trigger();
263         $event = $sink->get_events();
265         // Data checking.
266         $this->assertCount(1, $event);
267         $this->assertInstanceOf('\mod_choice\event\report_viewed', $event[0]);
268         $this->assertEquals($USER->id, $event[0]->userid);
269         $this->assertEquals(context_module::instance($this->choice->cmid), $event[0]->get_context());
270         $expected = array($this->course->id, "choice", "report", 'report.php?id=' . $this->context->instanceid,
271                 $this->choice->id, $this->context->instanceid);
272         $this->assertEventLegacyLogData($expected, $event[0]);
273         $this->assertEventContextNotUsed($event[0]);
274         $sink->close();
275     }
277     /**
278      * Test to ensure that event data is being stored correctly.
279      */
280     public function test_report_downloaded() {
281         global $USER;
283         $this->resetAfterTest();
285         // Generate user data.
286         $this->setAdminUser();
288         $eventdata = array();
289         $eventdata['context'] = $this->context;
290         $eventdata['courseid'] = $this->course->id;
291         $eventdata['other']['content'] = 'choicereportcontentviewed';
292         $eventdata['other']['format'] = 'csv';
293         $eventdata['other']['choiceid'] = $this->choice->id;
295         // This is fired in a page view so we can't run this through a function.
296         $event = \mod_choice\event\report_downloaded::create($eventdata);
298         // Redirect event.
299         $sink = $this->redirectEvents();
300         $event->trigger();
301         $event = $sink->get_events();
303         // Data checking.
304         $this->assertCount(1, $event);
305         $this->assertInstanceOf('\mod_choice\event\report_downloaded', $event[0]);
306         $this->assertEquals($USER->id, $event[0]->userid);
307         $this->assertEquals(context_module::instance($this->choice->cmid), $event[0]->get_context());
308         $this->assertEquals('csv', $event[0]->other['format']);
309         $this->assertEquals($this->choice->id, $event[0]->other['choiceid']);
310         $this->assertEventContextNotUsed($event[0]);
311         $sink->close();
312     }
314     /**
315      * Test to ensure that event data is being stored correctly.
316      */
317     public function test_course_module_viewed() {
318         global $USER;
320         // Generate user data.
321         $this->setAdminUser();
323         $eventdata = array();
324         $eventdata['objectid'] = $this->choice->id;
325         $eventdata['context'] = $this->context;
326         $eventdata['courseid'] = $this->course->id;
327         $eventdata['other']['content'] = 'pageresourceview';
329         // This is fired in a page view so we can't run this through a function.
330         $event = \mod_choice\event\course_module_viewed::create($eventdata);
332         // Redirect event.
333         $sink = $this->redirectEvents();
334         $event->trigger();
335         $event = $sink->get_events();
337         // Data checking.
338         $this->assertCount(1, $event);
339         $this->assertInstanceOf('\mod_choice\event\course_module_viewed', $event[0]);
340         $this->assertEquals($USER->id, $event[0]->userid);
341         $this->assertEquals(context_module::instance($this->choice->cmid), $event[0]->get_context());
342         $expected = array($this->course->id, "choice", "view", 'view.php?id=' . $this->context->instanceid,
343                 $this->choice->id, $this->context->instanceid);
344         $this->assertEventLegacyLogData($expected, $event[0]);
345         $this->assertEventContextNotUsed($event[0]);
346         $sink->close();
347     }
349     /**
350      * Test to ensure that event data is being stored correctly.
351      */
352     public function test_course_module_instance_list_viewed_viewed() {
353         global $USER;
355         // Not much can be tested here as the event is only triggered on a page load,
356         // let's just check that the event contains the expected basic information.
357         $this->setAdminUser();
359         $params = array('context' => context_course::instance($this->course->id));
360         $event = \mod_choice\event\course_module_instance_list_viewed::create($params);
361         $sink = $this->redirectEvents();
362         $event->trigger();
363         $events = $sink->get_events();
364         $event = reset($events);
365         $this->assertInstanceOf('\mod_choice\event\course_module_instance_list_viewed', $event);
366         $this->assertEquals($USER->id, $event->userid);
367         $this->assertEquals(context_course::instance($this->course->id), $event->get_context());
368         $expected = array($this->course->id, 'choice', 'view all', 'index.php?id=' . $this->course->id, '');
369         $this->assertEventLegacyLogData($expected, $event);
370         $this->assertEventContextNotUsed($event);
371     }