on-demand release 3.6dev+
[moodle.git] / admin / tool / log / store / legacy / tests / privacy_test.php
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/>.
17 /**
18  * Data provider tests.
19  *
20  * @package    logstore_legacy
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  */
27 defined('MOODLE_INTERNAL') || die();
28 global $CFG;
30 use core_privacy\tests\provider_testcase;
31 use core_privacy\local\request\contextlist;
32 use core_privacy\local\request\approved_contextlist;
33 use core_privacy\local\request\transform;
34 use core_privacy\local\request\writer;
35 use logstore_legacy\privacy\provider;
36 use logstore_legacy\event\unittest_executed;
38 require_once(__DIR__ . '/fixtures/event.php');
40 /**
41  * Data provider testcase class.
42  *
43  * @package    logstore_legacy
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  */
49 class logstore_legacy_privacy_testcase extends provider_testcase {
51     public function setUp() {
52         $this->resetAfterTest();
53     }
55     public function test_get_contexts_for_userid() {
56         $u1 = $this->getDataGenerator()->create_user();
57         $u2 = $this->getDataGenerator()->create_user();
58         $u3 = $this->getDataGenerator()->create_user();
59         $c1 = $this->getDataGenerator()->create_course();
60         $cm1 = $this->getDataGenerator()->create_module('url', ['course' => $c1]);
61         $sysctx = context_system::instance();
62         $c1ctx = context_course::instance($c1->id);
63         $cm1ctx = context_module::instance($cm1->cmid);
65         $this->enable_logging();
66         $manager = get_log_manager(true);
68         // User 1 is the author.
69         $this->setUser($u1);
70         $this->assert_contextlist_equals($this->get_contextlist_for_user($u1), []);
71         $e = unittest_executed::create(['context' => $cm1ctx, 'other' => ['sample' => 1]]);
72         $e->trigger();
73         $this->assert_contextlist_equals($this->get_contextlist_for_user($u1), [$cm1ctx]);
75         // User 2 is the author.
76         $this->setUser($u2);
77         $this->assert_contextlist_equals($this->get_contextlist_for_user($u2), []);
78         $e = unittest_executed::create(['context' => $cm1ctx, 'other' => ['sample' => 2]]);
79         $e->trigger();
80         $this->assert_contextlist_equals($this->get_contextlist_for_user($u2), [$cm1ctx]);
82         // User 3 is the author.
83         $this->setUser($u3);
84         $this->assert_contextlist_equals($this->get_contextlist_for_user($u3), []);
85         $e = unittest_executed::create(['context' => $sysctx, 'other' => ['sample' => 3]]);
86         $e->trigger();
87         $this->assert_contextlist_equals($this->get_contextlist_for_user($u3), [$sysctx]);
88     }
90     public function test_delete_data_for_user() {
91         global $DB;
93         $u1 = $this->getDataGenerator()->create_user();
94         $u2 = $this->getDataGenerator()->create_user();
95         $u3 = $this->getDataGenerator()->create_user();
96         $c1 = $this->getDataGenerator()->create_course();
97         $c2 = $this->getDataGenerator()->create_course();
98         $cm1 = $this->getDataGenerator()->create_module('url', ['course' => $c1]);
99         $sysctx = context_system::instance();
100         $c1ctx = context_course::instance($c1->id);
101         $c2ctx = context_course::instance($c2->id);
102         $cm1ctx = context_module::instance($cm1->cmid);
104         $this->enable_logging();
105         $manager = get_log_manager(true);
107         // User 1 is the author.
108         $this->setUser($u1);
109         $e = unittest_executed::create(['context' => $cm1ctx, 'other' => ['sample' => 1]]);
110         $e->trigger();
111         $e = unittest_executed::create(['context' => $cm1ctx, 'other' => ['sample' => 2]]);
112         $e->trigger();
113         $e = unittest_executed::create(['context' => $c1ctx, 'other' => ['sample' => 3]]);
114         $e->trigger();
115         $e = unittest_executed::create(['context' => $sysctx, 'other' => ['sample' => 4]]);
116         $e->trigger();
118         // User 2 is the author.
119         $this->setUser($u2);
120         $e = unittest_executed::create(['context' => $cm1ctx, 'other' => ['sample' => 5]]);
121         $e->trigger();
122         $e = unittest_executed::create(['context' => $c1ctx, 'other' => ['sample' => 6]]);
123         $e->trigger();
124         $e = unittest_executed::create(['context' => $sysctx, 'other' => ['sample' => 7]]);
125         $e->trigger();
127         // Assert what we have.
128         $this->assertTrue($DB->record_exists('log', ['userid' => $u1->id, 'cmid' => $cm1->cmid, 'course' => $c1->id]));
129         $this->assertTrue($DB->record_exists('log', ['userid' => $u1->id, 'cmid' => 0, 'course' => $c1->id]));
130         $this->assertTrue($DB->record_exists('log', ['userid' => $u1->id, 'cmid' => 0, 'course' => 0]));
131         $this->assertEquals(4, $DB->count_records('log', ['userid' => $u1->id]));
132         $this->assertEquals(3, $DB->count_records('log', ['userid' => $u2->id]));
134         // Delete other context.
135         provider::delete_data_for_user(new approved_contextlist($u1, 'logstore_legacy', [$c2ctx->id]));
136         $this->assertTrue($DB->record_exists('log', ['userid' => $u1->id, 'cmid' => $cm1->cmid, 'course' => $c1->id]));
137         $this->assertTrue($DB->record_exists('log', ['userid' => $u1->id, 'cmid' => 0, 'course' => $c1->id]));
138         $this->assertTrue($DB->record_exists('log', ['userid' => $u1->id, 'cmid' => 0, 'course' => 0]));
139         $this->assertEquals(4, $DB->count_records('log', ['userid' => $u1->id]));
140         $this->assertEquals(3, $DB->count_records('log', ['userid' => $u2->id]));
142         // Delete system.
143         provider::delete_data_for_user(new approved_contextlist($u1, 'logstore_legacy', [$sysctx->id]));
144         $this->assertTrue($DB->record_exists('log', ['userid' => $u1->id, 'cmid' => $cm1->cmid, 'course' => $c1->id]));
145         $this->assertTrue($DB->record_exists('log', ['userid' => $u1->id, 'cmid' => 0, 'course' => $c1->id]));
146         $this->assertFalse($DB->record_exists('log', ['userid' => $u1->id, 'cmid' => 0, 'course' => 0]));
147         $this->assertEquals(3, $DB->count_records('log', ['userid' => $u1->id]));
148         $this->assertEquals(3, $DB->count_records('log', ['userid' => $u2->id]));
150         // Delete course.
151         provider::delete_data_for_user(new approved_contextlist($u1, 'logstore_legacy', [$c1ctx->id]));
152         $this->assertTrue($DB->record_exists('log', ['userid' => $u1->id, 'cmid' => $cm1->cmid, 'course' => $c1->id]));
153         $this->assertFalse($DB->record_exists('log', ['userid' => $u1->id, 'cmid' => 0, 'course' => $c1->id]));
154         $this->assertFalse($DB->record_exists('log', ['userid' => $u1->id, 'cmid' => 0, 'course' => 0]));
155         $this->assertEquals(2, $DB->count_records('log', ['userid' => $u1->id]));
156         $this->assertEquals(3, $DB->count_records('log', ['userid' => $u2->id]));
158         // Delete course.
159         provider::delete_data_for_user(new approved_contextlist($u1, 'logstore_legacy', [$cm1ctx->id]));
160         $this->assertFalse($DB->record_exists('log', ['userid' => $u1->id, 'cmid' => $cm1->cmid, 'course' => $c1->id]));
161         $this->assertFalse($DB->record_exists('log', ['userid' => $u1->id, 'cmid' => 0, 'course' => $c1->id]));
162         $this->assertFalse($DB->record_exists('log', ['userid' => $u1->id, 'cmid' => 0, 'course' => 0]));
163         $this->assertEquals(0, $DB->count_records('log', ['userid' => $u1->id]));
164         $this->assertEquals(3, $DB->count_records('log', ['userid' => $u2->id]));
165     }
167     public function test_delete_data_for_all_users_in_context() {
168         global $DB;
170         $u1 = $this->getDataGenerator()->create_user();
171         $u2 = $this->getDataGenerator()->create_user();
172         $u3 = $this->getDataGenerator()->create_user();
173         $c1 = $this->getDataGenerator()->create_course();
174         $c2 = $this->getDataGenerator()->create_course();
175         $cm1 = $this->getDataGenerator()->create_module('url', ['course' => $c1]);
176         $sysctx = context_system::instance();
177         $c1ctx = context_course::instance($c1->id);
178         $c2ctx = context_course::instance($c2->id);
179         $cm1ctx = context_module::instance($cm1->cmid);
181         $this->enable_logging();
182         $manager = get_log_manager(true);
184         // User 1 is the author.
185         $this->setUser($u1);
186         $e = unittest_executed::create(['context' => $cm1ctx, 'other' => ['sample' => 1]]);
187         $e->trigger();
188         $e = unittest_executed::create(['context' => $cm1ctx, 'other' => ['sample' => 2]]);
189         $e->trigger();
190         $e = unittest_executed::create(['context' => $c1ctx, 'other' => ['sample' => 3]]);
191         $e->trigger();
192         $e = unittest_executed::create(['context' => $sysctx, 'other' => ['sample' => 4]]);
193         $e->trigger();
195         // User 2 is the author.
196         $this->setUser($u2);
197         $e = unittest_executed::create(['context' => $cm1ctx, 'other' => ['sample' => 5]]);
198         $e->trigger();
199         $e = unittest_executed::create(['context' => $c1ctx, 'other' => ['sample' => 6]]);
200         $e->trigger();
201         $e = unittest_executed::create(['context' => $sysctx, 'other' => ['sample' => 7]]);
202         $e->trigger();
204         // Assert what we have.
205         $this->assertTrue($DB->record_exists('log', ['cmid' => $cm1->cmid, 'course' => $c1->id]));
206         $this->assertTrue($DB->record_exists('log', ['cmid' => 0, 'course' => $c1->id]));
207         $this->assertTrue($DB->record_exists('log', ['cmid' => 0, 'course' => 0]));
208         $this->assertEquals(4, $DB->count_records('log', ['userid' => $u1->id]));
209         $this->assertEquals(3, $DB->count_records('log', ['userid' => $u2->id]));
211         // Delete other context.
212         provider::delete_data_for_all_users_in_context($c2ctx);
213         $this->assertTrue($DB->record_exists('log', ['cmid' => $cm1->cmid, 'course' => $c1->id]));
214         $this->assertTrue($DB->record_exists('log', ['cmid' => 0, 'course' => $c1->id]));
215         $this->assertTrue($DB->record_exists('log', ['cmid' => 0, 'course' => 0]));
216         $this->assertEquals(4, $DB->count_records('log', ['userid' => $u1->id]));
217         $this->assertEquals(3, $DB->count_records('log', ['userid' => $u2->id]));
219         // Delete system.
220         provider::delete_data_for_all_users_in_context($sysctx);
221         $this->assertTrue($DB->record_exists('log', ['cmid' => $cm1->cmid, 'course' => $c1->id]));
222         $this->assertTrue($DB->record_exists('log', ['cmid' => 0, 'course' => $c1->id]));
223         $this->assertFalse($DB->record_exists('log', ['cmid' => 0, 'course' => 0]));
224         $this->assertEquals(3, $DB->count_records('log', ['userid' => $u1->id]));
225         $this->assertEquals(2, $DB->count_records('log', ['userid' => $u2->id]));
227         // Delete course.
228         provider::delete_data_for_all_users_in_context($c1ctx);
229         $this->assertTrue($DB->record_exists('log', ['cmid' => $cm1->cmid, 'course' => $c1->id]));
230         $this->assertFalse($DB->record_exists('log', ['cmid' => 0, 'course' => $c1->id]));
231         $this->assertFalse($DB->record_exists('log', ['cmid' => 0, 'course' => 0]));
232         $this->assertEquals(2, $DB->count_records('log', ['userid' => $u1->id]));
233         $this->assertEquals(1, $DB->count_records('log', ['userid' => $u2->id]));
235         // Delete course.
236         provider::delete_data_for_all_users_in_context($cm1ctx);
237         $this->assertFalse($DB->record_exists('log', ['cmid' => $cm1->cmid, 'course' => $c1->id]));
238         $this->assertFalse($DB->record_exists('log', ['cmid' => 0, 'course' => $c1->id]));
239         $this->assertFalse($DB->record_exists('log', ['cmid' => 0, 'course' => 0]));
240         $this->assertEquals(0, $DB->count_records('log', ['userid' => $u1->id]));
241         $this->assertEquals(0, $DB->count_records('log', ['userid' => $u2->id]));
242     }
244     public function test_export_data_for_user() {
245         global $DB;
247         $u1 = $this->getDataGenerator()->create_user();
248         $u2 = $this->getDataGenerator()->create_user();
249         $u3 = $this->getDataGenerator()->create_user();
250         $c1 = $this->getDataGenerator()->create_course();
251         $c2 = $this->getDataGenerator()->create_course();
252         $cm1 = $this->getDataGenerator()->create_module('url', ['course' => $c1]);
253         $sysctx = context_system::instance();
254         $c1ctx = context_course::instance($c1->id);
255         $c2ctx = context_course::instance($c2->id);
256         $cm1ctx = context_module::instance($cm1->cmid);
258         $this->enable_logging();
259         $manager = get_log_manager(true);
260         $path = [get_string('privacy:path:logs', 'tool_log'), get_string('pluginname', 'logstore_legacy')];
262         // User 1 is the author.
263         $this->setUser($u1);
264         $e = unittest_executed::create(['context' => $cm1ctx, 'other' => ['sample' => 1]]);
265         $e->trigger();
266         $e = unittest_executed::create(['context' => $cm1ctx, 'other' => ['sample' => 2]]);
267         $e->trigger();
268         $e = unittest_executed::create(['context' => $c1ctx, 'other' => ['sample' => 3]]);
269         $e->trigger();
270         $e = unittest_executed::create(['context' => $sysctx, 'other' => ['sample' => 4]]);
271         $e->trigger();
273         // User 2 is the author.
274         $this->setUser($u2);
275         $e = unittest_executed::create(['context' => $cm1ctx, 'other' => ['sample' => 5]]);
276         $e->trigger();
277         $e = unittest_executed::create(['context' => $c1ctx, 'other' => ['sample' => 6]]);
278         $e->trigger();
279         $e = unittest_executed::create(['context' => $sysctx, 'other' => ['sample' => 7]]);
280         $e->trigger();
282         // Test export.
283         provider::export_user_data(new approved_contextlist($u1, 'logstore_legacy', [$cm1ctx->id]));
284         $data = writer::with_context($c1ctx)->get_data($path);
285         $this->assertEmpty($data);
286         $data = writer::with_context($cm1ctx)->get_data($path);
287         $this->assertCount(2, $data->logs);
289         writer::reset();
290         provider::export_user_data(new approved_contextlist($u1, 'logstore_legacy', [$c1ctx->id]));
291         $data = writer::with_context($cm1ctx)->get_data($path);
292         $this->assertEmpty($data);
293         $data = writer::with_context($c1ctx)->get_data($path);
294         $this->assertCount(1, $data->logs);
296         writer::reset();
297         provider::export_user_data(new approved_contextlist($u1, 'logstore_legacy', [$sysctx->id]));
298         $data = writer::with_context($sysctx)->get_data($path);
299         $this->assertCount(1, $data->logs);
300     }
302     /**
303      * Assert the content of a context list.
304      *
305      * @param contextlist $contextlist The collection.
306      * @param array $expected List of expected contexts or IDs.
307      * @return void
308      */
309     protected function assert_contextlist_equals($contextlist, array $expected) {
310         $expectedids = array_map(function($context) {
311             if (is_object($context)) {
312                 return $context->id;
313             }
314             return $context;
315         }, $expected);
316         $contextids = array_map('intval', $contextlist->get_contextids());
317         sort($contextids);
318         sort($expectedids);
319         $this->assertEquals($expectedids, $contextids);
320     }
322     /**
323      * Enable logging.
324      *
325      * @return void
326      */
327     protected function enable_logging() {
328         set_config('enabled_stores', 'logstore_legacy', 'tool_log');
329         set_config('loglegacy', 1, 'logstore_legacy');
330         get_log_manager(true);
331     }
333     /**
334      * Get the contextlist for a user.
335      *
336      * @param object $user The user.
337      * @return contextlist
338      */
339     protected function get_contextlist_for_user($user) {
340         $contextlist = new contextlist();
341         provider::add_contexts_for_userid($contextlist, $user->id);
342         return $contextlist;
343     }