MDL-62426 core_enrol: control enrolment subcontexts at the provider
[moodle.git] / enrol / flatfile / tests / privacy_provider_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  * Privacy tests for enrol_flatfile.
19  *
20  * @package    enrol_flatfile
21  * @category   test
22  * @copyright  2018 Jake Dallimore <jrhdallimore@gmail.com>
23  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24  */
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;
34 /**
35  * Privacy tests for enrol_flatfile.
36  *
37  * @copyright  2018 Jake Dallimore <jrhdallimore@gmail.com>
38  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39  */
40 class enrol_flatfile_privacy_testcase extends provider_testcase {
42     /** @var \stdClass $user1 a test user.*/
43     protected $user1;
45     /** @var \stdClass $user2 a test user.*/
46     protected $user2;
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;
57     /**
58      * Called before every test.
59      */
60     public function setUp() {
61         $this->resetAfterTest(true);
62     }
64     /**
65      * Verify that get_metadata returns the database table mapping.
66      */
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]);
73     }
75     /**
76      * Verify that the relevant course contexts are returned for users with pending enrolment records.
77      */
78     public function test_get_contexts_for_user() {
79         global $DB;
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);
97     }
99     /**
100      * Verify the export includes any future enrolment records for the user.
101      */
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(
111             $this->user1,
112             'enrol_flatfile',
113             $contextlist->get_contextids()
114         );
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));
131     }
133     /**
134      * Verify export will limit any future enrolment records to only those contextids provided.
135      */
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(
142             $this->user1,
143             'enrol_flatfile',
144             [$this->coursecontext1->id]
145         );
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));
159     }
161     /**
162      * Verify that records can be deleted by context.
163      */
164     public function test_delete_data_for_all_users_in_context() {
165         global $DB;
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]));
175     }
177     public function test_delete_data_for_user() {
178         global $DB;
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(
190             $this->user1,
191             'enrol_flatfile',
192             $contextids
193         );
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]));
200     }
202     /**
203      * Helper to sync a file and create the enrol_flatfile DB entries, for use with the get, export and delete tests.
204      */
205     protected function create_future_enrolments() {
206         global $CFG;
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);
217         $now = time();
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);
232     }
234     /**
235      * Enables the flatfile plugin for testing.
236      */
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));
242     }