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 |
* Unit tests for (some of) mod/imscp/lib.php.
|
|
|
19 |
*
|
|
|
20 |
* @package mod_imscp
|
|
|
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 |
*/
|
|
|
25 |
namespace mod_imscp;
|
|
|
26 |
|
|
|
27 |
defined('MOODLE_INTERNAL') || die();
|
|
|
28 |
|
|
|
29 |
global $CFG;
|
|
|
30 |
require_once($CFG->dirroot . '/mod/imscp/lib.php');
|
|
|
31 |
|
|
|
32 |
/**
|
|
|
33 |
* Unit tests for (some of) mod/imscp/lib.php.
|
|
|
34 |
*
|
|
|
35 |
* @package mod_imscp
|
|
|
36 |
* @category test
|
|
|
37 |
* @copyright 2015 Juan Leyva <juan@moodle.com>
|
|
|
38 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
39 |
*/
|
|
|
40 |
class lib_test extends \advanced_testcase {
|
|
|
41 |
|
11 |
efrain |
42 |
public function test_export_contents(): void {
|
1 |
efrain |
43 |
global $DB, $USER;
|
|
|
44 |
|
|
|
45 |
$this->resetAfterTest(true);
|
|
|
46 |
|
|
|
47 |
$user = $this->getDataGenerator()->create_user();
|
|
|
48 |
$course = $this->getDataGenerator()->create_course();
|
|
|
49 |
$studentrole = $DB->get_record('role', array('shortname' => 'student'));
|
|
|
50 |
$this->getDataGenerator()->enrol_user($user->id, $course->id, $studentrole->id);
|
|
|
51 |
|
|
|
52 |
$this->setAdminUser();
|
|
|
53 |
$imscp = $this->getDataGenerator()->create_module('imscp', array('course' => $course->id));
|
|
|
54 |
$cm = get_coursemodule_from_id('imscp', $imscp->cmid);
|
|
|
55 |
|
|
|
56 |
$this->setUser($user);
|
|
|
57 |
$contents = imscp_export_contents($cm, '');
|
|
|
58 |
|
|
|
59 |
// The test package contains 47 files.
|
|
|
60 |
$this->assertCount(47, $contents);
|
|
|
61 |
// The structure is present.
|
|
|
62 |
$this->assertEquals('structure', $contents[0]['filename']);
|
|
|
63 |
// The structure is returned and it maches the expected one.
|
|
|
64 |
$this->assertEquals(json_encode(unserialize($imscp->structure)), $contents[0]['content']);
|
|
|
65 |
|
|
|
66 |
}
|
|
|
67 |
|
|
|
68 |
/**
|
|
|
69 |
* Test imscp_view
|
|
|
70 |
* @return void
|
|
|
71 |
*/
|
11 |
efrain |
72 |
public function test_imscp_view(): void {
|
1 |
efrain |
73 |
global $CFG;
|
|
|
74 |
|
|
|
75 |
$CFG->enablecompletion = 1;
|
|
|
76 |
$this->resetAfterTest();
|
|
|
77 |
|
|
|
78 |
$this->setAdminUser();
|
|
|
79 |
// Setup test data.
|
|
|
80 |
$course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
|
|
|
81 |
$imscp = $this->getDataGenerator()->create_module('imscp', array('course' => $course->id),
|
|
|
82 |
array('completion' => 2, 'completionview' => 1));
|
|
|
83 |
$context = \context_module::instance($imscp->cmid);
|
|
|
84 |
$cm = get_coursemodule_from_instance('imscp', $imscp->id);
|
|
|
85 |
|
|
|
86 |
// Trigger and capture the event.
|
|
|
87 |
$sink = $this->redirectEvents();
|
|
|
88 |
|
|
|
89 |
imscp_view($imscp, $course, $cm, $context);
|
|
|
90 |
|
|
|
91 |
$events = $sink->get_events();
|
|
|
92 |
// 2 additional events thanks to completion.
|
|
|
93 |
$this->assertCount(3, $events);
|
|
|
94 |
$event = array_shift($events);
|
|
|
95 |
|
|
|
96 |
// Checking that the event contains the expected values.
|
|
|
97 |
$this->assertInstanceOf('\mod_imscp\event\course_module_viewed', $event);
|
|
|
98 |
$this->assertEquals($context, $event->get_context());
|
|
|
99 |
$moodleurl = new \moodle_url('/mod/imscp/view.php', array('id' => $cm->id));
|
|
|
100 |
$this->assertEquals($moodleurl, $event->get_url());
|
|
|
101 |
$this->assertEventContextNotUsed($event);
|
|
|
102 |
$this->assertNotEmpty($event->get_name());
|
|
|
103 |
|
|
|
104 |
// Check completion status.
|
|
|
105 |
$completion = new \completion_info($course);
|
|
|
106 |
$completiondata = $completion->get_data($cm);
|
|
|
107 |
$this->assertEquals(1, $completiondata->completionstate);
|
|
|
108 |
}
|
|
|
109 |
|
11 |
efrain |
110 |
public function test_imscp_core_calendar_provide_event_action(): void {
|
1 |
efrain |
111 |
$this->resetAfterTest();
|
|
|
112 |
$this->setAdminUser();
|
|
|
113 |
|
|
|
114 |
// Create the activity.
|
|
|
115 |
$course = $this->getDataGenerator()->create_course();
|
|
|
116 |
$imscp = $this->getDataGenerator()->create_module('imscp', array('course' => $course->id));
|
|
|
117 |
|
|
|
118 |
// Create a calendar event.
|
|
|
119 |
$event = $this->create_action_event($course->id, $imscp->id,
|
|
|
120 |
\core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED);
|
|
|
121 |
|
|
|
122 |
// Create an action factory.
|
|
|
123 |
$factory = new \core_calendar\action_factory();
|
|
|
124 |
|
|
|
125 |
// Decorate action event.
|
|
|
126 |
$actionevent = mod_imscp_core_calendar_provide_event_action($event, $factory);
|
|
|
127 |
|
|
|
128 |
// Confirm the event was decorated.
|
|
|
129 |
$this->assertInstanceOf('\core_calendar\local\event\value_objects\action', $actionevent);
|
|
|
130 |
$this->assertEquals(get_string('view'), $actionevent->get_name());
|
|
|
131 |
$this->assertInstanceOf('moodle_url', $actionevent->get_url());
|
|
|
132 |
$this->assertEquals(1, $actionevent->get_item_count());
|
|
|
133 |
$this->assertTrue($actionevent->is_actionable());
|
|
|
134 |
}
|
|
|
135 |
|
11 |
efrain |
136 |
public function test_imscp_core_calendar_provide_event_action_for_user(): void {
|
1 |
efrain |
137 |
global $CFG;
|
|
|
138 |
|
|
|
139 |
$this->resetAfterTest();
|
|
|
140 |
$this->setAdminUser();
|
|
|
141 |
|
|
|
142 |
// Create a course.
|
|
|
143 |
$course = $this->getDataGenerator()->create_course();
|
|
|
144 |
|
|
|
145 |
// Create a student.
|
|
|
146 |
$student = $this->getDataGenerator()->create_and_enrol($course, 'student');
|
|
|
147 |
|
|
|
148 |
// Create the activity.
|
|
|
149 |
$imscp = $this->getDataGenerator()->create_module('imscp', array('course' => $course->id));
|
|
|
150 |
|
|
|
151 |
// Create a calendar event.
|
|
|
152 |
$event = $this->create_action_event($course->id, $imscp->id,
|
|
|
153 |
\core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED);
|
|
|
154 |
|
|
|
155 |
// Now log out.
|
|
|
156 |
$CFG->forcelogin = true; // We don't want to be logged in as guest, as guest users might still have some capabilities.
|
|
|
157 |
$this->setUser();
|
|
|
158 |
|
|
|
159 |
// Create an action factory.
|
|
|
160 |
$factory = new \core_calendar\action_factory();
|
|
|
161 |
|
|
|
162 |
// Decorate action event for the student.
|
|
|
163 |
$actionevent = mod_imscp_core_calendar_provide_event_action($event, $factory, $student->id);
|
|
|
164 |
|
|
|
165 |
// Confirm the event was decorated.
|
|
|
166 |
$this->assertInstanceOf('\core_calendar\local\event\value_objects\action', $actionevent);
|
|
|
167 |
$this->assertEquals(get_string('view'), $actionevent->get_name());
|
|
|
168 |
$this->assertInstanceOf('moodle_url', $actionevent->get_url());
|
|
|
169 |
$this->assertEquals(1, $actionevent->get_item_count());
|
|
|
170 |
$this->assertTrue($actionevent->is_actionable());
|
|
|
171 |
}
|
|
|
172 |
|
11 |
efrain |
173 |
public function test_imscp_core_calendar_provide_event_action_as_non_user(): void {
|
1 |
efrain |
174 |
global $CFG;
|
|
|
175 |
|
|
|
176 |
$this->resetAfterTest();
|
|
|
177 |
$this->setAdminUser();
|
|
|
178 |
|
|
|
179 |
// Create the activity.
|
|
|
180 |
$course = $this->getDataGenerator()->create_course();
|
|
|
181 |
$imscp = $this->getDataGenerator()->create_module('imscp', array('course' => $course->id));
|
|
|
182 |
|
|
|
183 |
// Create a calendar event.
|
|
|
184 |
$event = $this->create_action_event($course->id, $imscp->id,
|
|
|
185 |
\core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED);
|
|
|
186 |
|
|
|
187 |
// Now log out.
|
|
|
188 |
$CFG->forcelogin = true; // We don't want to be logged in as guest, as guest users might still have some capabilities.
|
|
|
189 |
$this->setUser();
|
|
|
190 |
|
|
|
191 |
// Create an action factory.
|
|
|
192 |
$factory = new \core_calendar\action_factory();
|
|
|
193 |
|
|
|
194 |
// Decorate action event.
|
|
|
195 |
$actionevent = mod_imscp_core_calendar_provide_event_action($event, $factory);
|
|
|
196 |
|
|
|
197 |
// Confirm the event is not shown at all.
|
|
|
198 |
$this->assertNull($actionevent);
|
|
|
199 |
}
|
|
|
200 |
|
11 |
efrain |
201 |
public function test_imscp_core_calendar_provide_event_action_in_hidden_section(): void {
|
1 |
efrain |
202 |
global $CFG;
|
|
|
203 |
|
|
|
204 |
$this->resetAfterTest();
|
|
|
205 |
$this->setAdminUser();
|
|
|
206 |
|
|
|
207 |
// Create a course.
|
|
|
208 |
$course = $this->getDataGenerator()->create_course();
|
|
|
209 |
|
|
|
210 |
// Create a student.
|
|
|
211 |
$student = $this->getDataGenerator()->create_and_enrol($course, 'student');
|
|
|
212 |
|
|
|
213 |
// Create the activity.
|
|
|
214 |
$imscp = $this->getDataGenerator()->create_module('imscp', array('course' => $course->id));
|
|
|
215 |
|
|
|
216 |
// Create a calendar event.
|
|
|
217 |
$event = $this->create_action_event($course->id, $imscp->id,
|
|
|
218 |
\core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED);
|
|
|
219 |
|
|
|
220 |
// Set sections 0 as hidden.
|
|
|
221 |
set_section_visible($course->id, 0, 0);
|
|
|
222 |
|
|
|
223 |
// Now log out.
|
|
|
224 |
$CFG->forcelogin = true; // We don't want to be logged in as guest, as guest users might still have some capabilities.
|
|
|
225 |
$this->setUser();
|
|
|
226 |
|
|
|
227 |
// Create an action factory.
|
|
|
228 |
$factory = new \core_calendar\action_factory();
|
|
|
229 |
|
|
|
230 |
// Decorate action event for the student.
|
|
|
231 |
$actionevent = mod_imscp_core_calendar_provide_event_action($event, $factory, $student->id);
|
|
|
232 |
|
|
|
233 |
// Confirm the event is not shown at all.
|
|
|
234 |
$this->assertNull($actionevent);
|
|
|
235 |
}
|
|
|
236 |
|
11 |
efrain |
237 |
public function test_imscp_core_calendar_provide_event_action_already_completed(): void {
|
1 |
efrain |
238 |
global $CFG;
|
|
|
239 |
|
|
|
240 |
$this->resetAfterTest();
|
|
|
241 |
$this->setAdminUser();
|
|
|
242 |
|
|
|
243 |
$CFG->enablecompletion = 1;
|
|
|
244 |
|
|
|
245 |
// Create the activity.
|
|
|
246 |
$course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
|
|
|
247 |
$imscp = $this->getDataGenerator()->create_module('imscp', array('course' => $course->id),
|
|
|
248 |
array('completion' => 2, 'completionview' => 1, 'completionexpected' => time() + DAYSECS));
|
|
|
249 |
|
|
|
250 |
// Get some additional data.
|
|
|
251 |
$cm = get_coursemodule_from_instance('imscp', $imscp->id);
|
|
|
252 |
|
|
|
253 |
// Create a calendar event.
|
|
|
254 |
$event = $this->create_action_event($course->id, $imscp->id,
|
|
|
255 |
\core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED);
|
|
|
256 |
|
|
|
257 |
// Mark the activity as completed.
|
|
|
258 |
$completion = new \completion_info($course);
|
|
|
259 |
$completion->set_module_viewed($cm);
|
|
|
260 |
|
|
|
261 |
// Create an action factory.
|
|
|
262 |
$factory = new \core_calendar\action_factory();
|
|
|
263 |
|
|
|
264 |
// Decorate action event.
|
|
|
265 |
$actionevent = mod_imscp_core_calendar_provide_event_action($event, $factory);
|
|
|
266 |
|
|
|
267 |
// Ensure result was null.
|
|
|
268 |
$this->assertNull($actionevent);
|
|
|
269 |
}
|
|
|
270 |
|
11 |
efrain |
271 |
public function test_imscp_core_calendar_provide_event_action_already_completed_for_user(): void {
|
1 |
efrain |
272 |
global $CFG;
|
|
|
273 |
|
|
|
274 |
$this->resetAfterTest();
|
|
|
275 |
$this->setAdminUser();
|
|
|
276 |
|
|
|
277 |
$CFG->enablecompletion = 1;
|
|
|
278 |
|
|
|
279 |
// Create a course.
|
|
|
280 |
$course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
|
|
|
281 |
|
|
|
282 |
// Create a student.
|
|
|
283 |
$student = $this->getDataGenerator()->create_and_enrol($course, 'student');
|
|
|
284 |
|
|
|
285 |
// Create the activity.
|
|
|
286 |
$imscp = $this->getDataGenerator()->create_module('imscp', array('course' => $course->id),
|
|
|
287 |
array('completion' => 2, 'completionview' => 1, 'completionexpected' => time() + DAYSECS));
|
|
|
288 |
|
|
|
289 |
// Get some additional data.
|
|
|
290 |
$cm = get_coursemodule_from_instance('imscp', $imscp->id);
|
|
|
291 |
|
|
|
292 |
// Create a calendar event.
|
|
|
293 |
$event = $this->create_action_event($course->id, $imscp->id,
|
|
|
294 |
\core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED);
|
|
|
295 |
|
|
|
296 |
// Mark the activity as completed.
|
|
|
297 |
$completion = new \completion_info($course);
|
|
|
298 |
$completion->set_module_viewed($cm, $student->id);
|
|
|
299 |
|
|
|
300 |
// Create an action factory.
|
|
|
301 |
$factory = new \core_calendar\action_factory();
|
|
|
302 |
|
|
|
303 |
// Decorate action event for the student.
|
|
|
304 |
$actionevent = mod_imscp_core_calendar_provide_event_action($event, $factory, $student->id);
|
|
|
305 |
|
|
|
306 |
// Ensure result was null.
|
|
|
307 |
$this->assertNull($actionevent);
|
|
|
308 |
}
|
|
|
309 |
|
|
|
310 |
/**
|
|
|
311 |
* Creates an action event.
|
|
|
312 |
*
|
|
|
313 |
* @param int $courseid The course id.
|
|
|
314 |
* @param int $instanceid The instance id.
|
|
|
315 |
* @param string $eventtype The event type.
|
|
|
316 |
* @return bool|calendar_event
|
|
|
317 |
*/
|
|
|
318 |
private function create_action_event($courseid, $instanceid, $eventtype) {
|
|
|
319 |
$event = new \stdClass();
|
|
|
320 |
$event->name = 'Calendar event';
|
|
|
321 |
$event->modulename = 'imscp';
|
|
|
322 |
$event->courseid = $courseid;
|
|
|
323 |
$event->instance = $instanceid;
|
|
|
324 |
$event->type = CALENDAR_EVENT_TYPE_ACTION;
|
|
|
325 |
$event->eventtype = $eventtype;
|
|
|
326 |
$event->timestart = time();
|
|
|
327 |
|
|
|
328 |
return \calendar_event::create($event);
|
|
|
329 |
}
|
|
|
330 |
}
|