Proyectos de Subversion Moodle

Rev

Rev 11 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
 * 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
 */
24
 
25
namespace mod_scorm\event;
26
 
27
defined('MOODLE_INTERNAL') || die();
28
 
29
global $CFG;
30
require_once($CFG->dirroot . '/mod/scorm/locallib.php');
31
require_once($CFG->dirroot . '/mod/scorm/lib.php');
32
 
33
/**
34
 * Test class for various events related to Scorm.
35
 *
36
 * @package    mod_scorm
37
 * @copyright  2013 onwards Ankit Agarwal
38
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39
 */
1441 ariadna 40
final class events_test extends \advanced_testcase {
1 efrain 41
 
42
    /** @var stdClass store course object */
43
    protected $eventcourse;
44
 
45
    /** @var stdClass store user object */
46
    protected $eventuser;
47
 
48
    /** @var stdClass store scorm object */
49
    protected $eventscorm;
50
 
51
    /** @var stdClass store course module object */
52
    protected $eventcm;
53
 
54
    protected function setUp(): void {
1441 ariadna 55
        parent::setUp();
1 efrain 56
        $this->setAdminUser();
57
        $this->eventcourse = $this->getDataGenerator()->create_course();
58
        $this->eventuser = $this->getDataGenerator()->create_user();
59
        $record = new \stdClass();
60
        $record->course = $this->eventcourse->id;
61
        $this->eventscorm = $this->getDataGenerator()->create_module('scorm', $record);
62
        $this->eventcm = get_coursemodule_from_instance('scorm', $this->eventscorm->id);
63
    }
64
 
65
    /**
66
     * Tests for attempt deleted event
67
     */
11 efrain 68
    public function test_attempt_deleted_event(): void {
1 efrain 69
 
70
        global $USER;
71
 
72
        $this->resetAfterTest();
73
        scorm_insert_track(2, $this->eventscorm->id, 1, 4, 'cmi.core.score.raw', 10);
74
        $sink = $this->redirectEvents();
75
        scorm_delete_attempt(2, $this->eventscorm, 4);
76
        $events = $sink->get_events();
77
        $sink->close();
78
        $event = reset($events);
79
 
80
        // Verify data.
81
        $this->assertCount(3, $events);
82
        $this->assertInstanceOf('\mod_scorm\event\attempt_deleted', $event);
83
        $this->assertEquals($USER->id, $event->userid);
84
        $this->assertEquals(\context_module::instance($this->eventcm->id), $event->get_context());
85
        $this->assertEquals(4, $event->other['attemptid']);
86
        $this->assertEquals(2, $event->relateduserid);
87
        $this->assertEventContextNotUsed($event);
88
 
89
        // Test event validations.
90
        $this->expectException(\coding_exception::class);
91
        \mod_scorm\event\attempt_deleted::create(array(
92
            'contextid' => 5,
93
            'relateduserid' => 2
94
        ));
95
    }
96
 
97
    /**
98
     * Tests for interactions viewed validations.
99
     */
11 efrain 100
    public function test_interactions_viewed_event_validations(): void {
1 efrain 101
        $this->resetAfterTest();
102
        try {
103
            \mod_scorm\event\interactions_viewed::create(array(
104
                'context' => \context_module::instance($this->eventcm->id),
105
                'courseid' => $this->eventcourse->id,
106
                'other' => array('attemptid' => 2)
107
            ));
108
            $this->fail("Event validation should not allow \\mod_scorm\\event\\interactions_viewed to be triggered without
109
                    other['instanceid']");
110
        } catch (\Exception $e) {
111
            $this->assertInstanceOf('coding_exception', $e);
112
        }
113
        try {
114
            \mod_scorm\event\interactions_viewed::create(array(
115
                'context' => \context_module::instance($this->eventcm->id),
116
                'courseid' => $this->eventcourse->id,
117
                'other' => array('instanceid' => 2)
118
            ));
119
            $this->fail("Event validation should not allow \\mod_scorm\\event\\interactions_viewed to be triggered without
120
                    other['attemptid']");
121
        } catch (\Exception $e) {
122
            $this->assertInstanceOf('coding_exception', $e);
123
        }
124
    }
125
 
126
    /**
127
     * Tests for tracks viewed event validations.
128
     */
11 efrain 129
    public function test_tracks_viewed_event_validations(): void {
1 efrain 130
        $this->resetAfterTest();
131
        try {
132
            \mod_scorm\event\tracks_viewed::create(array(
133
                'context' => \context_module::instance($this->eventcm->id),
134
                'courseid' => $this->eventcourse->id,
135
                'other' => array('attemptid' => 2, 'scoid' => 2)
136
            ));
137
            $this->fail("Event validation should not allow \\mod_scorm\\event\\tracks_viewed to be triggered without
138
                    other['instanceid']");
139
        } catch (\Exception $e) {
140
            $this->assertInstanceOf('coding_exception', $e);
141
        }
142
        try {
143
            \mod_scorm\event\tracks_viewed::create(array(
144
                'context' => \context_module::instance($this->eventcm->id),
145
                'courseid' => $this->eventcourse->id,
146
                'other' => array('instanceid' => 2, 'scoid' => 2)
147
            ));
148
            $this->fail("Event validation should not allow \\mod_scorm\\event\\tracks_viewed to be triggered without
149
                    other['attemptid']");
150
        } catch (\Exception $e) {
151
            $this->assertInstanceOf('coding_exception', $e);
152
        }
153
 
154
        try {
155
            \mod_scorm\event\tracks_viewed::create(array(
156
                'context' => \context_module::instance($this->eventcm->id),
157
                'courseid' => $this->eventcourse->id,
158
                'other' => array('attemptid' => 2, 'instanceid' => 2)
159
            ));
160
            $this->fail("Event validation should not allow \\mod_scorm\\event\\tracks_viewed to be triggered without
161
                    other['scoid']");
162
        } catch (\Exception $e) {
163
            $this->assertInstanceOf('coding_exception', $e);
164
        }
165
    }
166
 
167
    /**
168
     * Tests for userreport viewed event validations.
169
     */
11 efrain 170
    public function test_user_report_viewed_event_validations(): void {
1 efrain 171
        $this->resetAfterTest();
172
        try {
173
            \mod_scorm\event\user_report_viewed::create(array(
174
                'context' => \context_module::instance($this->eventcm->id),
175
                'courseid' => $this->eventcourse->id,
176
                'other' => array('attemptid' => 2)
177
            ));
178
            $this->fail("Event validation should not allow \\mod_scorm\\event\\user_report_viewed to be triggered without
179
                    other['instanceid']");
180
        } catch (\Exception $e) {
181
            $this->assertInstanceOf('coding_exception', $e);
182
        }
183
        try {
184
            \mod_scorm\event\user_report_viewed::create(array(
185
                'context' => \context_module::instance($this->eventcm->id),
186
                'courseid' => $this->eventcourse->id,
187
                'other' => array('instanceid' => 2)
188
            ));
189
            $this->fail("Event validation should not allow \\mod_scorm\\event\\user_report_viewed to be triggered without
190
                    other['attemptid']");
191
        } catch (\Exception $e) {
192
            $this->assertInstanceOf('coding_exception', $e);
193
        }
194
    }
195
 
196
    /**
197
     * dataProvider for test_scoreraw_submitted_event().
198
     */
199
    public function get_scoreraw_submitted_event_provider() {
200
        return array(
201
            // SCORM 1.2.
202
            // - cmi.core.score.raw.
203
            'cmi.core.score.raw => 100' => array('cmi.core.score.raw', '100'),
204
            'cmi.core.score.raw => 90' => array('cmi.core.score.raw', '90'),
205
            'cmi.core.score.raw => 50' => array('cmi.core.score.raw', '50'),
206
            'cmi.core.score.raw => 10' => array('cmi.core.score.raw', '10'),
207
            // Check an edge case (PHP empty() vs isset()): score value equals to '0'.
208
            'cmi.core.score.raw => 0' => array('cmi.core.score.raw', '0'),
209
            // SCORM 1.3 AKA 2004.
210
            // - cmi.score.raw.
211
            'cmi.score.raw => 100' => array('cmi.score.raw', '100'),
212
            'cmi.score.raw => 90' => array('cmi.score.raw', '90'),
213
            'cmi.score.raw => 50' => array('cmi.score.raw', '50'),
214
            'cmi.score.raw => 10' => array('cmi.score.raw', '10'),
215
            // Check an edge case (PHP empty() vs isset()): score value equals to '0'.
216
            'cmi.score.raw => 0' => array('cmi.score.raw', '0'),
217
        );
218
    }
219
 
220
    /**
221
     * dataProvider for test_scoreraw_submitted_event_validations().
222
     */
1441 ariadna 223
    public static function get_scoreraw_submitted_event_validations(): array {
1 efrain 224
        return array(
225
            'scoreraw_submitted => missing cmielement' => array(
226
                null, '50',
227
                "Event validation should not allow \\mod_scorm\\event\\scoreraw_submitted " .
228
                    "to be triggered without other['cmielement']",
229
                'Coding error detected, it must be fixed by a programmer: ' .
230
                    "The 'cmielement' must be set in other."
231
            ),
232
            'scoreraw_submitted => missing cmivalue' => array(
233
                'cmi.core.score.raw', null,
234
                "Event validation should not allow \\mod_scorm\\event\\scoreraw_submitted " .
235
                    "to be triggered without other['cmivalue']",
236
                'Coding error detected, it must be fixed by a programmer: ' .
237
                    "The 'cmivalue' must be set in other."
238
            ),
239
            'scoreraw_submitted => wrong CMI element' => array(
240
                'cmi.core.lesson_status', '50',
241
                "Event validation should not allow \\mod_scorm\\event\\scoreraw_submitted " .
242
                    'to be triggered with a CMI element not representing a raw score',
243
                'Coding error detected, it must be fixed by a programmer: ' .
244
                    "The 'cmielement' must represents a valid CMI raw score (cmi.core.lesson_status)."
245
            ),
246
        );
247
    }
248
 
249
    /**
250
     * Tests for score submitted event validations.
251
     *
252
     * @dataProvider get_scoreraw_submitted_event_validations
253
     *
254
     * @param string $cmielement a valid CMI raw score element
255
     * @param string $cmivalue a valid CMI raw score value
256
     * @param string $failmessage the message used to fail the test in case of missing to violate a validation rule
257
     * @param string $excmessage the exception message when violating the validations rules
258
     */
11 efrain 259
    public function test_scoreraw_submitted_event_validations($cmielement, $cmivalue, $failmessage, $excmessage): void {
1 efrain 260
        $this->resetAfterTest();
261
        try {
262
            $data = array(
263
                'context' => \context_module::instance($this->eventcm->id),
264
                'courseid' => $this->eventcourse->id,
265
                'other' => array('attemptid' => 2)
266
            );
267
            if ($cmielement != null) {
268
                $data['other']['cmielement'] = $cmielement;
269
            }
270
            if ($cmivalue != null) {
271
                $data['other']['cmivalue'] = $cmivalue;
272
            }
273
            \mod_scorm\event\scoreraw_submitted::create($data);
274
            $this->fail($failmessage);
275
        } catch (\Exception $e) {
276
            $this->assertInstanceOf('coding_exception', $e);
277
            $this->assertEquals($excmessage, $e->getMessage());
278
        }
279
    }
280
 
281
    /**
282
     * dataProvider for test_status_submitted_event().
283
     */
284
    public function get_status_submitted_event_provider() {
285
        return array(
286
            // SCORM 1.2.
287
            // 1. Status: cmi.core.lesson_status.
288
            'cmi.core.lesson_status => passed' => array('cmi.core.lesson_status', 'passed'),
289
            'cmi.core.lesson_status => completed' => array('cmi.core.lesson_status', 'completed'),
290
            'cmi.core.lesson_status => failed' => array('cmi.core.lesson_status', 'failed'),
291
            'cmi.core.lesson_status => incomplete' => array('cmi.core.lesson_status', 'incomplete'),
292
            'cmi.core.lesson_status => browsed' => array('cmi.core.lesson_status', 'browsed'),
293
            'cmi.core.lesson_status => not attempted' => array('cmi.core.lesson_status', 'not attempted'),
294
            // SCORM 1.3 AKA 2004.
295
            // 1. Completion status: cmi.completion_status.
296
            'cmi.completion_status => completed' => array('cmi.completion_status', 'completed'),
297
            'cmi.completion_status => incomplete' => array('cmi.completion_status', 'incomplete'),
298
            'cmi.completion_status => not attempted' => array('cmi.completion_status', 'not attempted'),
299
            'cmi.completion_status => unknown' => array('cmi.completion_status', 'unknown'),
300
            // 2. Success status: cmi.success_status.
301
            'cmi.success_status => passed' => array('cmi.success_status', 'passed'),
302
            'cmi.success_status => failed' => array('cmi.success_status', 'failed'),
303
            'cmi.success_status => unknown' => array('cmi.success_status', 'unknown')
304
        );
305
    }
306
 
307
    /**
308
     * dataProvider for test_status_submitted_event_validations().
309
     */
1441 ariadna 310
    public static function get_status_submitted_event_validations(): array {
1 efrain 311
        return array(
312
            'status_submitted => missing cmielement' => array(
313
                null, 'passed',
314
                "Event validation should not allow \\mod_scorm\\event\\status_submitted " .
315
                    "to be triggered without other['cmielement']",
316
                'Coding error detected, it must be fixed by a programmer: ' .
317
                    "The 'cmielement' must be set in other."
318
            ),
319
            'status_submitted => missing cmivalue' => array(
320
                'cmi.core.lesson_status', null,
321
                "Event validation should not allow \\mod_scorm\\event\\status_submitted " .
322
                    "to be triggered without other['cmivalue']",
323
                'Coding error detected, it must be fixed by a programmer: ' .
324
                    "The 'cmivalue' must be set in other."
325
            ),
326
            'status_submitted => wrong CMI element' => array(
327
                'cmi.core.score.raw', 'passed',
328
                "Event validation should not allow \\mod_scorm\\event\\status_submitted " .
329
                    'to be triggered with a CMI element not representing a valid CMI status element',
330
                'Coding error detected, it must be fixed by a programmer: ' .
331
                    "The 'cmielement' must represents a valid CMI status element (cmi.core.score.raw)."
332
            ),
333
            'status_submitted => wrong CMI value' => array(
334
                'cmi.core.lesson_status', 'blahblahblah',
335
                "Event validation should not allow \\mod_scorm\\event\\status_submitted " .
336
                    'to be triggered with a CMI element not representing a valid CMI status',
337
                'Coding error detected, it must be fixed by a programmer: ' .
338
                    "The 'cmivalue' must represents a valid CMI status value (blahblahblah)."
339
            ),
340
        );
341
    }
342
 
343
    /**
344
     * Tests for status submitted event validations.
345
     *
346
     * @dataProvider get_status_submitted_event_validations
347
     *
348
     * @param string $cmielement a valid CMI status element
349
     * @param string $cmivalue a valid CMI status value
350
     * @param string $failmessage the message used to fail the test in case of missing to violate a validation rule
351
     * @param string $excmessage the exception message when violating the validations rules
352
     */
11 efrain 353
    public function test_status_submitted_event_validations($cmielement, $cmivalue, $failmessage, $excmessage): void {
1 efrain 354
        $this->resetAfterTest();
355
        try {
356
            $data = array(
357
                'context' => \context_module::instance($this->eventcm->id),
358
                'courseid' => $this->eventcourse->id,
359
                'other' => array('attemptid' => 2)
360
            );
361
            if ($cmielement != null) {
362
                $data['other']['cmielement'] = $cmielement;
363
            }
364
            if ($cmivalue != null) {
365
                $data['other']['cmivalue'] = $cmivalue;
366
            }
367
            \mod_scorm\event\status_submitted::create($data);
368
            $this->fail($failmessage);
369
        } catch (\Exception $e) {
370
            $this->assertInstanceOf('coding_exception', $e);
371
            $this->assertEquals($excmessage, $e->getMessage());
372
        }
373
    }
374
}