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 * Privacy provider tests.
20 * @package core_backup
21 * @copyright 2018 Mark Nelson <markn@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 use core_backup\privacy\provider
;
26 use core_privacy\local\request\approved_userlist
;
28 defined('MOODLE_INTERNAL') ||
die();
31 * Privacy provider tests class.
33 * @copyright 2018 Mark Nelson <markn@moodle.com>
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 class core_backup_privacy_provider_testcase
extends \core_privacy\tests\provider_testcase
{
39 * Test getting the context for the user ID related to this plugin.
41 public function test_get_contexts_for_userid() {
44 $this->resetAfterTest();
46 $course = $this->getDataGenerator()->create_course();
47 $user = $this->getDataGenerator()->create_user();
49 // Just insert directly into the 'backup_controllers' table.
52 'operation' => 'restore',
54 'itemid' => $course->id
,
55 'format' => 'moodle2',
58 'userid' => $user->id
,
62 'checksum' => 'checksumyolo',
63 'timecreated' => time(),
64 'timemodified' => time(),
67 $DB->insert_record('backup_controllers', $bcdata);
69 $contextlist = provider
::get_contexts_for_userid($user->id
);
70 $this->assertCount(1, $contextlist);
71 $contextforuser = $contextlist->current();
72 $context = context_course
::instance($course->id
);
73 $this->assertEquals($context->id
, $contextforuser->id
);
77 * Test for provider::export_user_data().
79 public function test_export_for_context() {
82 $this->resetAfterTest();
84 $course = $this->getDataGenerator()->create_course();
85 $user1 = $this->getDataGenerator()->create_user();
87 // Just insert directly into the 'backup_controllers' table.
90 'operation' => 'restore',
92 'itemid' => $course->id
,
93 'format' => 'moodle2',
96 'userid' => $user1->id
,
100 'checksum' => 'checksumyolo',
101 'timecreated' => time(),
102 'timemodified' => time(),
105 $DB->insert_record('backup_controllers', $bcdata1);
107 // Create another user who will perform a backup operation.
108 $user2 = $this->getDataGenerator()->create_user();
109 $bcdata2 = (object) [
111 'operation' => 'restore',
113 'itemid' => $course->id
,
114 'format' => 'moodle2',
117 'userid' => $user2->id
,
120 'executiontime' => 0,
121 'checksum' => 'checksumyolo',
122 'timecreated' => time(),
123 'timemodified' => time(),
126 $DB->insert_record('backup_controllers', $bcdata2);
128 // Create another backup_controllers record.
129 $bcdata3 = (object) [
131 'operation' => 'backup',
133 'itemid' => $course->id
,
134 'format' => 'moodle2',
137 'userid' => $user1->id
,
140 'executiontime' => 0,
141 'checksum' => 'checksumyolo',
142 'timecreated' => time() + DAYSECS
,
143 'timemodified' => time() + DAYSECS
,
146 $DB->insert_record('backup_controllers', $bcdata3);
148 $coursecontext = context_course
::instance($course->id
);
150 // Export all of the data for the context.
151 $this->export_context_data_for_user($user1->id
, $coursecontext, 'core_backup');
152 $writer = \core_privacy\local\request\writer
::with_context($coursecontext);
153 $this->assertTrue($writer->has_any_data());
155 $data = (array) $writer->get_data([get_string('backup'), $course->id
]);
157 $this->assertCount(2, $data);
159 $bc1 = array_shift($data);
160 $this->assertEquals('restore', $bc1['operation']);
162 $bc2 = array_shift($data);
163 $this->assertEquals('backup', $bc2['operation']);
167 * Test for provider::delete_data_for_all_users_in_context().
169 public function test_delete_data_for_all_users_in_context() {
172 $this->resetAfterTest();
174 $course = $this->getDataGenerator()->create_course();
175 $user1 = $this->getDataGenerator()->create_user();
177 // Just insert directly into the 'backup_controllers' table.
178 $bcdata1 = (object) [
180 'operation' => 'restore',
182 'itemid' => $course->id
,
183 'format' => 'moodle2',
186 'userid' => $user1->id
,
189 'executiontime' => 0,
190 'checksum' => 'checksumyolo',
191 'timecreated' => time(),
192 'timemodified' => time(),
195 $DB->insert_record('backup_controllers', $bcdata1);
197 // Create another user who will perform a backup operation.
198 $user2 = $this->getDataGenerator()->create_user();
199 $bcdata2 = (object) [
201 'operation' => 'restore',
203 'itemid' => $course->id
,
204 'format' => 'moodle2',
207 'userid' => $user2->id
,
210 'executiontime' => 0,
211 'checksum' => 'checksumyolo',
212 'timecreated' => time(),
213 'timemodified' => time(),
216 $DB->insert_record('backup_controllers', $bcdata2);
218 // Before deletion, we should have 2 operations.
219 $count = $DB->count_records('backup_controllers', ['itemid' => $course->id
]);
220 $this->assertEquals(2, $count);
222 // Delete data based on context.
223 $coursecontext = context_course
::instance($course->id
);
224 provider
::delete_data_for_all_users_in_context($coursecontext);
226 // After deletion, the operations for that course should have been deleted.
227 $count = $DB->count_records('backup_controllers', ['itemid' => $course->id
]);
228 $this->assertEquals(0, $count);
232 * Test for provider::delete_data_for_user().
234 public function test_delete_data_for_user() {
237 $this->resetAfterTest();
239 $course = $this->getDataGenerator()->create_course();
240 $user1 = $this->getDataGenerator()->create_user();
242 // Just insert directly into the 'backup_controllers' table.
243 $bcdata1 = (object) [
245 'operation' => 'restore',
247 'itemid' => $course->id
,
248 'format' => 'moodle2',
251 'userid' => $user1->id
,
254 'executiontime' => 0,
255 'checksum' => 'checksumyolo',
256 'timecreated' => time(),
257 'timemodified' => time(),
260 $DB->insert_record('backup_controllers', $bcdata1);
262 // Create another user who will perform a backup operation.
263 $user2 = $this->getDataGenerator()->create_user();
264 $bcdata2 = (object) [
266 'operation' => 'restore',
268 'itemid' => $course->id
,
269 'format' => 'moodle2',
272 'userid' => $user2->id
,
275 'executiontime' => 0,
276 'checksum' => 'checksumyolo',
277 'timecreated' => time(),
278 'timemodified' => time(),
281 $DB->insert_record('backup_controllers', $bcdata2);
283 // Before deletion, we should have 2 operations.
284 $count = $DB->count_records('backup_controllers', ['itemid' => $course->id
]);
285 $this->assertEquals(2, $count);
287 $coursecontext = context_course
::instance($course->id
);
288 $contextlist = new \core_privacy\local\request\approved_contextlist
($user1, 'core_backup',
289 [context_system
::instance()->id
, $coursecontext->id
]);
290 provider
::delete_data_for_user($contextlist);
292 // After deletion, the backup operation for the user should have been deleted.
293 $count = $DB->count_records('backup_controllers', ['itemid' => $course->id
, 'userid' => $user1->id
]);
294 $this->assertEquals(0, $count);
296 // Confirm we still have the other users record.
297 $bcs = $DB->get_records('backup_controllers');
298 $this->assertCount(1, $bcs);
299 $lastsubmission = reset($bcs);
300 $this->assertNotEquals($user1->id
, $lastsubmission->userid
);
304 * Test that only users with a course and module context are fetched.
306 public function test_get_users_in_context() {
309 $this->resetAfterTest();
311 $component = 'core_backup';
313 $course = $this->getDataGenerator()->create_course();
314 $activity = $this->getDataGenerator()->create_module('chat', ['course' => $course->id
]);
316 $user = $this->getDataGenerator()->create_user();
317 $user2 = $this->getDataGenerator()->create_user();
319 $coursecontext = context_course
::instance($course->id
);
320 $activitycontext = \context_module
::instance($activity->cmid
);
322 // The list of users for course context should return the user.
323 $userlist = new \core_privacy\local\request\
userlist($coursecontext, $component);
324 provider
::get_users_in_context($userlist);
325 $this->assertCount(0, $userlist);
327 // Create a course backup.
328 // Just insert directly into the 'backup_controllers' table.
331 'operation' => 'restore',
333 'itemid' => $course->id
,
334 'format' => 'moodle2',
337 'userid' => $user->id
,
340 'executiontime' => 0,
341 'checksum' => 'checksumyolo',
342 'timecreated' => time(),
343 'timemodified' => time(),
347 $DB->insert_record('backup_controllers', $bcdata);
349 // The list of users for the course context should return user.
350 provider
::get_users_in_context($userlist);
351 $this->assertCount(1, $userlist);
352 $expected = [$user->id
];
353 $actual = $userlist->get_userids();
354 $this->assertEquals($expected, $actual);
356 // Create an activity backup.
357 // Just insert directly into the 'backup_controllers' table.
360 'operation' => 'restore',
361 'type' => 'activity',
362 'itemid' => $activity->cmid
,
363 'format' => 'moodle2',
366 'userid' => $user2->id
,
369 'executiontime' => 0,
370 'checksum' => 'checksumyolo',
371 'timecreated' => time(),
372 'timemodified' => time(),
376 $DB->insert_record('backup_controllers', $bcdata);
378 // The list of users for the course context should return user2.
379 $userlist = new \core_privacy\local\request\
userlist($activitycontext, $component);
380 provider
::get_users_in_context($userlist);
381 $this->assertCount(1, $userlist);
382 $expected = [$user2->id
];
383 $actual = $userlist->get_userids();
384 $this->assertEquals($expected, $actual);
386 // The list of users for system context should not return any users.
387 $systemcontext = context_system
::instance();
388 $userlist = new \core_privacy\local\request\
userlist($systemcontext, $component);
389 provider
::get_users_in_context($userlist);
390 $this->assertCount(0, $userlist);
394 * Test that data for users in approved userlist is deleted.
396 public function test_delete_data_for_users() {
399 $this->resetAfterTest();
401 $component = 'core_backup';
404 $course1 = $this->getDataGenerator()->create_course();
405 $coursecontext = context_course
::instance($course1->id
);
407 $course2 = $this->getDataGenerator()->create_course();
408 $coursecontext2 = context_course
::instance($course2->id
);
409 // Create an activity.
410 $activity = $this->getDataGenerator()->create_module('chat', ['course' => $course1->id
]);
411 $activitycontext = \context_module
::instance($activity->cmid
);
413 $user1 = $this->getDataGenerator()->create_user();
415 $user2 = $this->getDataGenerator()->create_user();
417 $user3 = $this->getDataGenerator()->create_user();
419 // Just insert directly into the 'backup_controllers' table.
420 $bcdata1 = (object) [
422 'operation' => 'restore',
424 'itemid' => $course1->id
,
425 'format' => 'moodle2',
428 'userid' => $user1->id
,
431 'executiontime' => 0,
432 'checksum' => 'checksumyolo',
433 'timecreated' => time(),
434 'timemodified' => time(),
437 $DB->insert_record('backup_controllers', $bcdata1);
439 // Just insert directly into the 'backup_controllers' table.
440 $bcdata2 = (object) [
442 'operation' => 'backup',
444 'itemid' => $course1->id
,
445 'format' => 'moodle2',
448 'userid' => $user2->id
,
451 'executiontime' => 0,
452 'checksum' => 'checksumyolo',
453 'timecreated' => time(),
454 'timemodified' => time(),
457 $DB->insert_record('backup_controllers', $bcdata2);
459 // Just insert directly into the 'backup_controllers' table.
460 $bcdata3 = (object) [
462 'operation' => 'restore',
463 'type' => 'activity',
464 'itemid' => $activity->cmid
,
465 'format' => 'moodle2',
468 'userid' => $user3->id
,
471 'executiontime' => 0,
472 'checksum' => 'checksumyolo',
473 'timecreated' => time(),
474 'timemodified' => time(),
477 $DB->insert_record('backup_controllers', $bcdata3);
479 // The list of users for coursecontext should return user1 and user2.
480 $userlist1 = new \core_privacy\local\request\
userlist($coursecontext, $component);
481 provider
::get_users_in_context($userlist1);
482 $this->assertCount(2, $userlist1);
483 $expected = [$user1->id
, $user2->id
];
484 $actual = $userlist1->get_userids();
485 $this->assertEquals($expected, $actual, '', 0.0, 10, true);
487 // The list of users for coursecontext2 should not return users.
488 $userlist2 = new \core_privacy\local\request\
userlist($coursecontext2, $component);
489 provider
::get_users_in_context($userlist2);
490 $this->assertCount(0, $userlist2);
492 // The list of users for activitycontext should return user3.
493 $userlist3 = new \core_privacy\local\request\
userlist($activitycontext, $component);
494 provider
::get_users_in_context($userlist3);
495 $this->assertCount(1, $userlist3);
496 $expected = [$user3->id
];
497 $actual = $userlist3->get_userids();
498 $this->assertEquals($expected, $actual);
500 // Add user1 to the approved user list.
501 $approvedlist = new approved_userlist($coursecontext, $component, [$user1->id
]);
502 // Delete user data using delete_data_for_user for usercontext1.
503 provider
::delete_data_for_users($approvedlist);
505 // Re-fetch users in coursecontext - The user list should now return only user2.
506 $userlist1 = new \core_privacy\local\request\
userlist($coursecontext, $component);
507 provider
::get_users_in_context($userlist1);
508 $this->assertCount(1, $userlist1);
509 $expected = [$user2->id
];
510 $actual = $userlist1->get_userids();
511 $this->assertEquals($expected, $actual);
513 // Re-fetch users in activitycontext - The user list should not be empty (user3).
514 $userlist3 = new \core_privacy\local\request\
userlist($activitycontext, $component);
515 provider
::get_users_in_context($userlist3);
516 $this->assertCount(1, $userlist3);
518 // Add user1 to the approved user list.
519 $approvedlist = new approved_userlist($activitycontext, $component, [$user3->id
]);
520 // Delete user data using delete_data_for_user for usercontext1.
521 provider
::delete_data_for_users($approvedlist);
523 // Re-fetch users in activitycontext - The user list should not return any users.
524 $userlist3 = new \core_privacy\local\request\
userlist($activitycontext, $component);
525 provider
::get_users_in_context($userlist3);
526 $this->assertCount(0, $userlist3);
528 // User data should be only removed in the course context and module context.
529 $systemcontext = context_system
::instance();
530 // Add userlist2 to the approved user list in the system context.
531 $approvedlist = new approved_userlist($systemcontext, $component, $userlist2->get_userids());
532 // Delete user1 data using delete_data_for_user.
533 provider
::delete_data_for_users($approvedlist);
534 // Re-fetch users in usercontext2 - The user list should not be empty (user2).
535 $userlist2 = new \core_privacy\local\request\
userlist($coursecontext, $component);
536 provider
::get_users_in_context($userlist2);
537 $this->assertCount(1, $userlist2);