Merge branch 'w06_MDL-40921_m27_workshopevents' of https://github.com/skodak/moodle
[moodle.git] / mod / workshop / 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  * 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  */
26 defined('MOODLE_INTERNAL') || die();
28 global $CFG;
29 require_once($CFG->dirroot . '/mod/workshop/lib.php'); // Include the code to test.
30 require_once($CFG->dirroot . '/mod/workshop/locallib.php'); // Include the code to test.
31 require_once($CFG->dirroot . '/lib/cronlib.php'); // Include the code to test.
32 require_once(__DIR__ . '/fixtures/testable.php');
35 /**
36  * Test cases for the internal workshop api
37  */
38 class mod_workshop_events_testcase extends advanced_testcase {
40     /** @var stdClass $workshop Basic workshop data stored in an object. */
41     protected $workshop;
42     /** @var stdClass $course Generated Random Course. */
43     protected $course;
44     /** @var stdClass mod info */
45     protected $cm;
46     /** @var context $context Course module context. */
47     protected $context;
49     /**
50      * Set up the testing environment.
51      */
52     protected function setUp() {
53         parent::setUp();
54         $this->setAdminUser();
56         // Create a workshop activity.
57         $this->course = $this->getDataGenerator()->create_course();
58         $this->workshop = $this->getDataGenerator()->create_module('workshop', array('course' => $this->course));
59         $this->cm = get_coursemodule_from_instance('workshop', $this->workshop->id);
60         $this->context = context_module::instance($this->cm->id);
61     }
63     protected function tearDown() {
64         $this->workshop = null;
65         $this->course = null;
66         $this->cm = null;
67         $this->context = null;
68         parent::tearDown();
69     }
71     /**
72      * This event is triggered in view.php and workshop/lib.php through the function workshop_cron().
73      */
74     public function test_phase_switched_event() {
75         $this->resetAfterTest();
76         $this->setAdminUser();
78         // Add additional workshop information.
79         $this->workshop->phase = 20;
80         $this->workshop->phaseswitchassessment = 1;
81         $this->workshop->submissionend = time() - 1;
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         ));
90         // Trigger and capture the event.
91         $sink = $this->redirectEvents();
92         $event->trigger();
93         $events = $sink->get_events();
94         $event = reset($events);
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,
98             $this->workshop->phase, $this->cm->id);
99         $this->assertEventLegacyLogData($expected, $event);
101         $sink->close();
102     }
104     public function test_assessment_evaluated() {
105         $this->resetAfterTest();
106         $this->setAdminUser();
108         $cm = get_coursemodule_from_instance('workshop', $this->workshop->id, $this->course->id, false, MUST_EXIST);
110         $workshop = new testable_workshop($this->workshop, $cm, $this->course);
112         $assessments = array();
113         $assessments[] = (object)array('reviewerid' => 2, 'gradinggrade' => null,
114             'gradinggradeover' => null, 'aggregationid' => null, 'aggregatedgrade' => 12);
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);
122         $this->assertInstanceOf('\mod_workshop\event\assessment_evaluated', $event);
123         $this->assertEquals('workshop_aggregations', $event->objecttable);
124         $this->assertEquals(context_module::instance($cm->id), $event->get_context());
126         $sink->close();
127     }
129     public function test_assessment_reevaluated() {
130         $this->resetAfterTest();
131         $this->setAdminUser();
133         $cm = get_coursemodule_from_instance('workshop', $this->workshop->id, $this->course->id, false, MUST_EXIST);
135         $workshop = new testable_workshop($this->workshop, $cm, $this->course);
137         $assessments = array();
138         $assessments[] = (object)array('reviewerid' => 2, 'gradinggrade' => null, 'gradinggradeover' => null,
139             'aggregationid' => 2, 'aggregatedgrade' => 12);
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);
147         $this->assertInstanceOf('\mod_workshop\event\assessment_reevaluated', $event);
148         $this->assertEquals('workshop_aggregations', $event->objecttable);
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);
152         $this->assertEventLegacyLogData($expected, $event);
154         $sink->close();
155     }
157     /**
158      * There is no api involved so the best we can do is test legacy data by triggering event manually.
159      */
160     public function test_aggregate_grades_reset_event() {
161         $this->resetAfterTest();
162         $this->setAdminUser();
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         ));
170         // Trigger and capture the event.
171         $sink = $this->redirectEvents();
172         $event->trigger();
173         $events = $sink->get_events();
174         $event = reset($events);
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,
178             $this->workshop->id, $this->cm->id);
179         $this->assertEventLegacyLogData($expected, $event);
181         $sink->close();
182     }
184     /**
185      * There is no api involved so the best we can do is test legacy data by triggering event manually.
186      */
187     public function test_instances_list_viewed_event() {
188         $this->resetAfterTest();
189         $this->setAdminUser();
191         $context = context_course::instance($this->course->id);
193         $event = \mod_workshop\event\instances_list_viewed::create(array('context' => $context));
195         // Trigger and capture the event.
196         $sink = $this->redirectEvents();
197         $event->trigger();
198         $events = $sink->get_events();
199         $event = reset($events);
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);
205         $sink->close();
206     }
208     /**
209      * There is no api involved so the best we can do is test legacy data by triggering event manually.
210      */
211     public function test_submission_created_event() {
212         $this->resetAfterTest();
213         $this->setAdminUser();
215         $user = $this->getDataGenerator()->create_user();
216         $submissionid = 48;
218         $event = \mod_workshop\event\submission_created::create(array(
219                 'objectid'      => $submissionid,
220                 'context'       => $this->context,
221                 'courseid'      => $this->course->id,
222                 'relateduserid' => $user->id,
223                 'other'         => array(
224                     'workshopid' => $this->workshop->id
225                 )
226             )
227         );
229         // Trigger and capture the event.
230         $sink = $this->redirectEvents();
231         $event->trigger();
232         $events = $sink->get_events();
233         $event = reset($events);
235         // Check that the legacy log data is valid.
236         $expected = array($this->course->id, 'workshop', 'add submission',
237             'submission.php?cmid=' . $this->workshop->id . '&id=' . $submissionid, $submissionid, $this->cm->id);
238         $this->assertEventLegacyLogData($expected, $event);
240         $sink->close();
241     }
243     /**
244      * There is no api involved so the best we can do is test legacy data by triggering event manually.
245      */
246     public function test_submission_updated_event() {
247         $this->resetAfterTest();
248         $this->setAdminUser();
250         $user = $this->getDataGenerator()->create_user();
251         $submissionid = 48;
253         $event = \mod_workshop\event\submission_updated::create(array(
254                 'objectid'      => $submissionid,
255                 'context'       => $this->context,
256                 'courseid'      => $this->course->id,
257                 'relateduserid' => $user->id,
258                 'other'         => array(
259                     'workshopid' => $this->workshop->id
260                 )
261             )
262         );
264         // Trigger and capture the event.
265         $sink = $this->redirectEvents();
266         $event->trigger();
267         $events = $sink->get_events();
268         $event = reset($events);
270         // Check that the legacy log data is valid.
271         $expected = array($this->course->id, 'workshop', 'update submission',
272             'submission.php?cmid=' . $this->workshop->id . '&id=' . $submissionid, $submissionid, $this->cm->id);
273         $this->assertEventLegacyLogData($expected, $event);
275         $sink->close();
276     }
278     /**
279      * There is no api involved so the best we can do is test legacy data by triggering event manually.
280      */
281     public function test_submission_viewed_event() {
282         $this->resetAfterTest();
283         $this->setAdminUser();
285         $user = $this->getDataGenerator()->create_user();
286         $submissionid = 48;
288         $event = \mod_workshop\event\submission_viewed::create(array(
289                 'objectid'      => $submissionid,
290                 'context'       => $this->context,
291                 'courseid'      => $this->course->id,
292                 'relateduserid' => $user->id,
293                 'other'         => array(
294                     'workshopid' => $this->workshop->id
295                 )
296             )
297         );
299         // Trigger and capture the event.
300         $sink = $this->redirectEvents();
301         $event->trigger();
302         $events = $sink->get_events();
303         $event = reset($events);
305         // Check that the legacy log data is valid.
306         $expected = array($this->course->id, 'workshop', 'view submission',
307             'submission.php?cmid=' . $this->workshop->id . '&id=' . $submissionid, $submissionid, $this->cm->id);
308         $this->assertEventLegacyLogData($expected, $event);
310         $sink->close();
311     }