Proyectos de Subversion Moodle

Rev

Rev 1 | | 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
/**
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
namespace mod_lti\privacy;
25
 
26
use core_privacy\local\metadata\collection;
27
use core_privacy\local\request\approved_contextlist;
28
use core_privacy\local\request\approved_userlist;
29
use mod_lti\privacy\provider;
30
 
31
defined('MOODLE_INTERNAL') || die();
32
 
33
global $CFG;
34
 
35
require_once($CFG->dirroot . '/mod/lti/locallib.php');
36
 
37
/**
38
 * Privacy provider tests class.
39
 *
40
 * @package    mod_lti
41
 * @copyright  2018 Mark Nelson <markn@moodle.com>
42
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
43
 */
44
class provider_test extends \core_privacy\tests\provider_testcase {
45
 
46
    /**
47
     * Test for provider::get_metadata().
48
     */
11 efrain 49
    public function test_get_metadata(): void {
1 efrain 50
        $collection = new collection('mod_lti');
51
        $newcollection = provider::get_metadata($collection);
52
        $itemcollection = $newcollection->get_collection();
53
        $this->assertCount(4, $itemcollection);
54
 
55
        $ltiproviderexternal = array_shift($itemcollection);
56
        $this->assertEquals('lti_provider', $ltiproviderexternal->get_name());
57
 
58
        $ltisubmissiontable = array_shift($itemcollection);
59
        $this->assertEquals('lti_submission', $ltisubmissiontable->get_name());
60
 
61
        $ltitoolproxies = array_shift($itemcollection);
62
        $this->assertEquals('lti_tool_proxies', $ltitoolproxies->get_name());
63
 
64
        $ltitypestable = array_shift($itemcollection);
65
        $this->assertEquals('lti_types', $ltitypestable->get_name());
66
 
67
        $privacyfields = $ltisubmissiontable->get_privacy_fields();
68
        $this->assertArrayHasKey('userid', $privacyfields);
69
        $this->assertArrayHasKey('datesubmitted', $privacyfields);
70
        $this->assertArrayHasKey('dateupdated', $privacyfields);
71
        $this->assertArrayHasKey('gradepercent', $privacyfields);
72
        $this->assertArrayHasKey('originalgrade', $privacyfields);
73
        $this->assertEquals('privacy:metadata:lti_submission', $ltisubmissiontable->get_summary());
74
 
75
        $privacyfields = $ltitoolproxies->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_tool_proxies', $ltitoolproxies->get_summary());
81
 
82
        $privacyfields = $ltitypestable->get_privacy_fields();
83
        $this->assertArrayHasKey('name', $privacyfields);
84
        $this->assertArrayHasKey('createdby', $privacyfields);
85
        $this->assertArrayHasKey('timecreated', $privacyfields);
86
        $this->assertArrayHasKey('timemodified', $privacyfields);
87
        $this->assertEquals('privacy:metadata:lti_types', $ltitypestable->get_summary());
88
    }
89
 
90
    /**
91
     * Test for provider::get_contexts_for_userid().
92
     */
11 efrain 93
    public function test_get_contexts_for_userid(): void {
1 efrain 94
        $this->resetAfterTest();
95
 
96
        $course = $this->getDataGenerator()->create_course();
97
 
98
        // The LTI activity the user will have submitted something for.
99
        $lti = $this->getDataGenerator()->create_module('lti', array('course' => $course->id));
100
 
101
        // Another LTI activity that has no user activity.
102
        $this->getDataGenerator()->create_module('lti', array('course' => $course->id));
103
 
104
        // Create a user which will make a submission.
105
        $user = $this->getDataGenerator()->create_user();
106
 
107
        $this->create_lti_submission($lti->id, $user->id);
108
 
109
        // Check the contexts supplied are correct.
110
        $contextlist = provider::get_contexts_for_userid($user->id);
111
        $this->assertCount(2, $contextlist);
112
 
113
        $contextformodule = $contextlist->current();
114
        $cmcontext = \context_module::instance($lti->cmid);
115
        $this->assertEquals($cmcontext->id, $contextformodule->id);
116
 
117
        $contextlist->next();
118
        $contextforsystem = $contextlist->current();
119
        $this->assertEquals(SYSCONTEXTID, $contextforsystem->id);
120
    }
121
 
122
    /**
123
     * Test for provider::test_get_users_in_context()
124
     */
11 efrain 125
    public function test_get_users_in_context(): void {
1 efrain 126
        $this->resetAfterTest();
127
 
128
        $course = $this->getDataGenerator()->create_course();
129
        $component = 'mod_lti';
130
 
131
        // The LTI activity the user will have submitted something for.
132
        $lti1 = $this->getDataGenerator()->create_module('lti', array('course' => $course->id));
133
 
134
        // Another LTI activity that has no user activity.
135
        $lti2 = $this->getDataGenerator()->create_module('lti', array('course' => $course->id));
136
 
137
        // Create user which will make a submission each.
138
        $user1 = $this->getDataGenerator()->create_user();
139
        $user2 = $this->getDataGenerator()->create_user();
140
 
141
        $this->create_lti_submission($lti1->id, $user1->id);
142
        $this->create_lti_submission($lti1->id, $user2->id);
143
 
144
        $context = \context_module::instance($lti1->cmid);
145
        $userlist = new \core_privacy\local\request\userlist($context, $component);
146
        provider::get_users_in_context($userlist);
147
 
148
        $this->assertCount(2, $userlist);
149
        $expected = [$user1->id, $user2->id];
150
        $actual = $userlist->get_userids();
151
        sort($expected);
152
        sort($actual);
153
 
154
        $this->assertEquals($expected, $actual);
155
 
156
        $context = \context_module::instance($lti2->cmid);
157
        $userlist = new \core_privacy\local\request\userlist($context, $component);
158
        provider::get_users_in_context($userlist);
159
 
160
        $this->assertEmpty($userlist);
161
    }
162
 
163
    /**
164
     * Test for provider::export_user_data().
165
     */
11 efrain 166
    public function test_export_for_context_submissions(): void {
1 efrain 167
        $this->resetAfterTest();
168
 
169
        $course = $this->getDataGenerator()->create_course();
170
 
171
        $lti = $this->getDataGenerator()->create_module('lti', array('course' => $course->id));
172
 
173
        // Create users which will make submissions.
174
        $user1 = $this->getDataGenerator()->create_user();
175
        $user2 = $this->getDataGenerator()->create_user();
176
 
177
        $this->create_lti_submission($lti->id, $user1->id);
178
        $this->create_lti_submission($lti->id, $user1->id);
179
        $this->create_lti_submission($lti->id, $user2->id);
180
 
181
        // Export all of the data for the context for user 1.
182
        $cmcontext = \context_module::instance($lti->cmid);
183
        $this->export_context_data_for_user($user1->id, $cmcontext, 'mod_lti');
184
        $writer = \core_privacy\local\request\writer::with_context($cmcontext);
185
 
186
        $this->assertTrue($writer->has_any_data());
187
 
188
        $data = $writer->get_data();
189
        $this->assertCount(2, $data->submissions);
190
    }
191
 
192
    /**
193
     * Test for provider::export_user_data().
194
     */
11 efrain 195
    public function test_export_for_context_tool_types(): void {
1 efrain 196
        $this->resetAfterTest();
197
 
198
        $course1 = $this->getDataGenerator()->create_course();
199
        $course2 = $this->getDataGenerator()->create_course();
200
 
201
        // Create a user which will make a tool type.
202
        $user = $this->getDataGenerator()->create_user();
203
        $this->setUser($user);
204
 
205
        // Create a user that will not make a tool type.
206
        $this->getDataGenerator()->create_user();
207
 
208
        $type = new \stdClass();
209
        $type->baseurl = 'http://moodle.org';
210
        $type->course = $course1->id;
211
        lti_add_type($type, new \stdClass());
212
 
213
        $type = new \stdClass();
214
        $type->baseurl = 'http://moodle.org';
215
        $type->course = $course1->id;
216
        lti_add_type($type, new \stdClass());
217
 
218
        $type = new \stdClass();
219
        $type->baseurl = 'http://moodle.org';
220
        $type->course = $course2->id;
221
        lti_add_type($type, new \stdClass());
222
 
223
        // Export all of the data for the context.
224
        $coursecontext = \context_course::instance($course1->id);
225
        $this->export_context_data_for_user($user->id, $coursecontext, 'mod_lti');
226
        $writer = \core_privacy\local\request\writer::with_context($coursecontext);
227
 
228
        $this->assertTrue($writer->has_any_data());
229
 
230
        $data = $writer->get_data();
231
        $this->assertCount(2, $data->lti_types);
232
 
233
        $coursecontext = \context_course::instance($course2->id);
234
        $this->export_context_data_for_user($user->id, $coursecontext, 'mod_lti');
235
        $writer = \core_privacy\local\request\writer::with_context($coursecontext);
236
 
237
        $this->assertTrue($writer->has_any_data());
238
 
239
        $data = $writer->get_data();
240
        $this->assertCount(1, $data->lti_types);
241
    }
242
 
243
    /**
244
     * Test for provider::export_user_data().
245
     */
11 efrain 246
    public function test_export_for_context_tool_proxies(): void {
1 efrain 247
        $this->resetAfterTest();
248
 
249
        // Create a user that will not make a tool proxy.
250
        $user = $this->getDataGenerator()->create_user();
251
        $this->setUser($user);
252
 
253
        $toolproxy = new \stdClass();
254
        $toolproxy->createdby = $user;
255
        lti_add_tool_proxy($toolproxy);
256
 
257
        // Export all of the data for the context.
258
        $systemcontext = \context_system::instance();
259
        $this->export_context_data_for_user($user->id, $systemcontext, 'mod_lti');
260
        $writer = \core_privacy\local\request\writer::with_context($systemcontext);
261
 
262
        $this->assertTrue($writer->has_any_data());
263
 
264
        $data = $writer->get_data();
265
        $this->assertCount(1, $data->lti_tool_proxies);
266
    }
267
 
268
    /**
269
     * Test for provider::delete_data_for_all_users_in_context().
270
     */
11 efrain 271
    public function test_delete_data_for_all_users_in_context(): void {
1 efrain 272
        global $DB;
273
 
274
        $this->resetAfterTest();
275
 
276
        $course = $this->getDataGenerator()->create_course();
277
 
278
        $lti = $this->getDataGenerator()->create_module('lti', array('course' => $course->id));
279
 
280
        // Create users that will make submissions.
281
        $user1 = $this->getDataGenerator()->create_user();
282
        $user2 = $this->getDataGenerator()->create_user();
283
 
284
        $this->create_lti_submission($lti->id, $user1->id);
285
        $this->create_lti_submission($lti->id, $user2->id);
286
 
287
        // Before deletion, we should have 2 responses.
288
        $count = $DB->count_records('lti_submission', ['ltiid' => $lti->id]);
289
        $this->assertEquals(2, $count);
290
 
291
        // Delete data based on context.
292
        $cmcontext = \context_module::instance($lti->cmid);
293
        provider::delete_data_for_all_users_in_context($cmcontext);
294
 
295
        // After deletion, the lti submissions for that lti activity should have been deleted.
296
        $count = $DB->count_records('lti_submission', ['ltiid' => $lti->id]);
297
        $this->assertEquals(0, $count);
298
    }
299
 
300
    /**
301
     * Test for provider::delete_data_for_user().
302
     */
11 efrain 303
    public function test_delete_data_for_user(): void {
1 efrain 304
        global $DB;
305
 
306
        $this->resetAfterTest();
307
 
308
        $course = $this->getDataGenerator()->create_course();
309
 
310
        $lti = $this->getDataGenerator()->create_module('lti', array('course' => $course->id));
311
 
312
        // Create users that will make submissions.
313
        $user1 = $this->getDataGenerator()->create_user();
314
        $user2 = $this->getDataGenerator()->create_user();
315
 
316
        $this->create_lti_submission($lti->id, $user1->id);
317
        $this->create_lti_submission($lti->id, $user2->id);
318
 
319
        // Before deletion we should have 2 responses.
320
        $count = $DB->count_records('lti_submission', ['ltiid' => $lti->id]);
321
        $this->assertEquals(2, $count);
322
 
323
        $context = \context_module::instance($lti->cmid);
324
        $contextlist = new approved_contextlist($user1, 'lti',
325
            [\context_system::instance()->id, $context->id]);
326
        provider::delete_data_for_user($contextlist);
327
 
328
        // After deletion the lti submission for the first user should have been deleted.
329
        $count = $DB->count_records('lti_submission', ['ltiid' => $lti->id, 'userid' => $user1->id]);
330
        $this->assertEquals(0, $count);
331
 
332
        // Check the submission for the other user is still there.
333
        $ltisubmission = $DB->get_records('lti_submission');
334
        $this->assertCount(1, $ltisubmission);
335
        $lastsubmission = reset($ltisubmission);
336
        $this->assertEquals($user2->id, $lastsubmission->userid);
337
    }
338
 
339
    /**
340
     * Test for provider::delete_data_for_users().
341
     */
11 efrain 342
    public function test_delete_data_for_users(): void {
1 efrain 343
        global $DB;
344
        $component = 'mod_lti';
345
 
346
        $this->resetAfterTest();
347
 
348
        $course = $this->getDataGenerator()->create_course();
349
 
350
        $lti = $this->getDataGenerator()->create_module('lti', array('course' => $course->id));
351
 
352
        // Create users that will make submissions.
353
        $user1 = $this->getDataGenerator()->create_user();
354
        $user2 = $this->getDataGenerator()->create_user();
355
        $user3 = $this->getDataGenerator()->create_user();
356
 
357
        $this->create_lti_submission($lti->id, $user1->id);
358
        $this->create_lti_submission($lti->id, $user2->id);
359
        $this->create_lti_submission($lti->id, $user3->id);
360
 
361
        // Before deletion we should have 2 responses.
362
        $count = $DB->count_records('lti_submission', ['ltiid' => $lti->id]);
363
        $this->assertEquals(3, $count);
364
 
365
        $context = \context_module::instance($lti->cmid);
366
        $approveduserids = [$user1->id, $user2->id];
367
        $approvedlist = new approved_userlist($context, $component, $approveduserids);
368
        provider::delete_data_for_users($approvedlist);
369
 
370
        // After deletion the lti submission for the first two users should have been deleted.
371
        list($insql, $inparams) = $DB->get_in_or_equal($approveduserids, SQL_PARAMS_NAMED);
372
        $sql = "ltiid = :ltiid AND userid {$insql}";
373
        $params = array_merge($inparams, ['ltiid' => $lti->id]);
374
        $count = $DB->count_records_select('lti_submission', $sql, $params);
375
        $this->assertEquals(0, $count);
376
 
377
        // Check the submission for the third user is still there.
378
        $ltisubmission = $DB->get_records('lti_submission');
379
        $this->assertCount(1, $ltisubmission);
380
        $lastsubmission = reset($ltisubmission);
381
        $this->assertEquals($user3->id, $lastsubmission->userid);
382
    }
383
 
384
    /**
385
     * Mimicks the creation of an LTI submission.
386
     *
387
     * There is no API we can use to insert an LTI submission, so we
388
     * will simply insert directly into the database.
389
     *
390
     * @param int $ltiid
391
     * @param int $userid
392
     */
393
    protected function create_lti_submission(int $ltiid, int $userid) {
394
        global $DB;
395
 
396
        $ltisubmissiondata = [
397
            'ltiid' => $ltiid,
398
            'userid' => $userid,
399
            'datesubmitted' => time(),
400
            'dateupdated' => time(),
401
            'gradepercent' => 65,
402
            'originalgrade' => 70,
403
            'launchid' => 3,
404
            'state' => 1
405
        ];
406
 
407
        $DB->insert_record('lti_submission', $ltisubmissiondata);
408
    }
409
}