MDL-52683 mod_scorm: Test for scorm_get_last_completed_attempt
[moodle.git] / mod / scorm / tests / lib_test.php
CommitLineData
ade341de
JL
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 * 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 */
26
27defined('MOODLE_INTERNAL') || die();
28
29global $CFG;
30
31require_once($CFG->dirroot . '/webservice/tests/helpers.php');
32require_once($CFG->dirroot . '/mod/scorm/lib.php');
33
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 */
43class mod_scorm_lib_testcase extends externallib_advanced_testcase {
44
22de67f4
JL
45 /**
46 * Set up for every test
47 */
48 public function setUp() {
49 global $DB;
50 $this->resetAfterTest();
51 $this->setAdminUser();
52
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);
58
59 // Create users.
60 $this->student = self::getDataGenerator()->create_user();
61 $this->teacher = self::getDataGenerator()->create_user();
62
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 }
69
ade341de
JL
70 /**
71 * Test scorm_view
72 * @return void
73 */
74 public function test_scorm_view() {
75 global $CFG;
76
ade341de
JL
77 // Trigger and capture the event.
78 $sink = $this->redirectEvents();
79
22de67f4 80 scorm_view($this->scorm, $this->course, $this->cm, $this->context);
ade341de
JL
81
82 $events = $sink->get_events();
83 $this->assertCount(1, $events);
84 $event = array_shift($events);
85
86 // Checking that the event contains the expected values.
87 $this->assertInstanceOf('\mod_scorm\event\course_module_viewed', $event);
22de67f4
JL
88 $this->assertEquals($this->context, $event->get_context());
89 $url = new \moodle_url('/mod/scorm/view.php', array('id' => $this->cm->id));
ade341de
JL
90 $this->assertEquals($url, $event->get_url());
91 $this->assertEventContextNotUsed($event);
92 $this->assertNotEmpty($event->get_name());
93 }
94
22de67f4
JL
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;
101
102 // Set to the student user.
103 self::setUser($this->student);
104
105 // Usual case.
106 list($status, $warnings) = scorm_get_availability_status($this->scorm, false);
107 $this->assertEquals(true, $status);
108 $this->assertCount(0, $warnings);
109
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);
115
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);
122
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);
128
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);
133
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);
140
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);
147
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);
153
154 // As teacher now.
155 self::setUser($this->teacher);
156
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);
162
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);
169
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)));
174
175 // Now as student other condition.
176 self::setUser($this->student);
177 $this->scorm->timeopen = 0;
178 $this->scorm->timeclose = time() - DAYSECS;
179
180 $this->setExpectedException('moodle_exception', get_string("expired", "scorm", userdate($this->scorm->timeclose)));
181 scorm_require_available($this->scorm, false);
182 }
183
33854a29
DM
184 /**
185 * Test scorm_get_last_completed_attempt
186 *
187 * @return void
188 */
189 public function test_scorm_get_last_completed_attempt() {
190 $this->assertEquals(1, scorm_get_last_completed_attempt($this->scorm->id, $this->student->id));
191 }
ade341de 192}