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
/**
18
 * Unit tests for mod_lti lib
19
 *
20
 * @package    mod_lti
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_lti;
27
 
28
defined('MOODLE_INTERNAL') || die();
29
 
30
/**
31
 * Unit tests for mod_lti lib
32
 *
33
 * @package    mod_lti
34
 * @category   external
35
 * @copyright  2015 Juan Leyva <juan@moodle.com>
36
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37
 * @since      Moodle 3.0
38
 */
1441 ariadna 39
final class lib_test extends \advanced_testcase {
1 efrain 40
 
41
    /**
42
     * Prepares things before this test case is initialised
43
     * @return void
44
     */
45
    public static function setUpBeforeClass(): void {
46
        global $CFG;
47
        require_once($CFG->dirroot . '/mod/lti/lib.php');
1441 ariadna 48
        parent::setUpBeforeClass();
1 efrain 49
    }
50
 
51
    /**
52
     * Test lti_view
53
     * @return void
54
     */
11 efrain 55
    public function test_lti_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
        $lti = $this->getDataGenerator()->create_module('lti', array('course' => $course->id),
65
                                                            array('completion' => 2, 'completionview' => 1));
66
        $context = \context_module::instance($lti->cmid);
67
        $cm = get_coursemodule_from_instance('lti', $lti->id);
68
 
69
        // Trigger and capture the event.
70
        $sink = $this->redirectEvents();
71
 
72
        lti_view($lti, $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_lti\event\course_module_viewed', $event);
81
        $this->assertEquals($context, $event->get_context());
82
        $moodleurl = new \moodle_url('/mod/lti/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
     * Test deleting LTI instance.
96
     */
11 efrain 97
    public function test_lti_delete_instance(): void {
1 efrain 98
        $this->resetAfterTest();
99
 
100
        $this->setAdminUser();
101
        $course = $this->getDataGenerator()->create_course(array());
102
        $lti = $this->getDataGenerator()->create_module('lti', array('course' => $course->id));
103
        $cm = get_coursemodule_from_instance('lti', $lti->id);
104
 
105
        // Must not throw notices.
106
        course_delete_module($cm->id);
107
    }
108
 
11 efrain 109
    public function test_lti_core_calendar_provide_event_action(): void {
1 efrain 110
        $this->resetAfterTest();
111
        $this->setAdminUser();
112
 
113
        // Create the activity.
114
        $course = $this->getDataGenerator()->create_course();
115
        $lti = $this->getDataGenerator()->create_module('lti', array('course' => $course->id));
116
 
117
        // Create a calendar event.
118
        $event = $this->create_action_event($course->id, $lti->id,
119
            \core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED);
120
 
121
        // Create an action factory.
122
        $factory = new \core_calendar\action_factory();
123
 
124
        // Decorate action event.
125
        $actionevent = mod_lti_core_calendar_provide_event_action($event, $factory);
126
 
127
        // Confirm the event was decorated.
128
        $this->assertInstanceOf('\core_calendar\local\event\value_objects\action', $actionevent);
129
        $this->assertEquals(get_string('view'), $actionevent->get_name());
130
        $this->assertInstanceOf('moodle_url', $actionevent->get_url());
131
        $this->assertEquals(1, $actionevent->get_item_count());
132
        $this->assertTrue($actionevent->is_actionable());
133
    }
134
 
11 efrain 135
    public function test_lti_core_calendar_provide_event_action_as_non_user(): void {
1 efrain 136
        global $CFG;
137
 
138
        $this->resetAfterTest();
139
        $this->setAdminUser();
140
 
141
        // Create the activity.
142
        $course = $this->getDataGenerator()->create_course();
143
        $lti = $this->getDataGenerator()->create_module('lti', array('course' => $course->id));
144
 
145
        // Create a calendar event.
146
        $event = $this->create_action_event($course->id, $lti->id,
147
            \core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED);
148
 
149
        // Now, log out.
150
        $CFG->forcelogin = true; // We don't want to be logged in as guest, as guest users might still have some capabilities.
151
        $this->setUser();
152
 
153
        // Create an action factory.
154
        $factory = new \core_calendar\action_factory();
155
 
156
        // Decorate action event.
157
        $actionevent = mod_lti_core_calendar_provide_event_action($event, $factory);
158
 
159
        // Confirm the event is not shown at all.
160
        $this->assertNull($actionevent);
161
    }
162
 
11 efrain 163
    public function test_lti_core_calendar_provide_event_action_for_user(): void {
1 efrain 164
        global $CFG;
165
 
166
        $this->resetAfterTest();
167
        $this->setAdminUser();
168
 
169
        // Create the activity.
170
        $course = $this->getDataGenerator()->create_course();
171
        $lti = $this->getDataGenerator()->create_module('lti', array('course' => $course->id));
172
 
173
        // Enrol a student in the course.
174
        $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
175
 
176
        // Create a calendar event.
177
        $event = $this->create_action_event($course->id, $lti->id,
178
            \core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED);
179
 
180
        // Now, log out.
181
        $CFG->forcelogin = true; // We don't want to be logged in as guest, as guest users might still have some capabilities.
182
        $this->setUser();
183
 
184
        // Create an action factory.
185
        $factory = new \core_calendar\action_factory();
186
 
187
        // Decorate action event for the student.
188
        $actionevent = mod_lti_core_calendar_provide_event_action($event, $factory, $student->id);
189
 
190
        // Confirm the event was decorated.
191
        $this->assertInstanceOf('\core_calendar\local\event\value_objects\action', $actionevent);
192
        $this->assertEquals(get_string('view'), $actionevent->get_name());
193
        $this->assertInstanceOf('moodle_url', $actionevent->get_url());
194
        $this->assertEquals(1, $actionevent->get_item_count());
195
        $this->assertTrue($actionevent->is_actionable());
196
    }
197
 
11 efrain 198
    public function test_lti_core_calendar_provide_event_action_already_completed(): void {
1 efrain 199
        global $CFG;
200
 
201
        $this->resetAfterTest();
202
        $this->setAdminUser();
203
 
204
        $CFG->enablecompletion = 1;
205
 
206
        // Create the activity.
207
        $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
208
        $lti = $this->getDataGenerator()->create_module('lti', array('course' => $course->id),
209
            array('completion' => 2, 'completionview' => 1, 'completionexpected' => time() + DAYSECS));
210
 
211
        // Get some additional data.
212
        $cm = get_coursemodule_from_instance('lti', $lti->id);
213
 
214
        // Create a calendar event.
215
        $event = $this->create_action_event($course->id, $lti->id,
216
            \core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED);
217
 
218
        // Mark the activity as completed.
219
        $completion = new \completion_info($course);
220
        $completion->set_module_viewed($cm);
221
 
222
        // Create an action factory.
223
        $factory = new \core_calendar\action_factory();
224
 
225
        // Decorate action event.
226
        $actionevent = mod_lti_core_calendar_provide_event_action($event, $factory);
227
 
228
        // Ensure result was null.
229
        $this->assertNull($actionevent);
230
    }
231
 
11 efrain 232
    public function test_lti_core_calendar_provide_event_action_already_completed_as_non_user(): void {
1 efrain 233
        global $CFG;
234
 
235
        $this->resetAfterTest();
236
        $this->setAdminUser();
237
 
238
        $CFG->enablecompletion = 1;
239
 
240
        // Create the activity.
241
        $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
242
        $lti = $this->getDataGenerator()->create_module('lti', array('course' => $course->id),
243
            array('completion' => 2, 'completionview' => 1, 'completionexpected' => time() + DAYSECS));
244
 
245
        // Get some additional data.
246
        $cm = get_coursemodule_from_instance('lti', $lti->id);
247
 
248
        // Create a calendar event.
249
        $event = $this->create_action_event($course->id, $lti->id,
250
            \core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED);
251
 
252
        // Mark the activity as completed.
253
        $completion = new \completion_info($course);
254
        $completion->set_module_viewed($cm);
255
 
256
        // Now, log out.
257
        $CFG->forcelogin = true; // We don't want to be logged in as guest, as guest users might still have some capabilities.
258
        $this->setUser();
259
 
260
        // Create an action factory.
261
        $factory = new \core_calendar\action_factory();
262
 
263
        // Decorate action event.
264
        $actionevent = mod_lti_core_calendar_provide_event_action($event, $factory);
265
 
266
        // Ensure result was null.
267
        $this->assertNull($actionevent);
268
    }
269
 
11 efrain 270
    public function test_lti_core_calendar_provide_event_action_already_completed_for_user(): void {
1 efrain 271
        global $CFG;
272
 
273
        $this->resetAfterTest();
274
        $this->setAdminUser();
275
 
276
        $CFG->enablecompletion = 1;
277
 
278
        // Create the activity.
279
        $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
280
        $lti = $this->getDataGenerator()->create_module('lti', array('course' => $course->id),
281
            array('completion' => 2, 'completionview' => 1, 'completionexpected' => time() + DAYSECS));
282
 
283
        // Enrol 2 students in the course.
284
        $student1 = $this->getDataGenerator()->create_and_enrol($course, 'student');
285
        $student2 = $this->getDataGenerator()->create_and_enrol($course, 'student');
286
 
287
        // Get some additional data.
288
        $cm = get_coursemodule_from_instance('lti', $lti->id);
289
 
290
        // Create a calendar event.
291
        $event = $this->create_action_event($course->id, $lti->id,
292
            \core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED);
293
 
294
        // Mark the activity as completed for $student1.
295
        $completion = new \completion_info($course);
296
        $completion->set_module_viewed($cm, $student1->id);
297
 
298
        // Now, log in as $student2.
299
        $this->setUser($student2);
300
 
301
        // Create an action factory.
302
        $factory = new \core_calendar\action_factory();
303
 
304
        // Decorate action event for $student1.
305
        $actionevent = mod_lti_core_calendar_provide_event_action($event, $factory, $student1->id);
306
 
307
        // Ensure result was null.
308
        $this->assertNull($actionevent);
309
    }
310
 
311
    /**
312
     * Creates an action event.
313
     *
314
     * @param int $courseid The course id.
315
     * @param int $instanceid The instance id.
316
     * @param string $eventtype The event type.
317
     * @return bool|calendar_event
318
     */
319
    private function create_action_event($courseid, $instanceid, $eventtype) {
320
        $event = new \stdClass();
321
        $event->name = 'Calendar event';
322
        $event->modulename  = 'lti';
323
        $event->courseid = $courseid;
324
        $event->instance = $instanceid;
325
        $event->type = CALENDAR_EVENT_TYPE_ACTION;
326
        $event->eventtype = $eventtype;
327
        $event->timestart = time();
328
 
329
        return \calendar_event::create($event);
330
    }
331
 
332
    /**
333
     * Test verifying the output of the lti_get_course_content_items and lti_get_all_content_items callbacks.
334
     */
11 efrain 335
    public function test_content_item_callbacks(): void {
1 efrain 336
        $this->resetAfterTest();
337
        global $DB, $CFG;
338
        require_once($CFG->dirroot . '/mod/lti/locallib.php');
339
 
340
        $admin = get_admin();
341
        $time = time();
342
        $course = $this->getDataGenerator()->create_course();
343
        $teacher = $this->getDataGenerator()->create_and_enrol($course, 'editingteacher');
344
        $course2 = $this->getDataGenerator()->create_course();
345
        $teacher2 = $this->getDataGenerator()->create_and_enrol($course2, 'editingteacher');
346
 
347
        // Create some preconfigured tools.
348
        $sitetoolrecord = (object) [
349
            'name' => 'Site level tool which is available in the activity chooser',
350
            'baseurl' => 'http://example.com',
351
            'createdby' => $admin->id,
352
            'course' => SITEID,
353
            'ltiversion' => 'LTI-1p0',
354
            'timecreated' => $time,
355
            'timemodified' => $time,
356
            'state' => LTI_TOOL_STATE_CONFIGURED,
357
            'coursevisible' => LTI_COURSEVISIBLE_ACTIVITYCHOOSER
358
        ];
359
        $sitetoolrecordnonchooser = (object) [
360
            'name' => 'Site level tool which is NOT available in the course activity chooser',
361
            'baseurl' => 'http://example2.com',
362
            'createdby' => $admin->id,
363
            'course' => SITEID,
364
            'ltiversion' => 'LTI-1p0',
365
            'timecreated' => $time,
366
            'timemodified' => $time,
367
            'state' => LTI_TOOL_STATE_CONFIGURED,
368
            'coursevisible' => LTI_COURSEVISIBLE_PRECONFIGURED
369
        ];
370
        $course1toolrecord = (object) [
371
            'name' => 'Course created tool which is available in the activity chooser',
372
            'baseurl' => 'http://example3.com',
373
            'createdby' => $teacher->id,
374
            'course' => $course->id,
375
            'ltiversion' => 'LTI-1p0',
376
            'timecreated' => $time,
377
            'timemodified' => $time,
378
            'state' => LTI_TOOL_STATE_CONFIGURED,
379
            'coursevisible' => LTI_COURSEVISIBLE_ACTIVITYCHOOSER
380
        ];
381
        $course2toolrecord = (object) [
382
            'name' => 'Course created tool which is available in the activity chooser',
383
            'baseurl' => 'http://example4.com',
384
            'createdby' => $teacher2->id,
385
            'course' => $course2->id,
386
            'ltiversion' => 'LTI-1p0',
387
            'timecreated' => $time,
388
            'timemodified' => $time,
389
            'state' => LTI_TOOL_STATE_CONFIGURED,
390
            'coursevisible' => LTI_COURSEVISIBLE_ACTIVITYCHOOSER
391
        ];
392
        $tool1id = $DB->insert_record('lti_types', $sitetoolrecord);
393
        $tool2id = $DB->insert_record('lti_types', $sitetoolrecordnonchooser);
394
        $tool3id = $DB->insert_record('lti_types', $course1toolrecord);
395
        $tool4id = $DB->insert_record('lti_types', $course2toolrecord);
396
        $sitetoolrecord->id = $tool1id;
397
        $sitetoolrecordnonchooser->id = $tool2id;
398
        $course1toolrecord->id = $tool3id;
399
        $course2toolrecord->id = $tool4id;
400
 
401
        $defaultmodulecontentitem = new \core_course\local\entity\content_item(
402
            '1',
403
            'default module content item',
404
            new \core_course\local\entity\string_title('Content item title'),
405
            new \moodle_url(''),
406
            'icon',
407
            'Description of the module',
408
            MOD_ARCHETYPE_OTHER,
409
            'mod_lti',
410
            MOD_PURPOSE_CONTENT
411
        );
412
 
413
        // The lti_get_lti_types_by_course method (used by the callbacks) assumes the global user.
414
        $this->setUser($teacher);
415
 
416
        // Teacher in course1 should be able to see the site preconfigured tool and the tool created in course1.
417
        $courseitems = lti_get_course_content_items($defaultmodulecontentitem, $teacher, $course);
418
        $this->assertCount(2, $courseitems);
419
        $ids = [];
420
        foreach ($courseitems as $item) {
421
            $ids[] = $item->get_id();
422
        }
423
        $this->assertContains($sitetoolrecord->id + 1, $ids);
424
        $this->assertContains($course1toolrecord->id + 1, $ids);
425
        $this->assertNotContains($sitetoolrecordnonchooser->id + 1, $ids);
426
 
427
        // The content items for teacher2 in course2 include the site preconfigured tool and the tool created in course2.
428
        $this->setUser($teacher2);
429
        $course2items = lti_get_course_content_items($defaultmodulecontentitem, $teacher2, $course2);
430
        $this->assertCount(2, $course2items);
431
        $ids = [];
432
        foreach ($course2items as $item) {
433
            $ids[] = $item->get_id();
434
        }
435
        $this->assertContains($sitetoolrecord->id + 1, $ids);
436
        $this->assertContains($course2toolrecord->id + 1, $ids);
437
        $this->assertNotContains($sitetoolrecordnonchooser->id + 1, $ids);
438
 
439
        // Removing the capability to use preconfigured (site or course level) tools, should result in no content items.
440
        $teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher'));
441
        assign_capability('mod/lti:addpreconfiguredinstance', CAP_PROHIBIT, $teacherrole->id,
442
            \core\context\course::instance($course2->id));
443
        $course2items = lti_get_course_content_items($defaultmodulecontentitem, $teacher2, $course2);
444
        $this->assertCount(0, $course2items);
445
 
446
        // When fetching all content items, we expect to see all items available in activity choosers (in any course).
447
        $this->setAdminUser();
448
        $allitems = mod_lti_get_all_content_items($defaultmodulecontentitem);
449
        $this->assertCount(3, $allitems);
450
        $ids = [];
451
        foreach ($allitems as $item) {
452
            $ids[] = $item->get_id();
453
        }
454
        $this->assertContains($sitetoolrecord->id + 1, $ids);
455
        $this->assertContains($course1toolrecord->id + 1, $ids);
456
        $this->assertContains($course2toolrecord->id + 1, $ids);
457
        $this->assertNotContains($sitetoolrecordnonchooser->id + 1, $ids);
458
    }
459
}