MDL-62257 mnetservice_enrol: Implement Privacy API
[moodle.git] / mnet / service / enrol / tests / privacy_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/>.
16 /**
17  * Privacy test for the mnetservice_enrol implementation of the privacy API.
18  *
19  * @package    mnetservice_enrol
20  * @category   test
21  * @copyright  2018 Carlos Escobedo <carlos@moodle.com>
22  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23  */
24 defined('MOODLE_INTERNAL') || die();
25 use mnetservice_enrol\privacy\provider;
26 use core_privacy\local\request\approved_contextlist;
27 use core_privacy\local\request\writer;
28 use core_privacy\local\request\transform;
29 use core_privacy\tests\provider_testcase;
30 /**
31  * Privacy test for the mnetservice_enrol.
32  *
33  * @copyright  2018 Carlos Escobedo <carlos@moodle.com>
34  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35  */
36 class mnetservice_enrol_privacy_testcase extends provider_testcase {
37     /** @var stdClass the mnet host we are using to test. */
38     protected $mnethost;
39     /** @var stdClass the mnet service enrolment to test. */
40     protected $enrolment;
42     /**
43      * Test set up.
44      *
45      * This is executed before running any test in this file.
46      */
47     public function setUp() {
48         global $DB;
50         // Add a mnet host.
51         $this->mnethost = new stdClass();
52         $this->mnethost->name = 'A mnet host';
53         $this->mnethost->public_key = 'A random public key!';
54         $this->mnethost->id = $DB->insert_record('mnet_host', $this->mnethost);
55     }
56     /**
57      * Check that a user context is returned if there is any user data for this user.
58      */
59     public function test_get_contexts_for_userid() {
60         $this->resetAfterTest();
61         $user = $this->getDataGenerator()->create_user();
62         $this->assertEmpty(provider::get_contexts_for_userid($user->id));
64         // Create a test MNet service enrol enrolments.
65         $remotecourseid = 101;
66         $this->insert_mnetservice_enrol_courses($remotecourseid);
67         $this->insert_mnetservice_enrol_enrolments($user->id, $remotecourseid);
69         $contextlist = provider::get_contexts_for_userid($user->id);
70         // Check that we only get back two context.
71         $this->assertCount(1, $contextlist);
72         // Check that the contexts are returned are the expected.
73         $usercontext = \context_user::instance($user->id);
74         $this->assertEquals($usercontext->id, $contextlist->get_contextids()[0]);
75     }
76     /**
77      * Test that user data is exported correctly.
78      */
79     public function test_export_user_data() {
80         global $DB;
82         $this->resetAfterTest();
83         $user = $this->getDataGenerator()->create_user();
84         $this->assertEmpty(provider::get_contexts_for_userid($user->id));
86         // Create a test MNet service enrol enrolments.
87         $remotecourseid = 101;
88         $this->insert_mnetservice_enrol_courses($remotecourseid);
89         $this->insert_mnetservice_enrol_enrolments($user->id, $remotecourseid);
91         $subcontexts = [
92             get_string('privacy:metadata:mnetservice_enrol_enrolments', 'mnetservice_enrol')
93         ];
94         $usercontext = \context_user::instance($user->id);
95         $writer = writer::with_context($usercontext);
96         $this->assertFalse($writer->has_any_data());
97         $approvedlist = new approved_contextlist($user, 'mnetservice_enrol', [$usercontext->id]);
98         provider::export_user_data($approvedlist);
99         $data = $writer->get_data($subcontexts);
100         $this->assertCount(1, (array)$data);
101         $this->assertEquals($this->mnethost->name, reset($data)->host);
102         $remotecoursename = $DB->get_field('mnetservice_enrol_courses', 'fullname',
103             array('remoteid' => $this->enrolment->remotecourseid));
104         $this->assertEquals($remotecoursename, reset($data)->remotecourseid);
105         $this->assertEquals($this->enrolment->rolename, reset($data)->rolename);
106         $this->assertEquals($this->enrolment->enroltype, reset($data)->enroltype);
107         $this->assertEquals(transform::datetime($this->enrolment->enroltime), reset($data)->enroltime);
108     }
110     /**
111      * Test deleting all user data for a specific context.
112      */
113     public function test_delete_data_for_all_users_in_context() {
114         global $DB;
116         $this->resetAfterTest();
117         $user = $this->getDataGenerator()->create_user();
118         $user2 = $this->getDataGenerator()->create_user();
119         $user3 = $this->getDataGenerator()->create_user();
120         $this->assertEmpty(provider::get_contexts_for_userid($user->id));
122         // Create a test MNet service enrol enrolments.
123         $remotecourseid = 101;
124         $this->insert_mnetservice_enrol_courses($remotecourseid);
125         $this->insert_mnetservice_enrol_enrolments($user->id, $remotecourseid);
126         $this->insert_mnetservice_enrol_enrolments($user2->id, $remotecourseid);
127         $this->insert_mnetservice_enrol_enrolments($user3->id, $remotecourseid);
128         $usercontext = \context_user::instance($user->id);
129         // Get all user enrolments.
130         $userenrolments = $DB->get_records('mnetservice_enrol_enrolments', array());
131         $this->assertCount(3, $userenrolments);
132         // Get all user enrolments match with user.
133         $userenrolments = $DB->get_records('mnetservice_enrol_enrolments', array('userid' => $user->id));
134         $this->assertCount(1, $userenrolments);
135         // Delete everything for the first user context.
136         provider::delete_data_for_all_users_in_context($usercontext);
137         // Get all user enrolments match with user.
138         $userenrolments = $DB->get_records('mnetservice_enrol_enrolments', ['userid' => $user->id]);
139         $this->assertCount(0, $userenrolments);
140         // Get all user enrolments.
141         $userenrolments = $DB->get_records('mnetservice_enrol_enrolments', array());
142         $this->assertCount(2, $userenrolments);
143     }
144     /**
145      * This should work identical to the above test.
146      */
147     public function test_delete_data_for_user() {
148         global $DB;
150         $this->resetAfterTest();
151         $user = $this->getDataGenerator()->create_user();
152         $user2 = $this->getDataGenerator()->create_user();
153         $user3 = $this->getDataGenerator()->create_user();
154         $this->assertEmpty(provider::get_contexts_for_userid($user->id));
156         $remotecourseid = 101;
157         $this->insert_mnetservice_enrol_courses($remotecourseid);
158         $this->insert_mnetservice_enrol_enrolments($user->id, $remotecourseid);
159         $this->insert_mnetservice_enrol_enrolments($user2->id, $remotecourseid);
160         $this->insert_mnetservice_enrol_enrolments($user3->id, $remotecourseid);
161         $remotecourseid2 = 102;
162         $this->insert_mnetservice_enrol_courses($remotecourseid2);
163         $this->insert_mnetservice_enrol_enrolments($user->id, $remotecourseid2);
165         $usercontext = \context_user::instance($user->id);
166         // Get all user enrolments.
167         $userenrolments = $DB->get_records('mnetservice_enrol_enrolments', array());
168         $this->assertCount(4, $userenrolments);
169         // Get all user enrolments match with user.
170         $userenrolments = $DB->get_records('mnetservice_enrol_enrolments', array('userid' => $user->id));
171         $this->assertCount(2, $userenrolments);
172         // Delete everything for the first user.
173         $approvedlist = new approved_contextlist($user, 'mnetservice_enrol', [$usercontext->id]);
174         provider::delete_data_for_user($approvedlist);
175         // Get all user enrolments match with user.
176         $userenrolments = $DB->get_records('mnetservice_enrol_enrolments', ['userid' => $user->id]);
177         $this->assertCount(0, $userenrolments);
178         // Get all user enrolments accounts.
179         $userenrolments = $DB->get_records('mnetservice_enrol_enrolments', array());
180         $this->assertCount(2, $userenrolments);
181     }
183     /**
184      * Help function to create a simulation of MNet service enrol.
185      * Create a Dummy Enrol into mnetservice_enrol_enrolments.
186      *
187      * @param  int $userid  Userid.
188      * @param  int $remotecourseid  Remotecourseid.
189      */
190     protected function insert_mnetservice_enrol_enrolments($userid, $remotecourseid) {
191         global $DB;
193         // Create a test MNet service enrol enrolments.
194         $this->enrolment                  = new stdclass();
195         $this->enrolment->hostid          = $this->mnethost->id;
196         $this->enrolment->userid          = $userid;
197         $this->enrolment->remotecourseid  = $remotecourseid;
198         $this->enrolment->rolename        = 'student';
199         $this->enrolment->enroltime       = time();
200         $this->enrolment->enroltype       = 'mnet';
201         $DB->insert_record('mnetservice_enrol_enrolments', $this->enrolment);
202     }
204     /**
205      * Help function to create a simualtion of MNet service enrol.
206      * Create a Dummy Course into mnetservice_enrol_courses.
207      * Important: The real course is on the host.
208      *
209      * @param  int    $remoteid  Remote courseid.
210      */
211     protected function insert_mnetservice_enrol_courses($remoteid) {
212         global $DB;
214         // Create a Dummy Remote Course to test.
215         $course                 = new stdclass();
216         $course->hostid         = $this->mnethost->id;
217         $course->remoteid       = $remoteid;
218         $course->categoryid     = 1;
219         $course->categoryname   = 'Miscellaneous';
220         $course->sortorder      = 10001;
221         $course->fullname       = 'Test Remote Course '.$remoteid;
222         $course->shortname      = 'testremotecourse '.$remoteid;
223         $course->idnumber       = 'IdnumberRemote '.$remoteid;
224         $course->summary        = 'TestSummaryRemote '.$remoteid;
225         $course->summaryformat  = FORMAT_MOODLE;
226         $course->startdate      = time();
227         $course->roleid         = 5;
228         $course->rolename       = 'student';
229         $DB->insert_record('mnetservice_enrol_courses', $course);
230     }