MDL-62357 enrol_cohort: Add privacy implementation for enrol_cohort
[moodle.git] / enrol / cohort / 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  * Base class for unit tests for enrol_cohort.
18  *
19  * @package    enrol_cohort
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_privacy\local\request\writer;
26 use \core_privacy\local\request\approved_contextlist;
27 use \enrol_cohort\privacy\provider;
28 /**
29  * Unit tests for the enrol_cohort implementation of the privacy API.
30  *
31  * @copyright  2018 Carlos Escobedo <carlos@moodle.com>
32  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33  */
34 class enrol_cohort_privacy_testcase extends \core_privacy\tests\provider_testcase {
35     /**
36      * Test getting the context for the user ID related to this plugin.
37      */
38     public function test_get_contexts_for_userid() {
39         global $DB;
41         $this->resetAfterTest();
42         $trace = new null_progress_trace();
44         $cohortplugin = enrol_get_plugin('cohort');
45         $user1 = $this->getDataGenerator()->create_user();
46         $cat1 = $this->getDataGenerator()->create_category();
47         $course1 = $this->getDataGenerator()->create_course(array('category' => $cat1->id));
48         $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course1->id));
49         $studentrole = $DB->get_record('role', array('shortname' => 'student'));
50         $cohort1 = $this->getDataGenerator()->create_cohort(
51             array('contextid' => context_coursecat::instance($cat1->id)->id));
52         $cohortplugin->add_instance($course1, array(
53             'customint1' => $cohort1->id,
54             'roleid' => $studentrole->id,
55             'customint2' => $group1->id)
56         );
58         cohort_add_member($cohort1->id, $user1->id);
59         enrol_cohort_sync($trace, $course1->id);
60         // Check if user1 is enrolled into course1 in group 1.
61         $this->assertEquals(1, $DB->count_records('role_assignments', array()));
62         $this->assertTrue($DB->record_exists('groups_members', array(
63             'groupid' => $group1->id,
64             'userid' => $user1->id,
65             'component' => 'enrol_cohort')
66         ));
67         // Check context course fro provider to user1.
68         $context = \context_course::instance($course1->id);
69         $contextlist = provider::get_contexts_for_userid($user1->id);
70         $this->assertEquals($context->id, $contextlist->current()->id);
71     }
73     /**
74      * Test that user data is exported correctly.
75      */
76     public function test_export_user_data() {
77         global $DB;
79         $this->resetAfterTest();
80         $trace = new null_progress_trace();
82         $cohortplugin = enrol_get_plugin('cohort');
83         $user1 = $this->getDataGenerator()->create_user();
84         $cat1 = $this->getDataGenerator()->create_category();
85         $course1 = $this->getDataGenerator()->create_course(array('category' => $cat1->id));
86         $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course1->id));
87         $studentrole = $DB->get_record('role', array('shortname' => 'student'));
88         $cohort1 = $this->getDataGenerator()->create_cohort(
89             array('contextid' => context_coursecat::instance($cat1->id)->id));
90         $cohortplugin->add_instance($course1, array(
91             'customint1' => $cohort1->id,
92             'roleid' => $studentrole->id,
93             'customint2' => $group1->id)
94         );
96         cohort_add_member($cohort1->id, $user1->id);
97         enrol_cohort_sync($trace, $course1->id);
98         // Check if user1 is enrolled into course1 in group 1.
99         $this->assertEquals(1, $DB->count_records('role_assignments', array()));
100         $this->assertTrue($DB->record_exists('groups_members', array(
101             'groupid' => $group1->id,
102             'userid' => $user1->id,
103             'component' => 'enrol_cohort')
104         ));
106         $this->setUser($user1);
107         $contextlist = provider::get_contexts_for_userid($user1->id);
108         $approvedcontextlist = new approved_contextlist($user1, 'enrol_cohort', $contextlist->get_contextids());
109         provider::export_user_data($approvedcontextlist);
110         foreach ($contextlist as $context) {
111             $writer = writer::with_context($context);
112             $data = $writer->get_data([
113                 get_string('pluginname', 'enrol_cohort'),
114                 get_string('groups', 'core_group')
115             ]);
116             $this->assertTrue($writer->has_any_data());
117             if ($context->contextlevel == CONTEXT_COURSE) {
118                 $exportedgroups = $data->groups;
119                 // User1 only belongs to group1 via enrol_cohort.
120                 $this->assertCount(1, $exportedgroups);
121                 $exportedgroup = reset($exportedgroups);
122                 $this->assertEquals($group1->name, $exportedgroup->name);
123             }
124         }
125     }
126     /**
127      * Test for provider::delete_data_for_all_users_in_context().
128      */
129     public function test_delete_data_for_all_users_in_context() {
130         global $DB;
132         $this->resetAfterTest();
133         $trace = new null_progress_trace();
135         $cohortplugin = enrol_get_plugin('cohort');
136         $user1 = $this->getDataGenerator()->create_user();
137         $user2 = $this->getDataGenerator()->create_user();
138         $user3 = $this->getDataGenerator()->create_user();
139         $cat1 = $this->getDataGenerator()->create_category();
140         $course1 = $this->getDataGenerator()->create_course(array('category' => $cat1->id));
141         $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course1->id));
142         $studentrole = $DB->get_record('role', array('shortname' => 'student'));
143         $cohort1 = $this->getDataGenerator()->create_cohort(
144             array('contextid' => context_coursecat::instance($cat1->id)->id));
145         $cohortplugin->add_instance($course1, array(
146             'customint1' => $cohort1->id,
147             'roleid' => $studentrole->id,
148             'customint2' => $group1->id)
149         );
151         cohort_add_member($cohort1->id, $user1->id);
152         cohort_add_member($cohort1->id, $user2->id);
153         cohort_add_member($cohort1->id, $user3->id);
154         enrol_cohort_sync($trace, $course1->id);
155         $this->assertEquals(
156                 3,
157                 $DB->count_records_sql("SELECT COUNT(gm.id)
158                                       FROM {groups_members} gm
159                                       JOIN {groups} g ON gm.groupid = g.id
160                                      WHERE g.courseid = ?", [$course1->id])
161         );
163         $coursecontext1 = context_course::instance($course1->id);
164         provider::delete_data_for_all_users_in_context($coursecontext1);
165         $this->assertEquals(
166             0,
167             $DB->count_records_sql("SELECT COUNT(gm.id)
168                                       FROM {groups_members} gm
169                                       JOIN {groups} g ON gm.groupid = g.id
170                                      WHERE g.courseid = ?", [$course1->id])
171         );
172     }
173     /**
174      * Test for provider::delete_data_for_user().
175      */
176     public function test_delete_data_for_user() {
177         global $DB;
179         $this->resetAfterTest();
180         $trace = new null_progress_trace();
182         $cohortplugin = enrol_get_plugin('cohort');
183         $user1 = $this->getDataGenerator()->create_user();
184         $user2 = $this->getDataGenerator()->create_user();
185         $user3 = $this->getDataGenerator()->create_user();
186         $cat1 = $this->getDataGenerator()->create_category();
187         $course1 = $this->getDataGenerator()->create_course(array('category' => $cat1->id));
188         $course2 = $this->getDataGenerator()->create_course(array('category' => $cat1->id));
189         $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course1->id));
190         $group2 = $this->getDataGenerator()->create_group(array('courseid' => $course2->id));
191         $studentrole = $DB->get_record('role', array('shortname' => 'student'));
192         $cohort1 = $this->getDataGenerator()->create_cohort(
193             array('contextid' => context_coursecat::instance($cat1->id)->id));
194         $cohortplugin->add_instance($course1, array(
195             'customint1' => $cohort1->id,
196             'roleid' => $studentrole->id,
197             'customint2' => $group1->id)
198         );
199         $cohortplugin->add_instance($course2, array(
200             'customint1' => $cohort1->id,
201             'roleid' => $studentrole->id,
202             'customint2' => $group2->id)
203         );
205         $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
206         $this->getDataGenerator()->enrol_user($user3->id, $course1->id);
207         $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id, 'userid' => $user2->id));
208         $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id, 'userid' => $user3->id));
210         cohort_add_member($cohort1->id, $user1->id);
211         enrol_cohort_sync($trace, $course1->id);
213         $this->assertEquals(
214                 3,
215                 $DB->count_records_sql("SELECT COUNT(gm.id)
216                                           FROM {groups_members} gm
217                                           JOIN {groups} g ON gm.groupid = g.id
218                                          WHERE g.courseid = ?", [$course1->id])
219         );
221         $this->assertEquals(
222                 1,
223                 $DB->count_records_sql("SELECT COUNT(gm.id)
224                                           FROM {groups_members} gm
225                                           JOIN {groups} g ON gm.groupid = g.id
226                                          WHERE g.courseid = ?", [$course2->id])
227         );
229         $this->setUser($user1);
230         $coursecontext1 = context_course::instance($course1->id);
231         $coursecontext2 = context_course::instance($course2->id);
232         $approvedcontextlist = new \core_privacy\tests\request\approved_contextlist($user1, 'enrol_cohort',
233                 [$coursecontext1->id, $coursecontext2->id]);
234         provider::delete_data_for_user($approvedcontextlist);
235         // Check we have 2 users in groups because we are deleted user1.
236         $this->assertEquals(
237                 2,
238                 $DB->count_records_sql("SELECT COUNT(gm.id)
239                                           FROM {groups_members} gm
240                                           JOIN {groups} g ON gm.groupid = g.id
241                                          WHERE g.courseid = ?", [$course1->id])
242         );
243         // Check we have not users in groups.
244         $this->assertEquals(
245                 0,
246                 $DB->count_records_sql("SELECT COUNT(gm.id)
247                                           FROM {groups_members} gm
248                                           JOIN {groups} g ON gm.groupid = g.id
249                                          WHERE g.courseid = ?", [$course2->id])
250         );
251     }