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 |
namespace mod_label;
|
|
|
18 |
|
|
|
19 |
/**
|
|
|
20 |
* Unit tests for the activity label's lib.
|
|
|
21 |
*
|
|
|
22 |
* @package mod_label
|
|
|
23 |
* @category test
|
|
|
24 |
* @copyright 2017 Mark Nelson <markn@moodle.com>
|
|
|
25 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
26 |
*/
|
|
|
27 |
class lib_test extends \advanced_testcase {
|
|
|
28 |
|
|
|
29 |
/**
|
|
|
30 |
* Set up.
|
|
|
31 |
*/
|
|
|
32 |
public function setUp(): void {
|
|
|
33 |
$this->resetAfterTest();
|
|
|
34 |
$this->setAdminUser();
|
|
|
35 |
}
|
|
|
36 |
|
11 |
efrain |
37 |
public function test_label_core_calendar_provide_event_action(): void {
|
1 |
efrain |
38 |
// Create the activity.
|
|
|
39 |
$course = $this->getDataGenerator()->create_course();
|
|
|
40 |
$label = $this->getDataGenerator()->create_module('label', array('course' => $course->id));
|
|
|
41 |
|
|
|
42 |
// Create a calendar event.
|
|
|
43 |
$event = $this->create_action_event($course->id, $label->id,
|
|
|
44 |
\core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED);
|
|
|
45 |
|
|
|
46 |
// Create an action factory.
|
|
|
47 |
$factory = new \core_calendar\action_factory();
|
|
|
48 |
|
|
|
49 |
// Decorate action event.
|
|
|
50 |
$actionevent = mod_label_core_calendar_provide_event_action($event, $factory);
|
|
|
51 |
|
|
|
52 |
// Confirm the event was decorated.
|
|
|
53 |
$this->assertInstanceOf('\core_calendar\local\event\value_objects\action', $actionevent);
|
|
|
54 |
$this->assertEquals(get_string('view'), $actionevent->get_name());
|
|
|
55 |
$this->assertInstanceOf('moodle_url', $actionevent->get_url());
|
|
|
56 |
$this->assertEquals(1, $actionevent->get_item_count());
|
|
|
57 |
$this->assertTrue($actionevent->is_actionable());
|
|
|
58 |
}
|
|
|
59 |
|
11 |
efrain |
60 |
public function test_label_core_calendar_provide_event_action_as_non_user(): void {
|
1 |
efrain |
61 |
global $CFG;
|
|
|
62 |
|
|
|
63 |
// Create the activity.
|
|
|
64 |
$course = $this->getDataGenerator()->create_course();
|
|
|
65 |
$label = $this->getDataGenerator()->create_module('label', array('course' => $course->id));
|
|
|
66 |
|
|
|
67 |
// Create a calendar event.
|
|
|
68 |
$event = $this->create_action_event($course->id, $label->id,
|
|
|
69 |
\core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED);
|
|
|
70 |
|
|
|
71 |
// Now log out.
|
|
|
72 |
$CFG->forcelogin = true; // We don't want to be logged in as guest, as guest users might still have some capabilities.
|
|
|
73 |
$this->setUser();
|
|
|
74 |
|
|
|
75 |
// Create an action factory.
|
|
|
76 |
$factory = new \core_calendar\action_factory();
|
|
|
77 |
|
|
|
78 |
// Decorate action event.
|
|
|
79 |
$actionevent = mod_label_core_calendar_provide_event_action($event, $factory);
|
|
|
80 |
|
|
|
81 |
// Confirm the event is not shown at all.
|
|
|
82 |
$this->assertNull($actionevent);
|
|
|
83 |
}
|
|
|
84 |
|
11 |
efrain |
85 |
public function test_label_core_calendar_provide_event_action_in_hidden_section(): void {
|
1 |
efrain |
86 |
// Create the activity.
|
|
|
87 |
$course = $this->getDataGenerator()->create_course();
|
|
|
88 |
$label = $this->getDataGenerator()->create_module('label', array('course' => $course->id));
|
|
|
89 |
|
|
|
90 |
// Create a student.
|
|
|
91 |
$student = $this->getDataGenerator()->create_and_enrol($course, 'student');
|
|
|
92 |
|
|
|
93 |
// Create a calendar event.
|
|
|
94 |
$event = $this->create_action_event($course->id, $label->id,
|
|
|
95 |
\core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED);
|
|
|
96 |
|
|
|
97 |
// Set sections 0 as hidden.
|
|
|
98 |
set_section_visible($course->id, 0, 0);
|
|
|
99 |
|
|
|
100 |
// Create an action factory.
|
|
|
101 |
$factory = new \core_calendar\action_factory();
|
|
|
102 |
|
|
|
103 |
// Decorate action event for the student.
|
|
|
104 |
$actionevent = mod_label_core_calendar_provide_event_action($event, $factory, $student->id);
|
|
|
105 |
|
|
|
106 |
// Confirm the event is not shown at all.
|
|
|
107 |
$this->assertNull($actionevent);
|
|
|
108 |
}
|
|
|
109 |
|
11 |
efrain |
110 |
public function test_label_core_calendar_provide_event_action_for_user(): void {
|
1 |
efrain |
111 |
global $CFG;
|
|
|
112 |
|
|
|
113 |
// Create the activity.
|
|
|
114 |
$course = $this->getDataGenerator()->create_course();
|
|
|
115 |
$label = $this->getDataGenerator()->create_module('label', array('course' => $course->id));
|
|
|
116 |
|
|
|
117 |
// Enrol a student in the course.
|
|
|
118 |
$student = $this->getDataGenerator()->create_and_enrol($course, 'student');
|
|
|
119 |
|
|
|
120 |
// Create a calendar event.
|
|
|
121 |
$event = $this->create_action_event($course->id, $label->id,
|
|
|
122 |
\core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED);
|
|
|
123 |
|
|
|
124 |
// Now, log out.
|
|
|
125 |
$CFG->forcelogin = true; // We don't want to be logged in as guest, as guest users might still have some capabilities.
|
|
|
126 |
$this->setUser();
|
|
|
127 |
|
|
|
128 |
// Create an action factory.
|
|
|
129 |
$factory = new \core_calendar\action_factory();
|
|
|
130 |
|
|
|
131 |
// Decorate action event for the student.
|
|
|
132 |
$actionevent = mod_label_core_calendar_provide_event_action($event, $factory, $student->id);
|
|
|
133 |
|
|
|
134 |
// Confirm the event was decorated.
|
|
|
135 |
$this->assertInstanceOf('\core_calendar\local\event\value_objects\action', $actionevent);
|
|
|
136 |
$this->assertEquals(get_string('view'), $actionevent->get_name());
|
|
|
137 |
$this->assertInstanceOf('moodle_url', $actionevent->get_url());
|
|
|
138 |
$this->assertEquals(1, $actionevent->get_item_count());
|
|
|
139 |
$this->assertTrue($actionevent->is_actionable());
|
|
|
140 |
}
|
|
|
141 |
|
11 |
efrain |
142 |
public function test_label_core_calendar_provide_event_action_already_completed(): void {
|
1 |
efrain |
143 |
global $CFG;
|
|
|
144 |
|
|
|
145 |
$CFG->enablecompletion = 1;
|
|
|
146 |
|
|
|
147 |
// Create the activity.
|
|
|
148 |
$course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
|
|
|
149 |
$label = $this->getDataGenerator()->create_module('label', array('course' => $course->id),
|
|
|
150 |
array('completion' => 2, 'completionview' => 1, 'completionexpected' => time() + DAYSECS));
|
|
|
151 |
|
|
|
152 |
// Get some additional data.
|
|
|
153 |
$cm = get_coursemodule_from_instance('label', $label->id);
|
|
|
154 |
|
|
|
155 |
// Create a calendar event.
|
|
|
156 |
$event = $this->create_action_event($course->id, $label->id,
|
|
|
157 |
\core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED);
|
|
|
158 |
|
|
|
159 |
// Mark the activity as completed.
|
|
|
160 |
$completion = new \completion_info($course);
|
|
|
161 |
$completion->set_module_viewed($cm);
|
|
|
162 |
|
|
|
163 |
// Create an action factory.
|
|
|
164 |
$factory = new \core_calendar\action_factory();
|
|
|
165 |
|
|
|
166 |
// Decorate action event.
|
|
|
167 |
$actionevent = mod_label_core_calendar_provide_event_action($event, $factory);
|
|
|
168 |
|
|
|
169 |
// Ensure result was null.
|
|
|
170 |
$this->assertNull($actionevent);
|
|
|
171 |
}
|
|
|
172 |
|
11 |
efrain |
173 |
public function test_label_core_calendar_provide_event_action_already_completed_for_user(): void {
|
1 |
efrain |
174 |
global $CFG;
|
|
|
175 |
|
|
|
176 |
$CFG->enablecompletion = 1;
|
|
|
177 |
|
|
|
178 |
// Create the activity.
|
|
|
179 |
$course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
|
|
|
180 |
$label = $this->getDataGenerator()->create_module('label', array('course' => $course->id),
|
|
|
181 |
array('completion' => 2, 'completionview' => 1, 'completionexpected' => time() + DAYSECS));
|
|
|
182 |
|
|
|
183 |
// Enrol a student in the course.
|
|
|
184 |
$student = $this->getDataGenerator()->create_and_enrol($course, 'student');
|
|
|
185 |
|
|
|
186 |
// Get some additional data.
|
|
|
187 |
$cm = get_coursemodule_from_instance('label', $label->id);
|
|
|
188 |
|
|
|
189 |
// Create a calendar event.
|
|
|
190 |
$event = $this->create_action_event($course->id, $label->id,
|
|
|
191 |
\core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED);
|
|
|
192 |
|
|
|
193 |
// Mark the activity as completed for the student.
|
|
|
194 |
$completion = new \completion_info($course);
|
|
|
195 |
$completion->set_module_viewed($cm, $student->id);
|
|
|
196 |
|
|
|
197 |
// Create an action factory.
|
|
|
198 |
$factory = new \core_calendar\action_factory();
|
|
|
199 |
|
|
|
200 |
// Decorate action event for the student.
|
|
|
201 |
$actionevent = mod_label_core_calendar_provide_event_action($event, $factory, $student->id);
|
|
|
202 |
|
|
|
203 |
// Ensure result was null.
|
|
|
204 |
$this->assertNull($actionevent);
|
|
|
205 |
}
|
|
|
206 |
|
|
|
207 |
/**
|
|
|
208 |
* Check label name with different content inserted in the label intro.
|
|
|
209 |
*
|
|
|
210 |
* @param string $name
|
|
|
211 |
* @param string $content
|
|
|
212 |
* @param string $format
|
|
|
213 |
* @param string $expectedname
|
|
|
214 |
* @return void
|
|
|
215 |
* @covers \get_label_name
|
|
|
216 |
* @dataProvider label_get_name_data_provider
|
|
|
217 |
*/
|
|
|
218 |
public function test_label_get_label_name(string $name, string $content, string $format, string $expectedname): void {
|
|
|
219 |
$course = $this->getDataGenerator()->create_course();
|
|
|
220 |
// When creating the module, get_label_name is called and fills label->name.
|
|
|
221 |
$label = $this->getDataGenerator()->create_module('label', [
|
|
|
222 |
'name' => $name,
|
|
|
223 |
'course' => $course->id,
|
|
|
224 |
'intro' => $content,
|
|
|
225 |
'introformat' => $format
|
|
|
226 |
]
|
|
|
227 |
);
|
|
|
228 |
|
|
|
229 |
$this->assertEquals($expectedname, $label->name);
|
|
|
230 |
}
|
|
|
231 |
|
|
|
232 |
/**
|
|
|
233 |
* Dataprovider for test_label_get_label_name
|
|
|
234 |
*
|
|
|
235 |
* @return array
|
|
|
236 |
*/
|
|
|
237 |
public function label_get_name_data_provider(): array {
|
|
|
238 |
return [
|
|
|
239 |
'withlabelname' => [
|
|
|
240 |
'name' => 'Test label 1',
|
|
|
241 |
'content' => '<p>Simple textual content<p>',
|
|
|
242 |
'format' => FORMAT_HTML,
|
|
|
243 |
'expected' => 'Test label 1'
|
|
|
244 |
],
|
|
|
245 |
'simple' => [
|
|
|
246 |
'name' => '',
|
|
|
247 |
'content' => '<p>Simple textual content<p>',
|
|
|
248 |
'format' => FORMAT_HTML,
|
|
|
249 |
'expected' => 'Simple textual content'
|
|
|
250 |
],
|
|
|
251 |
'empty' => [
|
|
|
252 |
'name' => '',
|
|
|
253 |
'content' => '',
|
|
|
254 |
'format' => FORMAT_HTML,
|
|
|
255 |
'expected' => 'Test label 1'
|
|
|
256 |
],
|
|
|
257 |
'withaudiocontent' => [
|
|
|
258 |
'name' => '',
|
|
|
259 |
'content' => '<p>Test with audio</p>
|
|
|
260 |
<p> <audio controls="controls">
|
|
|
261 |
<source src="@@PLUGINFILE@@/moodle-hit-song.mp3">
|
|
|
262 |
@@PLUGINFILE@@/moodle-hit-song.mp3
|
|
|
263 |
</audio> </p>',
|
|
|
264 |
'format' => FORMAT_HTML,
|
|
|
265 |
'expected' => 'Test with audio'
|
|
|
266 |
],
|
|
|
267 |
'withvideo' => [
|
|
|
268 |
'name' => '',
|
|
|
269 |
'content' => '<p>Test video</p>
|
|
|
270 |
<p> <video controls="controls">
|
|
|
271 |
<source src="https://www.youtube.com/watch?v=xxxyy">
|
|
|
272 |
https://www.youtube.com/watch?v=xxxyy
|
|
|
273 |
</video> </p>',
|
|
|
274 |
'format' => FORMAT_HTML,
|
|
|
275 |
'expected' => 'Test video https://www.youtube.com/watch?v=xxxyy'
|
|
|
276 |
],
|
|
|
277 |
'with video trimming' => [
|
|
|
278 |
'name' => '',
|
|
|
279 |
'content' => '<p>Test with video to be trimmed</p>
|
|
|
280 |
<p> <video controls="controls">
|
|
|
281 |
<source src="https://www.youtube.com/watch?v=xxxyy">
|
|
|
282 |
https://www.youtube.com/watch?v=xxxyy
|
|
|
283 |
</video> </p>',
|
|
|
284 |
'format' => FORMAT_HTML,
|
|
|
285 |
'expected' => 'Test with video to be trimmed https://www.youtube....'
|
|
|
286 |
],
|
|
|
287 |
'with plain text' => [
|
|
|
288 |
'name' => '',
|
|
|
289 |
'content' => 'Content with @@PLUGINFILE@@/moodle-hit-song.mp3 nothing',
|
|
|
290 |
'format' => FORMAT_HTML,
|
|
|
291 |
'expected' => 'Content with nothing'
|
|
|
292 |
],
|
|
|
293 |
'with several spaces' => [
|
|
|
294 |
'name' => '',
|
|
|
295 |
'content' => "Content with @@PLUGINFILE@@/moodle-hit-song.mp3 \r several spaces",
|
|
|
296 |
'format' => FORMAT_HTML,
|
|
|
297 |
'expected' => 'Content with several spaces'
|
|
|
298 |
],
|
|
|
299 |
'empty spaces' => [
|
|
|
300 |
'name' => '',
|
|
|
301 |
'content' => ' ',
|
|
|
302 |
'format' => FORMAT_HTML,
|
|
|
303 |
'expected' => 'Text and media area'
|
|
|
304 |
],
|
|
|
305 |
'only html' => [
|
|
|
306 |
'name' => '',
|
|
|
307 |
'content' => '<audio controls="controls"><source src=""></audio>',
|
|
|
308 |
'format' => FORMAT_HTML,
|
|
|
309 |
'expected' => 'Text and media area'
|
|
|
310 |
],
|
|
|
311 |
'markdown' => [
|
|
|
312 |
'name' => '',
|
|
|
313 |
'content' => "##Simple Title\n simple markdown format",
|
|
|
314 |
'format' => FORMAT_MARKDOWN,
|
|
|
315 |
'expected' => 'Simple Title simple markdown format'
|
|
|
316 |
],
|
|
|
317 |
'markdown with pluginfile' => [
|
|
|
318 |
'name' => '',
|
|
|
319 |
'content' => "##Simple Title\n simple markdown format @@PLUGINFILE@@/moodle-hit-song.mp3",
|
|
|
320 |
'format' => FORMAT_MARKDOWN,
|
|
|
321 |
'expected' => 'Simple Title simple markdown format'
|
|
|
322 |
],
|
|
|
323 |
'plain text' => [
|
|
|
324 |
'name' => '',
|
|
|
325 |
'content' => "Simple plain text @@PLUGINFILE@@/moodle-hit-song.mp3",
|
|
|
326 |
'format' => FORMAT_PLAIN,
|
|
|
327 |
'expected' => 'Simple plain text'
|
|
|
328 |
],
|
|
|
329 |
'moodle format text' => [
|
|
|
330 |
'name' => '',
|
|
|
331 |
'content' => "Simple plain text @@PLUGINFILE@@/moodle-hit-song.mp3",
|
|
|
332 |
'format' => FORMAT_MOODLE,
|
|
|
333 |
'expected' => 'Simple plain text'
|
|
|
334 |
],
|
|
|
335 |
'html format text' => [
|
|
|
336 |
'name' => '',
|
|
|
337 |
'content' => "<h1>Simple plain title</h1><p> with plain text</p> @@PLUGINFILE@@/moodle-hit-song.mp3",
|
|
|
338 |
'format' => FORMAT_HTML,
|
|
|
339 |
'expected' => 'Simple plain title with plain text'
|
|
|
340 |
],
|
|
|
341 |
];
|
|
|
342 |
}
|
|
|
343 |
|
|
|
344 |
/**
|
|
|
345 |
* Creates an action event.
|
|
|
346 |
*
|
|
|
347 |
* @param int $courseid The course id.
|
|
|
348 |
* @param int $instanceid The instance id.
|
|
|
349 |
* @param string $eventtype The event type.
|
|
|
350 |
* @return bool|calendar_event
|
|
|
351 |
*/
|
|
|
352 |
private function create_action_event($courseid, $instanceid, $eventtype) {
|
|
|
353 |
$event = new \stdClass();
|
|
|
354 |
$event->name = 'Calendar event';
|
|
|
355 |
$event->modulename = 'label';
|
|
|
356 |
$event->courseid = $courseid;
|
|
|
357 |
$event->instance = $instanceid;
|
|
|
358 |
$event->type = CALENDAR_EVENT_TYPE_ACTION;
|
|
|
359 |
$event->eventtype = $eventtype;
|
|
|
360 |
$event->timestart = time();
|
|
|
361 |
|
|
|
362 |
return \calendar_event::create($event);
|
|
|
363 |
}
|
|
|
364 |
}
|