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