Proyectos de Subversion Moodle

Rev

Rev 11 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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 test for the event monitor
19
 *
20
 * @package    tool_monitor
21
 * @category   test
22
 * @copyright  2018 Adrian Greeve <adriangreeve.com>
23
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
 */
25
namespace tool_monitor\privacy;
26
 
27
defined('MOODLE_INTERNAL') || die();
28
 
29
use tool_monitor\privacy\provider;
30
use core_privacy\local\request\approved_contextlist;
31
use core_privacy\local\request\approved_userlist;
32
use core_privacy\tests\provider_testcase;
33
 
34
/**
35
 * Privacy test for the event monitor
36
 *
37
 * @package    tool_monitor
38
 * @category   test
39
 * @copyright  2018 Adrian Greeve <adriangreeve.com>
40
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
41
 */
1441 ariadna 42
final class provider_test extends provider_testcase {
1 efrain 43
 
44
    /**
45
     * Set up method.
46
     */
47
    public function setUp(): void {
1441 ariadna 48
        parent::setUp();
1 efrain 49
        $this->resetAfterTest();
50
        // Enable monitor.
51
        set_config('enablemonitor', 1, 'tool_monitor');
52
    }
53
 
54
    /**
55
     * Assign a capability to $USER
56
     * The function creates a student $USER if $USER->id is empty
57
     *
58
     * @param string $capability capability name
59
     * @param int $contextid
60
     * @param int $roleid
61
     * @return int the role id - mainly returned for creation, so calling function can reuse it
62
     */
63
    public static function assign_user_capability($capability, $contextid, $roleid = null) {
64
        global $USER;
65
 
66
        // Create a new student $USER if $USER doesn't exist.
67
        if (empty($USER->id)) {
68
            $user  = self::getDataGenerator()->create_user();
69
            self::setUser($user);
70
        }
71
 
72
        if (empty($roleid)) {
73
            $roleid = create_role('Dummy role', 'dummyrole', 'dummy role description');
74
        }
75
 
76
        assign_capability($capability, CAP_ALLOW, $roleid, $contextid);
77
 
78
        role_assign($roleid, $USER->id, $contextid);
79
 
80
        accesslib_clear_all_caches_for_unit_testing();
81
 
82
        return $roleid;
83
    }
84
 
85
    /**
86
     * Test that a collection with data is returned when calling this function.
87
     */
11 efrain 88
    public function test_get_metadata(): void {
1 efrain 89
        $collection = new \core_privacy\local\metadata\collection('tool_monitor');
90
        $collection = provider::get_metadata($collection);
91
        $this->assertNotEmpty($collection);
92
    }
93
 
94
    /**
95
     * Check that a user context is returned if there is any user data for this user.
96
     */
11 efrain 97
    public function test_get_contexts_for_userid(): void {
1 efrain 98
        $user = $this->getDataGenerator()->create_user();
99
        $user2 = $this->getDataGenerator()->create_user();
100
        $usercontext = \context_user::instance($user->id);
101
        $usercontext2 = \context_user::instance($user2->id);
102
        $this->assertEmpty(provider::get_contexts_for_userid($user->id));
103
        $this->assertEmpty(provider::get_contexts_for_userid($user2->id));
104
 
105
        $monitorgenerator = $this->getDataGenerator()->get_plugin_generator('tool_monitor');
106
 
107
        // Create a rule with this user.
108
        $this->setUser($user);
109
        $rule = $monitorgenerator->create_rule();
110
        $contextlist = provider::get_contexts_for_userid($user->id);
111
 
112
        // Check that we only get back one context.
113
        $this->assertCount(1, $contextlist);
114
 
115
        // Check that a context is returned for just creating a rule.
116
        $this->assertEquals($usercontext->id, $contextlist->get_contextids()[0]);
117
 
118
        $this->setUser($user2);
119
 
120
        $record = new \stdClass();
121
        $record->courseid = 0;
122
        $record->userid = $user2->id;
123
        $record->ruleid = $rule->id;
124
 
125
        $subscription = $monitorgenerator->create_subscription($record);
126
        $contextlist = provider::get_contexts_for_userid($user2->id);
127
 
128
        // Check that we only get back one context.
129
        $this->assertCount(1, $contextlist);
130
 
131
        // Check that a context is returned for just subscribing to a rule.
132
        $this->assertEquals($usercontext2->id, $contextlist->get_contextids()[0]);
133
    }
134
 
135
    /**
136
     * Check that the correct userlist is returned if there is any user data for this context.
137
     */
11 efrain 138
    public function test_get_users_in_context(): void {
1 efrain 139
        $component = 'tool_monitor';
140
        $user = $this->getDataGenerator()->create_user();
141
        $user2 = $this->getDataGenerator()->create_user();
142
        $usercontext = \context_user::instance($user->id);
143
        $usercontext2 = \context_user::instance($user2->id);
144
 
145
        $userlist = new \core_privacy\local\request\userlist($usercontext, $component);
146
        provider::get_users_in_context($userlist);
147
        $this->assertEmpty($userlist);
148
 
149
        $userlist = new \core_privacy\local\request\userlist($usercontext2, $component);
150
        provider::get_users_in_context($userlist);
151
        $this->assertEmpty($userlist);
152
 
153
        $monitorgenerator = $this->getDataGenerator()->get_plugin_generator('tool_monitor');
154
 
155
        // Create a rule with user.
156
        $this->setUser($user);
157
        $rule = $monitorgenerator->create_rule();
158
        $userlist = new \core_privacy\local\request\userlist($usercontext, $component);
159
        provider::get_users_in_context($userlist);
160
 
161
        // Check that we only get back user.
162
        $userids = $userlist->get_userids();
163
        $this->assertCount(1, $userlist);
164
        $this->assertEquals($user->id, $userids[0]);
165
 
166
        // Create a subscription with user2.
167
        $this->setUser($user2);
168
 
169
        $record = new \stdClass();
170
        $record->courseid = 0;
171
        $record->userid = $user2->id;
172
        $record->ruleid = $rule->id;
173
 
174
        $subscription = $monitorgenerator->create_subscription($record);
175
        $userlist = new \core_privacy\local\request\userlist($usercontext2, $component);
176
        provider::get_users_in_context($userlist);
177
 
178
        // Check that user2 is returned for just subscribing to a rule.
179
        $userids = $userlist->get_userids();
180
        $this->assertCount(1, $userlist);
181
        $this->assertEquals($user2->id, $userids[0]);
182
    }
183
 
184
    /**
185
     * Test that user data is exported correctly.
186
     */
11 efrain 187
    public function test_export_user_data(): void {
1 efrain 188
        $user = $this->getDataGenerator()->create_user();
189
        $usercontext = \context_user::instance($user->id);
190
        $monitorgenerator = $this->getDataGenerator()->get_plugin_generator('tool_monitor');
191
 
192
        $this->setUser($user);
193
        $rulerecord = (object)['name' => 'privacy rule'];
194
        $rule = $monitorgenerator->create_rule($rulerecord);
195
 
196
        $secondrulerecord = (object)['name' => 'privacy rule2'];
197
        $rule2 = $monitorgenerator->create_rule($secondrulerecord);
198
 
199
        $subscription = (object)['ruleid' => $rule->id, 'userid' => $user->id];
200
        $subscription = $monitorgenerator->create_subscription($subscription);
201
 
202
        $writer = \core_privacy\local\request\writer::with_context($usercontext);
203
        $this->assertFalse($writer->has_any_data());
204
 
205
        $approvedlist = new approved_contextlist($user, 'tool_monitor', [$usercontext->id]);
206
        provider::export_user_data($approvedlist);
207
 
208
        // Check that the rules created by this user are exported.
209
        $this->assertEquals($rulerecord->name, $writer->get_data([get_string('privacy:createdrules', 'tool_monitor'),
210
                $rulerecord->name . '_' . $rule->id])->name);
211
        $this->assertEquals($secondrulerecord->name, $writer->get_data([get_string('privacy:createdrules', 'tool_monitor'),
212
                $secondrulerecord->name . '_' . $rule2->id])->name);
213
 
214
        // Check that the subscriptions for this user are also exported.
215
        $this->assertEquals($rulerecord->name, $writer->get_data([get_string('privacy:subscriptions', 'tool_monitor'),
216
                $rulerecord->name . '_' . $subscription->id, 'Site' , 'All events'])->name);
217
    }
218
 
219
    /**
220
     * Test deleting all user data for a specific context.
221
     */
11 efrain 222
    public function test_delete_data_for_all_users_in_context(): void {
1 efrain 223
        global $DB;
224
 
225
        $user = $this->getDataGenerator()->create_user();
226
        $user2 = $this->getDataGenerator()->create_user();
227
        $usercontext = \context_user::instance($user->id);
228
        $usercontext2 = \context_user::instance($user2->id);
229
        $monitorgenerator = $this->getDataGenerator()->get_plugin_generator('tool_monitor');
230
 
231
        $this->setUser($user);
232
        // Need to give user one the ability to manage rules.
233
        $this->assign_user_capability('tool/monitor:managerules', \context_system::instance());
234
 
235
        $rulerecord = (object)['name' => 'privacy rule'];
236
        $rule = $monitorgenerator->create_rule($rulerecord);
237
 
238
        $secondrulerecord = (object)['name' => 'privacy rule2'];
239
        $rule2 = $monitorgenerator->create_rule($secondrulerecord);
240
 
241
        $subscription = (object)['ruleid' => $rule->id, 'userid' => $user->id];
242
        $subscription = $monitorgenerator->create_subscription($subscription);
243
 
244
        // Have user 2 subscribe to the second rule created by user 1.
245
        $subscription2 = (object)['ruleid' => $rule2->id, 'userid' => $user2->id];
246
        $subscription2 = $monitorgenerator->create_subscription($subscription2);
247
 
248
        $this->setUser($user2);
249
        $thirdrulerecord = (object)['name' => 'privacy rule for second user'];
250
        $rule3 = $monitorgenerator->create_rule($thirdrulerecord);
251
 
252
        $subscription3 = (object)['ruleid' => $rule3->id, 'userid' => $user2->id];
253
        $subscription3 = $monitorgenerator->create_subscription($subscription3);
254
 
255
        // Try a different context first.
256
        provider::delete_data_for_all_users_in_context(\context_system::instance());
257
 
258
        // Get all of the monitor rules.
259
        $dbrules = $DB->get_records('tool_monitor_rules');
260
 
261
        // All of the rules should still be present.
262
        $this->assertCount(3, $dbrules);
263
        $this->assertEquals($user->id, $dbrules[$rule->id]->userid);
264
        $this->assertEquals($user->id, $dbrules[$rule2->id]->userid);
265
        $this->assertEquals($user2->id, $dbrules[$rule3->id]->userid);
266
 
267
        // Delete everything for the first user context.
268
        provider::delete_data_for_all_users_in_context($usercontext);
269
 
270
        // Get all of the monitor rules.
271
        $dbrules = $DB->get_records('tool_monitor_rules');
272
 
273
        // Only the rules for user 1 that does not have any more subscriptions should be deleted (the first rule).
274
        $this->assertCount(2, $dbrules);
275
        $this->assertEquals($user->id, $dbrules[$rule2->id]->userid);
276
        $this->assertEquals($user2->id, $dbrules[$rule3->id]->userid);
277
 
278
        // Get all of the monitor subscriptions.
279
        $dbsubs = $DB->get_records('tool_monitor_subscriptions');
280
        // There should be two subscriptions left, both for user 2.
281
        $this->assertCount(2, $dbsubs);
282
        $this->assertEquals($user2->id, $dbsubs[$subscription2->id]->userid);
283
        $this->assertEquals($user2->id, $dbsubs[$subscription3->id]->userid);
284
    }
285
 
286
    /**
287
     * This should work identical to the above test.
288
     */
11 efrain 289
    public function test_delete_data_for_user(): void {
1 efrain 290
        global $DB;
291
 
292
        $user = $this->getDataGenerator()->create_user();
293
        $user2 = $this->getDataGenerator()->create_user();
294
        $usercontext = \context_user::instance($user->id);
295
        $usercontext2 = \context_user::instance($user2->id);
296
        $monitorgenerator = $this->getDataGenerator()->get_plugin_generator('tool_monitor');
297
 
298
        $this->setUser($user);
299
        // Need to give user one the ability to manage rules.
300
        $this->assign_user_capability('tool/monitor:managerules', \context_system::instance());
301
 
302
        $rulerecord = (object)['name' => 'privacy rule'];
303
        $rule = $monitorgenerator->create_rule($rulerecord);
304
 
305
        $secondrulerecord = (object)['name' => 'privacy rule2'];
306
        $rule2 = $monitorgenerator->create_rule($secondrulerecord);
307
 
308
        $subscription = (object)['ruleid' => $rule->id, 'userid' => $user->id];
309
        $subscription = $monitorgenerator->create_subscription($subscription);
310
 
311
        // Have user 2 subscribe to the second rule created by user 1.
312
        $subscription2 = (object)['ruleid' => $rule2->id, 'userid' => $user2->id];
313
        $subscription2 = $monitorgenerator->create_subscription($subscription2);
314
 
315
        $this->setUser($user2);
316
        $thirdrulerecord = (object)['name' => 'privacy rule for second user'];
317
        $rule3 = $monitorgenerator->create_rule($thirdrulerecord);
318
 
319
        $subscription3 = (object)['ruleid' => $rule3->id, 'userid' => $user2->id];
320
        $subscription3 = $monitorgenerator->create_subscription($subscription3);
321
 
322
        $approvedlist = new approved_contextlist($user, 'tool_monitor', [$usercontext->id]);
323
 
324
        // Delete everything for the first user.
325
        provider::delete_data_for_user($approvedlist);
326
 
327
        // Get all of the monitor rules.
328
        $dbrules = $DB->get_records('tool_monitor_rules');
329
 
330
        // Only the rules for user 1 that does not have any more subscriptions should be deleted (the first rule).
331
        $this->assertCount(2, $dbrules);
332
        $this->assertEquals($user->id, $dbrules[$rule2->id]->userid);
333
        $this->assertEquals($user2->id, $dbrules[$rule3->id]->userid);
334
 
335
        // Get all of the monitor subscriptions.
336
        $dbsubs = $DB->get_records('tool_monitor_subscriptions');
337
        // There should be two subscriptions left, both for user 2.
338
        $this->assertCount(2, $dbsubs);
339
        $this->assertEquals($user2->id, $dbsubs[$subscription2->id]->userid);
340
        $this->assertEquals($user2->id, $dbsubs[$subscription3->id]->userid);
341
    }
342
 
343
    /**
344
     * Test deleting user data for an approved userlist in a context.
345
     */
11 efrain 346
    public function test_delete_data_for_users(): void {
1 efrain 347
        global $DB;
348
 
349
        $component = 'tool_monitor';
350
        $user = $this->getDataGenerator()->create_user();
351
        $user2 = $this->getDataGenerator()->create_user();
352
        $usercontext = \context_user::instance($user->id);
353
        $usercontext2 = \context_user::instance($user2->id);
354
        $monitorgenerator = $this->getDataGenerator()->get_plugin_generator('tool_monitor');
355
 
356
        $this->setUser($user);
357
        // Need to give user one the ability to manage rules.
358
        $this->assign_user_capability('tool/monitor:managerules', \context_system::instance());
359
 
360
        $rulerecord = (object)['name' => 'privacy rule'];
361
        $rule = $monitorgenerator->create_rule($rulerecord);
362
 
363
        $secondrulerecord = (object)['name' => 'privacy rule2'];
364
        $rule2 = $monitorgenerator->create_rule($secondrulerecord);
365
 
366
        $subscription = (object)['ruleid' => $rule->id, 'userid' => $user->id];
367
        $subscription = $monitorgenerator->create_subscription($subscription);
368
 
369
        // Have user 2 subscribe to the second rule created by user 1.
370
        $subscription2 = (object)['ruleid' => $rule2->id, 'userid' => $user2->id];
371
        $subscription2 = $monitorgenerator->create_subscription($subscription2);
372
 
373
        $this->setUser($user2);
374
        $thirdrulerecord = (object)['name' => 'privacy rule for second user'];
375
        $rule3 = $monitorgenerator->create_rule($thirdrulerecord);
376
 
377
        $subscription3 = (object)['ruleid' => $rule3->id, 'userid' => $user2->id];
378
        $subscription3 = $monitorgenerator->create_subscription($subscription3);
379
 
380
        // Get all of the monitor rules, ensure all exist.
381
        $dbrules = $DB->get_records('tool_monitor_rules');
382
        $this->assertCount(3, $dbrules);
383
 
384
        // Delete for user2 in first user's context, should have no effect.
385
        $approveduserids = [$user2->id];
386
        $approvedlist = new approved_userlist($usercontext, $component, $approveduserids);
387
        provider::delete_data_for_users($approvedlist);
388
 
389
        $dbrules = $DB->get_records('tool_monitor_rules');
390
        $this->assertCount(3, $dbrules);
391
 
392
        // Delete for user in usercontext.
393
        $approveduserids = [$user->id];
394
        $approvedlist = new approved_userlist($usercontext, $component, $approveduserids);
395
        provider::delete_data_for_users($approvedlist);
396
 
397
        // Only the rules for user 1 that does not have any more subscriptions should be deleted (the first rule).
398
        $dbrules = $DB->get_records('tool_monitor_rules');
399
        $this->assertCount(2, $dbrules);
400
        $this->assertEquals($user->id, $dbrules[$rule2->id]->userid);
401
        $this->assertEquals($user2->id, $dbrules[$rule3->id]->userid);
402
 
403
        // There should be two subscriptions left, both for user 2.
404
        $dbsubs = $DB->get_records('tool_monitor_subscriptions');
405
        $this->assertCount(2, $dbsubs);
406
        $this->assertEquals($user2->id, $dbsubs[$subscription2->id]->userid);
407
        $this->assertEquals($user2->id, $dbsubs[$subscription3->id]->userid);
408
 
409
        // Delete for user2 in context 2.
410
        $approveduserids = [$user2->id];
411
        $approvedlist = new approved_userlist($usercontext2, $component, $approveduserids);
412
        provider::delete_data_for_users($approvedlist);
413
 
414
        // There should be no subscriptions left.
415
        $dbsubs = $DB->get_records('tool_monitor_subscriptions');
416
        $this->assertEmpty($dbsubs);
417
    }
418
}