forked from moodle/moodle
/
privacy_provider_test.php
394 lines (331 loc) · 17.4 KB
/
privacy_provider_test.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Privacy provider tests.
*
* @package tool_policy
* @category test
* @copyright 2018 Sara Arjona <sara@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use core_privacy\local\metadata\collection;
use tool_policy\privacy\provider;
use tool_policy\api;
use tool_policy\policy_version;
use core_privacy\local\request\approved_contextlist;
use core_privacy\local\request\writer;
defined('MOODLE_INTERNAL') || die();
/**
* Privacy provider tests class.
*
* @copyright 2018 Sara Arjona <sara@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class tool_policy_privacy_provider_testcase extends \core_privacy\tests\provider_testcase {
/** @var stdClass The user object. */
protected $user;
/** @var stdClass The manager user object. */
protected $manager;
/** @var context_system The system context instance. */
protected $syscontext;
/**
* Setup function. Will create a user.
*/
protected function setUp(): void {
$this->resetAfterTest();
$generator = $this->getDataGenerator();
$this->user = $generator->create_user();
// Create manager user.
$this->manager = $generator->create_user();
$this->syscontext = context_system::instance();
$rolemanagerid = create_role('Policy manager', 'policymanager', 'Can manage policy documents');
assign_capability('tool/policy:managedocs', CAP_ALLOW, $rolemanagerid, $this->syscontext->id);
assign_capability('tool/policy:acceptbehalf', CAP_ALLOW, $rolemanagerid, $this->syscontext->id);
role_assign($rolemanagerid, $this->manager->id, $this->syscontext->id);
accesslib_clear_all_caches_for_unit_testing();
}
/**
* Test getting the context for the user ID related to this plugin.
*/
public function test_get_contexts_for_userid() {
global $CFG;
// When there are no policies or agreements context list is empty.
$contextlist = \tool_policy\privacy\provider::get_contexts_for_userid($this->manager->id);
$this->assertEmpty($contextlist);
$contextlist = \tool_policy\privacy\provider::get_contexts_for_userid($this->user->id);
$this->assertEmpty($contextlist);
// Create a policy.
$this->setUser($this->manager);
$CFG->sitepolicyhandler = 'tool_policy';
$policy = $this->add_policy();
api::make_current($policy->get('id'));
// After creating a policy, there should be manager context.
$contextlist = \tool_policy\privacy\provider::get_contexts_for_userid($this->manager->id);
$this->assertEquals(1, $contextlist->count());
// But when there are no agreements, user context list is empty.
$contextlist = \tool_policy\privacy\provider::get_contexts_for_userid($this->user->id);
$this->assertEmpty($contextlist);
// Agree to the policy.
$this->setUser($this->user);
api::accept_policies([$policy->get('id')]);
// There should be user context.
$contextlist = \tool_policy\privacy\provider::get_contexts_for_userid($this->user->id);
$this->assertEquals(1, $contextlist->count());
}
/**
* Test getting the user IDs within the context related to this plugin.
*/
public function test_get_users_in_context() {
global $CFG;
$component = 'tool_policy';
// System context should have nothing before a policy is added.
$userlist = new \core_privacy\local\request\userlist($this->syscontext, $component);
provider::get_users_in_context($userlist);
$this->assertEmpty($userlist);
// Create parent and child users.
$generator = $this->getDataGenerator();
$parentuser = $generator->create_user();
$childuser = $generator->create_user();
// Fetch relevant contexts.
$managercontext = \context_user::instance($this->manager->id);
$usercontext = $managercontext = \context_user::instance($this->user->id);
$parentcontext = $managercontext = \context_user::instance($parentuser->id);
$childcontext = $managercontext = \context_user::instance($childuser->id);
// Assign parent to accept on behalf of the child.
$roleparentid = create_role('Parent', 'parent', 'Can accept policies on behalf of their child');
assign_capability('tool/policy:acceptbehalf', CAP_ALLOW, $roleparentid, $this->syscontext->id);
role_assign($roleparentid, $parentuser->id, $childcontext->id);
// Create a policy.
$this->setUser($this->manager);
$CFG->sitepolicyhandler = 'tool_policy';
$policy = $this->add_policy();
api::make_current($policy->get('id'));
// Manager should exist in system context now they have created a policy.
$userlist = new \core_privacy\local\request\userlist($this->syscontext, $component);
provider::get_users_in_context($userlist);
$this->assertCount(1, $userlist);
$this->assertEquals([$this->manager->id], $userlist->get_userids());
// User contexts should be empty before policy acceptances.
$userlist = new \core_privacy\local\request\userlist($usercontext, $component);
provider::get_users_in_context($userlist);
$this->assertEmpty($userlist);
$userlist = new \core_privacy\local\request\userlist($parentcontext, $component);
provider::get_users_in_context($userlist);
$this->assertEmpty($userlist);
$userlist = new \core_privacy\local\request\userlist($childcontext, $component);
provider::get_users_in_context($userlist);
$this->assertEmpty($userlist);
// User accepts policy, parent accepts on behalf of child only.
$this->setUser($this->user);
api::accept_policies([$policy->get('id')]);
$this->setUser($parentuser);
api::accept_policies([$policy->get('id')], $childuser->id);
// Ensure user is fetched within its user context.
$userlist = new \core_privacy\local\request\userlist($usercontext, $component);
provider::get_users_in_context($userlist);
$this->assertCount(1, $userlist);
$this->assertEquals([$this->user->id], $userlist->get_userids());
// Ensure parent and child are both found within child's user context.
$userlist = new \core_privacy\local\request\userlist($childcontext, $component);
provider::get_users_in_context($userlist);
$this->assertCount(2, $userlist);
$expected = [$parentuser->id, $childuser->id];
$actual = $userlist->get_userids();
sort($expected);
sort($actual);
$this->assertEquals($expected, $actual);
// Parent has not accepted for itself, so should not be found within its user context.
$userlist = new \core_privacy\local\request\userlist($parentcontext, $component);
provider::get_users_in_context($userlist);
$this->assertCount(0, $userlist);
}
public function test_export_agreements() {
global $CFG;
$otheruser = $this->getDataGenerator()->create_user();
$otherusercontext = \context_user::instance($otheruser->id);
// Create policies and agree to them as manager.
$this->setUser($this->manager);
$managercontext = \context_user::instance($this->manager->id);
$systemcontext = \context_system::instance();
$agreementsubcontext = [
get_string('privacyandpolicies', 'admin'),
get_string('useracceptances', 'tool_policy')
];
$versionsubcontext = [
get_string('policydocuments', 'tool_policy')
];
$CFG->sitepolicyhandler = 'tool_policy';
$policy1 = $this->add_policy();
api::make_current($policy1->get('id'));
$policy2 = $this->add_policy();
api::make_current($policy2->get('id'));
api::accept_policies([$policy1->get('id'), $policy2->get('id')]);
// Agree to the policies for oneself.
$this->setUser($this->user);
$usercontext = \context_user::instance($this->user->id);
api::accept_policies([$policy1->get('id'), $policy2->get('id')]);
// Request export for this user.
$contextlist = provider::get_contexts_for_userid($this->user->id);
$this->assertCount(1, $contextlist);
$this->assertEquals([$usercontext->id], $contextlist->get_contextids());
$approvedcontextlist = new approved_contextlist($this->user, 'tool_policy', [$usercontext->id]);
provider::export_user_data($approvedcontextlist);
// User can not see manager's agreements but can see his own.
$writer = writer::with_context($managercontext);
$this->assertFalse($writer->has_any_data());
$writer = writer::with_context($usercontext);
$this->assertTrue($writer->has_any_data());
// Test policy 1.
$subcontext = array_merge($agreementsubcontext, [get_string('policynamedversion', 'tool_policy', $policy1->to_record())]);
$datauser = $writer->get_data($subcontext);
$this->assertEquals($policy1->get('name'), $datauser->name);
$this->assertEquals($this->user->id, $datauser->agreedby);
$this->assertEquals(strip_tags($policy1->get('summary')), strip_tags($datauser->summary));
$this->assertEquals(strip_tags($policy1->get('content')), strip_tags($datauser->content));
// Test policy 2.
$subcontext = array_merge($agreementsubcontext, [get_string('policynamedversion', 'tool_policy', $policy2->to_record())]);
$datauser = $writer->get_data($subcontext);
$this->assertEquals($policy2->get('name'), $datauser->name);
$this->assertEquals($this->user->id, $datauser->agreedby);
$this->assertEquals(strip_tags($policy2->get('summary')), strip_tags($datauser->summary));
$this->assertEquals(strip_tags($policy2->get('content')), strip_tags($datauser->content));
}
public function test_export_agreements_for_other() {
global $CFG;
$managercontext = \context_user::instance($this->manager->id);
$systemcontext = \context_system::instance();
$usercontext = \context_user::instance($this->user->id);
// Create policies and agree to them as manager.
$this->setUser($this->manager);
$agreementsubcontext = [
get_string('privacyandpolicies', 'admin'),
get_string('useracceptances', 'tool_policy')
];
$versionsubcontext = [
get_string('policydocuments', 'tool_policy')
];
$CFG->sitepolicyhandler = 'tool_policy';
$policy1 = $this->add_policy();
api::make_current($policy1->get('id'));
$policy2 = $this->add_policy();
api::make_current($policy2->get('id'));
api::accept_policies([$policy1->get('id'), $policy2->get('id')]);
// Agree to the other user's policies.
api::accept_policies([$policy1->get('id'), $policy2->get('id')], $this->user->id, 'My note');
// Request export for the manager.
$contextlist = provider::get_contexts_for_userid($this->manager->id);
$this->assertCount(3, $contextlist);
$this->assertEqualsCanonicalizing(
[$managercontext->id, $usercontext->id, $systemcontext->id],
$contextlist->get_contextids()
);
$approvedcontextlist = new approved_contextlist($this->user, 'tool_policy', [$usercontext->id]);
provider::export_user_data($approvedcontextlist);
// The user context has data.
$writer = writer::with_context($usercontext);
$this->assertTrue($writer->has_any_data());
// Test policy 1.
$writer = writer::with_context($usercontext);
$subcontext = array_merge($agreementsubcontext, [get_string('policynamedversion', 'tool_policy', $policy1->to_record())]);
$datauser = $writer->get_data($subcontext);
$this->assertEquals($policy1->get('name'), $datauser->name);
$this->assertEquals($this->manager->id, $datauser->agreedby);
$this->assertEquals(strip_tags($policy1->get('summary')), strip_tags($datauser->summary));
$this->assertEquals(strip_tags($policy1->get('content')), strip_tags($datauser->content));
// Test policy 2.
$subcontext = array_merge($agreementsubcontext, [get_string('policynamedversion', 'tool_policy', $policy2->to_record())]);
$datauser = $writer->get_data($subcontext);
$this->assertEquals($policy2->get('name'), $datauser->name);
$this->assertEquals($this->manager->id, $datauser->agreedby);
$this->assertEquals(strip_tags($policy2->get('summary')), strip_tags($datauser->summary));
$this->assertEquals(strip_tags($policy2->get('content')), strip_tags($datauser->content));
}
public function test_export_created_policies() {
global $CFG;
// Create policies and agree to them as manager.
$this->setUser($this->manager);
$managercontext = \context_user::instance($this->manager->id);
$systemcontext = \context_system::instance();
$agreementsubcontext = [
get_string('privacyandpolicies', 'admin'),
get_string('useracceptances', 'tool_policy')
];
$versionsubcontext = [
get_string('policydocuments', 'tool_policy')
];
$CFG->sitepolicyhandler = 'tool_policy';
$policy1 = $this->add_policy();
api::make_current($policy1->get('id'));
$policy2 = $this->add_policy();
api::make_current($policy2->get('id'));
api::accept_policies([$policy1->get('id'), $policy2->get('id')]);
// Agree to the policies for oneself.
$contextlist = provider::get_contexts_for_userid($this->manager->id);
$this->assertCount(2, $contextlist);
$this->assertEqualsCanonicalizing([$managercontext->id, $systemcontext->id], $contextlist->get_contextids());
$approvedcontextlist = new approved_contextlist($this->manager, 'tool_policy', $contextlist->get_contextids());
provider::export_user_data($approvedcontextlist);
// User has agreed to policies.
$writer = writer::with_context($managercontext);
$this->assertTrue($writer->has_any_data());
// Test policy 1.
$subcontext = array_merge($agreementsubcontext, [get_string('policynamedversion', 'tool_policy', $policy1->to_record())]);
$datauser = $writer->get_data($subcontext);
$this->assertEquals($policy1->get('name'), $datauser->name);
$this->assertEquals($this->manager->id, $datauser->agreedby);
$this->assertEquals(strip_tags($policy1->get('summary')), strip_tags($datauser->summary));
$this->assertEquals(strip_tags($policy1->get('content')), strip_tags($datauser->content));
// Test policy 2.
$subcontext = array_merge($agreementsubcontext, [get_string('policynamedversion', 'tool_policy', $policy2->to_record())]);
$datauser = $writer->get_data($subcontext);
$this->assertEquals($policy2->get('name'), $datauser->name);
$this->assertEquals($this->manager->id, $datauser->agreedby);
$this->assertEquals(strip_tags($policy2->get('summary')), strip_tags($datauser->summary));
$this->assertEquals(strip_tags($policy2->get('content')), strip_tags($datauser->content));
// User can see policy documents.
$writer = writer::with_context($systemcontext);
$this->assertTrue($writer->has_any_data());
$subcontext = array_merge($versionsubcontext, [get_string('policynamedversion', 'tool_policy', $policy1->to_record())]);
$dataversion = $writer->get_data($subcontext);
$this->assertEquals($policy1->get('name'), $dataversion->name);
$this->assertEquals(get_string('yes'), $dataversion->createdbyme);
$subcontext = array_merge($versionsubcontext, [get_string('policynamedversion', 'tool_policy', $policy2->to_record())]);
$dataversion = $writer->get_data($subcontext);
$this->assertEquals($policy2->get('name'), $dataversion->name);
$this->assertEquals(get_string('yes'), $dataversion->createdbyme);
}
/**
* Helper method that creates a new policy for testing
*
* @param array $params
* @return policy_version
*/
protected function add_policy($params = []) {
static $counter = 0;
$counter++;
$defaults = [
'name' => 'Policy '.$counter,
'summary_editor' => ['text' => "P$counter summary", 'format' => FORMAT_HTML, 'itemid' => 0],
'content_editor' => ['text' => "P$counter content", 'format' => FORMAT_HTML, 'itemid' => 0],
];
$params = (array)$params + $defaults;
$formdata = \tool_policy\api::form_policydoc_data(new policy_version(0));
foreach ($params as $key => $value) {
$formdata->$key = $value;
}
return api::form_policydoc_add($formdata);
}
}