Merge branch 'MDL-68954-master' of git://github.com/mihailges/moodle
[moodle.git] / portfolio / tests / privacy_provider_test.php
CommitLineData
e6f06ce9
JD
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 core_portfolio
21 * @copyright 2018 Jake Dallimore <jrhdallimore@gmail.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25defined('MOODLE_INTERNAL') || die();
26
44a0a2bd
MG
27use core_portfolio\privacy\provider;
28use core_privacy\local\request\approved_userlist;
29
e6f06ce9
JD
30/**
31 * Privacy provider tests class.
32 *
33 * @copyright 2018 Jake Dallimore <jrhdallimore@gmail.com>
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35 */
36class portfolio_privacy_provider_test extends \core_privacy\tests\provider_testcase {
37
ab78499b
AG
38 protected function create_portfolio_data($plugin, $name, $user, $preference, $value) {
39 global $DB;
40 $portfolioinstance = (object) [
41 'plugin' => $plugin,
42 'name' => $name,
43 'visible' => 1
44 ];
45 $portfolioinstance->id = $DB->insert_record('portfolio_instance', $portfolioinstance);
46 $userinstance = (object) [
47 'instance' => $portfolioinstance->id,
48 'userid' => $user->id,
49 'name' => $preference,
50 'value' => $value
51 ];
52 $DB->insert_record('portfolio_instance_user', $userinstance);
14f72964
MG
53
54 $DB->insert_record('portfolio_log', [
55 'portfolio' => $portfolioinstance->id,
56 'userid' => $user->id,
57 'caller_class' => 'forum_portfolio_caller',
58 'caller_component' => 'mod_forum',
59 'time' => time(),
60 ]);
61
62 $DB->insert_record('portfolio_log', [
63 'portfolio' => $portfolioinstance->id,
64 'userid' => $user->id,
65 'caller_class' => 'workshop_portfolio_caller',
66 'caller_component' => 'mod_workshop',
67 'time' => time(),
68 ]);
ab78499b
AG
69 }
70
e6f06ce9 71 /**
ab78499b 72 * Verify that a collection of metadata is returned for this component and that it just returns the righ types for 'portfolio'.
e6f06ce9
JD
73 */
74 public function test_get_metadata() {
75 $collection = new \core_privacy\local\metadata\collection('core_portfolio');
44a0a2bd 76 $collection = provider::get_metadata($collection);
e6f06ce9
JD
77 $this->assertNotEmpty($collection);
78 $items = $collection->get_collection();
14f72964 79 $this->assertEquals(4, count($items));
ab78499b 80 $this->assertInstanceOf(\core_privacy\local\metadata\types\database_table::class, $items[0]);
14f72964
MG
81 $this->assertInstanceOf(\core_privacy\local\metadata\types\database_table::class, $items[1]);
82 $this->assertInstanceOf(\core_privacy\local\metadata\types\database_table::class, $items[2]);
83 $this->assertInstanceOf(\core_privacy\local\metadata\types\plugintype_link::class, $items[3]);
ab78499b
AG
84 }
85
86 /**
87 * Test that the export for a user id returns a user context.
88 */
89 public function test_get_contexts_for_userid() {
90 $this->resetAfterTest();
91 $user = $this->getDataGenerator()->create_user();
92 $context = context_user::instance($user->id);
93 $this->create_portfolio_data('googledocs', 'Google Docs', $user, 'visible', 1);
44a0a2bd 94 $contextlist = provider::get_contexts_for_userid($user->id);
ab78499b
AG
95 $this->assertEquals($context->id, $contextlist->current()->id);
96 }
97
98 /**
99 * Test that exporting user data works as expected.
100 */
101 public function test_export_user_data() {
102 $this->resetAfterTest();
103 $user = $this->getDataGenerator()->create_user();
104 $context = context_user::instance($user->id);
105 $this->create_portfolio_data('googledocs', 'Google Docs', $user, 'visible', 1);
106 $contextlist = new \core_privacy\local\request\approved_contextlist($user, 'core_portfolio', [$context->id]);
44a0a2bd 107 provider::export_user_data($contextlist);
ab78499b
AG
108 $writer = \core_privacy\local\request\writer::with_context($context);
109 $portfoliodata = $writer->get_data([get_string('privacy:path', 'portfolio')]);
110 $this->assertEquals('Google Docs', $portfoliodata->{'Google Docs'}->name);
111 }
112
113 /**
114 * Test that deleting only results in the one context being removed.
115 */
116 public function test_delete_data_for_all_users_in_context() {
117 global $DB;
118
119 $this->resetAfterTest();
120 $user1 = $this->getDataGenerator()->create_user();
121 $user2 = $this->getDataGenerator()->create_user();
122 $this->create_portfolio_data('googledocs', 'Google Docs', $user1, 'visible', 1);
123 $this->create_portfolio_data('onedrive', 'Microsoft onedrive', $user2, 'visible', 1);
124 // Check a system context sent through.
125 $systemcontext = context_system::instance();
44a0a2bd 126 provider::delete_data_for_all_users_in_context($systemcontext);
ab78499b
AG
127 $records = $DB->get_records('portfolio_instance_user');
128 $this->assertCount(2, $records);
14f72964 129 $this->assertCount(4, $DB->get_records('portfolio_log'));
ab78499b 130 $context = context_user::instance($user1->id);
44a0a2bd 131 provider::delete_data_for_all_users_in_context($context);
ab78499b
AG
132 $records = $DB->get_records('portfolio_instance_user');
133 // Only one entry should remain for user 2.
134 $this->assertCount(1, $records);
135 $data = array_shift($records);
136 $this->assertEquals($user2->id, $data->userid);
14f72964 137 $this->assertCount(2, $DB->get_records('portfolio_log'));
ab78499b
AG
138 }
139
140 /**
141 * Test that deleting only results in one user's data being removed.
142 */
143 public function test_delete_data_for_user() {
144 global $DB;
145
146 $this->resetAfterTest();
147 $user1 = $this->getDataGenerator()->create_user();
148 $user2 = $this->getDataGenerator()->create_user();
149 $this->create_portfolio_data('googledocs', 'Google Docs', $user1, 'visible', 1);
150 $this->create_portfolio_data('onedrive', 'Microsoft onedrive', $user2, 'visible', 1);
151
152 $records = $DB->get_records('portfolio_instance_user');
153 $this->assertCount(2, $records);
14f72964 154 $this->assertCount(4, $DB->get_records('portfolio_log'));
ab78499b
AG
155
156 $context = context_user::instance($user1->id);
157 $contextlist = new \core_privacy\local\request\approved_contextlist($user1, 'core_portfolio', [$context->id]);
44a0a2bd 158 provider::delete_data_for_user($contextlist);
ab78499b
AG
159 $records = $DB->get_records('portfolio_instance_user');
160 // Only one entry should remain for user 2.
161 $this->assertCount(1, $records);
162 $data = array_shift($records);
163 $this->assertEquals($user2->id, $data->userid);
14f72964 164 $this->assertCount(2, $DB->get_records('portfolio_log'));
e6f06ce9 165 }
44a0a2bd
MG
166
167 /**
168 * Test that only users with a user context are fetched.
169 */
170 public function test_get_users_in_context() {
171 $this->resetAfterTest();
172
173 $component = 'core_portfolio';
174 // Create a user.
175 $user = $this->getDataGenerator()->create_user();
176 $usercontext = context_user::instance($user->id);
177 // The list of users should not return anything yet (related data still haven't been created).
178 $userlist = new \core_privacy\local\request\userlist($usercontext, $component);
179 provider::get_users_in_context($userlist);
180 $this->assertCount(0, $userlist);
181
182 // Create portfolio data for user.
183 $this->create_portfolio_data('googledocs', 'Google Docs', $user,
184 'visible', 1);
185
186 // The list of users for user context should return the user.
187 provider::get_users_in_context($userlist);
188 $this->assertCount(1, $userlist);
189 $expected = [$user->id];
190 $actual = $userlist->get_userids();
191 $this->assertEquals($expected, $actual);
192
193 // The list of users for system context should not return any users.
194 $systemcontext = context_system::instance();
195 $userlist = new \core_privacy\local\request\userlist($systemcontext, $component);
196 provider::get_users_in_context($userlist);
197 $this->assertCount(0, $userlist);
198 }
199
200 /**
201 * Test that data for users in approved userlist is deleted.
202 */
203 public function test_delete_data_for_users() {
204 $this->resetAfterTest();
205
206 $component = 'core_portfolio';
207 // Create user1.
208 $user1 = $this->getDataGenerator()->create_user();
209 $usercontext1 = context_user::instance($user1->id);
210 // Create user1.
211 $user2 = $this->getDataGenerator()->create_user();
212 $usercontext2 = context_user::instance($user2->id);
213
214 // Create portfolio data for user1 and user2.
215 $this->create_portfolio_data('googledocs', 'Google Docs', $user1,
216 'visible', 1);
217 $this->create_portfolio_data('onedrive', 'Microsoft onedrive', $user2,
218 'visible', 1);
219
220 // The list of users for usercontext1 should return user1.
221 $userlist1 = new \core_privacy\local\request\userlist($usercontext1, $component);
222 provider::get_users_in_context($userlist1);
223 $this->assertCount(1, $userlist1);
224 $expected = [$user1->id];
225 $actual = $userlist1->get_userids();
226 $this->assertEquals($expected, $actual);
227 // The list of users for usercontext2 should return user2.
228 $userlist2 = new \core_privacy\local\request\userlist($usercontext2, $component);
229 provider::get_users_in_context($userlist2);
230 $this->assertCount(1, $userlist2);
231 $expected = [$user2->id];
232 $actual = $userlist2->get_userids();
233 $this->assertEquals($expected, $actual);
234
235 // Add userlist1 to the approved user list.
236 $approvedlist = new approved_userlist($usercontext1, $component, $userlist1->get_userids());
237 // Delete user data using delete_data_for_user for usercontext1.
238 provider::delete_data_for_users($approvedlist);
239 // Re-fetch users in usercontext1 - The user list should now be empty.
240 $userlist1 = new \core_privacy\local\request\userlist($usercontext1, $component);
241 provider::get_users_in_context($userlist1);
242 $this->assertCount(0, $userlist1);
243 // Re-fetch users in usercontext2 - The user list should not be empty (user2).
244 $userlist2 = new \core_privacy\local\request\userlist($usercontext2, $component);
245 provider::get_users_in_context($userlist2);
246 $this->assertCount(1, $userlist2);
247
248 // User data should be only removed in the user context.
249 $systemcontext = context_system::instance();
250 // Add userlist2 to the approved user list in the system context.
251 $approvedlist = new approved_userlist($systemcontext, $component, $userlist2->get_userids());
252 // Delete user1 data using delete_data_for_user.
253 provider::delete_data_for_users($approvedlist);
254 // Re-fetch users in usercontext2 - The user list should not be empty (user2).
255 $userlist1 = new \core_privacy\local\request\userlist($usercontext2, $component);
256 provider::get_users_in_context($userlist1);
257 $this->assertCount(1, $userlist1);
258 }
e6f06ce9 259}