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
 * Data provider tests.
19
 *
20
 * @package    logstore_standard
21
 * @category   test
22
 * @copyright  2018 Frédéric Massart
23
 * @author     Frédéric Massart <fred@branchup.tech>
24
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25
 */
26
namespace logstore_standard\privacy;
27
 
28
defined('MOODLE_INTERNAL') || die();
29
global $CFG;
30
 
31
use core_privacy\tests\provider_testcase;
32
use core_privacy\local\request\contextlist;
33
use core_privacy\local\request\approved_contextlist;
34
use core_privacy\local\request\transform;
35
use core_privacy\local\request\writer;
36
use logstore_standard\privacy\provider;
37
 
38
require_once(__DIR__ . '/../fixtures/event.php');
39
 
40
/**
41
 * Data provider testcase class.
42
 *
43
 * @package    logstore_standard
44
 * @category   test
45
 * @copyright  2018 Frédéric Massart
46
 * @author     Frédéric Massart <fred@branchup.tech>
47
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
48
 */
1441 ariadna 49
final class provider_test extends provider_testcase {
1 efrain 50
 
51
    public function setUp(): void {
1441 ariadna 52
        parent::setUp();
1 efrain 53
        $this->resetAfterTest();
54
        $this->preventResetByRollback(); // Logging waits till the transaction gets committed.
55
    }
56
 
11 efrain 57
    public function test_get_contexts_for_userid(): void {
1 efrain 58
        $admin = \core_user::get_user(2);
59
        $u1 = $this->getDataGenerator()->create_user();
60
        $u2 = $this->getDataGenerator()->create_user();
61
        $u3 = $this->getDataGenerator()->create_user();
62
 
63
        $c1 = $this->getDataGenerator()->create_course();
64
        $cm1 = $this->getDataGenerator()->create_module('url', ['course' => $c1]);
65
        $c2 = $this->getDataGenerator()->create_course();
66
        $cm2 = $this->getDataGenerator()->create_module('url', ['course' => $c2]);
67
 
68
        $sysctx = \context_system::instance();
69
        $c1ctx = \context_course::instance($c1->id);
70
        $c2ctx = \context_course::instance($c2->id);
71
        $cm1ctx = \context_module::instance($cm1->cmid);
72
        $cm2ctx = \context_module::instance($cm2->cmid);
73
 
74
        $this->enable_logging();
75
        $manager = get_log_manager(true);
76
 
77
        // User 1 is the author.
78
        $this->setUser($u1);
79
        $this->assert_contextlist_equals($this->get_contextlist_for_user($u1), []);
80
        $e = \logstore_standard\event\unittest_executed::create(['context' => $cm1ctx]);
81
        $e->trigger();
82
        $this->assert_contextlist_equals($this->get_contextlist_for_user($u1), [$cm1ctx]);
83
 
84
        // User 2 is the related user.
85
        $this->setUser(0);
86
        $this->assert_contextlist_equals($this->get_contextlist_for_user($u2), []);
87
        $e = \logstore_standard\event\unittest_executed::create(['context' => $cm2ctx, 'relateduserid' => $u2->id]);
88
        $e->trigger();
89
        $this->assert_contextlist_equals($this->get_contextlist_for_user($u2), [$cm2ctx]);
90
 
91
        // Admin user is the real user.
92
        $this->assert_contextlist_equals($this->get_contextlist_for_user($admin), []);
93
        $this->assert_contextlist_equals($this->get_contextlist_for_user($u3), []);
94
        $this->setAdminUser();
95
        \core\session\manager::loginas($u3->id, $sysctx);
96
        $e = \logstore_standard\event\unittest_executed::create(['context' => $c1ctx]);
97
        $e->trigger();
98
        $this->assert_contextlist_equals($this->get_contextlist_for_user($admin), [$sysctx, $c1ctx]);
99
        $this->assert_contextlist_equals($this->get_contextlist_for_user($u3), [$sysctx, $c1ctx]);
100
 
101
        // By admin user masquerading u1 related to u3.
102
        $this->assert_contextlist_equals($this->get_contextlist_for_user($u1), [$cm1ctx]);
103
        $this->assert_contextlist_equals($this->get_contextlist_for_user($u3), [$sysctx, $c1ctx]);
104
        $this->assert_contextlist_equals($this->get_contextlist_for_user($admin), [$sysctx, $c1ctx]);
105
        $this->setAdminUser();
106
        \core\session\manager::loginas($u1->id, \context_system::instance());
107
        $e = \logstore_standard\event\unittest_executed::create(['context' => $c2ctx, 'relateduserid' => $u3->id]);
108
        $e->trigger();
109
        $this->assert_contextlist_equals($this->get_contextlist_for_user($u1), [$sysctx, $cm1ctx, $c2ctx]);
110
        $this->assert_contextlist_equals($this->get_contextlist_for_user($u3), [$sysctx, $c1ctx, $c2ctx]);
111
        $this->assert_contextlist_equals($this->get_contextlist_for_user($admin), [$sysctx, $c1ctx, $c2ctx]);
112
    }
113
 
11 efrain 114
    public function test_add_userids_for_context(): void {
1 efrain 115
        $admin = \core_user::get_user(2);
116
        $u1 = $this->getDataGenerator()->create_user();
117
        $u2 = $this->getDataGenerator()->create_user();
118
        $u3 = $this->getDataGenerator()->create_user();
119
        $u4 = $this->getDataGenerator()->create_user();
120
 
121
        $c1 = $this->getDataGenerator()->create_course();
122
 
123
        $sysctx = \context_system::instance();
124
        $c1ctx = \context_course::instance($c1->id);
125
 
126
        $this->enable_logging();
127
        $manager = get_log_manager(true);
128
 
129
        $userlist = new \core_privacy\local\request\userlist($sysctx, 'logstore_standard_log');
130
        $userids = $userlist->get_userids();
131
        $this->assertEmpty($userids);
132
        provider::add_userids_for_context($userlist);
133
        $userids = $userlist->get_userids();
134
        $this->assertEmpty($userids);
135
        // User one should be added (userid).
136
        $this->setUser($u1);
137
        $e = \logstore_standard\event\unittest_executed::create(['context' => $sysctx]);
138
        $e->trigger();
139
        // User two (userids) and three (relateduserid) should be added.
140
        $this->setUser($u2);
141
        $e = \logstore_standard\event\unittest_executed::create(['context' => $sysctx, 'relateduserid' => $u3->id]);
142
        $e->trigger();
143
        // The admin user should be added (realuserid).
144
        $this->setAdminUser();
145
        \core\session\manager::loginas($u2->id, \context_system::instance());
146
        $e = \logstore_standard\event\unittest_executed::create(['context' => $sysctx]);
147
        $e->trigger();
148
        // Set off an event in a different context. User 4 should not be returned below.
149
        $this->setUser($u4);
150
        $e = \logstore_standard\event\unittest_executed::create(['context' => $c1ctx]);
151
        $e->trigger();
152
 
153
        provider::add_userids_for_context($userlist);
154
        $userids = $userlist->get_userids();
155
        $this->assertCount(4, $userids);
156
        $expectedresult = [$admin->id, $u1->id, $u2->id, $u3->id];
157
        $this->assertEmpty(array_diff($expectedresult, $userids));
158
    }
159
 
11 efrain 160
    public function test_delete_data_for_user(): void {
1 efrain 161
        global $DB;
162
        $u1 = $this->getDataGenerator()->create_user();
163
        $u2 = $this->getDataGenerator()->create_user();
164
        $c1 = $this->getDataGenerator()->create_course();
165
        $c2 = $this->getDataGenerator()->create_course();
166
        $sysctx = \context_system::instance();
167
        $c1ctx = \context_course::instance($c1->id);
168
        $c2ctx = \context_course::instance($c2->id);
169
 
170
        $this->enable_logging();
171
        $manager = get_log_manager(true);
172
 
173
        // User 1 is the author.
174
        $this->setUser($u1);
175
        $e = \logstore_standard\event\unittest_executed::create(['context' => $c1ctx]);
176
        $e->trigger();
177
        $e = \logstore_standard\event\unittest_executed::create(['context' => $c1ctx]);
178
        $e->trigger();
179
        $e = \logstore_standard\event\unittest_executed::create(['context' => $c2ctx]);
180
        $e->trigger();
181
 
182
        // User 2 is the author.
183
        $this->setUser($u2);
184
        $e = \logstore_standard\event\unittest_executed::create(['context' => $c1ctx]);
185
        $e->trigger();
186
        $e = \logstore_standard\event\unittest_executed::create(['context' => $c2ctx]);
187
        $e->trigger();
188
 
189
        // Confirm data present.
190
        $this->assertTrue($DB->record_exists('logstore_standard_log', ['userid' => $u1->id, 'contextid' => $c1ctx->id]));
191
        $this->assertEquals(3, $DB->count_records('logstore_standard_log', ['userid' => $u1->id]));
192
        $this->assertEquals(2, $DB->count_records('logstore_standard_log', ['userid' => $u2->id]));
193
 
194
        // Delete all the things!
195
        provider::delete_data_for_user(new approved_contextlist($u1, 'logstore_standard', [$c1ctx->id]));
196
        $this->assertFalse($DB->record_exists('logstore_standard_log', ['userid' => $u1->id, 'contextid' => $c1ctx->id]));
197
        $this->assertEquals(1, $DB->count_records('logstore_standard_log', ['userid' => $u1->id]));
198
        $this->assertEquals(2, $DB->count_records('logstore_standard_log', ['userid' => $u2->id]));
199
    }
200
 
11 efrain 201
    public function test_delete_data_for_all_users_in_context(): void {
1 efrain 202
        global $DB;
203
        $u1 = $this->getDataGenerator()->create_user();
204
        $u2 = $this->getDataGenerator()->create_user();
205
        $c1 = $this->getDataGenerator()->create_course();
206
        $c2 = $this->getDataGenerator()->create_course();
207
        $sysctx = \context_system::instance();
208
        $c1ctx = \context_course::instance($c1->id);
209
        $c2ctx = \context_course::instance($c2->id);
210
 
211
        $this->enable_logging();
212
        $manager = get_log_manager(true);
213
 
214
        // User 1 is the author.
215
        $this->setUser($u1);
216
        $e = \logstore_standard\event\unittest_executed::create(['context' => $c1ctx]);
217
        $e->trigger();
218
        $e = \logstore_standard\event\unittest_executed::create(['context' => $c1ctx]);
219
        $e->trigger();
220
        $e = \logstore_standard\event\unittest_executed::create(['context' => $c2ctx]);
221
        $e->trigger();
222
 
223
        // User 2 is the author.
224
        $this->setUser($u2);
225
        $e = \logstore_standard\event\unittest_executed::create(['context' => $c1ctx]);
226
        $e->trigger();
227
        $e = \logstore_standard\event\unittest_executed::create(['context' => $c2ctx]);
228
        $e->trigger();
229
 
230
        // Confirm data present.
231
        $this->assertTrue($DB->record_exists('logstore_standard_log', ['contextid' => $c1ctx->id]));
232
        $this->assertEquals(3, $DB->count_records('logstore_standard_log', ['userid' => $u1->id]));
233
        $this->assertEquals(2, $DB->count_records('logstore_standard_log', ['userid' => $u2->id]));
234
 
235
        // Delete all the things!
236
        provider::delete_data_for_all_users_in_context($c1ctx);
237
        $this->assertFalse($DB->record_exists('logstore_standard_log', ['contextid' => $c1ctx->id]));
238
        $this->assertEquals(1, $DB->count_records('logstore_standard_log', ['userid' => $u1->id]));
239
        $this->assertEquals(1, $DB->count_records('logstore_standard_log', ['userid' => $u2->id]));
240
    }
241
 
11 efrain 242
    public function test_delete_data_for_userlist(): void {
1 efrain 243
        global $DB;
244
 
245
        $u1 = $this->getDataGenerator()->create_user();
246
        $u2 = $this->getDataGenerator()->create_user();
247
        $u3 = $this->getDataGenerator()->create_user();
248
        $u4 = $this->getDataGenerator()->create_user();
249
 
250
        $course = $this->getDataGenerator()->create_course();
251
        $sysctx = \context_system::instance();
252
        $c1ctx = \context_course::instance($course->id);
253
 
254
        $this->enable_logging();
255
        $manager = get_log_manager(true);
256
 
257
        $this->setUser($u1);
258
        $e = \logstore_standard\event\unittest_executed::create(['context' => $sysctx]);
259
        $e->trigger();
260
        $this->setUser($u2);
261
        $e = \logstore_standard\event\unittest_executed::create(['context' => $sysctx]);
262
        $e->trigger();
263
        $this->setUser($u3);
264
        $e = \logstore_standard\event\unittest_executed::create(['context' => $sysctx]);
265
        $e->trigger();
266
        $this->setUser($u4);
267
        $e = \logstore_standard\event\unittest_executed::create(['context' => $c1ctx]);
268
        $e->trigger();
269
 
270
        // Check that four records were created.
271
        $this->assertEquals(4, $DB->count_records('logstore_standard_log'));
272
 
273
        $userlist = new \core_privacy\local\request\approved_userlist($sysctx, 'logstore_standard_log', [$u1->id, $u3->id]);
274
        provider::delete_data_for_userlist($userlist);
275
        // We should have a record for u2 and u4.
276
        $this->assertEquals(2, $DB->count_records('logstore_standard_log'));
277
 
278
        $records = $DB->get_records('logstore_standard_log', ['contextid' => $sysctx->id]);
279
        $this->assertCount(1, $records);
280
        $currentrecord = array_shift($records);
281
        $this->assertEquals($u2->id, $currentrecord->userid);
282
    }
283
 
11 efrain 284
    public function test_export_data_for_user(): void {
1 efrain 285
        $admin = \core_user::get_user(2);
286
        $u1 = $this->getDataGenerator()->create_user();
287
        $u2 = $this->getDataGenerator()->create_user();
288
        $u3 = $this->getDataGenerator()->create_user();
289
        $u4 = $this->getDataGenerator()->create_user();
290
        $c1 = $this->getDataGenerator()->create_course();
291
        $cm1 = $this->getDataGenerator()->create_module('url', ['course' => $c1]);
292
        $c2 = $this->getDataGenerator()->create_course();
293
        $cm2 = $this->getDataGenerator()->create_module('url', ['course' => $c2]);
294
        $sysctx = \context_system::instance();
295
        $c1ctx = \context_course::instance($c1->id);
296
        $c2ctx = \context_course::instance($c2->id);
297
        $cm1ctx = \context_module::instance($cm1->cmid);
298
        $cm2ctx = \context_module::instance($cm2->cmid);
299
 
300
        $path = [get_string('privacy:path:logs', 'tool_log'), get_string('pluginname', 'logstore_standard')];
301
        $this->enable_logging();
302
        $manager = get_log_manager(true);
303
 
304
        // User 1 is the author.
305
        $this->setUser($u1);
306
        $e = \logstore_standard\event\unittest_executed::create(['context' => $c1ctx, 'other' => ['i' => 0]]);
307
        $e->trigger();
308
 
309
        // User 2 is related.
310
        $this->setUser(0);
311
        $e = \logstore_standard\event\unittest_executed::create(['context' => $c1ctx, 'relateduserid' => $u2->id,
312
            'other' => ['i' => 1]]);
313
        $e->trigger();
314
 
315
        // Admin user masquerades u3, which is related to u4.
316
        $this->setAdminUser();
317
        \core\session\manager::loginas($u3->id, $sysctx);
318
        $e = \logstore_standard\event\unittest_executed::create(['context' => $c1ctx, 'relateduserid' => $u4->id,
319
            'other' => ['i' => 2]]);
320
        $e->trigger();
321
 
322
        // Confirm data present for u1.
323
        provider::export_user_data(new approved_contextlist($u1, 'logstore_standard', [$c2ctx->id, $c1ctx->id]));
324
        $data = writer::with_context($c2ctx)->get_data($path);
325
        $this->assertEmpty($data);
326
        $data = writer::with_context($c1ctx)->get_data($path);
327
        $this->assertCount(1, $data->logs);
328
        $this->assertEquals(transform::yesno(true), $data->logs[0]['author_of_the_action_was_you']);
329
        $this->assertSame(0, $data->logs[0]['other']['i']);
330
 
331
        // Confirm data present for u2.
332
        writer::reset();
333
        provider::export_user_data(new approved_contextlist($u2, 'logstore_standard', [$c2ctx->id, $c1ctx->id]));
334
        $data = writer::with_context($c2ctx)->get_data($path);
335
        $this->assertEmpty($data);
336
        $data = writer::with_context($c1ctx)->get_data($path);
337
        $this->assertCount(1, $data->logs);
338
        $this->assertEquals(transform::yesno(false), $data->logs[0]['author_of_the_action_was_you']);
339
        $this->assertEquals(transform::yesno(true), $data->logs[0]['related_user_was_you']);
340
        $this->assertSame(1, $data->logs[0]['other']['i']);
341
 
342
        // Confirm data present for u3.
343
        writer::reset();
344
        provider::export_user_data(new approved_contextlist($u3, 'logstore_standard', [$c2ctx->id, $c1ctx->id]));
345
        $data = writer::with_context($c2ctx)->get_data($path);
346
        $this->assertEmpty($data);
347
        $data = writer::with_context($c1ctx)->get_data($path);
348
        $this->assertCount(1, $data->logs);
349
        $this->assertEquals(transform::yesno(true), $data->logs[0]['author_of_the_action_was_you']);
350
        $this->assertEquals(transform::yesno(false), $data->logs[0]['related_user_was_you']);
351
        $this->assertEquals(transform::yesno(true), $data->logs[0]['author_of_the_action_was_masqueraded']);
352
        $this->assertEquals(transform::yesno(false), $data->logs[0]['masquerading_user_was_you']);
353
        $this->assertSame(2, $data->logs[0]['other']['i']);
354
 
355
        // Confirm data present for u4.
356
        writer::reset();
357
        provider::export_user_data(new approved_contextlist($u4, 'logstore_standard', [$c2ctx->id, $c1ctx->id]));
358
        $data = writer::with_context($c2ctx)->get_data($path);
359
        $this->assertEmpty($data);
360
        $data = writer::with_context($c1ctx)->get_data($path);
361
        $this->assertCount(1, $data->logs);
362
        $this->assertEquals(transform::yesno(false), $data->logs[0]['author_of_the_action_was_you']);
363
        $this->assertEquals(transform::yesno(true), $data->logs[0]['related_user_was_you']);
364
        $this->assertEquals(transform::yesno(true), $data->logs[0]['author_of_the_action_was_masqueraded']);
365
        $this->assertEquals(transform::yesno(false), $data->logs[0]['masquerading_user_was_you']);
366
        $this->assertSame(2, $data->logs[0]['other']['i']);
367
 
368
        // Add anonymous events.
369
        $this->setUser($u1);
370
        $e = \logstore_standard\event\unittest_executed::create(['context' => $c2ctx, 'relateduserid' => $u2->id,
371
            'anonymous' => true]);
372
        $e->trigger();
373
        $this->setAdminUser();
374
        \core\session\manager::loginas($u3->id, $sysctx);
375
        $e = \logstore_standard\event\unittest_executed::create(['context' => $c2ctx, 'relateduserid' => $u4->id,
376
            'anonymous' => true]);
377
        $e->trigger();
378
 
379
        // Confirm data present for u1.
380
        provider::export_user_data(new approved_contextlist($u1, 'logstore_standard', [$c2ctx->id]));
381
        $data = writer::with_context($c2ctx)->get_data($path);
382
        $this->assertCount(1, $data->logs);
383
        $this->assertEquals(transform::yesno(true), $data->logs[0]['action_was_done_anonymously']);
384
        $this->assertEquals(transform::yesno(true), $data->logs[0]['author_of_the_action_was_you']);
385
 
386
        // Confirm data present for u2.
387
        writer::reset();
388
        provider::export_user_data(new approved_contextlist($u2, 'logstore_standard', [$c2ctx->id]));
389
        $data = writer::with_context($c2ctx)->get_data($path);
390
        $this->assertCount(1, $data->logs);
391
        $this->assertEquals(transform::yesno(true), $data->logs[0]['action_was_done_anonymously']);
392
        $this->assertArrayNotHasKey('author_of_the_action_was_you', $data->logs[0]);
393
        $this->assertArrayNotHasKey('authorid', $data->logs[0]);
394
        $this->assertEquals(transform::yesno(true), $data->logs[0]['related_user_was_you']);
395
 
396
        // Confirm data present for u3.
397
        writer::reset();
398
        provider::export_user_data(new approved_contextlist($u3, 'logstore_standard', [$c2ctx->id]));
399
        $data = writer::with_context($c2ctx)->get_data($path);
400
        $this->assertCount(1, $data->logs);
401
        $this->assertEquals(transform::yesno(true), $data->logs[0]['action_was_done_anonymously']);
402
        $this->assertEquals(transform::yesno(true), $data->logs[0]['author_of_the_action_was_you']);
403
        $this->assertEquals(transform::yesno(true), $data->logs[0]['author_of_the_action_was_masqueraded']);
404
        $this->assertArrayNotHasKey('masquerading_user_was_you', $data->logs[0]);
405
        $this->assertArrayNotHasKey('masqueradinguserid', $data->logs[0]);
406
 
407
        // Confirm data present for u4.
408
        writer::reset();
409
        provider::export_user_data(new approved_contextlist($u4, 'logstore_standard', [$c2ctx->id]));
410
        $data = writer::with_context($c2ctx)->get_data($path);
411
        $this->assertCount(1, $data->logs);
412
        $this->assertEquals(transform::yesno(true), $data->logs[0]['action_was_done_anonymously']);
413
        $this->assertArrayNotHasKey('author_of_the_action_was_you', $data->logs[0]);
414
        $this->assertArrayNotHasKey('authorid', $data->logs[0]);
415
        $this->assertEquals(transform::yesno(true), $data->logs[0]['related_user_was_you']);
416
        $this->assertEquals(transform::yesno(true), $data->logs[0]['author_of_the_action_was_masqueraded']);
417
        $this->assertArrayNotHasKey('masquerading_user_was_you', $data->logs[0]);
418
        $this->assertArrayNotHasKey('masqueradinguserid', $data->logs[0]);
419
    }
420
 
421
    /**
422
     * Assert the content of a context list.
423
     *
424
     * @param contextlist $contextlist The collection.
425
     * @param array $expected List of expected contexts or IDs.
426
     * @return void
427
     */
428
    protected function assert_contextlist_equals($contextlist, array $expected) {
429
        $expectedids = array_map(function($context) {
430
            if (is_object($context)) {
431
                return $context->id;
432
            }
433
            return $context;
434
        }, $expected);
435
        $contextids = array_map('intval', $contextlist->get_contextids());
436
        sort($contextids);
437
        sort($expectedids);
438
        $this->assertEquals($expectedids, $contextids);
439
    }
440
 
441
    /**
442
     * Enable logging.
443
     *
444
     * @return void
445
     */
446
    protected function enable_logging() {
447
        set_config('enabled_stores', 'logstore_standard', 'tool_log');
448
        set_config('buffersize', 0, 'logstore_standard');
449
        set_config('logguests', 1, 'logstore_standard');
450
    }
451
 
452
    /**
453
     * Get the contextlist for a user.
454
     *
455
     * @param object $user The user.
456
     * @return contextlist
457
     */
458
    protected function get_contextlist_for_user($user) {
459
        $contextlist = new contextlist();
460
        provider::add_contexts_for_userid($contextlist, $user->id);
461
        return $contextlist;
462
    }
463
}