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 * Privacy tests for enrol_flatfile.
20 * @package enrol_flatfile
22 * @copyright 2018 Jake Dallimore <jrhdallimore@gmail.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') || die();
28 use core_privacy\local\metadata\collection;
29 use core_privacy\tests\provider_testcase;
30 use core_privacy\local\request\approved_contextlist;
31 use core_privacy\local\request\writer;
32 use enrol_flatfile\privacy\provider;
35 * Privacy tests for enrol_flatfile.
37 * @copyright 2018 Jake Dallimore <jrhdallimore@gmail.com>
38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40 class enrol_flatfile_privacy_testcase extends provider_testcase {
42 /** @var \stdClass $user1 a test user.*/
45 /** @var \stdClass $user2 a test user.*/
48 /** @var \context $coursecontext1 a course context.*/
49 protected $coursecontext1;
51 /** @var \context $coursecontext2 a course context.*/
52 protected $coursecontext2;
54 /** @var \context $coursecontext3 a course context.*/
55 protected $coursecontext3;
58 * Called before every test.
60 public function setUp() {
61 $this->resetAfterTest(true);
65 * Verify that get_metadata returns the database table mapping.
67 public function test_get_metadata() {
68 $collection = new collection('enrol_flatfile');
69 $collection = provider::get_metadata($collection);
70 $collectiondata = $collection->get_collection();
71 $this->assertNotEmpty($collectiondata);
72 $this->assertInstanceOf(\core_privacy\local\metadata\types\database_table::class, $collectiondata[0]);
76 * Verify that the relevant course contexts are returned for users with pending enrolment records.
78 public function test_get_contexts_for_user() {
80 // Create, via flatfile syncing, the future enrolments entries in the enrol_flatfile table.
81 $this->create_future_enrolments();
83 $this->assertEquals(3, $DB->count_records('enrol_flatfile'));
85 // We expect to see 2 entries for user1, in course1 and course3.
86 $contextlist = provider::get_contexts_for_userid($this->user1->id);
87 $this->assertEquals(2, $contextlist->count());
88 $contextids = $contextlist->get_contextids();
89 $this->assertContains($this->coursecontext1->id, $contextids);
90 $this->assertContains($this->coursecontext3->id, $contextids);
92 // And 1 for user2 on course2.
93 $contextlist = provider::get_contexts_for_userid($this->user2->id);
94 $this->assertEquals(1, $contextlist->count());
95 $contextids = $contextlist->get_contextids();
96 $this->assertContains($this->coursecontext2->id, $contextids);
100 * Verify the export includes any future enrolment records for the user.
102 public function test_export_user_data() {
103 // Create, via flatfile syncing, the future enrolments entries in the enrol_flatfile table.
104 $this->create_future_enrolments();
106 // Get contexts containing user data.
107 $contextlist = provider::get_contexts_for_userid($this->user1->id);
108 $this->assertEquals(2, $contextlist->count());
110 $approvedcontextlist = new approved_contextlist(
113 $contextlist->get_contextids()
116 // Export for the approved contexts.
117 provider::export_user_data($approvedcontextlist);
119 // Verify we see one future course enrolment in course1, and one in course3.
120 $subcontext = \core_enrol\privacy\provider::get_subcontext([get_string('pluginname', 'enrol_flatfile')]);
122 $writer = writer::with_context($this->coursecontext1);
123 $this->assertNotEmpty($writer->get_data($subcontext));
125 $writer = writer::with_context($this->coursecontext3);
126 $this->assertNotEmpty($writer->get_data($subcontext));
128 // Verify we have nothing in course 2 for this user.
129 $writer = writer::with_context($this->coursecontext2);
130 $this->assertEmpty($writer->get_data($subcontext));
134 * Verify export will limit any future enrolment records to only those contextids provided.
136 public function test_export_user_data_restricted_context_subset() {
137 // Create, via flatfile syncing, the future enrolments entries in the enrol_flatfile table.
138 $this->create_future_enrolments();
140 // Now, limit the export scope to just course1's context and verify only that data is seen in any export.
141 $subsetapprovedcontextlist = new approved_contextlist(
144 [$this->coursecontext1->id]
147 // Export for the approved contexts.
148 provider::export_user_data($subsetapprovedcontextlist);
150 // Verify we see one future course enrolment in course1 only.
151 $subcontext = \core_enrol\privacy\provider::get_subcontext([get_string('pluginname', 'enrol_flatfile')]);
153 $writer = writer::with_context($this->coursecontext1);
154 $this->assertNotEmpty($writer->get_data($subcontext));
156 // And nothing in the course3 context.
157 $writer = writer::with_context($this->coursecontext3);
158 $this->assertEmpty($writer->get_data($subcontext));
162 * Verify that records can be deleted by context.
164 public function test_delete_data_for_all_users_in_context() {
166 // Create, via flatfile syncing, the future enrolments entries in the enrol_flatfile table.
167 $this->create_future_enrolments();
169 // Verify we have 1 future enrolments for course 1.
170 $this->assertEquals(1, $DB->count_records('enrol_flatfile', ['courseid' => $this->coursecontext1->instanceid]));
172 // Now, run delete by context and confirm that record is removed.
173 provider::delete_data_for_all_users_in_context($this->coursecontext1);
174 $this->assertEquals(0, $DB->count_records('enrol_flatfile', ['courseid' => $this->coursecontext1->instanceid]));
177 public function test_delete_data_for_user() {
179 // Create, via flatfile syncing, the future enrolments entries in the enrol_flatfile table.
180 $this->create_future_enrolments();
182 // Verify we have 2 future enrolments for course 1 and course 3.
183 $contextlist = provider::get_contexts_for_userid($this->user1->id);
184 $this->assertEquals(2, $contextlist->count());
185 $contextids = $contextlist->get_contextids();
186 $this->assertContains($this->coursecontext1->id, $contextids);
187 $this->assertContains($this->coursecontext3->id, $contextids);
189 $approvedcontextlist = new approved_contextlist(
195 // Now, run delete for user and confirm that both records are removed.
196 provider::delete_data_for_user($approvedcontextlist);
197 $contextlist = provider::get_contexts_for_userid($this->user1->id);
198 $this->assertEquals(0, $contextlist->count());
199 $this->assertEquals(0, $DB->count_records('enrol_flatfile', ['userid' => $this->user1->id]));
203 * Helper to sync a file and create the enrol_flatfile DB entries, for use with the get, export and delete tests.
205 protected function create_future_enrolments() {
207 $this->user1 = $this->getDataGenerator()->create_user(['idnumber' => 'u1']);
208 $this->user2 = $this->getDataGenerator()->create_user(['idnumber' => 'u2']);
210 $course1 = $this->getDataGenerator()->create_course(['idnumber' => 'c1']);
211 $course2 = $this->getDataGenerator()->create_course(['idnumber' => 'c2']);
212 $course3 = $this->getDataGenerator()->create_course(['idnumber' => 'c3']);
213 $this->coursecontext1 = context_course::instance($course1->id);
214 $this->coursecontext2 = context_course::instance($course2->id);
215 $this->coursecontext3 = context_course::instance($course3->id);
218 $future = $now + 60 * 60 * 5;
219 $farfuture = $now + 60 * 60 * 24 * 5;
221 $file = "$CFG->dataroot/enrol.txt";
222 $data = "add,student,u1,c1,$future,0
223 add,student,u2,c2,$future,0
224 add,student,u1,c3,$future,$farfuture";
225 file_put_contents($file, $data);
227 $trace = new null_progress_trace();
228 $this->enable_plugin();
229 $flatfileplugin = enrol_get_plugin('flatfile');
230 $flatfileplugin->set_config('location', $file);
231 $flatfileplugin->sync($trace);
235 * Enables the flatfile plugin for testing.
237 protected function enable_plugin() {
238 $enabled = enrol_get_plugins(true);
239 $enabled['flatfile'] = true;
240 $enabled = array_keys($enabled);
241 set_config('enrol_plugins_enabled', implode(',', $enabled));