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_lti;
18
 
19
use core_external\external_api;
20
use mod_lti_external;
21
use mod_lti_testcase;
22
 
23
defined('MOODLE_INTERNAL') || die();
24
 
25
global $CFG;
26
 
27
require_once($CFG->dirroot . '/webservice/tests/helpers.php');
28
require_once($CFG->dirroot . '/mod/lti/lib.php');
29
require_once($CFG->dirroot . '/mod/lti/tests/mod_lti_testcase.php');
30
 
31
/**
32
 * External tool module external functions tests
33
 *
34
 * @package    mod_lti
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
 */
1441 ariadna 40
final class externallib_test extends mod_lti_testcase {
1 efrain 41
 
42
    /**
43
     * Set up for every test
44
     */
45
    public function setUp(): void {
1441 ariadna 46
        parent::setUp();
1 efrain 47
        $this->resetAfterTest();
48
    }
49
 
50
    /**
51
     * Sets up some basic test data including course, users, roles, and an lti instance, for use in some tests.
52
     * @return array
53
     */
54
    protected function setup_test_data() {
55
        global $DB;
56
        $this->setAdminUser();
57
 
58
        // Setup test data.
59
        $course = $this->getDataGenerator()->create_course();
60
        $lti = $this->getDataGenerator()->create_module(
61
            'lti',
62
            ['course' => $course->id, 'toolurl' => 'http://localhost/not/real/tool.php']
63
        );
64
        $context = \context_module::instance($lti->cmid);
65
        $cm = get_coursemodule_from_instance('lti', $lti->id);
66
 
67
        // Create users.
68
        $student = self::getDataGenerator()->create_user();
69
        $teacher = self::getDataGenerator()->create_user();
70
 
71
        // Users enrolments.
72
        $studentrole = $DB->get_record('role', array('shortname' => 'student'));
73
        $teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher'));
74
        $this->getDataGenerator()->enrol_user($student->id, $course->id, $studentrole->id, 'manual');
75
        $this->getDataGenerator()->enrol_user($teacher->id, $course->id, $teacherrole->id, 'manual');
76
 
77
        return [
78
            'course' => $course,
79
            'lti' => $lti,
80
            'context' => $context,
81
            'cm' => $cm,
82
            'student' => $student,
83
            'teacher' => $teacher,
84
            'studentrole' => $studentrole,
85
            'teacherrole' => $teacherrole
86
        ];
87
    }
88
 
89
    /**
90
     * Test get_tool_proxies.
91
     */
11 efrain 92
    public function test_mod_lti_get_tool_proxies(): void {
1 efrain 93
        // Create two tool proxies. One to associate with tool, and one to leave orphaned.
94
        $this->setAdminUser();
95
        $proxy = $this->generate_tool_proxy("1");
96
        $orphanedproxy = $this->generate_tool_proxy("2");
97
        $this->generate_tool_type("1", $proxy->id); // Associate proxy 1 with tool type.
98
 
99
        // Fetch all proxies.
100
        $proxies = mod_lti_external::get_tool_proxies(false);
101
        $proxies = external_api::clean_returnvalue(mod_lti_external::get_tool_proxies_returns(), $proxies);
102
 
103
        $this->assertCount(2, $proxies);
104
        $this->assertEqualsCanonicalizing([(array) $proxy, (array) $orphanedproxy], $proxies);
105
    }
106
 
107
    /**
108
     * Test get_tool_proxies with orphaned proxies only.
109
     */
11 efrain 110
    public function test_mod_lti_get_orphaned_tool_proxies(): void {
1 efrain 111
        // Create two tool proxies. One to associate with tool, and one to leave orphaned.
112
        $this->setAdminUser();
113
        $proxy = $this->generate_tool_proxy("1");
114
        $orphanedproxy = $this->generate_tool_proxy("2");
115
        $this->generate_tool_type("1", $proxy->id); // Associate proxy 1 with tool type.
116
 
117
        // Fetch all proxies.
118
        $proxies = mod_lti_external::get_tool_proxies(true);
119
        $proxies = external_api::clean_returnvalue(mod_lti_external::get_tool_proxies_returns(), $proxies);
120
 
121
        $this->assertCount(1, $proxies);
122
        $this->assertEqualsCanonicalizing([(array) $orphanedproxy], $proxies);
123
    }
124
 
125
    /**
126
     * Test get_tool_launch_data.
127
     */
11 efrain 128
    public function test_get_tool_launch_data(): void {
1 efrain 129
        global $USER;
130
 
131
        [
132
            'course' => $course,
133
            'lti' => $lti
134
        ] = $this->setup_test_data();
135
 
136
        $result = mod_lti_external::get_tool_launch_data($lti->id);
137
        $result = external_api::clean_returnvalue(mod_lti_external::get_tool_launch_data_returns(), $result);
138
 
139
        // Basic test, the function returns what it's expected.
140
        self::assertEquals($lti->toolurl, $result['endpoint']);
141
        self::assertCount(36, $result['parameters']);
142
 
143
        // Check some parameters.
144
        $parameters = array();
145
        foreach ($result['parameters'] as $param) {
146
            $parameters[$param['name']] = $param['value'];
147
        }
148
        self::assertEquals($lti->resourcekey, $parameters['oauth_consumer_key']);
149
        self::assertEquals($course->fullname, $parameters['context_title']);
150
        self::assertEquals($course->shortname, $parameters['context_label']);
151
        self::assertEquals($USER->id, $parameters['user_id']);
152
        self::assertEquals($USER->firstname, $parameters['lis_person_name_given']);
153
        self::assertEquals($USER->lastname, $parameters['lis_person_name_family']);
154
        self::assertEquals(fullname($USER), $parameters['lis_person_name_full']);
155
        self::assertEquals($USER->username, $parameters['ext_user_username']);
156
        self::assertEquals("phpunit", $parameters['tool_consumer_instance_name']);
157
        self::assertEquals("PHPUnit test site", $parameters['tool_consumer_instance_description']);
158
    }
159
 
160
    /**
161
     * Test get_ltis_by_courses.
162
     */
11 efrain 163
    public function test_mod_lti_get_ltis_by_courses(): void {
1 efrain 164
        [
165
            'course' => $course,
166
            'lti' => $lti,
167
            'student' => $student,
168
            'teacher' => $teacher,
169
            'studentrole' => $studentrole,
170
        ] = $this->setup_test_data();
171
 
172
        // Create additional course.
173
        $course2 = self::getDataGenerator()->create_course();
174
 
175
        // Second lti.
176
        $record = new \stdClass();
177
        $record->course = $course2->id;
178
        $lti2 = self::getDataGenerator()->create_module('lti', $record);
179
 
180
        // Execute real Moodle enrolment as we'll call unenrol() method on the instance later.
181
        $enrol = enrol_get_plugin('manual');
182
        $enrolinstances = enrol_get_instances($course2->id, true);
183
        foreach ($enrolinstances as $courseenrolinstance) {
184
            if ($courseenrolinstance->enrol == "manual") {
185
                $instance2 = $courseenrolinstance;
186
                break;
187
            }
188
        }
189
        $enrol->enrol_user($instance2, $student->id, $studentrole->id);
190
 
191
        self::setUser($student);
192
 
193
        $returndescription = mod_lti_external::get_ltis_by_courses_returns();
194
 
195
        // Create what we expect to be returned when querying the two courses.
196
        // First for the student user.
197
        $expectedfields = array('id', 'coursemodule', 'course', 'name', 'intro', 'introformat', 'introfiles', 'lang',
198
            'launchcontainer', 'showtitlelaunch', 'showdescriptionlaunch', 'icon', 'secureicon');
199
 
200
        // Add expected coursemodule and data.
201
        $lti1 = $lti;
202
        $lti1->coursemodule = $lti1->cmid;
203
        $lti1->introformat = 1;
204
        $lti1->section = 0;
205
        $lti1->visible = true;
206
        $lti1->groupmode = 0;
207
        $lti1->groupingid = 0;
208
        $lti1->section = 0;
209
        $lti1->introfiles = [];
210
        $lti1->lang = '';
211
 
212
        $lti2->coursemodule = $lti2->cmid;
213
        $lti2->introformat = 1;
214
        $lti2->section = 0;
215
        $lti2->visible = true;
216
        $lti2->groupmode = 0;
217
        $lti2->groupingid = 0;
218
        $lti2->section = 0;
219
        $lti2->introfiles = [];
220
        $lti2->lang = '';
221
 
222
        foreach ($expectedfields as $field) {
223
            $expected1[$field] = $lti1->{$field};
224
            $expected2[$field] = $lti2->{$field};
225
        }
226
 
227
        $expectedltis = array($expected2, $expected1);
228
 
229
        // Call the external function passing course ids.
230
        $result = mod_lti_external::get_ltis_by_courses(array($course2->id, $course->id));
231
        $result = external_api::clean_returnvalue($returndescription, $result);
232
 
233
        $this->assertEquals($expectedltis, $result['ltis']);
234
        $this->assertCount(0, $result['warnings']);
235
 
236
        // Call the external function without passing course id.
237
        $result = mod_lti_external::get_ltis_by_courses();
238
        $result = external_api::clean_returnvalue($returndescription, $result);
239
        $this->assertEquals($expectedltis, $result['ltis']);
240
        $this->assertCount(0, $result['warnings']);
241
 
242
        // Unenrol user from second course and alter expected ltis.
243
        $enrol->unenrol_user($instance2, $student->id);
244
        array_shift($expectedltis);
245
 
246
        // Call the external function without passing course id.
247
        $result = mod_lti_external::get_ltis_by_courses();
248
        $result = external_api::clean_returnvalue($returndescription, $result);
249
        $this->assertEquals($expectedltis, $result['ltis']);
250
 
251
        // Call for the second course we unenrolled the user from, expected warning.
252
        $result = mod_lti_external::get_ltis_by_courses(array($course2->id));
253
        $result = external_api::clean_returnvalue($returndescription, $result);
254
        $this->assertCount(1, $result['warnings']);
255
        $this->assertEquals('1', $result['warnings'][0]['warningcode']);
256
        $this->assertEquals($course2->id, $result['warnings'][0]['itemid']);
257
 
258
        // Now, try as a teacher for getting all the additional fields.
259
        self::setUser($teacher);
260
 
261
        $additionalfields = array('timecreated', 'timemodified', 'typeid', 'toolurl', 'securetoolurl',
262
            'instructorchoicesendname', 'instructorchoicesendemailaddr', 'instructorchoiceallowroster',
263
            'instructorchoiceallowsetting', 'instructorcustomparameters', 'instructorchoiceacceptgrades', 'grade',
264
            'resourcekey', 'password', 'debuglaunch', 'servicesalt', 'visible', 'groupmode', 'groupingid', 'section', 'lang');
265
 
266
        foreach ($additionalfields as $field) {
267
            $expectedltis[0][$field] = $lti1->{$field};
268
        }
269
 
270
        $result = mod_lti_external::get_ltis_by_courses();
271
        $result = external_api::clean_returnvalue($returndescription, $result);
272
        $this->assertEquals($expectedltis, $result['ltis']);
273
 
274
        // Admin also should get all the information.
275
        self::setAdminUser();
276
 
277
        $result = mod_lti_external::get_ltis_by_courses(array($course->id));
278
        $result = external_api::clean_returnvalue($returndescription, $result);
279
        $this->assertEquals($expectedltis, $result['ltis']);
280
 
281
        // Now, prohibit capabilities.
282
        $this->setUser($student);
283
        $contextcourse1 = \context_course::instance($course->id);
284
        // Prohibit capability = mod:lti:view on Course1 for students.
285
        assign_capability('mod/lti:view', CAP_PROHIBIT, $studentrole->id, $contextcourse1->id);
286
        // Empty all the caches that may be affected by this change.
287
        accesslib_clear_all_caches_for_unit_testing();
288
        \course_modinfo::clear_instance_cache();
289
 
290
        $ltis = mod_lti_external::get_ltis_by_courses(array($course->id));
291
        $ltis = external_api::clean_returnvalue(mod_lti_external::get_ltis_by_courses_returns(), $ltis);
292
        $this->assertCount(0, $ltis['ltis']);
293
    }
294
 
295
    /**
296
     * Test view_lti with an invalid instance id.
297
     */
11 efrain 298
    public function test_view_lti_invalid_instanceid(): void {
1 efrain 299
        $this->expectException(\moodle_exception::class);
300
        mod_lti_external::view_lti(0);
301
    }
302
 
303
    /**
304
     * Test view_lti as a user who is not enrolled in the course.
305
     */
11 efrain 306
    public function test_view_lti_no_enrolment(): void {
1 efrain 307
        [
308
            'lti' => $lti
309
        ] = $this->setup_test_data();
310
 
311
        // Test not-enrolled user.
312
        $usernotenrolled = self::getDataGenerator()->create_user();
313
        $this->setUser($usernotenrolled);
314
 
315
        $this->expectException(\moodle_exception::class);
316
        mod_lti_external::view_lti($lti->id);
317
    }
318
 
319
    /**
320
     * Test view_lti for a user without the mod/lti:view capability.
321
     */
11 efrain 322
    public function test_view_lti_no_capability(): void {
1 efrain 323
        [
324
            'lti' => $lti,
325
            'student' => $student,
326
            'studentrole' => $studentrole,
327
            'context' => $context,
328
        ] = $this->setup_test_data();
329
 
330
        $this->setUser($student);
331
 
332
        // We need a explicit prohibit since this capability is only defined in authenticated user and guest roles.
333
        assign_capability('mod/lti:view', CAP_PROHIBIT, $studentrole->id, $context->id);
334
        // Empty all the caches that may be affected by this change.
335
        accesslib_clear_all_caches_for_unit_testing();
336
        \course_modinfo::clear_instance_cache();
337
 
338
        $this->expectException(\moodle_exception::class);
339
        mod_lti_external::view_lti($lti->id);
340
    }
341
 
342
    /**
343
     * Test view_lti for a user with the mod/lti:view capability in the course.
344
     */
11 efrain 345
    public function test_view_lti(): void {
1 efrain 346
        [
347
            'lti' => $lti,
348
            'context' => $context,
349
            'cm' => $cm,
350
            'student' => $student,
351
        ] = $this->setup_test_data();
352
 
353
        // Test user with full capabilities.
354
        $this->setUser($student);
355
 
356
        // Trigger and capture the event.
357
        $sink = $this->redirectEvents();
358
 
359
        $result = mod_lti_external::view_lti($lti->id);
360
        // The value of the result isn't needed but validation is.
361
        external_api::clean_returnvalue(mod_lti_external::view_lti_returns(), $result);
362
 
363
        $events = $sink->get_events();
364
        $this->assertCount(1, $events);
365
        $event = array_shift($events);
366
 
367
        // Checking that the event contains the expected values.
368
        $this->assertInstanceOf('\mod_lti\event\course_module_viewed', $event);
369
        $this->assertEquals($context, $event->get_context());
370
        $moodlelti = new \moodle_url('/mod/lti/view.php', array('id' => $cm->id));
371
        $this->assertEquals($moodlelti, $event->get_url());
372
        $this->assertEventContextNotUsed($event);
373
        $this->assertNotEmpty($event->get_name());
374
    }
375
 
376
    /**
377
     * Test create_tool_proxy.
378
     */
11 efrain 379
    public function test_mod_lti_create_tool_proxy(): void {
1 efrain 380
        $this->setAdminUser();
381
        $capabilities = ['AA', 'BB'];
382
        $proxy = mod_lti_external::create_tool_proxy('Test proxy', $this->getExternalTestFileUrl('/test.html'), $capabilities, []);
383
        $proxy = (object) external_api::clean_returnvalue(mod_lti_external::create_tool_proxy_returns(), $proxy);
384
 
385
        $this->assertEquals('Test proxy', $proxy->name);
386
        $this->assertEquals($this->getExternalTestFileUrl('/test.html'), $proxy->regurl);
387
        $this->assertEquals(LTI_TOOL_PROXY_STATE_PENDING, $proxy->state);
388
        $this->assertEquals(implode("\n", $capabilities), $proxy->capabilityoffered);
389
    }
390
 
391
    /**
392
     * Test create_tool_proxy with a duplicate url.
393
     */
11 efrain 394
    public function test_mod_lti_create_tool_proxy_duplicateurl(): void {
1 efrain 395
        $this->setAdminUser();
396
        mod_lti_external::create_tool_proxy('Test proxy 1', $this->getExternalTestFileUrl('/test.html'), array(), array());
397
 
398
        $this->expectException(\moodle_exception::class);
399
        mod_lti_external::create_tool_proxy('Test proxy 2', $this->getExternalTestFileUrl('/test.html'), array(), array());
400
    }
401
 
402
    /**
403
     * Test create_tool_proxy for a user without the required capability.
404
     */
11 efrain 405
    public function test_mod_lti_create_tool_proxy_without_capability(): void {
1 efrain 406
        $course = $this->getDataGenerator()->create_course();
407
        $teacher = $this->getDataGenerator()->create_and_enrol($course, 'editingteacher');
408
        $this->setUser($teacher);
409
        $this->expectException(\required_capability_exception::class);
410
        mod_lti_external::create_tool_proxy('Test proxy', $this->getExternalTestFileUrl('/test.html'), array(), array());
411
    }
412
 
413
    /**
414
     * Test delete_tool_proxy.
415
     */
11 efrain 416
    public function test_mod_lti_delete_tool_proxy(): void {
1 efrain 417
        $this->setAdminUser();
418
        $proxy = mod_lti_external::create_tool_proxy('Test proxy', $this->getExternalTestFileUrl('/test.html'), array(), array());
419
        $proxy = (object) external_api::clean_returnvalue(mod_lti_external::create_tool_proxy_returns(), $proxy);
420
        $this->assertNotEmpty(lti_get_tool_proxy($proxy->id));
421
 
422
        $proxy = mod_lti_external::delete_tool_proxy($proxy->id);
423
        $proxy = (object) external_api::clean_returnvalue(mod_lti_external::delete_tool_proxy_returns(), $proxy);
424
 
425
        $this->assertEquals('Test proxy', $proxy->name);
426
        $this->assertEquals($this->getExternalTestFileUrl('/test.html'), $proxy->regurl);
427
        $this->assertEquals(LTI_TOOL_PROXY_STATE_PENDING, $proxy->state);
428
        $this->assertEmpty(lti_get_tool_proxy($proxy->id));
429
    }
430
 
431
    /**
432
     * Test get_tool_proxy_registration_request.
433
     */
11 efrain 434
    public function test_mod_lti_get_tool_proxy_registration_request(): void {
1 efrain 435
        $this->setAdminUser();
436
        $proxy = mod_lti_external::create_tool_proxy('Test proxy', $this->getExternalTestFileUrl('/test.html'), array(), array());
437
        $proxy = (object) external_api::clean_returnvalue(mod_lti_external::create_tool_proxy_returns(), $proxy);
438
 
439
        $request = mod_lti_external::get_tool_proxy_registration_request($proxy->id);
440
        $request = external_api::clean_returnvalue(mod_lti_external::get_tool_proxy_registration_request_returns(),
441
            $request);
442
 
443
        $this->assertEquals('ToolProxyRegistrationRequest', $request['lti_message_type']);
444
        $this->assertEquals('LTI-2p0', $request['lti_version']);
445
    }
446
 
447
    /**
448
     * Test get_tool_types.
449
     */
11 efrain 450
    public function test_mod_lti_get_tool_types(): void {
1 efrain 451
        $this->setAdminUser();
452
        $proxy = mod_lti_external::create_tool_proxy('Test proxy', $this->getExternalTestFileUrl('/test.html'), array(), array());
453
        $proxy = (object) external_api::clean_returnvalue(mod_lti_external::create_tool_proxy_returns(), $proxy);
454
 
455
        // Create a tool type, associated with that proxy.
456
        $type = new \stdClass();
457
        $data = new \stdClass();
458
        $type->state = LTI_TOOL_STATE_CONFIGURED;
459
        $type->name = "Test tool";
460
        $type->description = "Example description";
461
        $type->toolproxyid = $proxy->id;
462
        $type->baseurl = $this->getExternalTestFileUrl('/test.html');
463
        lti_add_type($type, $data);
464
 
465
        $types = mod_lti_external::get_tool_types($proxy->id);
466
        $types = external_api::clean_returnvalue(mod_lti_external::get_tool_types_returns(), $types);
467
 
468
        $this->assertCount(1, $types);
469
        $type = $types[0];
470
        $this->assertEquals('Test tool', $type['name']);
471
        $this->assertEquals('Example description', $type['description']);
472
    }
473
 
474
    /**
475
     * Test create_tool_type.
476
     */
11 efrain 477
    public function test_mod_lti_create_tool_type(): void {
1 efrain 478
        $this->setAdminUser();
479
        $type = mod_lti_external::create_tool_type($this->getExternalTestFileUrl('/ims_cartridge_basic_lti_link.xml'), '', '');
480
        $type = external_api::clean_returnvalue(mod_lti_external::create_tool_type_returns(), $type);
481
 
482
        $this->assertEquals('Example tool', $type['name']);
483
        $this->assertEquals('Example tool description', $type['description']);
484
        $this->assertEquals('https://download.moodle.org/unittest/test.jpg', $type['urls']['icon']);
485
        $typeentry = lti_get_type($type['id']);
486
        $this->assertEquals('http://www.example.com/lti/provider.php', $typeentry->baseurl);
487
        $config = lti_get_type_config($type['id']);
488
        $this->assertTrue(isset($config['sendname']));
489
        $this->assertTrue(isset($config['sendemailaddr']));
490
        $this->assertTrue(isset($config['acceptgrades']));
491
        $this->assertTrue(isset($config['forcessl']));
492
    }
493
 
494
    /**
495
     * Test create_tool_type failure from non existent file.
496
     */
11 efrain 497
    public function test_mod_lti_create_tool_type_nonexistant_file(): void {
1 efrain 498
        $this->expectException(\moodle_exception::class);
499
        mod_lti_external::create_tool_type($this->getExternalTestFileUrl('/doesntexist.xml'), '', '');
500
    }
501
 
502
    /**
503
     * Test create_tool_type failure from xml that is not a cartridge.
504
     */
11 efrain 505
    public function test_mod_lti_create_tool_type_bad_file(): void {
1 efrain 506
        $this->expectException(\moodle_exception::class);
507
        mod_lti_external::create_tool_type($this->getExternalTestFileUrl('/rsstest.xml'), '', '');
508
    }
509
 
510
    /**
511
     * Test create_tool_type as a user without the required capability.
512
     */
11 efrain 513
    public function test_mod_lti_create_tool_type_without_capability(): void {
1 efrain 514
        $course = $this->getDataGenerator()->create_course();
515
        $teacher = $this->getDataGenerator()->create_and_enrol($course, 'editingteacher');
516
        $this->setUser($teacher);
517
        $this->expectException(\required_capability_exception::class);
518
        mod_lti_external::create_tool_type($this->getExternalTestFileUrl('/ims_cartridge_basic_lti_link.xml'), '', '');
519
    }
520
 
521
    /**
522
     * Test update_tool_type.
523
     */
11 efrain 524
    public function test_mod_lti_update_tool_type(): void {
1 efrain 525
        $this->setAdminUser();
526
        $type = mod_lti_external::create_tool_type($this->getExternalTestFileUrl('/ims_cartridge_basic_lti_link.xml'), '', '');
527
        $type = external_api::clean_returnvalue(mod_lti_external::create_tool_type_returns(), $type);
528
 
529
        $type = mod_lti_external::update_tool_type($type['id'], 'New name', 'New description', LTI_TOOL_STATE_PENDING);
530
        $type = external_api::clean_returnvalue(mod_lti_external::update_tool_type_returns(), $type);
531
 
532
        $this->assertEquals('New name', $type['name']);
533
        $this->assertEquals('New description', $type['description']);
534
        $this->assertEquals('Pending', $type['state']['text']);
535
    }
536
 
537
    /**
538
     * Test delete_tool_type for a user with the required capability.
539
     */
11 efrain 540
    public function test_mod_lti_delete_tool_type(): void {
1 efrain 541
        $this->setAdminUser();
542
        $type = mod_lti_external::create_tool_type($this->getExternalTestFileUrl('/ims_cartridge_basic_lti_link.xml'), '', '');
543
        $type = external_api::clean_returnvalue(mod_lti_external::create_tool_type_returns(), $type);
544
        $this->assertNotEmpty(lti_get_type($type['id']));
545
 
546
        $type = mod_lti_external::delete_tool_type($type['id']);
547
        $type = external_api::clean_returnvalue(mod_lti_external::delete_tool_type_returns(), $type);
548
        $this->assertEmpty(lti_get_type($type['id']));
549
    }
550
 
551
    /**
552
     * Test delete_tool_type for a user without the required capability.
553
     */
11 efrain 554
    public function test_mod_lti_delete_tool_type_without_capability(): void {
1 efrain 555
        $this->setAdminUser();
556
        $type = mod_lti_external::create_tool_type($this->getExternalTestFileUrl('/ims_cartridge_basic_lti_link.xml'), '', '');
557
        $type = external_api::clean_returnvalue(mod_lti_external::create_tool_type_returns(), $type);
558
        $this->assertNotEmpty(lti_get_type($type['id']));
559
 
560
        $course = $this->getDataGenerator()->create_course();
561
        $teacher = $this->getDataGenerator()->create_and_enrol($course, 'editingteacher');
562
        $this->setUser($teacher);
563
        $this->expectException(\required_capability_exception::class);
564
        mod_lti_external::delete_tool_type($type['id']);
565
    }
566
 
567
    /**
568
     * Test is_cartridge.
569
     */
11 efrain 570
    public function test_mod_lti_is_cartridge(): void {
1 efrain 571
        $this->setAdminUser();
572
        $result = mod_lti_external::is_cartridge($this->getExternalTestFileUrl('/ims_cartridge_basic_lti_link.xml'));
573
        $result = external_api::clean_returnvalue(mod_lti_external::is_cartridge_returns(), $result);
574
        $this->assertTrue($result['iscartridge']);
575
 
576
        $result = mod_lti_external::is_cartridge($this->getExternalTestFileUrl('/test.html'));
577
        $result = external_api::clean_returnvalue(mod_lti_external::is_cartridge_returns(), $result);
578
        $this->assertFalse($result['iscartridge']);
579
    }
580
}