weekly release 3.7dev
[moodle.git] / files / tests / privacy_test.php
CommitLineData
16ec426a
MG
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 * Base class for unit tests for core_cohort.
19 *
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
24 */
25
26defined('MOODLE_INTERNAL') || die();
27
28use core_files\privacy\provider;
29use core_privacy\local\request\approved_contextlist;
30use core_privacy\local\request\writer;
31use core_privacy\tests\provider_testcase;
32use core_privacy\local\request\approved_userlist;
33
34/**
35 * Unit tests for files\classes\privacy\provider.php
36 *
37 * @copyright 2018 Mihail Geshoski <mihail@moodle.com>
38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39 */
40class core_files_testcase extends provider_testcase {
41
42 /**
43 * Test getting the context for the user ID related to this plugin.
44 */
45 public function test_get_contexts_for_userid() {
46 $this->resetAfterTest();
47
48 // Create a user.
49 $user = $this->getDataGenerator()->create_user();
50 $userctx = \context_user::instance($user->id);
51
52 create_user_key('core_files', $user->id);
53
54 $contextlist = provider::get_contexts_for_userid($user->id);
55 $this->assertCount(1, (array) $contextlist->get_contextids());
56 $this->assertContains($userctx->id, $contextlist->get_contextids());
57 }
58
59 /**
60 * Test that data is exported correctly for this plugin.
61 */
62 public function test_export_user_data() {
63 global $DB;
64
65 $this->resetAfterTest();
66
67 // Create a user.
68 $user = $this->getDataGenerator()->create_user();
69 $usercontext = \context_user::instance($user->id);
70
71 $keyvalue = get_user_key('core_files', $user->id);
72 $key = $DB->get_record('user_private_key', ['value' => $keyvalue]);
73
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')
81 ];
82 $userkeydata = $writer->get_related_data($subcontext, 'userkeys');
83 $this->assertCount(1, $userkeydata->keys);
84 $this->assertEquals($key->script, reset($userkeydata->keys)->script);
85 }
86
87 /**
88 * Test for provider::delete_data_for_all_users_in_context().
89 */
90 public function test_delete_data_for_all_users_in_context() {
91 global $DB;
92
93 $this->resetAfterTest();
94
95 // Create a user.
96 $user = $this->getDataGenerator()->create_user();
97 $usercontext = \context_user::instance($user->id);
98
99 create_user_key('core_files', $user->id);
100
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);
104
105 // Delete data.
106 provider::delete_data_for_all_users_in_context($usercontext);
107
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);
111 }
112
113 /**
114 * Test for provider::delete_data_for_user().
115 */
116 public function test_delete_data_for_user() {
117 global $DB;
118
119 $this->resetAfterTest();
120
121 // Create a user.
122 $user = $this->getDataGenerator()->create_user();
123
124 create_user_key('core_files', $user->id);
125
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);
129
130 // Delete data.
131 $contextlist = provider::get_contexts_for_userid($user->id);
132 $approvedcontextlist = new approved_contextlist($user, 'core_files', $contextlist->get_contextids());
133
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);
138 }
139
140 /**
141 * Test that only users within a course context are fetched.
142 */
143 public function test_get_users_in_context() {
144 $this->resetAfterTest();
145
146 $component = 'core_files';
147
148 // Create a user.
149 $user = $this->getDataGenerator()->create_user();
150 $userctx = \context_user::instance($user->id);
151
152 $userlist = new \core_privacy\local\request\userlist($userctx, $component);
153 provider::get_users_in_context($userlist);
154 $this->assertCount(0, $userlist);
155
156 create_user_key('core_files', $user->id);
157
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);
164
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);
170 }
171
172 /**
173 * Test that data for users in approved userlist is deleted.
174 */
175 public function test_delete_data_for_users() {
176 $this->resetAfterTest();
177
178 $component = 'core_files';
179
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);
186
187 create_user_key('core_files', $user1->id);
188 create_user_key('core_files', $user2->id);
189
190 $userlist1 = new \core_privacy\local\request\userlist($userctx1, $component);
191 provider::get_users_in_context($userlist1);
192 $this->assertCount(1, $userlist1);
193
194 $userlist2 = new \core_privacy\local\request\userlist($userctx2, $component);
195 provider::get_users_in_context($userlist2);
196 $this->assertCount(1, $userlist2);
197
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);
202
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);
208
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);
214
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);
225 }
226}