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 $writer = writer::with_context($this->coursecontext1);
121 $this->assertNotEmpty($writer->get_data([get_string('flatfileenrolments', 'enrol_flatfile')]));
123 $writer = writer::with_context($this->coursecontext3);
124 $this->assertNotEmpty($writer->get_data([get_string('flatfileenrolments', 'enrol_flatfile')]));
126 // Verify we have nothing in course 2 for this user.
127 $writer = writer::with_context($this->coursecontext2);
128 $this->assertEmpty($writer->get_data([get_string('flatfileenrolments', 'enrol_flatfile')]));
132 * Verify export will limit any future enrolment records to only those contextids provided.
134 public function test_export_user_data_restricted_context_subset() {
135 // Create, via flatfile syncing, the future enrolments entries in the enrol_flatfile table.
136 $this->create_future_enrolments();
138 // Now, limit the export scope to just course1's context and verify only that data is seen in any export.
139 $subsetapprovedcontextlist = new approved_contextlist(
142 [$this->coursecontext1->id]
145 // Export for the approved contexts.
146 provider::export_user_data($subsetapprovedcontextlist);
148 // Verify we see one future course enrolment in course1 only.
149 $writer = writer::with_context($this->coursecontext1);
150 $this->assertNotEmpty($writer->get_data([get_string('flatfileenrolments', 'enrol_flatfile')]));
152 // And nothing in the course3 context.
153 $writer = writer::with_context($this->coursecontext3);
154 $this->assertEmpty($writer->get_data([get_string('flatfileenrolments', 'enrol_flatfile')]));
158 * Verify that records can be deleted by context.
160 public function test_delete_data_for_all_users_in_context() {
162 // Create, via flatfile syncing, the future enrolments entries in the enrol_flatfile table.
163 $this->create_future_enrolments();
165 // Verify we have 1 future enrolments for course 1.
166 $this->assertEquals(1, $DB->count_records('enrol_flatfile', ['courseid' => $this->coursecontext1->instanceid]));
168 // Now, run delete by context and confirm that record is removed.
169 provider::delete_data_for_all_users_in_context($this->coursecontext1);
170 $this->assertEquals(0, $DB->count_records('enrol_flatfile', ['courseid' => $this->coursecontext1->instanceid]));
173 public function test_delete_data_for_user() {
175 // Create, via flatfile syncing, the future enrolments entries in the enrol_flatfile table.
176 $this->create_future_enrolments();
178 // Verify we have 2 future enrolments for course 1 and course 3.
179 $contextlist = provider::get_contexts_for_userid($this->user1->id);
180 $this->assertEquals(2, $contextlist->count());
181 $contextids = $contextlist->get_contextids();
182 $this->assertContains($this->coursecontext1->id, $contextids);
183 $this->assertContains($this->coursecontext3->id, $contextids);
185 $approvedcontextlist = new approved_contextlist(
191 // Now, run delete for user and confirm that both records are removed.
192 provider::delete_data_for_user($approvedcontextlist);
193 $contextlist = provider::get_contexts_for_userid($this->user1->id);
194 $this->assertEquals(0, $contextlist->count());
195 $this->assertEquals(0, $DB->count_records('enrol_flatfile', ['userid' => $this->user1->id]));
199 * Helper to sync a file and create the enrol_flatfile DB entries, for use with the get, export and delete tests.
201 protected function create_future_enrolments() {
203 $this->user1 = $this->getDataGenerator()->create_user(['idnumber' => 'u1']);
204 $this->user2 = $this->getDataGenerator()->create_user(['idnumber' => 'u2']);
206 $course1 = $this->getDataGenerator()->create_course(['idnumber' => 'c1']);
207 $course2 = $this->getDataGenerator()->create_course(['idnumber' => 'c2']);
208 $course3 = $this->getDataGenerator()->create_course(['idnumber' => 'c3']);
209 $this->coursecontext1 = context_course::instance($course1->id);
210 $this->coursecontext2 = context_course::instance($course2->id);
211 $this->coursecontext3 = context_course::instance($course3->id);
214 $future = $now + 60 * 60 * 5;
215 $farfuture = $now + 60 * 60 * 24 * 5;
217 $file = "$CFG->dataroot/enrol.txt";
218 $data = "add,student,u1,c1,$future,0
219 add,student,u2,c2,$future,0
220 add,student,u1,c3,$future,$farfuture";
221 file_put_contents($file, $data);
223 $trace = new null_progress_trace();
224 $this->enable_plugin();
225 $flatfileplugin = enrol_get_plugin('flatfile');
226 $flatfileplugin->set_config('location', $file);
227 $flatfileplugin->sync($trace);
231 * Enables the flatfile plugin for testing.
233 protected function enable_plugin() {
234 $enabled = enrol_get_plugins(true);
235 $enabled['flatfile'] = true;
236 $enabled = array_keys($enabled);
237 set_config('enrol_plugins_enabled', implode(',', $enabled));