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 mod_resource lib
|
|
|
19 |
*
|
|
|
20 |
* @package mod_resource
|
|
|
21 |
* @category external
|
|
|
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 |
namespace mod_resource;
|
|
|
27 |
|
|
|
28 |
defined('MOODLE_INTERNAL') || die();
|
|
|
29 |
|
|
|
30 |
|
|
|
31 |
/**
|
|
|
32 |
* Unit tests for mod_resource lib
|
|
|
33 |
*
|
|
|
34 |
* @package mod_resource
|
|
|
35 |
* @category external
|
|
|
36 |
* @copyright 2015 Juan Leyva <juan@moodle.com>
|
|
|
37 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
38 |
* @since Moodle 3.0
|
|
|
39 |
*/
|
|
|
40 |
class lib_test extends \advanced_testcase {
|
|
|
41 |
|
|
|
42 |
/**
|
|
|
43 |
* Prepares things before this test case is initialised
|
|
|
44 |
* @return void
|
|
|
45 |
*/
|
|
|
46 |
public static function setUpBeforeClass(): void {
|
|
|
47 |
global $CFG;
|
|
|
48 |
require_once($CFG->dirroot . '/mod/resource/lib.php');
|
|
|
49 |
}
|
|
|
50 |
|
|
|
51 |
/**
|
|
|
52 |
* Test resource_view
|
|
|
53 |
* @return void
|
|
|
54 |
*/
|
11 |
efrain |
55 |
public function test_resource_view(): void {
|
1 |
efrain |
56 |
global $CFG;
|
|
|
57 |
|
|
|
58 |
$CFG->enablecompletion = 1;
|
|
|
59 |
$this->resetAfterTest();
|
|
|
60 |
|
|
|
61 |
$this->setAdminUser();
|
|
|
62 |
// Setup test data.
|
|
|
63 |
$course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
|
|
|
64 |
$resource = $this->getDataGenerator()->create_module('resource', array('course' => $course->id),
|
|
|
65 |
array('completion' => 2, 'completionview' => 1));
|
|
|
66 |
$context = \context_module::instance($resource->cmid);
|
|
|
67 |
$cm = get_coursemodule_from_instance('resource', $resource->id);
|
|
|
68 |
|
|
|
69 |
// Trigger and capture the event.
|
|
|
70 |
$sink = $this->redirectEvents();
|
|
|
71 |
|
|
|
72 |
resource_view($resource, $course, $cm, $context);
|
|
|
73 |
|
|
|
74 |
$events = $sink->get_events();
|
|
|
75 |
// 2 additional events thanks to completion.
|
|
|
76 |
$this->assertCount(3, $events);
|
|
|
77 |
$event = array_shift($events);
|
|
|
78 |
|
|
|
79 |
// Checking that the event contains the expected values.
|
|
|
80 |
$this->assertInstanceOf('\mod_resource\event\course_module_viewed', $event);
|
|
|
81 |
$this->assertEquals($context, $event->get_context());
|
|
|
82 |
$moodleurl = new \moodle_url('/mod/resource/view.php', array('id' => $cm->id));
|
|
|
83 |
$this->assertEquals($moodleurl, $event->get_url());
|
|
|
84 |
$this->assertEventContextNotUsed($event);
|
|
|
85 |
$this->assertNotEmpty($event->get_name());
|
|
|
86 |
|
|
|
87 |
// Check completion status.
|
|
|
88 |
$completion = new \completion_info($course);
|
|
|
89 |
$completiondata = $completion->get_data($cm);
|
|
|
90 |
$this->assertEquals(1, $completiondata->completionstate);
|
|
|
91 |
|
|
|
92 |
}
|
|
|
93 |
|
|
|
94 |
/**
|
|
|
95 |
* Tests the resource_get_coursemodule_info function.
|
|
|
96 |
*
|
|
|
97 |
* Note: This currently doesn't test every aspect of the function, mainly focusing on the icon.
|
|
|
98 |
*/
|
11 |
efrain |
99 |
public function test_get_coursemodule_info(): void {
|
1 |
efrain |
100 |
global $DB, $USER;
|
|
|
101 |
|
|
|
102 |
$this->resetAfterTest();
|
|
|
103 |
$this->setAdminUser();
|
|
|
104 |
|
|
|
105 |
// Create course.
|
|
|
106 |
$generator = $this->getDataGenerator();
|
|
|
107 |
$course = $generator->create_course();
|
|
|
108 |
|
|
|
109 |
// Create a resource with no files.
|
|
|
110 |
$draftid = file_get_unused_draft_itemid();
|
|
|
111 |
$resource1 = $generator->create_module('resource', array('course' => $course->id,
|
|
|
112 |
'name' => 'R1', 'files' => $draftid));
|
|
|
113 |
|
|
|
114 |
// Create a resource with one file.
|
|
|
115 |
$draftid = file_get_unused_draft_itemid();
|
|
|
116 |
$contextid = \context_user::instance($USER->id)->id;
|
|
|
117 |
$filerecord = array('component' => 'user', 'filearea' => 'draft', 'contextid' => $contextid,
|
|
|
118 |
'itemid' => $draftid, 'filename' => 'r2.txt', 'filepath' => '/');
|
|
|
119 |
$fs = get_file_storage();
|
|
|
120 |
$fs->create_file_from_string($filerecord, 'Test');
|
|
|
121 |
$resource2 = $generator->create_module('resource', array('course' => $course->id,
|
|
|
122 |
'name' => 'R2', 'files' => $draftid));
|
|
|
123 |
|
|
|
124 |
// Create a resource with two files.
|
|
|
125 |
$draftid = file_get_unused_draft_itemid();
|
|
|
126 |
$filerecord = array('component' => 'user', 'filearea' => 'draft', 'contextid' => $contextid,
|
|
|
127 |
'itemid' => $draftid, 'filename' => 'r3.txt', 'filepath' => '/', 'sortorder' => 1);
|
|
|
128 |
$fs->create_file_from_string($filerecord, 'Test');
|
|
|
129 |
$filerecord['filename'] = 'r3.doc';
|
|
|
130 |
$filerecord['sortorder'] = 2;
|
|
|
131 |
$fs->create_file_from_string($filerecord, 'Test');
|
|
|
132 |
$resource3 = $generator->create_module('resource', array('course' => $course->id,
|
|
|
133 |
'name' => 'R3', 'files' => $draftid));
|
|
|
134 |
|
|
|
135 |
// Try get_coursemodule_info for first one.
|
|
|
136 |
$info = resource_get_coursemodule_info(
|
|
|
137 |
$DB->get_record('course_modules', array('id' => $resource1->cmid)));
|
|
|
138 |
|
|
|
139 |
// The name should be set. There is no overridden icon.
|
|
|
140 |
$this->assertEquals('R1', $info->name);
|
|
|
141 |
$this->assertEmpty($info->icon);
|
|
|
142 |
|
|
|
143 |
// For second one, there should be an overridden icon.
|
|
|
144 |
$info = resource_get_coursemodule_info(
|
|
|
145 |
$DB->get_record('course_modules', array('id' => $resource2->cmid)));
|
|
|
146 |
$this->assertEquals('R2', $info->name);
|
|
|
147 |
$this->assertEquals('f/text', $info->icon);
|
|
|
148 |
|
|
|
149 |
// For third one, it should use the highest sortorder icon.
|
|
|
150 |
$info = resource_get_coursemodule_info(
|
|
|
151 |
$DB->get_record('course_modules', array('id' => $resource3->cmid)));
|
|
|
152 |
$this->assertEquals('R3', $info->name);
|
|
|
153 |
$this->assertEquals('f/document', $info->icon);
|
|
|
154 |
}
|
|
|
155 |
|
11 |
efrain |
156 |
public function test_resource_core_calendar_provide_event_action(): void {
|
1 |
efrain |
157 |
$this->resetAfterTest();
|
|
|
158 |
$this->setAdminUser();
|
|
|
159 |
|
|
|
160 |
// Create the activity.
|
|
|
161 |
$course = $this->getDataGenerator()->create_course();
|
|
|
162 |
$resource = $this->getDataGenerator()->create_module('resource', array('course' => $course->id));
|
|
|
163 |
|
|
|
164 |
// Create a calendar event.
|
|
|
165 |
$event = $this->create_action_event($course->id, $resource->id,
|
|
|
166 |
\core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED);
|
|
|
167 |
|
|
|
168 |
// Create an action factory.
|
|
|
169 |
$factory = new \core_calendar\action_factory();
|
|
|
170 |
|
|
|
171 |
// Decorate action event.
|
|
|
172 |
$actionevent = mod_resource_core_calendar_provide_event_action($event, $factory);
|
|
|
173 |
|
|
|
174 |
// Confirm the event was decorated.
|
|
|
175 |
$this->assertInstanceOf('\core_calendar\local\event\value_objects\action', $actionevent);
|
|
|
176 |
$this->assertEquals(get_string('view'), $actionevent->get_name());
|
|
|
177 |
$this->assertInstanceOf('moodle_url', $actionevent->get_url());
|
|
|
178 |
$this->assertEquals(1, $actionevent->get_item_count());
|
|
|
179 |
$this->assertTrue($actionevent->is_actionable());
|
|
|
180 |
}
|
|
|
181 |
|
11 |
efrain |
182 |
public function test_resource_core_calendar_provide_event_action_already_completed(): void {
|
1 |
efrain |
183 |
global $CFG;
|
|
|
184 |
|
|
|
185 |
$this->resetAfterTest();
|
|
|
186 |
$this->setAdminUser();
|
|
|
187 |
|
|
|
188 |
$CFG->enablecompletion = 1;
|
|
|
189 |
|
|
|
190 |
// Create the activity.
|
|
|
191 |
$course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
|
|
|
192 |
$resource = $this->getDataGenerator()->create_module('resource', array('course' => $course->id),
|
|
|
193 |
array('completion' => 2, 'completionview' => 1, 'completionexpected' => time() + DAYSECS));
|
|
|
194 |
|
|
|
195 |
// Get some additional data.
|
|
|
196 |
$cm = get_coursemodule_from_instance('resource', $resource->id);
|
|
|
197 |
|
|
|
198 |
// Create a calendar event.
|
|
|
199 |
$event = $this->create_action_event($course->id, $resource->id,
|
|
|
200 |
\core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED);
|
|
|
201 |
|
|
|
202 |
// Mark the activity as completed.
|
|
|
203 |
$completion = new \completion_info($course);
|
|
|
204 |
$completion->set_module_viewed($cm);
|
|
|
205 |
|
|
|
206 |
// Create an action factory.
|
|
|
207 |
$factory = new \core_calendar\action_factory();
|
|
|
208 |
|
|
|
209 |
// Decorate action event.
|
|
|
210 |
$actionevent = mod_resource_core_calendar_provide_event_action($event, $factory);
|
|
|
211 |
|
|
|
212 |
// Ensure result was null.
|
|
|
213 |
$this->assertNull($actionevent);
|
|
|
214 |
}
|
|
|
215 |
|
|
|
216 |
/**
|
|
|
217 |
* Test mod_resource_core_calendar_provide_event_action with user override
|
|
|
218 |
*/
|
11 |
efrain |
219 |
public function test_resource_core_calendar_provide_event_action_user_override(): void {
|
1 |
efrain |
220 |
global $CFG, $USER;
|
|
|
221 |
|
|
|
222 |
$this->resetAfterTest();
|
|
|
223 |
$this->setAdminUser();
|
|
|
224 |
$user = $this->getDataGenerator()->create_user();
|
|
|
225 |
$CFG->enablecompletion = 1;
|
|
|
226 |
|
|
|
227 |
// Create the activity.
|
|
|
228 |
$course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
|
|
|
229 |
$resource = $this->getDataGenerator()->create_module('resource', array('course' => $course->id),
|
|
|
230 |
array('completion' => 2, 'completionview' => 1, 'completionexpected' => time() + DAYSECS));
|
|
|
231 |
|
|
|
232 |
// Get some additional data.
|
|
|
233 |
$cm = get_coursemodule_from_instance('resource', $resource->id);
|
|
|
234 |
|
|
|
235 |
// Create a calendar event.
|
|
|
236 |
$event = $this->create_action_event($course->id, $resource->id,
|
|
|
237 |
\core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED);
|
|
|
238 |
|
|
|
239 |
// Mark the activity as completed.
|
|
|
240 |
$completion = new \completion_info($course);
|
|
|
241 |
$completion->set_module_viewed($cm);
|
|
|
242 |
|
|
|
243 |
// Create an action factory.
|
|
|
244 |
$factory = new \core_calendar\action_factory();
|
|
|
245 |
|
|
|
246 |
// Decorate action event.
|
|
|
247 |
$actionevent = mod_resource_core_calendar_provide_event_action($event, $factory, $USER->id);
|
|
|
248 |
|
|
|
249 |
// Decorate action with a userid override.
|
|
|
250 |
$actionevent2 = mod_resource_core_calendar_provide_event_action($event, $factory, $user->id);
|
|
|
251 |
|
|
|
252 |
// Ensure result was null because it has been marked as completed for the associated user.
|
|
|
253 |
// Logic was brought across from the "_already_completed" function.
|
|
|
254 |
$this->assertNull($actionevent);
|
|
|
255 |
|
|
|
256 |
// Confirm the event was decorated.
|
|
|
257 |
$this->assertNotNull($actionevent2);
|
|
|
258 |
$this->assertInstanceOf('\core_calendar\local\event\value_objects\action', $actionevent2);
|
|
|
259 |
$this->assertEquals(get_string('view'), $actionevent2->get_name());
|
|
|
260 |
$this->assertInstanceOf('moodle_url', $actionevent2->get_url());
|
|
|
261 |
$this->assertEquals(1, $actionevent2->get_item_count());
|
|
|
262 |
$this->assertTrue($actionevent2->is_actionable());
|
|
|
263 |
}
|
|
|
264 |
|
|
|
265 |
/**
|
|
|
266 |
* Creates an action event.
|
|
|
267 |
*
|
|
|
268 |
* @param int $courseid The course id.
|
|
|
269 |
* @param int $instanceid The instance id.
|
|
|
270 |
* @param string $eventtype The event type.
|
|
|
271 |
* @return bool|calendar_event
|
|
|
272 |
*/
|
|
|
273 |
private function create_action_event($courseid, $instanceid, $eventtype) {
|
|
|
274 |
$event = new \stdClass();
|
|
|
275 |
$event->name = 'Calendar event';
|
|
|
276 |
$event->modulename = 'resource';
|
|
|
277 |
$event->courseid = $courseid;
|
|
|
278 |
$event->instance = $instanceid;
|
|
|
279 |
$event->type = CALENDAR_EVENT_TYPE_ACTION;
|
|
|
280 |
$event->eventtype = $eventtype;
|
|
|
281 |
$event->timestart = time();
|
|
|
282 |
|
|
|
283 |
return \calendar_event::create($event);
|
|
|
284 |
}
|
|
|
285 |
}
|