MDL-62356 enrol_meta: Add privacy implementation for enrol_meta
[moodle.git] / enrol / meta / 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_meta.
18  *
19  * @package    enrol_meta
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_meta\privacy\provider;
28 /**
29  * Unit tests for the enrol_meta 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_meta_privacy_testcase extends \core_privacy\tests\provider_testcase {
35     /**
36      * Enable enrol_meta plugin.
37      */
38     protected function enable_plugin() {
39         $enabled = enrol_get_plugins(true);
40         $enabled['meta'] = true;
41         $enabled = array_keys($enabled);
42         set_config('enrol_plugins_enabled', implode(',', $enabled));
43     }
44     /**
45      * Test getting the context for the user ID related to this plugin.
46      */
47     public function test_get_contexts_for_userid() {
48         global $DB;
50         $this->resetAfterTest();
51         $metaplugin = enrol_get_plugin('meta');
52         $user1 = $this->getDataGenerator()->create_user();
53         $course1 = $this->getDataGenerator()->create_course();
54         $course2 = $this->getDataGenerator()->create_course();
55         $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course1->id));
56         $studentrole = $DB->get_record('role', array('shortname' => 'student'));
58         $this->enable_plugin();
59         $metaplugin->add_instance($course1, array('customint1' => $course2->id, 'customint2' => $group1->id));
60         $this->getDataGenerator()->enrol_user($user1->id, $course2->id, $studentrole->id);
61         // Check if user1 is enrolled into course1 in group 1.
62         $this->assertTrue(groups_is_member($group1->id, $user1->id));
63         $this->assertTrue($DB->record_exists('groups_members',
64             array(
65                 'groupid' => $group1->id,
66                 'userid' => $user1->id,
67                 'component' => 'enrol_meta'
68             )
69         ));
70         // Check context course fro provider to user1.
71         $context = \context_course::instance($course1->id);
72         $contextlist = provider::get_contexts_for_userid($user1->id);
73         $this->assertEquals($context->id, $contextlist->current()->id);
74     }
76     /**
77      * Test that user data is exported correctly.
78      */
79     public function test_export_user_data() {
80         global $DB;
82         $this->resetAfterTest();
83         $metaplugin = enrol_get_plugin('meta');
84         $user1 = $this->getDataGenerator()->create_user();
85         $course1 = $this->getDataGenerator()->create_course();
86         $course2 = $this->getDataGenerator()->create_course();
87         $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course1->id));
88         $studentrole = $DB->get_record('role', array('shortname' => 'student'));
90         $this->enable_plugin();
91         $metaplugin->add_instance($course1, array('customint1' => $course2->id, 'customint2' => $group1->id));
92         $this->getDataGenerator()->enrol_user($user1->id, $course2->id, $studentrole->id);
94         $this->setUser($user1);
95         $contextlist = provider::get_contexts_for_userid($user1->id);
96         $approvedcontextlist = new approved_contextlist($user1, 'enrol_meta', $contextlist->get_contextids());
97         provider::export_user_data($approvedcontextlist);
98         foreach ($contextlist as $context) {
99             $writer = writer::with_context($context);
100             $data = $writer->get_data([
101                 get_string('pluginname', 'enrol_meta'),
102                 get_string('groups', 'core_group')
103             ]);
104             $this->assertTrue($writer->has_any_data());
105             if ($context->contextlevel == CONTEXT_COURSE) {
106                 $exportedgroups = $data->groups;
107                 // User1 only belongs to group1 via enrol_meta.
108                 $this->assertCount(1, $exportedgroups);
109                 $exportedgroup = reset($exportedgroups);
110                 $this->assertEquals($group1->name, $exportedgroup->name);
111             }
112         }
113     }
114     /**
115      * Test for provider::delete_data_for_all_users_in_context().
116      */
117     public function test_delete_data_for_all_users_in_context() {
118         global $DB;
120         $this->resetAfterTest();
121         $metaplugin = enrol_get_plugin('meta');
122         $user1 = $this->getDataGenerator()->create_user();
123         $user2 = $this->getDataGenerator()->create_user();
124         $user3 = $this->getDataGenerator()->create_user();
125         $course1 = $this->getDataGenerator()->create_course();
126         $course2 = $this->getDataGenerator()->create_course();
127         $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course1->id));
128         $studentrole = $DB->get_record('role', array('shortname' => 'student'));
130         $this->enable_plugin();
131         $metaplugin->add_instance($course1, array('customint1' => $course2->id, 'customint2' => $group1->id));
132         $this->getDataGenerator()->enrol_user($user1->id, $course2->id, $studentrole->id);
133         $this->getDataGenerator()->enrol_user($user2->id, $course2->id, $studentrole->id);
134         $this->getDataGenerator()->enrol_user($user3->id, $course2->id, $studentrole->id);
135         $this->assertEquals(
136                 3,
137                 $DB->count_records_sql("SELECT COUNT(gm.id)
138                                       FROM {groups_members} gm
139                                       JOIN {groups} g ON gm.groupid = g.id
140                                      WHERE g.courseid = ?", [$course1->id])
141         );
143         $coursecontext1 = context_course::instance($course1->id);
144         provider::delete_data_for_all_users_in_context($coursecontext1);
145         $this->assertEquals(
146             0,
147             $DB->count_records_sql("SELECT COUNT(gm.id)
148                                       FROM {groups_members} gm
149                                       JOIN {groups} g ON gm.groupid = g.id
150                                      WHERE g.courseid = ?", [$course1->id])
151         );
152     }
153     /**
154      * Test for provider::delete_data_for_user().
155      */
156     public function test_delete_data_for_user() {
157         global $DB;
159         $this->resetAfterTest();
160         $metaplugin = enrol_get_plugin('meta');
161         $user1 = $this->getDataGenerator()->create_user();
162         $user2 = $this->getDataGenerator()->create_user();
163         $user3 = $this->getDataGenerator()->create_user();
164         $course1 = $this->getDataGenerator()->create_course();
165         $course2 = $this->getDataGenerator()->create_course();
166         $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course1->id));
167         $studentrole = $DB->get_record('role', array('shortname' => 'student'));
169         $this->enable_plugin();
170         $metaplugin->add_instance($course1, array('customint1' => $course2->id, 'customint2' => $group1->id));
171         $this->getDataGenerator()->enrol_user($user1->id, $course2->id, $studentrole->id);
172         $this->getDataGenerator()->enrol_user($user2->id, $course2->id, $studentrole->id);
173         $this->getDataGenerator()->enrol_user($user3->id, $course2->id, $studentrole->id);
175         $this->assertEquals(
176                 3,
177                 $DB->count_records_sql("SELECT COUNT(gm.id)
178                                       FROM {groups_members} gm
179                                       JOIN {groups} g ON gm.groupid = g.id
180                                      WHERE g.courseid = ?", [$course1->id])
181         );
183         $this->setUser($user1);
184         $coursecontext1 = context_course::instance($course1->id);
185         $coursecontext2 = context_course::instance($course2->id);
186         $approvedcontextlist = new \core_privacy\tests\request\approved_contextlist($user1, 'enrol_meta',
187                 [$coursecontext1->id]);
188         provider::delete_data_for_user($approvedcontextlist);
189         // Check we have 2 users in groups because we are deleted user1.
190         $this->assertEquals(
191                 2,
192                 $DB->count_records_sql("SELECT COUNT(gm.id)
193                                           FROM {groups_members} gm
194                                           JOIN {groups} g ON gm.groupid = g.id
195                                          WHERE g.courseid = ?", [$course1->id])
196         );
197     }