MDL-52154 mod_lti: Improve admin interface
[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() {
49 global $DB;
50 $this->resetAfterTest();
51 $this->setAdminUser();
52
53 // Setup test data.
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);
58
59 // Create users.
60 $this->student = self::getDataGenerator()->create_user();
61 $this->teacher = self::getDataGenerator()->create_user();
62
63 // Users enrolments.
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');
68 }
69
70 /**
71 * Test view_lti
72 */
73 public function test_get_tool_launch_data() {
7204d77b
JO
74 global $USER, $SITE;
75
d5ded9e7
JL
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);
78
79 // Basic test, the function returns what it's expected.
80 self::assertEquals($this->lti->toolurl, $result['endpoint']);
7204d77b 81 self::assertCount(36, $result['parameters']);
d5ded9e7
JL
82
83 // Check some parameters.
84 $parameters = array();
85 foreach ($result['parameters'] as $param) {
86 $parameters[$param['name']] = $param['value'];
87 }
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']);
2a5bb939
JO
96 self::assertEquals("phpunit", $parameters['tool_consumer_instance_name']);
97 self::assertEquals("PHPUnit test site", $parameters['tool_consumer_instance_description']);
d5ded9e7
JL
98
99 }
100
dfcdec12
JL
101 /*
102 * Test get ltis by courses
103 */
104 public function test_mod_lti_get_ltis_by_courses() {
105 global $DB;
106
107 // Create additional course.
108 $course2 = self::getDataGenerator()->create_course();
109
110 // Second lti.
111 $record = new stdClass();
112 $record->course = $course2->id;
113 $lti2 = self::getDataGenerator()->create_module('lti', $record);
114
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;
121 break;
122 }
123 }
124 $enrol->enrol_user($instance2, $this->student->id, $this->studentrole->id);
125
126 self::setUser($this->student);
127
128 $returndescription = mod_lti_external::get_ltis_by_courses_returns();
129
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', 'launchcontainer',
133 'showtitlelaunch', 'showdescriptionlaunch', 'icon', 'secureicon');
134
135 // Add expected coursemodule and data.
136 $lti1 = $this->lti;
137 $lti1->coursemodule = $lti1->cmid;
138 $lti1->introformat = 1;
139 $lti1->section = 0;
140 $lti1->visible = true;
141 $lti1->groupmode = 0;
142 $lti1->groupingid = 0;
143
144 $lti2->coursemodule = $lti2->cmid;
145 $lti2->introformat = 1;
146 $lti2->section = 0;
147 $lti2->visible = true;
148 $lti2->groupmode = 0;
149 $lti2->groupingid = 0;
150
151 foreach ($expectedfields as $field) {
152 $expected1[$field] = $lti1->{$field};
153 $expected2[$field] = $lti2->{$field};
154 }
155
156 $expectedltis = array($expected2, $expected1);
157
158 // Call the external function passing course ids.
159 $result = mod_lti_external::get_ltis_by_courses(array($course2->id, $this->course->id));
160 $result = external_api::clean_returnvalue($returndescription, $result);
161
162 $this->assertEquals($expectedltis, $result['ltis']);
163 $this->assertCount(0, $result['warnings']);
164
165 // Call the external function without passing course id.
166 $result = mod_lti_external::get_ltis_by_courses();
167 $result = external_api::clean_returnvalue($returndescription, $result);
168 $this->assertEquals($expectedltis, $result['ltis']);
169 $this->assertCount(0, $result['warnings']);
170
171 // Unenrol user from second course and alter expected ltis.
172 $enrol->unenrol_user($instance2, $this->student->id);
173 array_shift($expectedltis);
174
175 // Call the external function without passing course id.
176 $result = mod_lti_external::get_ltis_by_courses();
177 $result = external_api::clean_returnvalue($returndescription, $result);
178 $this->assertEquals($expectedltis, $result['ltis']);
179
180 // Call for the second course we unenrolled the user from, expected warning.
181 $result = mod_lti_external::get_ltis_by_courses(array($course2->id));
182 $this->assertCount(1, $result['warnings']);
183 $this->assertEquals('1', $result['warnings'][0]['warningcode']);
184 $this->assertEquals($course2->id, $result['warnings'][0]['itemid']);
185
186 // Now, try as a teacher for getting all the additional fields.
187 self::setUser($this->teacher);
188
189 $additionalfields = array('timecreated', 'timemodified', 'typeid', 'toolurl', 'securetoolurl',
190 'instructorchoicesendname', 'instructorchoicesendemailaddr', 'instructorchoiceallowroster',
191 'instructorchoiceallowsetting', 'instructorcustomparameters', 'instructorchoiceacceptgrades', 'grade',
192 'resourcekey', 'password', 'debuglaunch', 'servicesalt', 'visible', 'groupmode', 'groupingid');
193
194 foreach ($additionalfields as $field) {
195 $expectedltis[0][$field] = $lti1->{$field};
196 }
197
198 $result = mod_lti_external::get_ltis_by_courses();
199 $result = external_api::clean_returnvalue($returndescription, $result);
200 $this->assertEquals($expectedltis, $result['ltis']);
201
202 // Admin also should get all the information.
203 self::setAdminUser();
204
205 $result = mod_lti_external::get_ltis_by_courses(array($this->course->id));
206 $result = external_api::clean_returnvalue($returndescription, $result);
207 $this->assertEquals($expectedltis, $result['ltis']);
208
209 // Now, prohibit capabilities.
210 $this->setUser($this->student);
211 $contextcourse1 = context_course::instance($this->course->id);
212 // Prohibit capability = mod:lti:view on Course1 for students.
213 assign_capability('mod/lti:view', CAP_PROHIBIT, $this->studentrole->id, $contextcourse1->id);
0bf6c0be 214 // Empty all the caches that may be affected by this change.
dfcdec12 215 accesslib_clear_all_caches_for_unit_testing();
0bf6c0be 216 course_modinfo::clear_instance_cache();
dfcdec12
JL
217
218 $ltis = mod_lti_external::get_ltis_by_courses(array($this->course->id));
219 $ltis = external_api::clean_returnvalue(mod_lti_external::get_ltis_by_courses_returns(), $ltis);
0bf6c0be 220 $this->assertCount(0, $ltis['ltis']);
dfcdec12
JL
221 }
222
4bed1682
JL
223 /**
224 * Test view_lti
225 */
226 public function test_view_lti() {
227 global $DB;
228
229 // Test invalid instance id.
230 try {
231 mod_lti_external::view_lti(0);
232 $this->fail('Exception expected due to invalid mod_lti instance id.');
233 } catch (moodle_exception $e) {
234 $this->assertEquals('invalidrecord', $e->errorcode);
235 }
236
237 // Test not-enrolled user.
238 $usernotenrolled = self::getDataGenerator()->create_user();
239 $this->setUser($usernotenrolled);
240 try {
241 mod_lti_external::view_lti($this->lti->id);
242 $this->fail('Exception expected due to not enrolled user.');
243 } catch (moodle_exception $e) {
244 $this->assertEquals('requireloginerror', $e->errorcode);
245 }
246
247 // Test user with full capabilities.
248 $this->setUser($this->student);
249
250 // Trigger and capture the event.
251 $sink = $this->redirectEvents();
252
253 $result = mod_lti_external::view_lti($this->lti->id);
254 $result = external_api::clean_returnvalue(mod_lti_external::view_lti_returns(), $result);
255
256 $events = $sink->get_events();
257 $this->assertCount(1, $events);
258 $event = array_shift($events);
259
260 // Checking that the event contains the expected values.
261 $this->assertInstanceOf('\mod_lti\event\course_module_viewed', $event);
262 $this->assertEquals($this->context, $event->get_context());
263 $moodlelti = new \moodle_url('/mod/lti/view.php', array('id' => $this->cm->id));
264 $this->assertEquals($moodlelti, $event->get_url());
265 $this->assertEventContextNotUsed($event);
266 $this->assertNotEmpty($event->get_name());
267
268 // Test user with no capabilities.
269 // We need a explicit prohibit since this capability is only defined in authenticated user and guest roles.
270 assign_capability('mod/lti:view', CAP_PROHIBIT, $this->studentrole->id, $this->context->id);
0bf6c0be 271 // Empty all the caches that may be affected by this change.
4bed1682 272 accesslib_clear_all_caches_for_unit_testing();
0bf6c0be 273 course_modinfo::clear_instance_cache();
4bed1682
JL
274
275 try {
276 mod_lti_external::view_lti($this->lti->id);
277 $this->fail('Exception expected due to missing capability.');
278 } catch (moodle_exception $e) {
0bf6c0be 279 $this->assertEquals('requireloginerror', $e->errorcode);
4bed1682
JL
280 }
281
282 }
283
d5ded9e7 284}