Merge branch 'MDL-6074-master' of git://github.com/mihailges/moodle
[moodle.git] / repository / tests / privacy_test.php
blob75a9adce63cee1917c313580f77de2fab6f3070a
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.
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 * Unit tests for the core_repository implementation of the privacy API.
19 * @package core_repository
20 * @category test
21 * @copyright 2018 Zig Tan <zig@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 defined('MOODLE_INTERNAL') || die();
25 use \core_privacy\local\metadata\collection;
26 use \core_privacy\local\request\writer;
27 use \core_privacy\local\request\approved_contextlist;
28 use \core_repository\privacy\provider;
29 /**
30 * Unit tests for the core_repository implementation of the privacy API.
32 * @copyright 2018 Zig Tan <zig@moodle.com>
33 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35 class core_repository_privacy_testcase extends \core_privacy\tests\provider_testcase {
37 /**
38 * Overriding setUp() function to always reset after tests.
40 public function setUp() {
41 $this->resetAfterTest(true);
44 /**
45 * Test for provider::get_contexts_for_userid().
47 public function test_get_contexts_for_userid() {
48 // Test setup.
49 $user = $this->getDataGenerator()->create_user();
50 $this->setUser($user);
52 // Test the User's retrieved contextlist is empty because no repository_instances have added for the User yet.
53 $contextlist = provider::get_contexts_for_userid($user->id);
54 $contexts = $contextlist->get_contexts();
55 $this->assertCount(0, $contexts);
57 // Create 3 repository_instances records for the User.
58 $this->setup_test_scenario_data($user->id, 3);
60 // Test the User's retrieved contextlist contains only one context.
61 $contextlist = provider::get_contexts_for_userid($user->id);
62 $contexts = $contextlist->get_contexts();
63 $this->assertCount(1, $contexts);
65 // Test the User's contexts equal the User's own context.
66 $context = reset($contexts);
67 $this->assertEquals(CONTEXT_USER, $context->contextlevel);
68 $this->assertEquals($user->id, $context->instanceid);
71 /**
72 * Test for provider::export_user_data().
74 public function test_export_user_data() {
75 // Test setup.
76 $user = $this->getDataGenerator()->create_user();
77 $this->setUser($user);
79 // Create 3 repository_instances records for the User.
80 $this->setup_test_scenario_data($user->id, 3);
82 // Test the User's retrieved contextlist contains only one context.
83 $contextlist = provider::get_contexts_for_userid($user->id);
84 $contexts = $contextlist->get_contexts();
85 $this->assertCount(1, $contexts);
87 // Test the User's contexts equal the User's own context.
88 $context = reset($contexts);
89 $this->assertEquals(CONTEXT_USER, $context->contextlevel);
90 $this->assertEquals($user->id, $context->instanceid);
92 // Retrieve repository_instances data only for this user.
93 $approvedcontextlist = new approved_contextlist($user, 'core_repository', $contextlist->get_contextids());
94 provider::export_user_data($approvedcontextlist);
96 // Test the repository_instances data is exported at the User context level.
97 $user = $approvedcontextlist->get_user();
98 $contextuser = context_user::instance($user->id);
99 $writer = writer::with_context($contextuser);
100 $this->assertTrue($writer->has_any_data());
104 * Test for provider::delete_data_for_all_users_in_context().
106 public function test_delete_data_for_all_users_in_context() {
107 global $DB;
109 // Test setup.
110 $user = $this->getDataGenerator()->create_user();
111 $this->setUser($user);
113 // Create 3 repository_instances records for the User.
114 $this->setup_test_scenario_data($user->id, 3);
116 // Test the User's retrieved contextlist contains only one context.
117 $contextlist = provider::get_contexts_for_userid($user->id);
118 $contexts = $contextlist->get_contexts();
119 $this->assertCount(1, $contexts);
121 // Test the User's contexts equal the User's own context.
122 $context = reset($contexts);
123 $this->assertEquals(CONTEXT_USER, $context->contextlevel);
125 // Delete all the User's records in mdl_repository_instances table by the specified User context.
126 provider::delete_data_for_all_users_in_context($context);
128 // Test the cohort roles records in mdl_repository_instances table is equals zero.
129 $repositoryinstances = $DB->get_records('repository_instances', ['userid' => $user->id]);
130 $this->assertCount(0, $repositoryinstances);
134 * Test for provider::delete_data_for_user().
136 public function test_delete_data_for_user() {
137 global $DB;
139 // Test setup.
140 $user = $this->getDataGenerator()->create_user();
141 $this->setUser($user);
143 // Create 3 repository_instances records for the User.
144 $this->setup_test_scenario_data($user->id, 3);
146 // Test the User's retrieved contextlist contains only one context.
147 $contextlist = provider::get_contexts_for_userid($user->id);
148 $contexts = $contextlist->get_contexts();
149 $this->assertCount(1, $contexts);
151 // Test the User's contexts equal the User's own context.
152 $context = reset($contexts);
153 $this->assertEquals(CONTEXT_USER, $context->contextlevel);
155 // Delete all the User's records in mdl_repository_instances table by the specified User approved context list.
156 $approvedcontextlist = new approved_contextlist($user, 'repository_instances', $contextlist->get_contextids());
157 provider::delete_data_for_user($approvedcontextlist);
159 // Test the cohort roles records in mdl_repository_instances table is equals zero.
160 $repositoryinstances = $DB->get_records('repository_instances', ['userid' => $user->id]);
161 $this->assertCount(0, $repositoryinstances);
165 * Helper function to setup repository_instances records for testing a specific user.
167 * @param int $userid The Id of the User used for testing.
168 * @param int $noscenarios The number of repository_instance records to create for the User.
169 * @throws dml_exception
171 private function setup_test_scenario_data($userid, $noscenarios) {
172 global $DB;
174 for ($i = 0; $i < $noscenarios; $i++) {
175 $repositoryinstance = (object)[
176 'typeid' => ($i + 1),
177 'name' => 'My Test Repo',
178 'userid' => $userid,
179 'contextid' => 1,
180 'username' => 'some username',
181 'password' => 'some password',
182 'timecreated' => date('u'),
183 'timemodified' => date('u'),
184 'readonly' => 0
186 $DB->insert_record('repository_instances', $repositoryinstance);