Merge branch 'MDL-72791-311' of https://github.com/paulholden/moodle into MOODLE_311_...
[moodle.git] / files / tests / privacy_test.php
blob42e71467960922955b12647894651266371de596
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/>.
17 /**
18 * Base class for unit tests for core_cohort.
20 * @package core_files
21 * @category test
22 * @copyright 2018 Mihail Geshoski <mihail@moodle.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') || die();
28 use core_files\privacy\provider;
29 use core_privacy\local\request\approved_contextlist;
30 use core_privacy\local\request\writer;
31 use core_privacy\tests\provider_testcase;
32 use core_privacy\local\request\approved_userlist;
34 /**
35 * Unit tests for files\classes\privacy\provider.php
37 * @copyright 2018 Mihail Geshoski <mihail@moodle.com>
38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40 class core_files_testcase extends provider_testcase {
42 /**
43 * Test getting the context for the user ID related to this plugin.
45 public function test_get_contexts_for_userid() {
46 $this->resetAfterTest();
48 // Create a user.
49 $user = $this->getDataGenerator()->create_user();
50 $userctx = \context_user::instance($user->id);
52 create_user_key('core_files', $user->id);
54 $contextlist = provider::get_contexts_for_userid($user->id);
55 $this->assertCount(1, (array) $contextlist->get_contextids());
56 $this->assertContainsEquals($userctx->id, $contextlist->get_contextids());
59 /**
60 * Test that data is exported correctly for this plugin.
62 public function test_export_user_data() {
63 global $DB;
65 $this->resetAfterTest();
67 // Create a user.
68 $user = $this->getDataGenerator()->create_user();
69 $usercontext = \context_user::instance($user->id);
71 $keyvalue = get_user_key('core_files', $user->id);
72 $key = $DB->get_record('user_private_key', ['value' => $keyvalue]);
74 // Validate exported data.
75 $this->setUser($user);
76 $writer = writer::with_context($usercontext);
77 $this->assertFalse($writer->has_any_data());
78 $this->export_context_data_for_user($user->id, $usercontext, 'core_files');
79 $subcontext = [
80 get_string('files')
82 $userkeydata = $writer->get_related_data($subcontext, 'userkeys');
83 $this->assertCount(1, $userkeydata->keys);
84 $this->assertEquals($key->script, reset($userkeydata->keys)->script);
87 /**
88 * Test for provider::delete_data_for_all_users_in_context().
90 public function test_delete_data_for_all_users_in_context() {
91 global $DB;
93 $this->resetAfterTest();
95 // Create a user.
96 $user = $this->getDataGenerator()->create_user();
97 $usercontext = \context_user::instance($user->id);
99 create_user_key('core_files', $user->id);
101 // Before deletion, we should have 1 user_private_key.
102 $count = $DB->count_records('user_private_key', ['script' => 'core_files']);
103 $this->assertEquals(1, $count);
105 // Delete data.
106 provider::delete_data_for_all_users_in_context($usercontext);
108 // After deletion, the user_private_key entries should have been deleted.
109 $count = $DB->count_records('user_private_key', ['script' => 'core_files']);
110 $this->assertEquals(0, $count);
114 * Test for provider::delete_data_for_user().
116 public function test_delete_data_for_user() {
117 global $DB;
119 $this->resetAfterTest();
121 // Create a user.
122 $user = $this->getDataGenerator()->create_user();
124 create_user_key('core_files', $user->id);
126 // Before deletion, we should have 1 user_private_key.
127 $count = $DB->count_records('user_private_key', ['script' => 'core_files']);
128 $this->assertEquals(1, $count);
130 // Delete data.
131 $contextlist = provider::get_contexts_for_userid($user->id);
132 $approvedcontextlist = new approved_contextlist($user, 'core_files', $contextlist->get_contextids());
134 provider::delete_data_for_user($approvedcontextlist);
135 // After deletion, the user_private_key entries should have been deleted.
136 $count = $DB->count_records('user_private_key', ['script' => 'core_files']);
137 $this->assertEquals(0, $count);
141 * Test that only users within a course context are fetched.
143 public function test_get_users_in_context() {
144 $this->resetAfterTest();
146 $component = 'core_files';
148 // Create a user.
149 $user = $this->getDataGenerator()->create_user();
150 $userctx = \context_user::instance($user->id);
152 $userlist = new \core_privacy\local\request\userlist($userctx, $component);
153 provider::get_users_in_context($userlist);
154 $this->assertCount(0, $userlist);
156 create_user_key('core_files', $user->id);
158 // The list of users within the userctx context should contain user.
159 provider::get_users_in_context($userlist);
160 $this->assertCount(1, $userlist);
161 $expected = [$user->id];
162 $actual = $userlist->get_userids();
163 $this->assertEquals($expected, $actual);
165 // The list of users within contexts different than user should be empty.
166 $systemctx = \context_system::instance();
167 $userlist = new \core_privacy\local\request\userlist($systemctx, $component);
168 provider::get_users_in_context($userlist);
169 $this->assertCount(0, $userlist);
173 * Test that data for users in approved userlist is deleted.
175 public function test_delete_data_for_users() {
176 $this->resetAfterTest();
178 $component = 'core_files';
180 // Create user1.
181 $user1 = $this->getDataGenerator()->create_user();
182 $userctx1 = \context_user::instance($user1->id);
183 // Create user2.
184 $user2 = $this->getDataGenerator()->create_user();
185 $userctx2 = \context_user::instance($user2->id);
187 create_user_key('core_files', $user1->id);
188 create_user_key('core_files', $user2->id);
190 $userlist1 = new \core_privacy\local\request\userlist($userctx1, $component);
191 provider::get_users_in_context($userlist1);
192 $this->assertCount(1, $userlist1);
194 $userlist2 = new \core_privacy\local\request\userlist($userctx2, $component);
195 provider::get_users_in_context($userlist2);
196 $this->assertCount(1, $userlist2);
198 // Convert $userlist1 into an approved_contextlist.
199 $approvedlist1 = new approved_userlist($userctx1, $component, $userlist1->get_userids());
200 // Delete using delete_data_for_user.
201 provider::delete_data_for_users($approvedlist1);
203 // Re-fetch users in userctx1.
204 $userlist1 = new \core_privacy\local\request\userlist($userctx1, $component);
205 provider::get_users_in_context($userlist1);
206 // The user data in coursecategoryctx should be deleted.
207 $this->assertCount(0, $userlist1);
209 // Re-fetch users in userctx2.
210 $userlist2 = new \core_privacy\local\request\userlist($userctx2, $component);
211 provider::get_users_in_context($userlist2);
212 // The user data in userctx2 should be still present.
213 $this->assertCount(1, $userlist2);
215 // Convert $userlist2 into an approved_contextlist in the system context.
216 $systemctx = \context_system::instance();
217 $approvedlist3 = new approved_userlist($systemctx, $component, $userlist2->get_userids());
218 // Delete using delete_data_for_user.
219 provider::delete_data_for_users($approvedlist3);
220 // Re-fetch users in userctx2.
221 $userlist3 = new \core_privacy\local\request\userlist($userctx2, $component);
222 provider::get_users_in_context($userlist3);
223 // The user data in userctx2 should not be deleted.
224 $this->assertCount(1, $userlist3);