MDL-62010 core_enrol: Add privacy implementation for core_enrol
[moodle.git] / 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 core_enrol implementation of the privacy API.
18  *
19  * @package    core_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 core_enrol\privacy\provider;
26 use core_privacy\local\request\approved_contextlist;
27 use core_privacy\local\request\writer;
28 use core_privacy\tests\provider_testcase;
29 use \core_privacy\local\request\transform;
30 /**
31  * Privacy test for the core_enrol.
32  *
33  * @package    core_enrol
34  * @category   test
35  * @copyright  2018 Carlos Escobedo <carlos@moodle.com>
36  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37  */
38 class core_enrol_privacy_testcase extends provider_testcase {
39     /**
40      * Check that a course context is returned if there is any user data for this user.
41      */
42     public function test_get_contexts_for_userid() {
43         $this->resetAfterTest();
44         $user1 = $this->getDataGenerator()->create_user();
45         $course1 = $this->getDataGenerator()->create_course();
46         $this->assertEmpty(provider::get_contexts_for_userid($user1->id));
47         // Enrol user into courses and check contextlist.
48         $this->getDataGenerator()->enrol_user($user1->id, $course1->id,  null, 'manual');
49         $contextlist = provider::get_contexts_for_userid($user1->id);
50         // Check that we only get back two context.
51         $this->assertCount(1, $contextlist);
52         // Check that the context is returned is the expected.
53         $coursecontext1 = \context_course::instance($course1->id);
54         $this->assertEquals($coursecontext1->id, $contextlist->get_contextids()[0]);
55     }
56     /**
57      * Test that user data is exported correctly.
58      */
59     public function test_export_user_data() {
60         global $DB;
62         $this->resetAfterTest();
63         $user1 = $this->getDataGenerator()->create_user();
64         $course1 = $this->getDataGenerator()->create_course();
65         $course2 = $this->getDataGenerator()->create_course();
66         $this->getDataGenerator()->enrol_user($user1->id, $course1->id,  null, 'manual');
67         $this->getDataGenerator()->enrol_user($user1->id, $course1->id,  null, 'self');
68         $this->getDataGenerator()->enrol_user($user1->id, $course2->id,  null, 'manual');
69         $subcontexts = [
70             get_string('privacy:metadata:user_enrolments', 'core_enrol')
71         ];
72         $coursecontext1 = \context_course::instance($course1->id);
73         $coursecontext2 = \context_course::instance($course2->id);
74         $this->setUser($user1);
75         $writer = writer::with_context($coursecontext1);
76         $this->assertFalse($writer->has_any_data());
77         $this->export_context_data_for_user($user1->id, $coursecontext1, 'core_enrol');
78         $data = $writer->get_related_data($subcontexts);
79         $this->assertCount(2, (array)$data);
81         $sql = "SELECT ue.id,
82                        ue.status,
83                        ue.timestart,
84                        ue.timeend,
85                        ue.timecreated,
86                        ue.timemodified
87                   FROM {user_enrolments} ue
88                   JOIN {enrol} e
89                     ON e.id = ue.enrolid
90                    AND e.courseid = :courseid
91                  WHERE ue.userid = :userid";
92         $enrolmentcouse2 = $DB->get_record_sql($sql, array('userid' => $user1->id, 'courseid' => $course2->id));
93         writer::reset();
94         $writer = writer::with_context($coursecontext2);
95         $this->export_context_data_for_user($user1->id, $coursecontext2, 'core_enrol');
96         $data = $writer->get_related_data($subcontexts, 'manual');
97         $this->assertEquals($enrolmentcouse2->status, reset($data)->status);
98         $this->assertEquals(transform::datetime($enrolmentcouse2->timestart), reset($data)->timestart);
99         $this->assertEquals(transform::datetime($enrolmentcouse2->timeend), reset($data)->timeend);
100         $this->assertEquals(transform::datetime($enrolmentcouse2->timecreated), reset($data)->timecreated);
101         $this->assertEquals(transform::datetime($enrolmentcouse2->timemodified), reset($data)->timemodified);
102     }
103     /**
104      * Test deleting all user data for a specific context.
105      */
106     public function test_delete_data_for_all_users_in_context() {
107         global $DB;
109         $this->resetAfterTest();
110         $user1 = $this->getDataGenerator()->create_user();
111         $user2 = $this->getDataGenerator()->create_user();
112         $user3 = $this->getDataGenerator()->create_user();
113         $course1 = $this->getDataGenerator()->create_course();
114         $course2 = $this->getDataGenerator()->create_course();
115         $this->getDataGenerator()->enrol_user($user1->id, $course1->id,  null, 'manual');
116         $this->getDataGenerator()->enrol_user($user2->id, $course1->id,  null, 'manual');
117         $this->getDataGenerator()->enrol_user($user3->id, $course1->id,  null, 'manual');
118         $this->getDataGenerator()->enrol_user($user1->id, $course2->id,  null, 'manual');
119         $this->getDataGenerator()->enrol_user($user2->id, $course2->id,  null, 'manual');
120         // Get all user enrolments.
121         $userenrolments = $DB->get_records('user_enrolments', array());
122         $this->assertCount(5, $userenrolments);
123         // Get all user enrolments match with course1.
124         $sql = "SELECT ue.id
125                   FROM {user_enrolments} ue
126                   JOIN {enrol} e
127                     ON e.id = ue.enrolid
128                    AND e.courseid = :courseid";
129         $userenrolments = $DB->get_records_sql($sql, array('courseid' => $course1->id));
130         $this->assertCount(3, $userenrolments);
131         // Delete everything for the first course context.
132         $coursecontext1 = \context_course::instance($course1->id);
133         provider::delete_data_for_all_users_in_context($coursecontext1);
134         // Get all user enrolments match with this course contest.
135         $userenrolments = $DB->get_records_sql($sql, array('courseid' => $course1->id));
136         $this->assertCount(0, $userenrolments);
137         // Get all user enrolments.
138         $userenrolments = $DB->get_records('user_enrolments', array());
139         $this->assertCount(2, $userenrolments);
140     }
141     /**
142      * This should work identical to the above test.
143      */
144     public function test_delete_data_for_user() {
145         global $DB;
147         $this->resetAfterTest();
148         $user1 = $this->getDataGenerator()->create_user();
149         $user2 = $this->getDataGenerator()->create_user();
150         $user3 = $this->getDataGenerator()->create_user();
151         $course1 = $this->getDataGenerator()->create_course();
152         $course2 = $this->getDataGenerator()->create_course();
153         $this->getDataGenerator()->enrol_user($user1->id, $course1->id,  null, 'manual');
154         $this->getDataGenerator()->enrol_user($user2->id, $course1->id,  null, 'manual');
155         $this->getDataGenerator()->enrol_user($user3->id, $course1->id,  null, 'manual');
156         $this->getDataGenerator()->enrol_user($user1->id, $course2->id,  null, 'manual');
158         // Get all user enrolments.
159         $userenrolments = $DB->get_records('user_enrolments', array());
160         $this->assertCount(4, $userenrolments);
161         // Get all user enrolments match with user1.
162         $userenrolments = $DB->get_records('user_enrolments', array('userid' => $user1->id));
163         $this->assertCount(2, $userenrolments);
164         // Delete everything for the user1 in the context course 1.
165         $coursecontext1 = \context_course::instance($course1->id);
166         $approvedlist = new approved_contextlist($user1, 'core_enrol', [$coursecontext1->id]);
167         provider::delete_data_for_user($approvedlist);
168         // Get all user enrolments match with user.
169         $userenrolments = $DB->get_records('user_enrolments', ['userid' => $user1->id]);
170         $this->assertCount(1, $userenrolments);
171         // Get all user enrolments accounts.
172         $userenrolments = $DB->get_records('user_enrolments', array());
173         $this->assertCount(3, $userenrolments);
174     }