MDL-62456 mod_lti: add missing add_external_location_link call
[moodle.git] / mod / lti / tests / privacy_provider_test.php
CommitLineData
3467e8ce
MN
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 * Privacy provider tests.
19 *
20 * @package mod_lti
21 * @copyright 2018 Mark Nelson <markn@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25use core_privacy\local\metadata\collection;
26use mod_lti\privacy\provider;
27
28defined('MOODLE_INTERNAL') || die();
29
30/**
31 * Privacy provider tests class.
32 *
33 * @package mod_lti
34 * @copyright 2018 Mark Nelson <markn@moodle.com>
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 */
37class mod_lti_privacy_provider_testcase extends \core_privacy\tests\provider_testcase {
38
39 /**
40 * Test for provider::get_metadata().
41 */
42 public function test_get_metadata() {
43 $collection = new collection('mod_lti');
44 $newcollection = provider::get_metadata($collection);
45 $itemcollection = $newcollection->get_collection();
ad7f1438
MN
46 $this->assertCount(4, $itemcollection);
47
48 $ltiproviderexternal = array_shift($itemcollection);
49 $this->assertEquals('lti_provider', $ltiproviderexternal->get_name());
3467e8ce
MN
50
51 $ltisubmissiontable = array_shift($itemcollection);
52 $this->assertEquals('lti_submission', $ltisubmissiontable->get_name());
53
54 $ltitoolproxies = array_shift($itemcollection);
55 $this->assertEquals('lti_tool_proxies', $ltitoolproxies->get_name());
56
57 $ltitypestable = array_shift($itemcollection);
58 $this->assertEquals('lti_types', $ltitypestable->get_name());
59
60 $privacyfields = $ltisubmissiontable->get_privacy_fields();
61 $this->assertArrayHasKey('userid', $privacyfields);
62 $this->assertArrayHasKey('datesubmitted', $privacyfields);
63 $this->assertArrayHasKey('dateupdated', $privacyfields);
64 $this->assertArrayHasKey('gradepercent', $privacyfields);
65 $this->assertArrayHasKey('originalgrade', $privacyfields);
66 $this->assertEquals('privacy:metadata:lti_submission', $ltisubmissiontable->get_summary());
67
68 $privacyfields = $ltitoolproxies->get_privacy_fields();
69 $this->assertArrayHasKey('name', $privacyfields);
70 $this->assertArrayHasKey('createdby', $privacyfields);
71 $this->assertArrayHasKey('timecreated', $privacyfields);
72 $this->assertArrayHasKey('timemodified', $privacyfields);
73 $this->assertEquals('privacy:metadata:lti_tool_proxies', $ltitoolproxies->get_summary());
74
75 $privacyfields = $ltitypestable->get_privacy_fields();
76 $this->assertArrayHasKey('name', $privacyfields);
77 $this->assertArrayHasKey('createdby', $privacyfields);
78 $this->assertArrayHasKey('timecreated', $privacyfields);
79 $this->assertArrayHasKey('timemodified', $privacyfields);
80 $this->assertEquals('privacy:metadata:lti_types', $ltitypestable->get_summary());
81 }
82
83 /**
84 * Test for provider::get_contexts_for_userid().
85 */
86 public function test_get_contexts_for_userid() {
87 $this->resetAfterTest();
88
89 $course = $this->getDataGenerator()->create_course();
90
91 // The LTI activity the user will have submitted something for.
92 $lti = $this->getDataGenerator()->create_module('lti', array('course' => $course->id));
93
94 // Another LTI activity that has no user activity.
95 $this->getDataGenerator()->create_module('lti', array('course' => $course->id));
96
97 // Create a user which will make a submission.
98 $user = $this->getDataGenerator()->create_user();
99
100 $this->create_lti_submission($lti->id, $user->id);
101
102 // Check the contexts supplied are correct.
103 $contextlist = provider::get_contexts_for_userid($user->id);
104 $this->assertCount(2, $contextlist);
105
106 $contextformodule = $contextlist->current();
107 $cmcontext = context_module::instance($lti->cmid);
108 $this->assertEquals($cmcontext->id, $contextformodule->id);
109
110 $contextlist->next();
111 $contextforsystem = $contextlist->current();
112 $this->assertEquals(SYSCONTEXTID, $contextforsystem->id);
113 }
114
115 /**
116 * Test for provider::export_user_data().
117 */
118 public function test_export_for_context_submissions() {
119 $this->resetAfterTest();
120
121 $course = $this->getDataGenerator()->create_course();
122
123 $lti = $this->getDataGenerator()->create_module('lti', array('course' => $course->id));
124
125 // Create users which will make submissions.
126 $user1 = $this->getDataGenerator()->create_user();
127 $user2 = $this->getDataGenerator()->create_user();
128
129 $this->create_lti_submission($lti->id, $user1->id);
130 $this->create_lti_submission($lti->id, $user1->id);
131 $this->create_lti_submission($lti->id, $user2->id);
132
133 // Export all of the data for the context for user 1.
134 $cmcontext = context_module::instance($lti->cmid);
135 $this->export_context_data_for_user($user1->id, $cmcontext, 'mod_lti');
136 $writer = \core_privacy\local\request\writer::with_context($cmcontext);
137
138 $this->assertTrue($writer->has_any_data());
139
140 $data = $writer->get_data();
141 $this->assertCount(2, $data->submissions);
142 }
143
144 /**
145 * Test for provider::export_user_data().
146 */
147 public function test_export_for_context_tool_types() {
148 $this->resetAfterTest();
149
150 $course1 = $this->getDataGenerator()->create_course();
151 $course2 = $this->getDataGenerator()->create_course();
152
153 // Create a user which will make a tool type.
154 $user = $this->getDataGenerator()->create_user();
155 $this->setUser($user);
156
157 // Create a user that will not make a tool type.
158 $this->getDataGenerator()->create_user();
159
160 $type = new stdClass();
161 $type->baseurl = 'http://moodle.org';
162 $type->course = $course1->id;
163 lti_add_type($type, new stdClass());
164
165 $type = new stdClass();
166 $type->baseurl = 'http://moodle.org';
167 $type->course = $course1->id;
168 lti_add_type($type, new stdClass());
169
170 $type = new stdClass();
171 $type->baseurl = 'http://moodle.org';
172 $type->course = $course2->id;
173 lti_add_type($type, new stdClass());
174
175 // Export all of the data for the context.
176 $coursecontext = context_course::instance($course1->id);
177 $this->export_context_data_for_user($user->id, $coursecontext, 'mod_lti');
178 $writer = \core_privacy\local\request\writer::with_context($coursecontext);
179
180 $this->assertTrue($writer->has_any_data());
181
182 $data = $writer->get_data();
183 $this->assertCount(2, $data->lti_types);
184
185 $coursecontext = context_course::instance($course2->id);
186 $this->export_context_data_for_user($user->id, $coursecontext, 'mod_lti');
187 $writer = \core_privacy\local\request\writer::with_context($coursecontext);
188
189 $this->assertTrue($writer->has_any_data());
190
191 $data = $writer->get_data();
192 $this->assertCount(1, $data->lti_types);
193 }
194
195 /**
196 * Test for provider::export_user_data().
197 */
198 public function test_export_for_context_tool_proxies() {
199 $this->resetAfterTest();
200
201 // Create a user that will not make a tool proxy.
202 $user = $this->getDataGenerator()->create_user();
203 $this->setUser($user);
204
205 $toolproxy = new stdClass();
206 $toolproxy->createdby = $user;
207 lti_add_tool_proxy($toolproxy);
208
209 // Export all of the data for the context.
210 $systemcontext = context_system::instance();
211 $this->export_context_data_for_user($user->id, $systemcontext, 'mod_lti');
212 $writer = \core_privacy\local\request\writer::with_context($systemcontext);
213
214 $this->assertTrue($writer->has_any_data());
215
216 $data = $writer->get_data();
217 $this->assertCount(1, $data->lti_tool_proxies);
218 }
219
220 /**
221 * Test for provider::delete_data_for_all_users_in_context().
222 */
223 public function test_delete_data_for_all_users_in_context() {
224 global $DB;
225
226 $this->resetAfterTest();
227
228 $course = $this->getDataGenerator()->create_course();
229
230 $lti = $this->getDataGenerator()->create_module('lti', array('course' => $course->id));
231
232 // Create users that will make submissions.
233 $user1 = $this->getDataGenerator()->create_user();
234 $user2 = $this->getDataGenerator()->create_user();
235
236 $this->create_lti_submission($lti->id, $user1->id);
237 $this->create_lti_submission($lti->id, $user2->id);
238
239 // Before deletion, we should have 2 responses.
240 $count = $DB->count_records('lti_submission', ['ltiid' => $lti->id]);
241 $this->assertEquals(2, $count);
242
243 // Delete data based on context.
244 $cmcontext = context_module::instance($lti->cmid);
245 provider::delete_data_for_all_users_in_context($cmcontext);
246
247 // After deletion, the lti submissions for that lti activity should have been deleted.
248 $count = $DB->count_records('lti_submission', ['ltiid' => $lti->id]);
249 $this->assertEquals(0, $count);
250 }
251
252 /**
253 * Test for provider::delete_data_for_user().
254 */
255 public function test_delete_data_for_user() {
256 global $DB;
257
258 $this->resetAfterTest();
259
260 $course = $this->getDataGenerator()->create_course();
261
262 $lti = $this->getDataGenerator()->create_module('lti', array('course' => $course->id));
263
264 // Create users that will make submissions.
265 $user1 = $this->getDataGenerator()->create_user();
266 $user2 = $this->getDataGenerator()->create_user();
267
268 $this->create_lti_submission($lti->id, $user1->id);
269 $this->create_lti_submission($lti->id, $user2->id);
270
271 // Before deletion we should have 2 responses.
272 $count = $DB->count_records('lti_submission', ['ltiid' => $lti->id]);
273 $this->assertEquals(2, $count);
274
275 $context = \context_module::instance($lti->cmid);
276 $contextlist = new \core_privacy\local\request\approved_contextlist($user1, 'lti',
8e240412 277 [context_system::instance()->id, $context->id]);
3467e8ce
MN
278 provider::delete_data_for_user($contextlist);
279
280 // After deletion the lti submission for the first user should have been deleted.
281 $count = $DB->count_records('lti_submission', ['ltiid' => $lti->id, 'userid' => $user1->id]);
282 $this->assertEquals(0, $count);
283
284 // Check the submission for the other user is still there.
285 $ltisubmission = $DB->get_records('lti_submission');
286 $this->assertCount(1, $ltisubmission);
287 $lastsubmission = reset($ltisubmission);
288 $this->assertEquals($user2->id, $lastsubmission->userid);
289 }
290
291 /**
292 * Mimicks the creation of an LTI submission.
293 *
294 * There is no API we can use to insert an LTI submission, so we
295 * will simply insert directly into the database.
296 *
297 * @param int $ltiid
298 * @param int $userid
299 */
300 protected function create_lti_submission(int $ltiid, int $userid) {
301 global $DB;
302
303 $ltisubmissiondata = [
304 'ltiid' => $ltiid,
305 'userid' => $userid,
306 'datesubmitted' => time(),
307 'dateupdated' => time(),
308 'gradepercent' => 65,
309 'originalgrade' => 70,
310 'launchid' => 3,
311 'state' => 1
312 ];
313
314 $DB->insert_record('lti_submission', $ltisubmissiondata);
315 }
316}