97ee85ec9b1afe7066e69b6e9824e54216803668
[moodle.git] / mod / scorm / 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  * This file contains tests for scorm events.
19  *
20  * @package    mod_scorm
21  * @copyright  2013 onwards Ankit Agarwal
22  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23  */
25 global $CFG;
26 require_once($CFG->dirroot . '/mod/scorm/locallib.php');
27 require_once($CFG->dirroot . '/mod/scorm/lib.php');
29 /**
30  * Test class for various events related to Scorm.
31  *
32  * @package    mod_scorm
33  * @copyright  2013 onwards Ankit Agarwal
34  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35  */
36 class mod_scorm_event_testcase extends advanced_testcase {
38     /** @var stdClass store course object */
39     protected $eventcourse;
41     /** @var stdClass store user object */
42     protected $eventuser;
44     /** @var stdClass store scorm object */
45     protected $eventscorm;
47     /** @var stdClass store course module object */
48     protected $eventcm;
50     protected function setUp() {
51         $this->setAdminUser();
52         $this->eventcourse = $this->getDataGenerator()->create_course();
53         $this->eventuser = $this->getDataGenerator()->create_user();
54         $record = new stdClass();
55         $record->course = $this->eventcourse->id;
56         $this->eventscorm = $this->getDataGenerator()->create_module('scorm', $record);
57         $this->eventcm = get_coursemodule_from_instance('scorm', $this->eventscorm->id);
58     }
60     /** Tests for attempt deleted event */
61     public function test_attempt_deleted_event() {
63         global $USER;
65         $this->resetAfterTest();
66         scorm_insert_track(2, $this->eventscorm->id, 1, 4, 'cmi.core.score.raw', 10);
67         $sink = $this->redirectEvents();
68         scorm_delete_attempt(2, $this->eventscorm, 4);
69         $events = $sink->get_events();
70         $sink->close();
71         $event = reset($events);
73         // Verify data.
74         $this->assertCount(1, $events);
75         $this->assertInstanceOf('\mod_scorm\event\attempt_deleted', $event);
76         $this->assertEquals($USER->id, $event->userid);
77         $this->assertEquals(context_module::instance($this->eventcm->id), $event->get_context());
78         $this->assertEquals(4, $event->other['attemptid']);
79         $this->assertEquals(2, $event->relateduserid);
80         $expected = array($this->eventcourse->id, 'scorm', 'delete attempts', 'report.php?id=' . $this->eventcm->id,
81                 4, $this->eventcm->id);
82         $this->assertEventLegacyLogData($expected, $events[0]);
83         $this->assertEventContextNotUsed($event);
85         // Test event validations.
86         $this->setExpectedException('coding_exception');
87         \mod_scorm\event\attempt_deleted::create(array(
88             'contextid' => 5,
89             'relateduserid' => 2
90         ));
91         $this->fail('event \\mod_scorm\\event\\attempt_deleted is not validating events properly');
92     }
94     /**
95      * Tests for course module viewed event.
96      *
97      * There is no api involved so the best we can do is test legacy data by triggering event manually.
98      */
99     public function test_course_module_viewed_event() {
100         $this->resetAfterTest();
101         $event = \mod_scorm\event\course_module_viewed::create(array(
102             'objectid' => $this->eventscorm->id,
103             'context' => context_module::instance($this->eventcm->id),
104             'courseid' => $this->eventcourse->id
105         ));
107         // Trigger and capture the event.
108         $sink = $this->redirectEvents();
109         $event->trigger();
110         $events = $sink->get_events();
111         $event = reset($events);
113         // Check that the legacy log data is valid.
114         $expected = array($this->eventcourse->id, 'scorm', 'pre-view', 'view.php?id=' . $this->eventcm->id,
115                 $this->eventscorm->id, $this->eventcm->id);
116         $this->assertEventLegacyLogData($expected, $event);
117         $this->assertEventContextNotUsed($event);
118     }
120     /**
121      * Tests for instance list viewed event.
122      *
123      * There is no api involved so the best we can do is test legacy data by triggering event manually.
124      */
125     public function test_course_module_instance_list_viewed_event() {
126         $this->resetAfterTest();
127         $event = \mod_scorm\event\course_module_instance_list_viewed::create(array(
128             'context' => context_course::instance($this->eventcourse->id),
129             'courseid' => $this->eventcourse->id
130         ));
132         // Trigger and capture the event.
133         $sink = $this->redirectEvents();
134         $event->trigger();
135         $events = $sink->get_events();
136         $event = reset($events);
138         // Check that the legacy log data is valid.
139         $expected = array($this->eventcourse->id, 'scorm', 'view all', 'index.php?id=' . $this->eventcourse->id, '');
140         $this->assertEventLegacyLogData($expected, $event);
141         $this->assertEventContextNotUsed($event);
142     }
144     /**
145      * Tests for interactions viewed.
146      *
147      * There is no api involved so the best we can do is test legacy data by triggering event manually and test validations.
148      */
149     public function test_interactions_viewed_event() {
150         $this->resetAfterTest();
151         $event = \mod_scorm\event\interactions_viewed::create(array(
152             'relateduserid' => 5,
153             'context' => context_module::instance($this->eventcm->id),
154             'courseid' => $this->eventcourse->id,
155             'other' => array('attemptid' => 2, 'instanceid' => $this->eventscorm->id)
156         ));
158         // Trigger and capture the event.
159         $sink = $this->redirectEvents();
160         $event->trigger();
161         $events = $sink->get_events();
162         $event = reset($events);
164         // Check that the legacy log data is valid.
165         $expected = array($this->eventcourse->id, 'scorm', 'userreportinteractions', 'report/userreportinteractions.php?id=' .
166                 $this->eventcm->id . '&user=5&attempt=' . 2, $this->eventscorm->id, $this->eventcm->id);
167         $this->assertEventLegacyLogData($expected, $event);
168         $this->assertEventContextNotUsed($event);
169     }
171     /**
172      * Tests for interactions viewed validations.
173      */
174     public function test_interactions_viewed_event_validations() {
175         $this->resetAfterTest();
176         try {
177             \mod_scorm\event\interactions_viewed::create(array(
178                 'context' => context_module::instance($this->eventcm->id),
179                 'courseid' => $this->eventcourse->id,
180                 'other' => array('attemptid' => 2)
181             ));
182             $this->fail("Event validation should not allow \\mod_scorm\\event\\interactions_viewed to be triggered without
183                     other['instanceid']");
184         } catch (Exception $e) {
185             $this->assertInstanceOf('coding_exception', $e);
186         }
187         try {
188             \mod_scorm\event\interactions_viewed::create(array(
189                 'context' => context_module::instance($this->eventcm->id),
190                 'courseid' => $this->eventcourse->id,
191                 'other' => array('instanceid' => 2)
192             ));
193             $this->fail("Event validation should not allow \\mod_scorm\\event\\interactions_viewed to be triggered without
194                     other['attemptid']");
195         } catch (Exception $e) {
196             $this->assertInstanceOf('coding_exception', $e);
197         }
198     }
200     /** Tests for report viewed.
201      *
202      * There is no api involved so the best we can do is test legacy data and validations by triggering event manually.
203      */
204     public function test_report_viewed_event() {
205         $this->resetAfterTest();
206         $event = \mod_scorm\event\report_viewed::create(array(
207              'context' => context_module::instance($this->eventcm->id),
208              'courseid' => $this->eventcourse->id,
209              'other' => array(
210                  'scormid' => $this->eventscorm->id,
211                  'mode' => 'basic'
212              )
213         ));
215         // Trigger and capture the event.
216         $sink = $this->redirectEvents();
217         $event->trigger();
218         $events = $sink->get_events();
219         $event = reset($events);
221         // Check that the legacy log data is valid.
222         $expected = array($this->eventcourse->id, 'scorm', 'report', 'report.php?id=' . $this->eventcm->id . '&mode=basic',
223                 $this->eventscorm->id, $this->eventcm->id);
224         $this->assertEventLegacyLogData($expected, $event);
225         $this->assertEventContextNotUsed($event);
226     }
228     /** Tests for sco launched event.
229      *
230      * There is no api involved so the best we can do is test legacy data and validations by triggering event manually.
231      */
232     public function test_sco_launched_event() {
233         $this->resetAfterTest();
234         $event = \mod_scorm\event\sco_launched::create(array(
235              'objectid' => 2,
236              'context' => context_module::instance($this->eventcm->id),
237              'courseid' => $this->eventcourse->id,
238              'other' => array('loadedcontent' => 'url_to_content_that_was_laoded.php')
239         ));
241         // Trigger and capture the event.
242         $sink = $this->redirectEvents();
243         $event->trigger();
244         $events = $sink->get_events();
245         $event = reset($events);
247         // Check that the legacy log data is valid.
248         $expected = array($this->eventcourse->id, 'scorm', 'launch', 'view.php?id=' . $this->eventcm->id,
249                           'url_to_content_that_was_laoded.php', $this->eventcm->id);
250         $this->assertEventLegacyLogData($expected, $event);
251         $this->assertEventContextNotUsed($event);
253         // Test validations.
254         $this->setExpectedException('coding_exception');
255         \mod_scorm\event\sco_launched::create(array(
256              'objectid' => $this->eventscorm->id,
257              'context' => context_module::instance($this->eventcm->id),
258              'courseid' => $this->eventcourse->id,
259         ));
260         $this->fail('Event \\mod_scorm\\event\\sco_launched is not validating "loadedcontent" properly');
261     }
263     /**
264      * Tests for tracks viewed event.
265      *
266      * There is no api involved so the best we can do is test validations by triggering event manually.
267      */
268     public function test_tracks_viewed_event() {
269         $this->resetAfterTest();
270         $event = \mod_scorm\event\tracks_viewed::create(array(
271             'relateduserid' => 5,
272             'context' => context_module::instance($this->eventcm->id),
273             'courseid' => $this->eventcourse->id,
274             'other' => array('attemptid' => 2, 'instanceid' => $this->eventscorm->id, 'scoid' => 3)
275         ));
277         // Trigger and capture the event.
278         $sink = $this->redirectEvents();
279         $event->trigger();
280         $events = $sink->get_events();
281         $event = reset($events);
283         // Check that the legacy log data is valid.
284         $expected = array($this->eventcourse->id, 'scorm', 'userreporttracks', 'report/userreporttracks.php?id=' .
285                 $this->eventcm->id . '&user=5&attempt=' . 2 . '&scoid=3', $this->eventscorm->id, $this->eventcm->id);
286         $this->assertEventLegacyLogData($expected, $event);
287         $this->assertEventContextNotUsed($event);
288     }
290     /**
291      * Tests for tracks viewed event validations.
292      */
293     public function test_tracks_viewed_event_validations() {
294         $this->resetAfterTest();
295         try {
296             \mod_scorm\event\tracks_viewed::create(array(
297                 'context' => context_module::instance($this->eventcm->id),
298                 'courseid' => $this->eventcourse->id,
299                 'other' => array('attemptid' => 2, 'scoid' => 2)
300             ));
301             $this->fail("Event validation should not allow \\mod_scorm\\event\\tracks_viewed to be triggered without
302                     other['instanceid']");
303         } catch (Exception $e) {
304             $this->assertInstanceOf('coding_exception', $e);
305         }
306         try {
307             \mod_scorm\event\tracks_viewed::create(array(
308                 'context' => context_module::instance($this->eventcm->id),
309                 'courseid' => $this->eventcourse->id,
310                 'other' => array('instanceid' => 2, 'scoid' => 2)
311             ));
312             $this->fail("Event validation should not allow \\mod_scorm\\event\\tracks_viewed to be triggered without
313                     other['attemptid']");
314         } catch (Exception $e) {
315             $this->assertInstanceOf('coding_exception', $e);
316         }
318         try {
319             \mod_scorm\event\tracks_viewed::create(array(
320                 'context' => context_module::instance($this->eventcm->id),
321                 'courseid' => $this->eventcourse->id,
322                 'other' => array('attemptid' => 2, 'instanceid' => 2)
323             ));
324             $this->fail("Event validation should not allow \\mod_scorm\\event\\tracks_viewed to be triggered without
325                     other['scoid']");
326         } catch (Exception $e) {
327             $this->assertInstanceOf('coding_exception', $e);
328         }
329     }
331     /**
332      * Tests for userreport viewed event.
333      *
334      * There is no api involved so the best we can do is test validations and legacy log by triggering event manually.
335      */
336     public function test_user_report_viewed_event() {
337         $this->resetAfterTest();
338         $event = \mod_scorm\event\user_report_viewed::create(array(
339             'relateduserid' => 5,
340             'context' => context_module::instance($this->eventcm->id),
341             'courseid' => $this->eventcourse->id,
342             'other' => array('attemptid' => 2, 'instanceid' => $this->eventscorm->id)
343         ));
345         // Trigger and capture the event.
346         $sink = $this->redirectEvents();
347         $event->trigger();
348         $events = $sink->get_events();
349         $event = reset($events);
351         // Check that the legacy log data is valid.
352         $expected = array($this->eventcourse->id, 'scorm', 'userreport', 'report/userreport.php?id=' .
353                 $this->eventcm->id . '&user=5&attempt=' . 2, $this->eventscorm->id, $this->eventcm->id);
354         $this->assertEventLegacyLogData($expected, $event);
355         $this->assertEventContextNotUsed($event);
356     }
358     /**
359      * Tests for userreport viewed event validations.
360      */
361     public function test_user_report_viewed_event_validations() {
362         $this->resetAfterTest();
363         try {
364             \mod_scorm\event\user_report_viewed::create(array(
365                 'context' => context_module::instance($this->eventcm->id),
366                 'courseid' => $this->eventcourse->id,
367                 'other' => array('attemptid' => 2)
368             ));
369             $this->fail("Event validation should not allow \\mod_scorm\\event\\user_report_viewed to be triggered without
370                     other['instanceid']");
371         } catch (Exception $e) {
372             $this->assertInstanceOf('coding_exception', $e);
373         }
374         try {
375             \mod_scorm\event\user_report_viewed::create(array(
376                 'context' => context_module::instance($this->eventcm->id),
377                 'courseid' => $this->eventcourse->id,
378                 'other' => array('instanceid' => 2)
379             ));
380             $this->fail("Event validation should not allow \\mod_scorm\\event\\user_report_viewed to be triggered without
381                     other['attemptid']");
382         } catch (Exception $e) {
383             $this->assertInstanceOf('coding_exception', $e);
384         }
385     }