MDL-40921 mod_workshop: Add assertEventContextNotUsed to events tests.
[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 99 $this->assertEventLegacyLogData($expected, $event);
603427a9 100 $this->assertEventContextNotUsed($event);
d85fb0c6
AG
101
102 $sink->close();
103 }
104
0d0514cf 105 public function test_assessment_evaluated() {
d85fb0c6
AG
106 $this->resetAfterTest();
107 $this->setAdminUser();
108
109 $cm = get_coursemodule_from_instance('workshop', $this->workshop->id, $this->course->id, false, MUST_EXIST);
110
111 $workshop = new testable_workshop($this->workshop, $cm, $this->course);
112
113 $assessments = array();
0d0514cf
114 $assessments[] = (object)array('reviewerid' => 2, 'gradinggrade' => null,
115 'gradinggradeover' => null, 'aggregationid' => null, 'aggregatedgrade' => 12);
d85fb0c6
AG
116
117 // Trigger and capture the event.
118 $sink = $this->redirectEvents();
119 $workshop->aggregate_grading_grades_process($assessments);
120 $events = $sink->get_events();
121 $event = reset($events);
122
123 $this->assertInstanceOf('\mod_workshop\event\assessment_evaluated', $event);
124 $this->assertEquals('workshop_aggregations', $event->objecttable);
0d0514cf 125 $this->assertEquals(context_module::instance($cm->id), $event->get_context());
603427a9 126 $this->assertEventContextNotUsed($event);
d85fb0c6
AG
127
128 $sink->close();
129 }
130
0d0514cf 131 public function test_assessment_reevaluated() {
d85fb0c6
AG
132 $this->resetAfterTest();
133 $this->setAdminUser();
134
135 $cm = get_coursemodule_from_instance('workshop', $this->workshop->id, $this->course->id, false, MUST_EXIST);
136
137 $workshop = new testable_workshop($this->workshop, $cm, $this->course);
138
139 $assessments = array();
0d0514cf
140 $assessments[] = (object)array('reviewerid' => 2, 'gradinggrade' => null, 'gradinggradeover' => null,
141 'aggregationid' => 2, 'aggregatedgrade' => 12);
d85fb0c6
AG
142
143 // Trigger and capture the event.
144 $sink = $this->redirectEvents();
145 $workshop->aggregate_grading_grades_process($assessments);
146 $events = $sink->get_events();
147 $event = reset($events);
148
149 $this->assertInstanceOf('\mod_workshop\event\assessment_reevaluated', $event);
150 $this->assertEquals('workshop_aggregations', $event->objecttable);
0d0514cf
151 $this->assertEquals(context_module::instance($cm->id), $event->get_context());
152 $expected = array($this->course->id, 'workshop', 'update aggregate grade',
153 'view.php?id=' . $event->get_context()->instanceid, $event->objectid, $event->get_context()->instanceid);
d85fb0c6 154 $this->assertEventLegacyLogData($expected, $event);
603427a9 155 $this->assertEventContextNotUsed($event);
d85fb0c6
AG
156
157 $sink->close();
158 }
159
160 /**
161 * There is no api involved so the best we can do is test legacy data by triggering event manually.
162 */
0d0514cf 163 public function test_aggregate_grades_reset_event() {
d85fb0c6
AG
164 $this->resetAfterTest();
165 $this->setAdminUser();
166
167 $event = \mod_workshop\event\assessment_evaluations_reset::create(array(
168 'objectid' => $this->workshop->id,
169 'context' => $this->context,
170 'courseid' => $this->course->id,
171 ));
172
173 // Trigger and capture the event.
174 $sink = $this->redirectEvents();
175 $event->trigger();
176 $events = $sink->get_events();
177 $event = reset($events);
178
179 // Check that the legacy log data is valid.
180 $expected = array($this->course->id, 'workshop', 'update clear aggregated grade', 'view.php?id=' . $this->workshop->id,
0d0514cf 181 $this->workshop->id, $this->cm->id);
d85fb0c6
AG
182 $this->assertEventLegacyLogData($expected, $event);
183
184 $sink->close();
185 }
186
187 /**
188 * There is no api involved so the best we can do is test legacy data by triggering event manually.
189 */
0d0514cf 190 public function test_instances_list_viewed_event() {
d85fb0c6
AG
191 $this->resetAfterTest();
192 $this->setAdminUser();
193
194 $context = context_course::instance($this->course->id);
195
196 $event = \mod_workshop\event\instances_list_viewed::create(array('context' => $context));
197
198 // Trigger and capture the event.
199 $sink = $this->redirectEvents();
200 $event->trigger();
201 $events = $sink->get_events();
202 $event = reset($events);
203
204 // Check that the legacy log data is valid.
205 $expected = array($this->course->id, 'workshop', 'view all', 'index.php?id=' . $this->course->id, '');
206 $this->assertEventLegacyLogData($expected, $event);
603427a9 207 $this->assertEventContextNotUsed($event);
d85fb0c6
AG
208
209 $sink->close();
210 }
211
212 /**
213 * There is no api involved so the best we can do is test legacy data by triggering event manually.
214 */
0d0514cf 215 public function test_submission_created_event() {
d85fb0c6
AG
216 $this->resetAfterTest();
217 $this->setAdminUser();
218
219 $user = $this->getDataGenerator()->create_user();
220 $submissionid = 48;
221
222 $event = \mod_workshop\event\submission_created::create(array(
0d0514cf
223 'objectid' => $submissionid,
224 'context' => $this->context,
225 'courseid' => $this->course->id,
226 'relateduserid' => $user->id,
227 'other' => array(
228 'workshopid' => $this->workshop->id
d85fb0c6
AG
229 )
230 )
231 );
232
233 // Trigger and capture the event.
234 $sink = $this->redirectEvents();
235 $event->trigger();
236 $events = $sink->get_events();
237 $event = reset($events);
238
239 // Check that the legacy log data is valid.
240 $expected = array($this->course->id, 'workshop', 'add submission',
0d0514cf 241 'submission.php?cmid=' . $this->workshop->id . '&id=' . $submissionid, $submissionid, $this->cm->id);
d85fb0c6 242 $this->assertEventLegacyLogData($expected, $event);
603427a9 243 $this->assertEventContextNotUsed($event);
d85fb0c6
AG
244
245 $sink->close();
246 }
247
248 /**
249 * There is no api involved so the best we can do is test legacy data by triggering event manually.
250 */
0d0514cf 251 public function test_submission_updated_event() {
d85fb0c6
AG
252 $this->resetAfterTest();
253 $this->setAdminUser();
254
255 $user = $this->getDataGenerator()->create_user();
256 $submissionid = 48;
257
258 $event = \mod_workshop\event\submission_updated::create(array(
0d0514cf
259 'objectid' => $submissionid,
260 'context' => $this->context,
261 'courseid' => $this->course->id,
262 'relateduserid' => $user->id,
263 'other' => array(
264 'workshopid' => $this->workshop->id
d85fb0c6
AG
265 )
266 )
267 );
268
269 // Trigger and capture the event.
270 $sink = $this->redirectEvents();
271 $event->trigger();
272 $events = $sink->get_events();
273 $event = reset($events);
274
275 // Check that the legacy log data is valid.
276 $expected = array($this->course->id, 'workshop', 'update submission',
0d0514cf 277 'submission.php?cmid=' . $this->workshop->id . '&id=' . $submissionid, $submissionid, $this->cm->id);
d85fb0c6 278 $this->assertEventLegacyLogData($expected, $event);
603427a9 279 $this->assertEventContextNotUsed($event);
d85fb0c6
AG
280
281 $sink->close();
282 }
283
284 /**
285 * There is no api involved so the best we can do is test legacy data by triggering event manually.
286 */
0d0514cf 287 public function test_submission_viewed_event() {
d85fb0c6
AG
288 $this->resetAfterTest();
289 $this->setAdminUser();
290
291 $user = $this->getDataGenerator()->create_user();
292 $submissionid = 48;
293
294 $event = \mod_workshop\event\submission_viewed::create(array(
0d0514cf
295 'objectid' => $submissionid,
296 'context' => $this->context,
297 'courseid' => $this->course->id,
298 'relateduserid' => $user->id,
299 'other' => array(
300 'workshopid' => $this->workshop->id
d85fb0c6
AG
301 )
302 )
303 );
304
305 // Trigger and capture the event.
306 $sink = $this->redirectEvents();
307 $event->trigger();
308 $events = $sink->get_events();
309 $event = reset($events);
310
311 // Check that the legacy log data is valid.
312 $expected = array($this->course->id, 'workshop', 'view submission',
0d0514cf 313 'submission.php?cmid=' . $this->workshop->id . '&id=' . $submissionid, $submissionid, $this->cm->id);
d85fb0c6 314 $this->assertEventLegacyLogData($expected, $event);
603427a9 315 $this->assertEventContextNotUsed($event);
d85fb0c6
AG
316
317 $sink->close();
318 }
d85fb0c6 319}