MDL-40921 mod_workshop: Add assertEventContextNotUsed to events tests.
[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);
100         $this->assertEventContextNotUsed($event);
102         $sink->close();
103     }
105     public function test_assessment_evaluated() {
106         $this->resetAfterTest();
107         $this->setAdminUser();
109         $cm = get_coursemodule_from_instance('workshop', $this->workshop->id, $this->course->id, false, MUST_EXIST);
111         $workshop = new testable_workshop($this->workshop, $cm, $this->course);
113         $assessments = array();
114         $assessments[] = (object)array('reviewerid' => 2, 'gradinggrade' => null,
115             'gradinggradeover' => null, 'aggregationid' => null, 'aggregatedgrade' => 12);
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);
123         $this->assertInstanceOf('\mod_workshop\event\assessment_evaluated', $event);
124         $this->assertEquals('workshop_aggregations', $event->objecttable);
125         $this->assertEquals(context_module::instance($cm->id), $event->get_context());
126         $this->assertEventContextNotUsed($event);
128         $sink->close();
129     }
131     public function test_assessment_reevaluated() {
132         $this->resetAfterTest();
133         $this->setAdminUser();
135         $cm = get_coursemodule_from_instance('workshop', $this->workshop->id, $this->course->id, false, MUST_EXIST);
137         $workshop = new testable_workshop($this->workshop, $cm, $this->course);
139         $assessments = array();
140         $assessments[] = (object)array('reviewerid' => 2, 'gradinggrade' => null, 'gradinggradeover' => null,
141             'aggregationid' => 2, 'aggregatedgrade' => 12);
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);
149         $this->assertInstanceOf('\mod_workshop\event\assessment_reevaluated', $event);
150         $this->assertEquals('workshop_aggregations', $event->objecttable);
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);
154         $this->assertEventLegacyLogData($expected, $event);
155         $this->assertEventContextNotUsed($event);
157         $sink->close();
158     }
160     /**
161      * There is no api involved so the best we can do is test legacy data by triggering event manually.
162      */
163     public function test_aggregate_grades_reset_event() {
164         $this->resetAfterTest();
165         $this->setAdminUser();
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         ));
173         // Trigger and capture the event.
174         $sink = $this->redirectEvents();
175         $event->trigger();
176         $events = $sink->get_events();
177         $event = reset($events);
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,
181             $this->workshop->id, $this->cm->id);
182         $this->assertEventLegacyLogData($expected, $event);
184         $sink->close();
185     }
187     /**
188      * There is no api involved so the best we can do is test legacy data by triggering event manually.
189      */
190     public function test_instances_list_viewed_event() {
191         $this->resetAfterTest();
192         $this->setAdminUser();
194         $context = context_course::instance($this->course->id);
196         $event = \mod_workshop\event\instances_list_viewed::create(array('context' => $context));
198         // Trigger and capture the event.
199         $sink = $this->redirectEvents();
200         $event->trigger();
201         $events = $sink->get_events();
202         $event = reset($events);
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);
207         $this->assertEventContextNotUsed($event);
209         $sink->close();
210     }
212     /**
213      * There is no api involved so the best we can do is test legacy data by triggering event manually.
214      */
215     public function test_submission_created_event() {
216         $this->resetAfterTest();
217         $this->setAdminUser();
219         $user = $this->getDataGenerator()->create_user();
220         $submissionid = 48;
222         $event = \mod_workshop\event\submission_created::create(array(
223                 'objectid'      => $submissionid,
224                 'context'       => $this->context,
225                 'courseid'      => $this->course->id,
226                 'relateduserid' => $user->id,
227                 'other'         => array(
228                     'workshopid' => $this->workshop->id
229                 )
230             )
231         );
233         // Trigger and capture the event.
234         $sink = $this->redirectEvents();
235         $event->trigger();
236         $events = $sink->get_events();
237         $event = reset($events);
239         // Check that the legacy log data is valid.
240         $expected = array($this->course->id, 'workshop', 'add submission',
241             'submission.php?cmid=' . $this->workshop->id . '&id=' . $submissionid, $submissionid, $this->cm->id);
242         $this->assertEventLegacyLogData($expected, $event);
243         $this->assertEventContextNotUsed($event);
245         $sink->close();
246     }
248     /**
249      * There is no api involved so the best we can do is test legacy data by triggering event manually.
250      */
251     public function test_submission_updated_event() {
252         $this->resetAfterTest();
253         $this->setAdminUser();
255         $user = $this->getDataGenerator()->create_user();
256         $submissionid = 48;
258         $event = \mod_workshop\event\submission_updated::create(array(
259                 'objectid'      => $submissionid,
260                 'context'       => $this->context,
261                 'courseid'      => $this->course->id,
262                 'relateduserid' => $user->id,
263                 'other'         => array(
264                     'workshopid' => $this->workshop->id
265                 )
266             )
267         );
269         // Trigger and capture the event.
270         $sink = $this->redirectEvents();
271         $event->trigger();
272         $events = $sink->get_events();
273         $event = reset($events);
275         // Check that the legacy log data is valid.
276         $expected = array($this->course->id, 'workshop', 'update submission',
277             'submission.php?cmid=' . $this->workshop->id . '&id=' . $submissionid, $submissionid, $this->cm->id);
278         $this->assertEventLegacyLogData($expected, $event);
279         $this->assertEventContextNotUsed($event);
281         $sink->close();
282     }
284     /**
285      * There is no api involved so the best we can do is test legacy data by triggering event manually.
286      */
287     public function test_submission_viewed_event() {
288         $this->resetAfterTest();
289         $this->setAdminUser();
291         $user = $this->getDataGenerator()->create_user();
292         $submissionid = 48;
294         $event = \mod_workshop\event\submission_viewed::create(array(
295                 'objectid'      => $submissionid,
296                 'context'       => $this->context,
297                 'courseid'      => $this->course->id,
298                 'relateduserid' => $user->id,
299                 'other'         => array(
300                     'workshopid' => $this->workshop->id
301                 )
302             )
303         );
305         // Trigger and capture the event.
306         $sink = $this->redirectEvents();
307         $event->trigger();
308         $events = $sink->get_events();
309         $event = reset($events);
311         // Check that the legacy log data is valid.
312         $expected = array($this->course->id, 'workshop', 'view submission',
313             'submission.php?cmid=' . $this->workshop->id . '&id=' . $submissionid, $submissionid, $this->cm->id);
314         $this->assertEventLegacyLogData($expected, $event);
315         $this->assertEventContextNotUsed($event);
317         $sink->close();
318     }