2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * Data provider tests.
20 * @package logstore_legacy
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
27 defined('MOODLE_INTERNAL') || die();
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');
41 * Data provider testcase class.
43 * @package logstore_legacy
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
49 class logstore_legacy_privacy_testcase extends provider_testcase {
51 public function setUp() {
52 $this->resetAfterTest();
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.
70 $this->assert_contextlist_equals($this->get_contextlist_for_user($u1), []);
71 $e = unittest_executed::create(['context' => $cm1ctx, 'other' => ['sample' => 1]]);
73 $this->assert_contextlist_equals($this->get_contextlist_for_user($u1), [$cm1ctx]);
75 // User 2 is the author.
77 $this->assert_contextlist_equals($this->get_contextlist_for_user($u2), []);
78 $e = unittest_executed::create(['context' => $cm1ctx, 'other' => ['sample' => 2]]);
80 $this->assert_contextlist_equals($this->get_contextlist_for_user($u2), [$cm1ctx]);
82 // User 3 is the author.
84 $this->assert_contextlist_equals($this->get_contextlist_for_user($u3), []);
85 $e = unittest_executed::create(['context' => $sysctx, 'other' => ['sample' => 3]]);
87 $this->assert_contextlist_equals($this->get_contextlist_for_user($u3), [$sysctx]);
90 public function test_delete_data_for_user() {
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.
109 $e = unittest_executed::create(['context' => $cm1ctx, 'other' => ['sample' => 1]]);
111 $e = unittest_executed::create(['context' => $cm1ctx, 'other' => ['sample' => 2]]);
113 $e = unittest_executed::create(['context' => $c1ctx, 'other' => ['sample' => 3]]);
115 $e = unittest_executed::create(['context' => $sysctx, 'other' => ['sample' => 4]]);
118 // User 2 is the author.
120 $e = unittest_executed::create(['context' => $cm1ctx, 'other' => ['sample' => 5]]);
122 $e = unittest_executed::create(['context' => $c1ctx, 'other' => ['sample' => 6]]);
124 $e = unittest_executed::create(['context' => $sysctx, 'other' => ['sample' => 7]]);
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]));
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]));
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]));
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]));
167 public function test_delete_data_for_all_users_in_context() {
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.
186 $e = unittest_executed::create(['context' => $cm1ctx, 'other' => ['sample' => 1]]);
188 $e = unittest_executed::create(['context' => $cm1ctx, 'other' => ['sample' => 2]]);
190 $e = unittest_executed::create(['context' => $c1ctx, 'other' => ['sample' => 3]]);
192 $e = unittest_executed::create(['context' => $sysctx, 'other' => ['sample' => 4]]);
195 // User 2 is the author.
197 $e = unittest_executed::create(['context' => $cm1ctx, 'other' => ['sample' => 5]]);
199 $e = unittest_executed::create(['context' => $c1ctx, 'other' => ['sample' => 6]]);
201 $e = unittest_executed::create(['context' => $sysctx, 'other' => ['sample' => 7]]);
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]));
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]));
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]));
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]));
244 public function test_export_data_for_user() {
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.
264 $e = unittest_executed::create(['context' => $cm1ctx, 'other' => ['sample' => 1]]);
266 $e = unittest_executed::create(['context' => $cm1ctx, 'other' => ['sample' => 2]]);
268 $e = unittest_executed::create(['context' => $c1ctx, 'other' => ['sample' => 3]]);
270 $e = unittest_executed::create(['context' => $sysctx, 'other' => ['sample' => 4]]);
273 // User 2 is the author.
275 $e = unittest_executed::create(['context' => $cm1ctx, 'other' => ['sample' => 5]]);
277 $e = unittest_executed::create(['context' => $c1ctx, 'other' => ['sample' => 6]]);
279 $e = unittest_executed::create(['context' => $sysctx, 'other' => ['sample' => 7]]);
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);
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);
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);
303 * Assert the content of a context list.
305 * @param contextlist $contextlist The collection.
306 * @param array $expected List of expected contexts or IDs.
309 protected function assert_contextlist_equals($contextlist, array $expected) {
310 $expectedids = array_map(function($context) {
311 if (is_object($context)) {
316 $contextids = array_map('intval', $contextlist->get_contextids());
319 $this->assertEquals($expectedids, $contextids);
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);
334 * Get the contextlist for a user.
336 * @param object $user The user.
337 * @return contextlist
339 protected function get_contextlist_for_user($user) {
340 $contextlist = new contextlist();
341 provider::add_contexts_for_userid($contextlist, $user->id);