Proyectos de Subversion Moodle

Rev

Rev 11 | | Comparar con el anterior | Ultima modificación | Ver Log |

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