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