2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * External tool module external functions tests
22 * @copyright 2015 Juan Leyva <juan@moodle.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 defined('MOODLE_INTERNAL') ||
die();
31 require_once($CFG->dirroot
. '/webservice/tests/helpers.php');
32 require_once($CFG->dirroot
. '/mod/lti/lib.php');
35 * External tool module external functions tests
39 * @copyright 2015 Juan Leyva <juan@moodle.com>
40 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
43 class mod_lti_external_testcase
extends externallib_advanced_testcase
{
46 * Set up for every test
48 public function setUp() {
50 $this->resetAfterTest();
51 $this->setAdminUser();
54 $this->course
= $this->getDataGenerator()->create_course();
55 $this->lti
= $this->getDataGenerator()->create_module('lti', array('course' => $this->course
->id
));
56 $this->context
= context_module
::instance($this->lti
->cmid
);
57 $this->cm
= get_coursemodule_from_instance('lti', $this->lti
->id
);
60 $this->student
= self
::getDataGenerator()->create_user();
61 $this->teacher
= self
::getDataGenerator()->create_user();
64 $this->studentrole
= $DB->get_record('role', array('shortname' => 'student'));
65 $this->teacherrole
= $DB->get_record('role', array('shortname' => 'editingteacher'));
66 $this->getDataGenerator()->enrol_user($this->student
->id
, $this->course
->id
, $this->studentrole
->id
, 'manual');
67 $this->getDataGenerator()->enrol_user($this->teacher
->id
, $this->course
->id
, $this->teacherrole
->id
, 'manual');
73 public function test_get_tool_launch_data() {
76 $result = mod_lti_external
::get_tool_launch_data($this->lti
->id
);
77 $result = external_api
::clean_returnvalue(mod_lti_external
::get_tool_launch_data_returns(), $result);
79 // Basic test, the function returns what it's expected.
80 self
::assertEquals($this->lti
->toolurl
, $result['endpoint']);
81 self
::assertCount(36, $result['parameters']);
83 // Check some parameters.
84 $parameters = array();
85 foreach ($result['parameters'] as $param) {
86 $parameters[$param['name']] = $param['value'];
88 self
::assertEquals($this->lti
->resourcekey
, $parameters['oauth_consumer_key']);
89 self
::assertEquals($this->course
->fullname
, $parameters['context_title']);
90 self
::assertEquals($this->course
->shortname
, $parameters['context_label']);
91 self
::assertEquals($USER->id
, $parameters['user_id']);
92 self
::assertEquals($USER->firstname
, $parameters['lis_person_name_given']);
93 self
::assertEquals($USER->lastname
, $parameters['lis_person_name_family']);
94 self
::assertEquals(fullname($USER), $parameters['lis_person_name_full']);
95 self
::assertEquals($USER->username
, $parameters['ext_user_username']);
96 self
::assertEquals("phpunit", $parameters['tool_consumer_instance_name']);
97 self
::assertEquals("PHPUnit test site", $parameters['tool_consumer_instance_description']);
102 * Test get ltis by courses
104 public function test_mod_lti_get_ltis_by_courses() {
107 // Create additional course.
108 $course2 = self
::getDataGenerator()->create_course();
111 $record = new stdClass();
112 $record->course
= $course2->id
;
113 $lti2 = self
::getDataGenerator()->create_module('lti', $record);
115 // Execute real Moodle enrolment as we'll call unenrol() method on the instance later.
116 $enrol = enrol_get_plugin('manual');
117 $enrolinstances = enrol_get_instances($course2->id
, true);
118 foreach ($enrolinstances as $courseenrolinstance) {
119 if ($courseenrolinstance->enrol
== "manual") {
120 $instance2 = $courseenrolinstance;
124 $enrol->enrol_user($instance2, $this->student
->id
, $this->studentrole
->id
);
126 self
::setUser($this->student
);
128 $returndescription = mod_lti_external
::get_ltis_by_courses_returns();
130 // Create what we expect to be returned when querying the two courses.
131 // First for the student user.
132 $expectedfields = array('id', 'coursemodule', 'course', 'name', 'intro', 'introformat', 'introfiles', 'launchcontainer',
133 'showtitlelaunch', 'showdescriptionlaunch', 'icon', 'secureicon');
135 // Add expected coursemodule and data.
137 $lti1->coursemodule
= $lti1->cmid
;
138 $lti1->introformat
= 1;
140 $lti1->visible
= true;
141 $lti1->groupmode
= 0;
142 $lti1->groupingid
= 0;
143 $lti1->introfiles
= [];
145 $lti2->coursemodule
= $lti2->cmid
;
146 $lti2->introformat
= 1;
148 $lti2->visible
= true;
149 $lti2->groupmode
= 0;
150 $lti2->groupingid
= 0;
151 $lti2->introfiles
= [];
153 foreach ($expectedfields as $field) {
154 $expected1[$field] = $lti1->{$field};
155 $expected2[$field] = $lti2->{$field};
158 $expectedltis = array($expected2, $expected1);
160 // Call the external function passing course ids.
161 $result = mod_lti_external
::get_ltis_by_courses(array($course2->id
, $this->course
->id
));
162 $result = external_api
::clean_returnvalue($returndescription, $result);
164 $this->assertEquals($expectedltis, $result['ltis']);
165 $this->assertCount(0, $result['warnings']);
167 // Call the external function without passing course id.
168 $result = mod_lti_external
::get_ltis_by_courses();
169 $result = external_api
::clean_returnvalue($returndescription, $result);
170 $this->assertEquals($expectedltis, $result['ltis']);
171 $this->assertCount(0, $result['warnings']);
173 // Unenrol user from second course and alter expected ltis.
174 $enrol->unenrol_user($instance2, $this->student
->id
);
175 array_shift($expectedltis);
177 // Call the external function without passing course id.
178 $result = mod_lti_external
::get_ltis_by_courses();
179 $result = external_api
::clean_returnvalue($returndescription, $result);
180 $this->assertEquals($expectedltis, $result['ltis']);
182 // Call for the second course we unenrolled the user from, expected warning.
183 $result = mod_lti_external
::get_ltis_by_courses(array($course2->id
));
184 $this->assertCount(1, $result['warnings']);
185 $this->assertEquals('1', $result['warnings'][0]['warningcode']);
186 $this->assertEquals($course2->id
, $result['warnings'][0]['itemid']);
188 // Now, try as a teacher for getting all the additional fields.
189 self
::setUser($this->teacher
);
191 $additionalfields = array('timecreated', 'timemodified', 'typeid', 'toolurl', 'securetoolurl',
192 'instructorchoicesendname', 'instructorchoicesendemailaddr', 'instructorchoiceallowroster',
193 'instructorchoiceallowsetting', 'instructorcustomparameters', 'instructorchoiceacceptgrades', 'grade',
194 'resourcekey', 'password', 'debuglaunch', 'servicesalt', 'visible', 'groupmode', 'groupingid');
196 foreach ($additionalfields as $field) {
197 $expectedltis[0][$field] = $lti1->{$field};
200 $result = mod_lti_external
::get_ltis_by_courses();
201 $result = external_api
::clean_returnvalue($returndescription, $result);
202 $this->assertEquals($expectedltis, $result['ltis']);
204 // Admin also should get all the information.
205 self
::setAdminUser();
207 $result = mod_lti_external
::get_ltis_by_courses(array($this->course
->id
));
208 $result = external_api
::clean_returnvalue($returndescription, $result);
209 $this->assertEquals($expectedltis, $result['ltis']);
211 // Now, prohibit capabilities.
212 $this->setUser($this->student
);
213 $contextcourse1 = context_course
::instance($this->course
->id
);
214 // Prohibit capability = mod:lti:view on Course1 for students.
215 assign_capability('mod/lti:view', CAP_PROHIBIT
, $this->studentrole
->id
, $contextcourse1->id
);
216 // Empty all the caches that may be affected by this change.
217 accesslib_clear_all_caches_for_unit_testing();
218 course_modinfo
::clear_instance_cache();
220 $ltis = mod_lti_external
::get_ltis_by_courses(array($this->course
->id
));
221 $ltis = external_api
::clean_returnvalue(mod_lti_external
::get_ltis_by_courses_returns(), $ltis);
222 $this->assertCount(0, $ltis['ltis']);
228 public function test_view_lti() {
231 // Test invalid instance id.
233 mod_lti_external
::view_lti(0);
234 $this->fail('Exception expected due to invalid mod_lti instance id.');
235 } catch (moodle_exception
$e) {
236 $this->assertEquals('invalidrecord', $e->errorcode
);
239 // Test not-enrolled user.
240 $usernotenrolled = self
::getDataGenerator()->create_user();
241 $this->setUser($usernotenrolled);
243 mod_lti_external
::view_lti($this->lti
->id
);
244 $this->fail('Exception expected due to not enrolled user.');
245 } catch (moodle_exception
$e) {
246 $this->assertEquals('requireloginerror', $e->errorcode
);
249 // Test user with full capabilities.
250 $this->setUser($this->student
);
252 // Trigger and capture the event.
253 $sink = $this->redirectEvents();
255 $result = mod_lti_external
::view_lti($this->lti
->id
);
256 $result = external_api
::clean_returnvalue(mod_lti_external
::view_lti_returns(), $result);
258 $events = $sink->get_events();
259 $this->assertCount(1, $events);
260 $event = array_shift($events);
262 // Checking that the event contains the expected values.
263 $this->assertInstanceOf('\mod_lti\event\course_module_viewed', $event);
264 $this->assertEquals($this->context
, $event->get_context());
265 $moodlelti = new \
moodle_url('/mod/lti/view.php', array('id' => $this->cm
->id
));
266 $this->assertEquals($moodlelti, $event->get_url());
267 $this->assertEventContextNotUsed($event);
268 $this->assertNotEmpty($event->get_name());
270 // Test user with no capabilities.
271 // We need a explicit prohibit since this capability is only defined in authenticated user and guest roles.
272 assign_capability('mod/lti:view', CAP_PROHIBIT
, $this->studentrole
->id
, $this->context
->id
);
273 // Empty all the caches that may be affected by this change.
274 accesslib_clear_all_caches_for_unit_testing();
275 course_modinfo
::clear_instance_cache();
278 mod_lti_external
::view_lti($this->lti
->id
);
279 $this->fail('Exception expected due to missing capability.');
280 } catch (moodle_exception
$e) {
281 $this->assertEquals('requireloginerror', $e->errorcode
);
287 * Test create tool proxy
289 public function test_mod_lti_create_tool_proxy() {
290 $capabilities = ['AA', 'BB'];
291 $proxy = mod_lti_external
::create_tool_proxy('Test proxy', $this->getExternalTestFileUrl('/test.html'), $capabilities, []);
292 $this->assertEquals('Test proxy', $proxy->name
);
293 $this->assertEquals($this->getExternalTestFileUrl('/test.html'), $proxy->regurl
);
294 $this->assertEquals(LTI_TOOL_PROXY_STATE_PENDING
, $proxy->state
);
295 $this->assertEquals(implode("\n", $capabilities), $proxy->capabilityoffered
);
299 * Test create tool proxy with duplicate url
301 public function test_mod_lti_create_tool_proxy_duplicateurl() {
302 $this->expectException('moodle_exception');
303 $proxy = mod_lti_external
::create_tool_proxy('Test proxy 1', $this->getExternalTestFileUrl('/test.html'), array(), array());
304 $proxy = mod_lti_external
::create_tool_proxy('Test proxy 2', $this->getExternalTestFileUrl('/test.html'), array(), array());
308 * Test create tool proxy without sufficient capability
310 public function test_mod_lti_create_tool_proxy_without_capability() {
311 self
::setUser($this->teacher
);
312 $this->expectException('required_capability_exception');
313 $proxy = mod_lti_external
::create_tool_proxy('Test proxy', $this->getExternalTestFileUrl('/test.html'), array(), array());
317 * Test delete tool proxy
319 public function test_mod_lti_delete_tool_proxy() {
320 $proxy = mod_lti_external
::create_tool_proxy('Test proxy', $this->getExternalTestFileUrl('/test.html'), array(), array());
321 $this->assertNotEmpty(lti_get_tool_proxy($proxy->id
));
323 $proxy = mod_lti_external
::delete_tool_proxy($proxy->id
);
324 $this->assertEquals('Test proxy', $proxy->name
);
325 $this->assertEquals($this->getExternalTestFileUrl('/test.html'), $proxy->regurl
);
326 $this->assertEquals(LTI_TOOL_PROXY_STATE_PENDING
, $proxy->state
);
327 $this->assertEmpty(lti_get_tool_proxy($proxy->id
));
331 * Test get tool proxy registration request
333 public function test_mod_lti_get_tool_proxy_registration_request() {
334 $proxy = mod_lti_external
::create_tool_proxy('Test proxy', $this->getExternalTestFileUrl('/test.html'), array(), array());
335 $request = mod_lti_external
::get_tool_proxy_registration_request($proxy->id
);
336 $this->assertEquals('ToolProxyRegistrationRequest', $request['lti_message_type']);
337 $this->assertEquals('LTI-2p0', $request['lti_version']);
341 * Test get tool types
343 public function test_mod_lti_get_tool_types() {
344 // Create a tool proxy.
345 $proxy = mod_lti_external
::create_tool_proxy('Test proxy', $this->getExternalTestFileUrl('/test.html'), array(), array());
347 // Create a tool type, associated with that proxy.
348 $type = new stdClass();
349 $data = new stdClass();
350 $type->state
= LTI_TOOL_STATE_CONFIGURED
;
351 $type->name
= "Test tool";
352 $type->description
= "Example description";
353 $type->toolproxyid
= $proxy->id
;
354 $type->baseurl
= $this->getExternalTestFileUrl('/test.html');
355 $typeid = lti_add_type($type, $data);
357 $types = mod_lti_external
::get_tool_types($proxy->id
);
358 $this->assertEquals(1, count($types));
360 $this->assertEquals('Test tool', $type['name']);
361 $this->assertEquals('Example description', $type['description']);
365 * Test create tool type
367 public function test_mod_lti_create_tool_type() {
368 $type = mod_lti_external
::create_tool_type($this->getExternalTestFileUrl('/ims_cartridge_basic_lti_link.xml'), '', '');
369 $this->assertEquals('Example tool', $type['name']);
370 $this->assertEquals('Example tool description', $type['description']);
371 $this->assertEquals($this->getExternalTestFileUrl('/test.jpg', true), $type['urls']['icon']);
372 $typeentry = lti_get_type($type['id']);
373 $this->assertEquals('http://www.example.com/lti/provider.php', $typeentry->baseurl
);
374 $config = lti_get_type_config($type['id']);
375 $this->assertTrue(isset($config['sendname']));
376 $this->assertTrue(isset($config['sendemailaddr']));
377 $this->assertTrue(isset($config['acceptgrades']));
378 $this->assertTrue(isset($config['forcessl']));
382 * Test create tool type failure from non existant file
384 public function test_mod_lti_create_tool_type_nonexistant_file() {
385 $this->expectException('moodle_exception');
386 $type = mod_lti_external
::create_tool_type($this->getExternalTestFileUrl('/doesntexist.xml'), '', '');
390 * Test create tool type failure from xml that is not a cartridge
392 public function test_mod_lti_create_tool_type_bad_file() {
393 $this->expectException('moodle_exception');
394 $type = mod_lti_external
::create_tool_type($this->getExternalTestFileUrl('/rsstest.xml'), '', '');
398 * Test creating of tool types without sufficient capability
400 public function test_mod_lti_create_tool_type_without_capability() {
401 self
::setUser($this->teacher
);
402 $this->expectException('required_capability_exception');
403 $type = mod_lti_external
::create_tool_type($this->getExternalTestFileUrl('/ims_cartridge_basic_lti_link.xml'), '', '');
407 * Test update tool type
409 public function test_mod_lti_update_tool_type() {
410 $type = mod_lti_external
::create_tool_type($this->getExternalTestFileUrl('/ims_cartridge_basic_lti_link.xml'), '', '');
411 $type = mod_lti_external
::update_tool_type($type['id'], 'New name', 'New description', LTI_TOOL_STATE_PENDING
);
412 $this->assertEquals('New name', $type['name']);
413 $this->assertEquals('New description', $type['description']);
414 $this->assertEquals('Pending', $type['state']['text']);
418 * Test delete tool type
420 public function test_mod_lti_delete_tool_type() {
421 $type = mod_lti_external
::create_tool_type($this->getExternalTestFileUrl('/ims_cartridge_basic_lti_link.xml'), '', '');
422 $this->assertNotEmpty(lti_get_type($type['id']));
423 $type = mod_lti_external
::delete_tool_type($type['id']);
424 $this->assertEmpty(lti_get_type($type['id']));
428 * Test delete tool type without sufficient capability
430 public function test_mod_lti_delete_tool_type_without_capability() {
431 $type = mod_lti_external
::create_tool_type($this->getExternalTestFileUrl('/ims_cartridge_basic_lti_link.xml'), '', '');
432 $this->assertNotEmpty(lti_get_type($type['id']));
433 $this->expectException('required_capability_exception');
434 self
::setUser($this->teacher
);
435 $type = mod_lti_external
::delete_tool_type($type['id']);
441 public function test_mod_lti_is_cartridge() {
442 $result = mod_lti_external
::is_cartridge($this->getExternalTestFileUrl('/ims_cartridge_basic_lti_link.xml'));
443 $this->assertTrue($result['iscartridge']);
444 $result = mod_lti_external
::is_cartridge($this->getExternalTestFileUrl('/test.html'));
445 $this->assertFalse($result['iscartridge']);