31f4db0c13d424395b8e8dac44edd8f54321fd90
[moodle.git] / mod / scorm / tests / lib_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  * SCORM module library functions tests
19  *
20  * @package    mod_scorm
21  * @category   test
22  * @copyright  2015 Juan Leyva <juan@moodle.com>
23  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24  * @since      Moodle 3.0
25  */
27 defined('MOODLE_INTERNAL') || die();
29 global $CFG;
31 require_once($CFG->dirroot . '/webservice/tests/helpers.php');
32 require_once($CFG->dirroot . '/mod/scorm/lib.php');
34 /**
35  * SCORM module library functions tests
36  *
37  * @package    mod_scorm
38  * @category   test
39  * @copyright  2015 Juan Leyva <juan@moodle.com>
40  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
41  * @since      Moodle 3.0
42  */
43 class mod_scorm_lib_testcase extends externallib_advanced_testcase {
45     /**
46      * Set up for every test
47      */
48     public function setUp() {
49         global $DB;
50         $this->resetAfterTest();
51         $this->setAdminUser();
53         // Setup test data.
54         $this->course = $this->getDataGenerator()->create_course();
55         $this->scorm = $this->getDataGenerator()->create_module('scorm', array('course' => $this->course->id));
56         $this->context = context_module::instance($this->scorm->cmid);
57         $this->cm = get_coursemodule_from_instance('scorm', $this->scorm->id);
59         // Create users.
60         $this->student = self::getDataGenerator()->create_user();
61         $this->teacher = self::getDataGenerator()->create_user();
63         // Users enrolments.
64         $this->studentrole = $DB->get_record('role', array('shortname' => 'student'));
65         $this->teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher'));
66         $this->getDataGenerator()->enrol_user($this->student->id, $this->course->id, $this->studentrole->id, 'manual');
67         $this->getDataGenerator()->enrol_user($this->teacher->id, $this->course->id, $this->teacherrole->id, 'manual');
68     }
70     /**
71      * Test scorm_view
72      * @return void
73      */
74     public function test_scorm_view() {
75         global $CFG;
77         // Trigger and capture the event.
78         $sink = $this->redirectEvents();
80         scorm_view($this->scorm, $this->course, $this->cm, $this->context);
82         $events = $sink->get_events();
83         $this->assertCount(1, $events);
84         $event = array_shift($events);
86         // Checking that the event contains the expected values.
87         $this->assertInstanceOf('\mod_scorm\event\course_module_viewed', $event);
88         $this->assertEquals($this->context, $event->get_context());
89         $url = new \moodle_url('/mod/scorm/view.php', array('id' => $this->cm->id));
90         $this->assertEquals($url, $event->get_url());
91         $this->assertEventContextNotUsed($event);
92         $this->assertNotEmpty($event->get_name());
93     }
95     /**
96      * Test scorm_get_availability_status and scorm_require_available
97      * @return void
98      */
99     public function test_scorm_check_and_require_available() {
100         global $DB;
102         // Set to the student user.
103         self::setUser($this->student);
105         // Usual case.
106         list($status, $warnings) = scorm_get_availability_status($this->scorm, false);
107         $this->assertEquals(true, $status);
108         $this->assertCount(0, $warnings);
110         // SCORM not open.
111         $this->scorm->timeopen = time() + DAYSECS;
112         list($status, $warnings) = scorm_get_availability_status($this->scorm, false);
113         $this->assertEquals(false, $status);
114         $this->assertCount(1, $warnings);
116         // SCORM closed.
117         $this->scorm->timeopen = 0;
118         $this->scorm->timeclose = time() - DAYSECS;
119         list($status, $warnings) = scorm_get_availability_status($this->scorm, false);
120         $this->assertEquals(false, $status);
121         $this->assertCount(1, $warnings);
123         // SCORM not open and closed.
124         $this->scorm->timeopen = time() + DAYSECS;
125         list($status, $warnings) = scorm_get_availability_status($this->scorm, false);
126         $this->assertEquals(false, $status);
127         $this->assertCount(2, $warnings);
129         // Now additional checkings with different parameters values.
130         list($status, $warnings) = scorm_get_availability_status($this->scorm, true, $this->context);
131         $this->assertEquals(false, $status);
132         $this->assertCount(2, $warnings);
134         // SCORM not open.
135         $this->scorm->timeopen = time() + DAYSECS;
136         $this->scorm->timeclose = 0;
137         list($status, $warnings) = scorm_get_availability_status($this->scorm, true, $this->context);
138         $this->assertEquals(false, $status);
139         $this->assertCount(1, $warnings);
141         // SCORM closed.
142         $this->scorm->timeopen = 0;
143         $this->scorm->timeclose = time() - DAYSECS;
144         list($status, $warnings) = scorm_get_availability_status($this->scorm, true, $this->context);
145         $this->assertEquals(false, $status);
146         $this->assertCount(1, $warnings);
148         // SCORM not open and closed.
149         $this->scorm->timeopen = time() + DAYSECS;
150         list($status, $warnings) = scorm_get_availability_status($this->scorm, true, $this->context);
151         $this->assertEquals(false, $status);
152         $this->assertCount(2, $warnings);
154         // As teacher now.
155         self::setUser($this->teacher);
157         // SCORM not open and closed.
158         $this->scorm->timeopen = time() + DAYSECS;
159         list($status, $warnings) = scorm_get_availability_status($this->scorm, false);
160         $this->assertEquals(false, $status);
161         $this->assertCount(2, $warnings);
163         // Now, we use the special capability.
164         // SCORM not open and closed.
165         $this->scorm->timeopen = time() + DAYSECS;
166         list($status, $warnings) = scorm_get_availability_status($this->scorm, true, $this->context);
167         $this->assertEquals(true, $status);
168         $this->assertCount(0, $warnings);
170         // Check exceptions does not broke anything.
171         scorm_require_available($this->scorm, true, $this->context);
172         // Now, expect exceptions.
173         $this->setExpectedException('moodle_exception', get_string("notopenyet", "scorm", userdate($this->scorm->timeopen)));
175         // Now as student other condition.
176         self::setUser($this->student);
177         $this->scorm->timeopen = 0;
178         $this->scorm->timeclose = time() - DAYSECS;
180         $this->setExpectedException('moodle_exception', get_string("expired", "scorm", userdate($this->scorm->timeclose)));
181         scorm_require_available($this->scorm, false);
182     }