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    tool_log
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
27defined('MOODLE_INTERNAL') || die();
28global $CFG;
29
30use core_privacy\tests\provider_testcase;
31use core_privacy\local\request\contextlist;
32use core_privacy\local\request\approved_contextlist;
33use core_privacy\local\request\transform;
34use core_privacy\local\request\writer;
35use tool_log\privacy\provider;
36
37require_once($CFG->dirroot . '/admin/tool/log/store/standard/tests/fixtures/event.php');
38
39/**
40 * Data provider testcase class.
41 *
42 * We're not testing the full functionality, just that the provider passes the requests
43 * down to at least one of its subplugin. Each subplugin should have tests to cover the
44 * different provider methods in depth.
45 *
46 * @package    tool_log
47 * @category   test
48 * @copyright  2018 Frédéric Massart
49 * @author     Frédéric Massart <fred@branchup.tech>
50 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
51 */
52class tool_log_privacy_testcase extends provider_testcase {
53
54    public function setUp() {
55        $this->resetAfterTest();
56        $this->preventResetByRollback(); // Logging waits till the transaction gets committed.
57    }
58
59    public function test_get_contexts_for_userid() {
60        $admin = \core_user::get_user(2);
61        $u1 = $this->getDataGenerator()->create_user();
62        $c1 = $this->getDataGenerator()->create_course();
63        $c1ctx = context_course::instance($c1->id);
64
65        $this->enable_logging();
66        $manager = get_log_manager(true);
67
68        $this->setUser($u1);
69        $this->assertEmpty(provider::get_contexts_for_userid($u1->id)->get_contextids());
70        $e = \logstore_standard\event\unittest_executed::create(['context' => $c1ctx]);
71        $e->trigger();
72        $this->assertEquals($c1ctx->id, provider::get_contexts_for_userid($u1->id)->get_contextids()[0]);
73    }
74
75    public function test_delete_data_for_user() {
76        global $DB;
77        $u1 = $this->getDataGenerator()->create_user();
78        $u2 = $this->getDataGenerator()->create_user();
79        $c1 = $this->getDataGenerator()->create_course();
80        $c1ctx = context_course::instance($c1->id);
81
82        $this->enable_logging();
83        $manager = get_log_manager(true);
84
85        // User 1 is the author.
86        $this->setUser($u1);
87        $e = \logstore_standard\event\unittest_executed::create(['context' => $c1ctx]);
88        $e->trigger();
89        $e = \logstore_standard\event\unittest_executed::create(['context' => $c1ctx]);
90        $e->trigger();
91
92        // User 2 is the author.
93        $this->setUser($u2);
94        $e = \logstore_standard\event\unittest_executed::create(['context' => $c1ctx]);
95        $e->trigger();
96
97        // Confirm data present.
98        $this->assertTrue($DB->record_exists('logstore_standard_log', ['userid' => $u1->id, 'contextid' => $c1ctx->id]));
99        $this->assertEquals(2, $DB->count_records('logstore_standard_log', ['userid' => $u1->id]));
100        $this->assertEquals(1, $DB->count_records('logstore_standard_log', ['userid' => $u2->id]));
101
102        // Delete all the things!
103        provider::delete_data_for_user(new approved_contextlist($u1, 'logstore_standard', [$c1ctx->id]));
104        $this->assertFalse($DB->record_exists('logstore_standard_log', ['userid' => $u1->id, 'contextid' => $c1ctx->id]));
105        $this->assertEquals(0, $DB->count_records('logstore_standard_log', ['userid' => $u1->id]));
106        $this->assertEquals(1, $DB->count_records('logstore_standard_log', ['userid' => $u2->id]));
107    }
108
109    public function test_delete_data_for_all_users_in_context() {
110        global $DB;
111        $u1 = $this->getDataGenerator()->create_user();
112        $u2 = $this->getDataGenerator()->create_user();
113        $c1 = $this->getDataGenerator()->create_course();
114        $c1ctx = context_course::instance($c1->id);
115
116        $this->enable_logging();
117        $manager = get_log_manager(true);
118
119        // User 1 is the author.
120        $this->setUser($u1);
121        $e = \logstore_standard\event\unittest_executed::create(['context' => $c1ctx]);
122        $e->trigger();
123        $e = \logstore_standard\event\unittest_executed::create(['context' => $c1ctx]);
124        $e->trigger();
125
126        // User 2 is the author.
127        $this->setUser($u2);
128        $e = \logstore_standard\event\unittest_executed::create(['context' => $c1ctx]);
129        $e->trigger();
130
131        // Confirm data present.
132        $this->assertTrue($DB->record_exists('logstore_standard_log', ['contextid' => $c1ctx->id]));
133        $this->assertEquals(2, $DB->count_records('logstore_standard_log', ['userid' => $u1->id]));
134        $this->assertEquals(1, $DB->count_records('logstore_standard_log', ['userid' => $u2->id]));
135
136        // Delete all the things!
137        provider::delete_data_for_all_users_in_context($c1ctx);
138        $this->assertFalse($DB->record_exists('logstore_standard_log', ['contextid' => $c1ctx->id]));
139        $this->assertEquals(0, $DB->count_records('logstore_standard_log', ['userid' => $u1->id]));
140        $this->assertEquals(0, $DB->count_records('logstore_standard_log', ['userid' => $u2->id]));
141    }
142
143    public function test_export_data_for_user() {
144        $admin = \core_user::get_user(2);
145        $u1 = $this->getDataGenerator()->create_user();
146        $c1 = $this->getDataGenerator()->create_course();
147        $c1ctx = context_course::instance($c1->id);
148
149        $path = [get_string('privacy:path:logs', 'tool_log'), get_string('pluginname', 'logstore_standard')];
150        $this->enable_logging();
151        $manager = get_log_manager(true);
152
153        // User 1 is the author.
154        $this->setUser($u1);
155        $e = \logstore_standard\event\unittest_executed::create(['context' => $c1ctx, 'other' => ['i' => 123]]);
156        $e->trigger();
157
158        // Confirm data present for u1.
159        provider::export_user_data(new approved_contextlist($u1, 'tool_log', [$c1ctx->id]));
160        $data = writer::with_context($c1ctx)->get_data($path);
161        $this->assertCount(1, $data->logs);
162        $this->assertEquals(transform::yesno(true), $data->logs[0]['author_of_the_action_was_you']);
163        $this->assertSame(123, $data->logs[0]['other']['i']);
164    }
165
166    /**
167     * Enable logging.
168     *
169     * @return void
170     */
171    protected function enable_logging() {
172        set_config('enabled_stores', 'logstore_standard', 'tool_log');
173        set_config('buffersize', 0, 'logstore_standard');
174        set_config('logguests', 1, 'logstore_standard');
175    }
176}
177