19590293b0f1021d0135ddfadb40bb18e3debba0
[moodle.git] / group / 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 provider tests.
19  *
20  * @package    core_group
21  * @category   test
22  * @copyright  2018 Shamim Rezaie <shamim@moodle.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\tests\provider_testcase;
29 use core_privacy\local\metadata\collection;
30 use core_group\privacy\provider;
31 use core_privacy\local\request\writer;
33 /**
34  * Class core_group_privacy_provider_testcase.
35  *
36  * @copyright  2018 Shamim Rezaie <shamim@moodle.com>
37  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38  */
39 class core_group_privacy_provider_testcase extends provider_testcase {
41     /**
42      * Test for provider::get_metadata().
43      */
44     public function test_get_metadata() {
45         $collection = new collection('core_group');
46         $newcollection = provider::get_metadata($collection);
47         $itemcollection = $newcollection->get_collection();
48         $this->assertCount(1, $itemcollection);
50         $table = reset($itemcollection);
52         $this->assertEquals('groups_members', $table->get_name());
53         $this->assertEquals('privacy:metadata:groups', $table->get_summary());
55         $privacyfields = $table->get_privacy_fields();
56         $this->assertArrayHasKey('groupid', $privacyfields);
57         $this->assertArrayHasKey('userid', $privacyfields);
58         $this->assertArrayHasKey('timeadded', $privacyfields);
59     }
61     /**
62      * Test for provider::export_groups() to export manual group memberships.
63      */
64     public function test_export_groups() {
65         $this->resetAfterTest();
67         $course = $this->getDataGenerator()->create_course();
68         $user1 = $this->getDataGenerator()->create_user();
69         $user2 = $this->getDataGenerator()->create_user();
70         $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
71         $group2 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
72         $group3 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
73         $group4 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
74         $this->getDataGenerator()->enrol_user($user1->id, $course->id);
75         $this->getDataGenerator()->enrol_user($user2->id, $course->id);
77         // Add user1 to group1 and group2.
78         $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id, 'userid' => $user1->id));
79         $this->getDataGenerator()->create_group_member(array('groupid' => $group2->id, 'userid' => $user1->id));
81         // Add user2 to group2 and group3.
82         $this->getDataGenerator()->create_group_member(array('groupid' => $group2->id, 'userid' => $user2->id));
83         $this->getDataGenerator()->create_group_member(array('groupid' => $group3->id, 'userid' => $user2->id));
85         $context = context_course::instance($course->id);
87         // Retrieve groups for user1.
88         $this->setUser($user1);
89         $writer = writer::with_context($context);
90         provider::export_groups($context, '');
92         $data = $writer->get_data([get_string('groups', 'core_group')]);
93         $exportedgroups = $data->groups;
95         // User1 belongs to group1 and group2.
96         $this->assertEquals(
97                 [$group1->name, $group2->name],
98                 array_column($exportedgroups, 'name'),
99                 '', 0.0, 10, true);
100     }
102     /**
103      * Test for provider::export_groups() to export group memberships of a component.
104      */
105     public function test_export_groups_for_component() {
106         $this->resetAfterTest();
108         $course = $this->getDataGenerator()->create_course();
109         $user1 = $this->getDataGenerator()->create_user();
110         $user2 = $this->getDataGenerator()->create_user();
111         $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
112         $group2 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
113         $group3 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
114         $group4 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
115         $group5 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
116         $this->getDataGenerator()->enrol_user($user1->id, $course->id, null, 'self');
117         $this->getDataGenerator()->enrol_user($user2->id, $course->id, null, 'self');
119         // Add user1 to group1 (via enrol_self) and group2 and group3.
120         $this->getDataGenerator()->create_group_member(
121                 array('groupid' => $group1->id, 'userid' => $user1->id, 'component' => 'enrol_self'));
122         $this->getDataGenerator()->create_group_member(array('groupid' => $group2->id, 'userid' => $user1->id));
123         $this->getDataGenerator()->create_group_member(array('groupid' => $group3->id, 'userid' => $user1->id));
125         // Add user2 to group3 (via enrol_self) and group4.
126         $this->getDataGenerator()->create_group_member(
127                 array('groupid' => $group3->id, 'userid' => $user2->id, 'component' => 'enrol_self'));
128         $this->getDataGenerator()->create_group_member(array('groupid' => $group4->id, 'userid' => $user2->id));
130         $context = context_course::instance($course->id);
132         // Retrieve groups for user1.
133         $this->setUser($user1);
134         $writer = writer::with_context($context);
135         provider::export_groups($context, 'enrol_self');
137         $data = $writer->get_data([get_string('groups', 'core_group')]);
138         $exportedgroups = $data->groups;
140         // User1 only belongs to group1 via enrol_self.
141         $this->assertCount(1, $exportedgroups);
142         $exportedgroup = reset($exportedgroups);
143         $this->assertEquals($group1->name, $exportedgroup->name);
144     }
146     /**
147      * Test for provider::delete_groups_for_all_users() to delete manual group memberships.
148      */
149     public function test_delete_groups_for_all_users() {
150         global $DB;
152         $this->resetAfterTest();
154         $course1 = $this->getDataGenerator()->create_course();
155         $course2 = $this->getDataGenerator()->create_course();
157         $group1a = $this->getDataGenerator()->create_group(array('courseid' => $course1->id));
158         $group1b = $this->getDataGenerator()->create_group(array('courseid' => $course1->id));
159         $group2a = $this->getDataGenerator()->create_group(array('courseid' => $course2->id));
160         $group2b = $this->getDataGenerator()->create_group(array('courseid' => $course2->id));
162         $user1 = $this->getDataGenerator()->create_user();
163         $user2 = $this->getDataGenerator()->create_user();
165         $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
166         $this->getDataGenerator()->enrol_user($user1->id, $course2->id);
167         $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
168         $this->getDataGenerator()->enrol_user($user2->id, $course2->id);
170         $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user1->id));
171         $this->getDataGenerator()->create_group_member(array('groupid' => $group1b->id, 'userid' => $user2->id));
172         $this->getDataGenerator()->create_group_member(array('groupid' => $group2a->id, 'userid' => $user1->id));
173         $this->getDataGenerator()->create_group_member(array('groupid' => $group2b->id, 'userid' => $user2->id));
175         $this->assertEquals(
176                 2,
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         );
182         $this->assertEquals(
183                 2,
184                 $DB->count_records_sql("SELECT COUNT(gm.id)
185                                       FROM {groups_members} gm
186                                       JOIN {groups} g ON gm.groupid = g.id
187                                      WHERE g.courseid = ?", [$course2->id])
188         );
190         $coursecontext1 = context_course::instance($course1->id);
191         provider::delete_groups_for_all_users($coursecontext1, '');
193         $this->assertEquals(
194             0,
195             $DB->count_records_sql("SELECT COUNT(gm.id)
196                                       FROM {groups_members} gm
197                                       JOIN {groups} g ON gm.groupid = g.id
198                                      WHERE g.courseid = ?", [$course1->id])
199         );
200         $this->assertEquals(
201             2,
202             $DB->count_records_sql("SELECT COUNT(gm.id)
203                                       FROM {groups_members} gm
204                                       JOIN {groups} g ON gm.groupid = g.id
205                                      WHERE g.courseid = ?", [$course2->id])
206         );
207     }
209     /**
210      * Test for provider::delete_groups_for_all_users() to delete group memberships of a component.
211      */
212     public function test_delete_groups_for_all_users_for_component() {
213         global $DB;
215         $this->resetAfterTest();
217         $course1 = $this->getDataGenerator()->create_course();
218         $course2 = $this->getDataGenerator()->create_course();
220         $group1a = $this->getDataGenerator()->create_group(array('courseid' => $course1->id));
221         $group1b = $this->getDataGenerator()->create_group(array('courseid' => $course1->id));
222         $group2a = $this->getDataGenerator()->create_group(array('courseid' => $course2->id));
223         $group2b = $this->getDataGenerator()->create_group(array('courseid' => $course2->id));
225         $user1 = $this->getDataGenerator()->create_user();
226         $user2 = $this->getDataGenerator()->create_user();
228         $this->getDataGenerator()->enrol_user($user1->id, $course1->id, null, 'self');
229         $this->getDataGenerator()->enrol_user($user1->id, $course2->id, null, 'self');
230         $this->getDataGenerator()->enrol_user($user2->id, $course1->id, null, 'self');
231         $this->getDataGenerator()->enrol_user($user2->id, $course2->id, null, 'self');
233         $this->getDataGenerator()->create_group_member(
234                 array('groupid' => $group1a->id, 'userid' => $user1->id, 'component' => 'enrol_self'));
235         $this->getDataGenerator()->create_group_member(array('groupid' => $group1b->id, 'userid' => $user2->id));
236         $this->getDataGenerator()->create_group_member(
237                 array('groupid' => $group2a->id, 'userid' => $user1->id, 'component' => 'enrol_self'));
238         $this->getDataGenerator()->create_group_member(array('groupid' => $group2b->id, 'userid' => $user2->id));
240         $this->assertEquals(
241                 2,
242                 $DB->count_records_sql("SELECT COUNT(gm.id)
243                                       FROM {groups_members} gm
244                                       JOIN {groups} g ON gm.groupid = g.id
245                                      WHERE g.courseid = ?", [$course1->id])
246         );
247         $this->assertEquals(
248                 2,
249                 $DB->count_records_sql("SELECT COUNT(gm.id)
250                                       FROM {groups_members} gm
251                                       JOIN {groups} g ON gm.groupid = g.id
252                                      WHERE g.courseid = ?", [$course2->id])
253         );
255         $coursecontext1 = context_course::instance($course1->id);
256         provider::delete_groups_for_all_users($coursecontext1, 'enrol_self');
258         $this->assertEquals(
259             1,
260             $DB->count_records_sql("SELECT COUNT(gm.id)
261                                       FROM {groups_members} gm
262                                       JOIN {groups} g ON gm.groupid = g.id
263                                      WHERE g.courseid = ?", [$course1->id])
264         );
265         $this->assertEquals(
266             2,
267             $DB->count_records_sql("SELECT COUNT(gm.id)
268                                       FROM {groups_members} gm
269                                       JOIN {groups} g ON gm.groupid = g.id
270                                      WHERE g.courseid = ?", [$course2->id])
271         );
272     }
274     /**
275      * Test for provider::delete_groups_for_all_users() to check deleting from cache.
276      */
277     public function test_delete_groups_for_all_users_deletes_cache() {
278         $this->resetAfterTest();
280         $course = $this->getDataGenerator()->create_course();
282         $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
283         $group2 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
285         $user1 = $this->getDataGenerator()->create_user();
286         $user2 = $this->getDataGenerator()->create_user();
288         $this->getDataGenerator()->enrol_user($user1->id, $course->id);
289         $this->getDataGenerator()->enrol_user($user2->id, $course->id);
291         $this->getDataGenerator()->create_group_member(array('userid' => $user1->id, 'groupid' => $group1->id));
292         $this->getDataGenerator()->create_group_member(array('userid' => $user1->id, 'groupid' => $group2->id));
293         $this->getDataGenerator()->create_group_member(array('userid' => $user2->id, 'groupid' => $group1->id));
295         $this->assertEquals([[$group1->id, $group2->id]], groups_get_user_groups($course->id, $user1->id), '', 0.0, 10, true);
296         $this->assertEquals([[$group1->id]], groups_get_user_groups($course->id, $user2->id));
298         $coursecontext = context_course::instance($course->id);
299         provider::delete_groups_for_all_users($coursecontext, '');
301         $this->assertEquals([[]], groups_get_user_groups($course->id, $user1->id));
302         $this->assertEquals([[]], groups_get_user_groups($course->id, $user2->id));
303     }
305     /**
306      * Test for provider::delete_groups_for_user() to delete manual group memberships.
307      */
308     public function test_delete_groups_for_user() {
309         global $DB;
311         $this->resetAfterTest();
313         $course1 = $this->getDataGenerator()->create_course();
314         $course2 = $this->getDataGenerator()->create_course();
315         $course3 = $this->getDataGenerator()->create_course();
317         $group1a = $this->getDataGenerator()->create_group(array('courseid' => $course1->id));
318         $group1b = $this->getDataGenerator()->create_group(array('courseid' => $course1->id));
319         $group2a = $this->getDataGenerator()->create_group(array('courseid' => $course2->id));
320         $group2b = $this->getDataGenerator()->create_group(array('courseid' => $course2->id));
321         $group3a = $this->getDataGenerator()->create_group(array('courseid' => $course3->id));
322         $group3b = $this->getDataGenerator()->create_group(array('courseid' => $course3->id));
324         $user1 = $this->getDataGenerator()->create_user();
325         $user2 = $this->getDataGenerator()->create_user();
327         $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
328         $this->getDataGenerator()->enrol_user($user1->id, $course2->id);
329         $this->getDataGenerator()->enrol_user($user1->id, $course3->id);
330         $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
331         $this->getDataGenerator()->enrol_user($user2->id, $course2->id);
332         $this->getDataGenerator()->enrol_user($user2->id, $course3->id);
334         $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user1->id));
335         $this->getDataGenerator()->create_group_member(array('groupid' => $group1b->id, 'userid' => $user2->id));
336         $this->getDataGenerator()->create_group_member(array('groupid' => $group2a->id, 'userid' => $user1->id));
337         $this->getDataGenerator()->create_group_member(array('groupid' => $group2b->id, 'userid' => $user2->id));
338         $this->getDataGenerator()->create_group_member(array('groupid' => $group3a->id, 'userid' => $user1->id));
339         $this->getDataGenerator()->create_group_member(array('groupid' => $group3b->id, 'userid' => $user2->id));
341         $this->assertEquals(
342                 2,
343                 $DB->count_records_sql("SELECT COUNT(gm.id)
344                                           FROM {groups_members} gm
345                                           JOIN {groups} g ON gm.groupid = g.id
346                                          WHERE g.courseid = ?", [$course1->id])
347         );
348         $this->assertEquals(
349                 2,
350                 $DB->count_records_sql("SELECT COUNT(gm.id)
351                                           FROM {groups_members} gm
352                                           JOIN {groups} g ON gm.groupid = g.id
353                                          WHERE g.courseid = ?", [$course2->id])
354         );
355         $this->assertEquals(
356                 2,
357                 $DB->count_records_sql("SELECT COUNT(gm.id)
358                                           FROM {groups_members} gm
359                                           JOIN {groups} g ON gm.groupid = g.id
360                                          WHERE g.courseid = ?", [$course2->id])
361         );
362         $this->assertEquals(
363                 3,
364                 $DB->count_records_sql("SELECT COUNT(gm.id)
365                                           FROM {groups_members} gm
366                                           JOIN {groups} g ON gm.groupid = g.id
367                                          WHERE gm.userid = ?", [$user1->id])
368         );
370         $this->setUser($user1);
371         $coursecontext1 = context_course::instance($course1->id);
372         $coursecontext2 = context_course::instance($course2->id);
373         $approvedcontextlist = new \core_privacy\tests\request\approved_contextlist($user1, 'core_group',
374                 [$coursecontext1->id, $coursecontext2->id]);
375         provider::delete_groups_for_user($approvedcontextlist, '');
377         $this->assertEquals(
378                 1,
379                 $DB->count_records_sql("SELECT COUNT(gm.id)
380                                           FROM {groups_members} gm
381                                           JOIN {groups} g ON gm.groupid = g.id
382                                          WHERE g.courseid = ?", [$course1->id])
383         );
384         $this->assertEquals(
385                 1,
386                 $DB->count_records_sql("SELECT COUNT(gm.id)
387                                           FROM {groups_members} gm
388                                           JOIN {groups} g ON gm.groupid = g.id
389                                          WHERE g.courseid = ?", [$course2->id])
390         );
391         $this->assertEquals(
392                 2,
393                 $DB->count_records_sql("SELECT COUNT(gm.id)
394                                           FROM {groups_members} gm
395                                           JOIN {groups} g ON gm.groupid = g.id
396                                          WHERE g.courseid = ?", [$course3->id])
397         );
398         $this->assertEquals(
399                 1,
400                 $DB->count_records_sql("SELECT COUNT(gm.id)
401                                           FROM {groups_members} gm
402                                           JOIN {groups} g ON gm.groupid = g.id
403                                          WHERE gm.userid = ?", [$user1->id])
404         );
405     }
407     /**
408      * Test for provider::delete_groups_for_user() to delete group memberships of a component.
409      */
410     public function test_delete_groups_for_user_for_component() {
411         global $DB;
413         $this->resetAfterTest();
415         $course1 = $this->getDataGenerator()->create_course();
416         $course2 = $this->getDataGenerator()->create_course();
417         $course3 = $this->getDataGenerator()->create_course();
419         $group1a = $this->getDataGenerator()->create_group(array('courseid' => $course1->id));
420         $group1b = $this->getDataGenerator()->create_group(array('courseid' => $course1->id));
421         $group2a = $this->getDataGenerator()->create_group(array('courseid' => $course2->id));
422         $group2b = $this->getDataGenerator()->create_group(array('courseid' => $course2->id));
423         $group3a = $this->getDataGenerator()->create_group(array('courseid' => $course3->id));
424         $group3b = $this->getDataGenerator()->create_group(array('courseid' => $course3->id));
426         $user1 = $this->getDataGenerator()->create_user();
427         $user2 = $this->getDataGenerator()->create_user();
429         $this->getDataGenerator()->enrol_user($user1->id, $course1->id, null, 'self');
430         $this->getDataGenerator()->enrol_user($user1->id, $course2->id, null, 'self');
431         $this->getDataGenerator()->enrol_user($user1->id, $course3->id, null, 'self');
432         $this->getDataGenerator()->enrol_user($user2->id, $course1->id, null, 'self');
433         $this->getDataGenerator()->enrol_user($user2->id, $course2->id, null, 'self');
434         $this->getDataGenerator()->enrol_user($user2->id, $course3->id, null, 'self');
436         $this->getDataGenerator()->create_group_member(
437                 array('groupid' => $group1a->id, 'userid' => $user1->id, 'component' => 'enrol_self'));
438         $this->getDataGenerator()->create_group_member(
439                 array('groupid' => $group1b->id, 'userid' => $user2->id, 'component' => 'enrol_self'));
440         $this->getDataGenerator()->create_group_member(array('groupid' => $group2a->id, 'userid' => $user1->id));
441         $this->getDataGenerator()->create_group_member(array('groupid' => $group2b->id, 'userid' => $user2->id));
442         $this->getDataGenerator()->create_group_member(array('groupid' => $group3a->id, 'userid' => $user1->id));
443         $this->getDataGenerator()->create_group_member(array('groupid' => $group3b->id, 'userid' => $user2->id));
445         $this->assertEquals(
446                 2,
447                 $DB->count_records_sql("SELECT COUNT(gm.id)
448                                           FROM {groups_members} gm
449                                           JOIN {groups} g ON gm.groupid = g.id
450                                          WHERE g.courseid = ?", [$course1->id])
451         );
452         $this->assertEquals(
453                 2,
454                 $DB->count_records_sql("SELECT COUNT(gm.id)
455                                           FROM {groups_members} gm
456                                           JOIN {groups} g ON gm.groupid = g.id
457                                          WHERE g.courseid = ?", [$course2->id])
458         );
459         $this->assertEquals(
460                 2,
461                 $DB->count_records_sql("SELECT COUNT(gm.id)
462                                           FROM {groups_members} gm
463                                           JOIN {groups} g ON gm.groupid = g.id
464                                          WHERE g.courseid = ?", [$course2->id])
465         );
466         $this->assertEquals(
467                 3,
468                 $DB->count_records_sql("SELECT COUNT(gm.id)
469                                           FROM {groups_members} gm
470                                           JOIN {groups} g ON gm.groupid = g.id
471                                          WHERE gm.userid = ?", [$user1->id])
472         );
474         $this->setUser($user1);
475         $coursecontext1 = context_course::instance($course1->id);
476         $coursecontext2 = context_course::instance($course2->id);
477         $approvedcontextlist = new \core_privacy\tests\request\approved_contextlist($user1, 'core_group',
478                 [$coursecontext1->id, $coursecontext2->id]);
479         provider::delete_groups_for_user($approvedcontextlist, 'enrol_self');
481         $this->assertEquals(
482                 1,
483                 $DB->count_records_sql("SELECT COUNT(gm.id)
484                                           FROM {groups_members} gm
485                                           JOIN {groups} g ON gm.groupid = g.id
486                                          WHERE g.courseid = ?", [$course1->id])
487         );
488         $this->assertEquals(
489                 2,
490                 $DB->count_records_sql("SELECT COUNT(gm.id)
491                                           FROM {groups_members} gm
492                                           JOIN {groups} g ON gm.groupid = g.id
493                                          WHERE g.courseid = ?", [$course2->id])
494         );
495         $this->assertEquals(
496                 2,
497                 $DB->count_records_sql("SELECT COUNT(gm.id)
498                                           FROM {groups_members} gm
499                                           JOIN {groups} g ON gm.groupid = g.id
500                                          WHERE g.courseid = ?", [$course3->id])
501         );
502         $this->assertEquals(
503                 2,
504                 $DB->count_records_sql("SELECT COUNT(gm.id)
505                                           FROM {groups_members} gm
506                                           JOIN {groups} g ON gm.groupid = g.id
507                                          WHERE gm.userid = ?", [$user1->id])
508         );
509     }
511     /**
512      * Test for provider::delete_groups_for_user() to check deleting from cache.
513      */
514     public function test_delete_groups_for_user_deletes_cache() {
515         $this->resetAfterTest();
517         $course = $this->getDataGenerator()->create_course();
519         $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
520         $group2 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
522         $user = $this->getDataGenerator()->create_user();
524         $this->getDataGenerator()->enrol_user($user->id, $course->id);
526         $this->getDataGenerator()->create_group_member(array('userid' => $user->id, 'groupid' => $group1->id));
527         $this->getDataGenerator()->create_group_member(array('userid' => $user->id, 'groupid' => $group2->id));
529         $this->assertEquals([[$group1->id, $group2->id]], groups_get_user_groups($course->id, $user->id), '', 0.0, 10, true);
531         $this->setUser($user);
532         $coursecontext = context_course::instance($course->id);
533         $approvedcontextlist = new \core_privacy\tests\request\approved_contextlist($user, 'core_group', [$coursecontext->id]);
534         provider::delete_groups_for_user($approvedcontextlist, '');
536         $this->assertEquals([[]], groups_get_user_groups($course->id, $user->id));
537     }
539     /**
540      * Test for provider::get_contexts_for_userid().
541      */
542     public function test_get_contexts_for_userid() {
543         $this->resetAfterTest();
545         $course1 = $this->getDataGenerator()->create_course();
546         $course2 = $this->getDataGenerator()->create_course();
547         $course3 = $this->getDataGenerator()->create_course();
549         $group1a = $this->getDataGenerator()->create_group(array('courseid' => $course1->id));
550         $group1b = $this->getDataGenerator()->create_group(array('courseid' => $course1->id));
551         $group2a = $this->getDataGenerator()->create_group(array('courseid' => $course2->id));
552         $group2b = $this->getDataGenerator()->create_group(array('courseid' => $course2->id));
553         $group3a = $this->getDataGenerator()->create_group(array('courseid' => $course3->id));
554         $group3b = $this->getDataGenerator()->create_group(array('courseid' => $course3->id));
556         $user1 = $this->getDataGenerator()->create_user();
557         $user2 = $this->getDataGenerator()->create_user();
559         $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
560         $this->getDataGenerator()->enrol_user($user1->id, $course2->id);
561         $this->getDataGenerator()->enrol_user($user1->id, $course3->id);
562         $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
563         $this->getDataGenerator()->enrol_user($user2->id, $course2->id);
564         $this->getDataGenerator()->enrol_user($user2->id, $course3->id);
566         $this->getDataGenerator()->create_group_member(array('userid' => $user1->id, 'groupid' => $group1a->id));
567         $this->getDataGenerator()->create_group_member(array('userid' => $user1->id, 'groupid' => $group2a->id));
568         $this->getDataGenerator()->create_group_member(array('userid' => $user2->id, 'groupid' => $group1b->id));
569         $this->getDataGenerator()->create_group_member(array('userid' => $user2->id, 'groupid' => $group2b->id));
570         $this->getDataGenerator()->create_group_member(array('userid' => $user2->id, 'groupid' => $group3b->id));
572         $coursecontext1 = context_course::instance($course1->id);
573         $coursecontext2 = context_course::instance($course2->id);
575         // User1 is member of some groups in course1 and course2.
576         $contextlist = provider::get_contexts_for_userid($user1->id);
577         $this->assertCount(2, $contextlist);
578         $this->assertEquals(
579                 [$coursecontext1->id, $coursecontext2->id],
580                 $contextlist->get_contextids(),
581                 '', 0.0, 10, true);
582     }
584     /**
585      * Test for provider::get_contexts_for_userid() when there are group memberships from other components.
586      */
587     public function test_get_contexts_for_userid_component() {
588         $this->resetAfterTest();
590         $course1 = $this->getDataGenerator()->create_course();
591         $course2 = $this->getDataGenerator()->create_course();
593         $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course1->id));
594         $group2 = $this->getDataGenerator()->create_group(array('courseid' => $course2->id));
596         $user = $this->getDataGenerator()->create_user();
598         $this->getDataGenerator()->enrol_user($user->id, $course1->id);
599         $this->getDataGenerator()->enrol_user($user->id, $course2->id);
601         $this->getDataGenerator()->create_group_member(
602                 array(
603                     'userid' => $user->id,
604                     'groupid' => $group1->id
605                 ));
606         $this->getDataGenerator()->create_group_member(
607                 array(
608                     'userid' => $user->id,
609                     'groupid' => $group2->id,
610                     'component' => 'enrol_meta'
611                 ));
613         $coursecontext1 = context_course::instance($course1->id);
615         // User is member of some groups in course1 and course2,
616         // but only the membership in course1 is directly managed by core_group.
617         $contextlist = provider::get_contexts_for_userid($user->id);
618         $this->assertEquals([$coursecontext1->id], $contextlist->get_contextids());
619     }
621     /**
622      * Test for provider::export_user_data().
623      */
624     public function test_export_user_data() {
625         $this->resetAfterTest();
627         $course = $this->getDataGenerator()->create_course();
628         $user1 = $this->getDataGenerator()->create_user();
629         $user2 = $this->getDataGenerator()->create_user();
630         $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
631         $group2 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
632         $group3 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
633         $group4 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
634         $this->getDataGenerator()->enrol_user($user1->id, $course->id);
635         $this->getDataGenerator()->enrol_user($user2->id, $course->id);
637         // Add user1 to group1 and group2.
638         $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id, 'userid' => $user1->id));
639         $this->getDataGenerator()->create_group_member(array('groupid' => $group2->id, 'userid' => $user1->id));
641         // Add user2 to group2 and group3.
642         $this->getDataGenerator()->create_group_member(array('groupid' => $group2->id, 'userid' => $user2->id));
643         $this->getDataGenerator()->create_group_member(array('groupid' => $group3->id, 'userid' => $user2->id));
645         $context = context_course::instance($course->id);
647         $this->setUser($user1);
649         // Export all of the data for the context.
650         $this->export_context_data_for_user($user1->id, $context, 'core_group');
652         $writer = writer::with_context($context);
653         $this->assertTrue($writer->has_any_data());
655         $data = $writer->get_data([get_string('groups', 'core_group')]);
656         $exportedgroups = $data->groups;
658         // User1 belongs to group1 and group2.
659         $this->assertEquals(
660                 [$group1->name, $group2->name],
661                 array_column($exportedgroups, 'name'),
662                 '', 0.0, 10, true);
663     }
665     /**
666      * Test for provider::delete_data_for_all_users_in_context().
667      */
668     public function test_delete_data_for_all_users_in_context() {
669         global $DB;
671         $this->resetAfterTest();
673         $course1 = $this->getDataGenerator()->create_course();
674         $course2 = $this->getDataGenerator()->create_course();
676         $group1a = $this->getDataGenerator()->create_group(array('courseid' => $course1->id));
677         $group1b = $this->getDataGenerator()->create_group(array('courseid' => $course1->id));
678         $group2a = $this->getDataGenerator()->create_group(array('courseid' => $course2->id));
679         $group2b = $this->getDataGenerator()->create_group(array('courseid' => $course2->id));
681         $user1 = $this->getDataGenerator()->create_user();
682         $user2 = $this->getDataGenerator()->create_user();
684         $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
685         $this->getDataGenerator()->enrol_user($user1->id, $course2->id);
686         $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
687         $this->getDataGenerator()->enrol_user($user2->id, $course2->id);
689         $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user1->id));
690         $this->getDataGenerator()->create_group_member(array('groupid' => $group1b->id, 'userid' => $user2->id));
691         $this->getDataGenerator()->create_group_member(array('groupid' => $group2a->id, 'userid' => $user1->id));
692         $this->getDataGenerator()->create_group_member(array('groupid' => $group2b->id, 'userid' => $user2->id));
694         $this->assertEquals(
695                 2,
696                 $DB->count_records_sql("SELECT COUNT(gm.id)
697                                           FROM {groups_members} gm
698                                           JOIN {groups} g ON gm.groupid = g.id
699                                          WHERE g.courseid = ?", [$course1->id])
700         );
701         $this->assertEquals(
702                 2,
703                 $DB->count_records_sql("SELECT COUNT(gm.id)
704                                           FROM {groups_members} gm
705                                           JOIN {groups} g ON gm.groupid = g.id
706                                          WHERE g.courseid = ?", [$course2->id])
707         );
709         $coursecontext1 = context_course::instance($course1->id);
710         provider::delete_data_for_all_users_in_context($coursecontext1);
712         $this->assertEquals(
713                 0,
714                 $DB->count_records_sql("SELECT COUNT(gm.id)
715                                           FROM {groups_members} gm
716                                           JOIN {groups} g ON gm.groupid = g.id
717                                          WHERE g.courseid = ?", [$course1->id])
718         );
719         $this->assertEquals(
720                 2,
721                 $DB->count_records_sql("SELECT COUNT(gm.id)
722                                           FROM {groups_members} gm
723                                           JOIN {groups} g ON gm.groupid = g.id
724                                          WHERE g.courseid = ?", [$course2->id])
725         );
726     }
728     /**
729      * Test for provider::delete_data_for_user().
730      */
731     public function test_delete_data_for_user() {
732         global $DB;
734         $this->resetAfterTest();
736         $course1 = $this->getDataGenerator()->create_course();
737         $course2 = $this->getDataGenerator()->create_course();
738         $course3 = $this->getDataGenerator()->create_course();
740         $group1a = $this->getDataGenerator()->create_group(array('courseid' => $course1->id));
741         $group1b = $this->getDataGenerator()->create_group(array('courseid' => $course1->id));
742         $group2a = $this->getDataGenerator()->create_group(array('courseid' => $course2->id));
743         $group2b = $this->getDataGenerator()->create_group(array('courseid' => $course2->id));
744         $group3a = $this->getDataGenerator()->create_group(array('courseid' => $course3->id));
745         $group3b = $this->getDataGenerator()->create_group(array('courseid' => $course3->id));
747         $user1 = $this->getDataGenerator()->create_user();
748         $user2 = $this->getDataGenerator()->create_user();
750         $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
751         $this->getDataGenerator()->enrol_user($user1->id, $course2->id);
752         $this->getDataGenerator()->enrol_user($user1->id, $course3->id);
753         $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
754         $this->getDataGenerator()->enrol_user($user2->id, $course2->id);
755         $this->getDataGenerator()->enrol_user($user2->id, $course3->id);
757         $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user1->id));
758         $this->getDataGenerator()->create_group_member(array('groupid' => $group1b->id, 'userid' => $user2->id));
759         $this->getDataGenerator()->create_group_member(array('groupid' => $group2a->id, 'userid' => $user1->id));
760         $this->getDataGenerator()->create_group_member(array('groupid' => $group2b->id, 'userid' => $user2->id));
761         $this->getDataGenerator()->create_group_member(array('groupid' => $group3a->id, 'userid' => $user1->id));
762         $this->getDataGenerator()->create_group_member(array('groupid' => $group3b->id, 'userid' => $user2->id));
764         $this->assertEquals(
765                 2,
766                 $DB->count_records_sql("SELECT COUNT(gm.id)
767                                           FROM {groups_members} gm
768                                           JOIN {groups} g ON gm.groupid = g.id
769                                          WHERE g.courseid = ?", [$course1->id])
770         );
771         $this->assertEquals(
772                 2,
773                 $DB->count_records_sql("SELECT COUNT(gm.id)
774                                           FROM {groups_members} gm
775                                           JOIN {groups} g ON gm.groupid = g.id
776                                          WHERE g.courseid = ?", [$course2->id])
777         );
778         $this->assertEquals(
779                 2,
780                 $DB->count_records_sql("SELECT COUNT(gm.id)
781                                           FROM {groups_members} gm
782                                           JOIN {groups} g ON gm.groupid = g.id
783                                          WHERE g.courseid = ?", [$course2->id])
784         );
785         $this->assertEquals(
786                 3,
787                 $DB->count_records_sql("SELECT COUNT(gm.id)
788                                           FROM {groups_members} gm
789                                           JOIN {groups} g ON gm.groupid = g.id
790                                          WHERE gm.userid = ?", [$user1->id])
791         );
793         $this->setUser($user1);
794         $coursecontext1 = context_course::instance($course1->id);
795         $coursecontext2 = context_course::instance($course2->id);
796         $approvedcontextlist = new \core_privacy\tests\request\approved_contextlist($user1, 'core_group',
797                 [$coursecontext1->id, $coursecontext2->id]);
798         provider::delete_data_for_user($approvedcontextlist);
800         $this->assertEquals(
801                 1,
802                 $DB->count_records_sql("SELECT COUNT(gm.id)
803                                           FROM {groups_members} gm
804                                           JOIN {groups} g ON gm.groupid = g.id
805                                          WHERE g.courseid = ?", [$course1->id])
806         );
807         $this->assertEquals(
808                 1,
809                 $DB->count_records_sql("SELECT COUNT(gm.id)
810                                           FROM {groups_members} gm
811                                           JOIN {groups} g ON gm.groupid = g.id
812                                          WHERE g.courseid = ?", [$course2->id])
813         );
814         $this->assertEquals(
815                 2,
816                 $DB->count_records_sql("SELECT COUNT(gm.id)
817                                           FROM {groups_members} gm
818                                           JOIN {groups} g ON gm.groupid = g.id
819                                          WHERE g.courseid = ?", [$course3->id])
820         );
821         $this->assertEquals(
822                 1,
823                 $DB->count_records_sql("SELECT COUNT(gm.id)
824                                           FROM {groups_members} gm
825                                           JOIN {groups} g ON gm.groupid = g.id
826                                          WHERE gm.userid = ?", [$user1->id])
827         );
828     }
830     /**
831      * Test for provider::delete_data_for_users().
832      */
833     public function test_delete_data_for_users() {
834         global $DB;
836         $this->resetAfterTest();
838         $course1 = $this->getDataGenerator()->create_course();
839         $course2 = $this->getDataGenerator()->create_course();
841         $group1a = $this->getDataGenerator()->create_group(array('courseid' => $course1->id));
842         $group1b = $this->getDataGenerator()->create_group(array('courseid' => $course1->id));
843         $group1c = $this->getDataGenerator()->create_group(array('courseid' => $course1->id));
844         $group2a = $this->getDataGenerator()->create_group(array('courseid' => $course2->id));
845         $group2b = $this->getDataGenerator()->create_group(array('courseid' => $course2->id));
846         $group2c = $this->getDataGenerator()->create_group(array('courseid' => $course2->id));
848         $user1 = $this->getDataGenerator()->create_user();
849         $user2 = $this->getDataGenerator()->create_user();
850         $user3 = $this->getDataGenerator()->create_user();
852         $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
853         $this->getDataGenerator()->enrol_user($user1->id, $course2->id);
854         $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
855         $this->getDataGenerator()->enrol_user($user2->id, $course2->id);
856         $this->getDataGenerator()->enrol_user($user3->id, $course1->id);
857         $this->getDataGenerator()->enrol_user($user3->id, $course2->id);
859         $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user1->id));
860         $this->getDataGenerator()->create_group_member(array('groupid' => $group1b->id, 'userid' => $user2->id));
861         $this->getDataGenerator()->create_group_member(array('groupid' => $group1c->id, 'userid' => $user3->id));
862         $this->getDataGenerator()->create_group_member(array('groupid' => $group2a->id, 'userid' => $user1->id));
863         $this->getDataGenerator()->create_group_member(array('groupid' => $group2b->id, 'userid' => $user2->id));
864         $this->getDataGenerator()->create_group_member(array('groupid' => $group2c->id, 'userid' => $user3->id));
866         $this->assertEquals(
867                 3,
868                 $DB->count_records_sql("SELECT COUNT(gm.id)
869                                           FROM {groups_members} gm
870                                           JOIN {groups} g ON gm.groupid = g.id
871                                          WHERE g.courseid = ?", [$course1->id])
872         );
873         $this->assertEquals(
874                 3,
875                 $DB->count_records_sql("SELECT COUNT(gm.id)
876                                           FROM {groups_members} gm
877                                           JOIN {groups} g ON gm.groupid = g.id
878                                          WHERE g.courseid = ?", [$course2->id])
879         );
881         $coursecontext1 = context_course::instance($course1->id);
882         $approveduserlist = new \core_privacy\local\request\approved_userlist($coursecontext1, 'core_group',
883                 [$user1->id, $user2->id]);
884         provider::delete_data_for_users($approveduserlist);
886         $this->assertEquals(
887                 [$user3->id],
888                 $DB->get_fieldset_sql("SELECT gm.userid
889                                          FROM {groups_members} gm
890                                          JOIN {groups} g ON gm.groupid = g.id
891                                         WHERE g.courseid = ?", [$course1->id])
892         );
893         $this->assertEquals(
894                 3,
895                 $DB->count_records_sql("SELECT COUNT(gm.id)
896                                           FROM {groups_members} gm
897                                           JOIN {groups} g ON gm.groupid = g.id
898                                          WHERE g.courseid = ?", [$course2->id])
899         );
900     }
902     /**
903      * Test for provider::get_users_in_context().
904      */
905     public function test_get_users_in_context() {
906         $this->resetAfterTest();
908         $course1 = $this->getDataGenerator()->create_course();
909         $course2 = $this->getDataGenerator()->create_course();
911         $group1a = $this->getDataGenerator()->create_group(array('courseid' => $course1->id));
912         $group1b = $this->getDataGenerator()->create_group(array('courseid' => $course1->id));
913         $group2a = $this->getDataGenerator()->create_group(array('courseid' => $course2->id));
914         $group2b = $this->getDataGenerator()->create_group(array('courseid' => $course2->id));
916         $user1 = $this->getDataGenerator()->create_user();
917         $user2 = $this->getDataGenerator()->create_user();
918         $user3 = $this->getDataGenerator()->create_user();
920         $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
921         $this->getDataGenerator()->enrol_user($user1->id, $course2->id);
922         $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
923         $this->getDataGenerator()->enrol_user($user2->id, $course2->id);
924         $this->getDataGenerator()->enrol_user($user3->id, $course1->id);
925         $this->getDataGenerator()->enrol_user($user3->id, $course2->id);
927         $this->getDataGenerator()->create_group_member(array('userid' => $user1->id, 'groupid' => $group1a->id));
928         $this->getDataGenerator()->create_group_member(array('userid' => $user1->id, 'groupid' => $group2a->id));
929         $this->getDataGenerator()->create_group_member(array('userid' => $user2->id, 'groupid' => $group1b->id));
930         $this->getDataGenerator()->create_group_member(array('userid' => $user2->id, 'groupid' => $group2b->id));
931         $this->getDataGenerator()->create_group_member(array('userid' => $user3->id, 'groupid' => $group2a->id));
933         $coursecontext1 = context_course::instance($course1->id);
935         $userlist = new \core_privacy\local\request\userlist($coursecontext1, 'core_group');
936         \core_group\privacy\provider::get_users_in_context($userlist);
938         // Only user1 and user2. User3 is not member of any group in course1.
939         $this->assertCount(2, $userlist);
940         $this->assertEquals(
941                 [$user1->id, $user2->id],
942                 $userlist->get_userids(),
943                 '', 0.0, 10, true);
944     }