MDL-40921 mod_workshop: Alter event levels as requested by David.
[moodle.git] / mod / workshop / tests / events_test.php
CommitLineData
d85fb0c6
AG
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 * Unit tests for workshop events.
19 *
20 * @package mod_workshop
21 * @category phpunit
22 * @copyright 2013 Adrian Greeve <adrian@moodle.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26defined('MOODLE_INTERNAL') || die();
27
28global $CFG;
29require_once($CFG->dirroot . '/mod/workshop/lib.php'); // Include the code to test
30require_once($CFG->dirroot . '/mod/workshop/locallib.php'); // Include the code to test
31require_once($CFG->dirroot . '/lib/cronlib.php'); // Include the code to test
32
33
34/**
35 * Test cases for the internal workshop api
36 */
37class mod_workshop_events_testcase extends advanced_testcase {
38
39 /** $workshop Basic workshop data stored in an object. */
40 protected $workshop;
41 /** $course Generated Random Course. */
42 protected $course;
43 /** $context Course module context. */
44 protected $context;
45
46 /**
47 * Set up the testing environment.
48 */
49 protected function setUp() {
50 parent::setUp();
51 $this->setAdminUser();
52
53 // Create a workshop activity.
54 $this->course = $this->getDataGenerator()->create_course();
55 $this->workshop = $this->getDataGenerator()->create_module('workshop', array('course' => $this->course));
56 $this->context = context_module::instance($this->workshop->id);
57 }
58
59 protected function tearDown() {
60 $this->workshop = null;
61 parent::tearDown();
62 }
63
64 /**
65 * This event is triggered in view.php and workshop/lib.php through the function workshop_cron().
66 */
67 function test_phase_switched_event() {
68 $this->resetAfterTest();
69 $this->setAdminUser();
70
71 // Add additional workshop information.
72 $this->workshop->phase = 20;
73 $this->workshop->phaseswitchassessment = 1;
74 $this->workshop->submissionend = time() - 1;
75
76 $event = \mod_workshop\event\phase_switched::create(array(
77 'objectid' => $this->workshop->id,
78 'context' => $this->context,
79 'courseid' => $this->course->id,
80 'other' => array('workshopphase' => $this->workshop->phase)
81 ));
82
83 // Trigger and capture the event.
84 $sink = $this->redirectEvents();
85 $event->trigger();
86 $events = $sink->get_events();
87 $event = reset($events);
88
89 // Check that the legacy log data is valid.
90 $expected = array($this->course->id, 'workshop', 'update switch phase', 'view.php?id=' . $this->workshop->id,
91 $this->workshop->phase, $this->workshop->id);
92 $this->assertEventLegacyLogData($expected, $event);
93
94 $sink->close();
95 }
96
97 function test_assessment_evaluated() {
98 $this->resetAfterTest();
99 $this->setAdminUser();
100
101 $cm = get_coursemodule_from_instance('workshop', $this->workshop->id, $this->course->id, false, MUST_EXIST);
102
103 $workshop = new testable_workshop($this->workshop, $cm, $this->course);
104
105 $assessments = array();
106 $assessments[] = (object)array('reviewerid'=>2, 'gradinggrade'=>null, 'gradinggradeover'=>null, 'aggregationid'=>null,
107 'aggregatedgrade'=>12);
108
109 // Trigger and capture the event.
110 $sink = $this->redirectEvents();
111 $workshop->aggregate_grading_grades_process($assessments);
112 $events = $sink->get_events();
113 $event = reset($events);
114
115 $this->assertInstanceOf('\mod_workshop\event\assessment_evaluated', $event);
116 $this->assertEquals('workshop_aggregations', $event->objecttable);
117 $this->assertEquals(context_module::instance($workshop->id), $event->get_context());
118
119 $sink->close();
120 }
121
122 function test_assessment_reevaluated() {
123 $this->resetAfterTest();
124 $this->setAdminUser();
125
126 $cm = get_coursemodule_from_instance('workshop', $this->workshop->id, $this->course->id, false, MUST_EXIST);
127
128 $workshop = new testable_workshop($this->workshop, $cm, $this->course);
129
130 $assessments = array();
131 $assessments[] = (object)array('reviewerid'=>2, 'gradinggrade'=>null, 'gradinggradeover'=>null, 'aggregationid'=>2,
132 'aggregatedgrade'=>12);
133
134 // Trigger and capture the event.
135 $sink = $this->redirectEvents();
136 $workshop->aggregate_grading_grades_process($assessments);
137 $events = $sink->get_events();
138 $event = reset($events);
139
140 $this->assertInstanceOf('\mod_workshop\event\assessment_reevaluated', $event);
141 $this->assertEquals('workshop_aggregations', $event->objecttable);
142 $this->assertEquals(context_module::instance($workshop->id), $event->get_context());
143 $expected = array($this->course->id, 'workshop', 'update aggregate grade', 'view.php?id=' . $event->get_context()->instanceid,
144 $event->objectid, $event->get_context()->instanceid);
145 $this->assertEventLegacyLogData($expected, $event);
146
147 $sink->close();
148 }
149
150 /**
151 * There is no api involved so the best we can do is test legacy data by triggering event manually.
152 */
153 function test_aggregate_grades_reset_event() {
154 $this->resetAfterTest();
155 $this->setAdminUser();
156
157 $event = \mod_workshop\event\assessment_evaluations_reset::create(array(
158 'objectid' => $this->workshop->id,
159 'context' => $this->context,
160 'courseid' => $this->course->id,
161 ));
162
163 // Trigger and capture the event.
164 $sink = $this->redirectEvents();
165 $event->trigger();
166 $events = $sink->get_events();
167 $event = reset($events);
168
169 // Check that the legacy log data is valid.
170 $expected = array($this->course->id, 'workshop', 'update clear aggregated grade', 'view.php?id=' . $this->workshop->id,
171 $this->workshop->id, $this->workshop->id);
172 $this->assertEventLegacyLogData($expected, $event);
173
174 $sink->close();
175 }
176
177 /**
178 * There is no api involved so the best we can do is test legacy data by triggering event manually.
179 */
180 function test_instances_list_viewed_event() {
181 $this->resetAfterTest();
182 $this->setAdminUser();
183
184 $context = context_course::instance($this->course->id);
185
186 $event = \mod_workshop\event\instances_list_viewed::create(array('context' => $context));
187
188 // Trigger and capture the event.
189 $sink = $this->redirectEvents();
190 $event->trigger();
191 $events = $sink->get_events();
192 $event = reset($events);
193
194 // Check that the legacy log data is valid.
195 $expected = array($this->course->id, 'workshop', 'view all', 'index.php?id=' . $this->course->id, '');
196 $this->assertEventLegacyLogData($expected, $event);
197
198 $sink->close();
199 }
200
201 /**
202 * There is no api involved so the best we can do is test legacy data by triggering event manually.
203 */
204 function test_submission_created_event() {
205 $this->resetAfterTest();
206 $this->setAdminUser();
207
208 $user = $this->getDataGenerator()->create_user();
209 $submissionid = 48;
210
211 $event = \mod_workshop\event\submission_created::create(array(
212 'objectid' => $submissionid,
213 'context' => $this->context,
214 'courseid' => $this->course->id,
215 'relateduserid' => $user->id,
216 'other' => array(
217 'workshopid' => $this->workshop->id
218 )
219 )
220 );
221
222 // Trigger and capture the event.
223 $sink = $this->redirectEvents();
224 $event->trigger();
225 $events = $sink->get_events();
226 $event = reset($events);
227
228 // Check that the legacy log data is valid.
229 $expected = array($this->course->id, 'workshop', 'add submission',
230 'submission.php?cmid=' . $this->workshop->id . '&id=' . $submissionid, $submissionid, $this->workshop->id);
231 $this->assertEventLegacyLogData($expected, $event);
232
233 $sink->close();
234 }
235
236 /**
237 * There is no api involved so the best we can do is test legacy data by triggering event manually.
238 */
239 function test_submission_updated_event() {
240 $this->resetAfterTest();
241 $this->setAdminUser();
242
243 $user = $this->getDataGenerator()->create_user();
244 $submissionid = 48;
245
246 $event = \mod_workshop\event\submission_updated::create(array(
247 'objectid' => $submissionid,
248 'context' => $this->context,
249 'courseid' => $this->course->id,
250 'relateduserid' => $user->id,
251 'other' => array(
252 'workshopid' => $this->workshop->id
253 )
254 )
255 );
256
257 // Trigger and capture the event.
258 $sink = $this->redirectEvents();
259 $event->trigger();
260 $events = $sink->get_events();
261 $event = reset($events);
262
263 // Check that the legacy log data is valid.
264 $expected = array($this->course->id, 'workshop', 'update submission',
265 'submission.php?cmid=' . $this->workshop->id . '&id=' . $submissionid, $submissionid, $this->workshop->id);
266 $this->assertEventLegacyLogData($expected, $event);
267
268 $sink->close();
269 }
270
271 /**
272 * There is no api involved so the best we can do is test legacy data by triggering event manually.
273 */
274 function test_submission_viewed_event() {
275 $this->resetAfterTest();
276 $this->setAdminUser();
277
278 $user = $this->getDataGenerator()->create_user();
279 $submissionid = 48;
280
281 $event = \mod_workshop\event\submission_viewed::create(array(
282 'objectid' => $submissionid,
283 'context' => $this->context,
284 'courseid' => $this->course->id,
285 'relateduserid' => $user->id,
286 'other' => array(
287 'workshopid' => $this->workshop->id
288 )
289 )
290 );
291
292 // Trigger and capture the event.
293 $sink = $this->redirectEvents();
294 $event->trigger();
295 $events = $sink->get_events();
296 $event = reset($events);
297
298 // Check that the legacy log data is valid.
299 $expected = array($this->course->id, 'workshop', 'view submission',
300 'submission.php?cmid=' . $this->workshop->id . '&id=' . $submissionid, $submissionid, $this->workshop->id);
301 $this->assertEventLegacyLogData($expected, $event);
302
303 $sink->close();
304 }
305
306}
307
308/**
309 * Test subclass that makes all the protected methods we want to test public.
310 */
311class testable_workshop extends workshop {
312
313 public function aggregate_grading_grades_process(array $assessments, $timegraded = null) {
314 parent::aggregate_grading_grades_process($assessments, $timegraded);
315 }
316
317}