MDL-63692 core_group: Add group conversation messages to Privacy API
[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(2, $itemcollection);
50         $table = array_shift($itemcollection);
51         $this->assertEquals('groups_members', $table->get_name());
52         $this->assertEquals('privacy:metadata:groups', $table->get_summary());
54         $privacyfields = $table->get_privacy_fields();
55         $this->assertArrayHasKey('groupid', $privacyfields);
56         $this->assertArrayHasKey('userid', $privacyfields);
57         $this->assertArrayHasKey('timeadded', $privacyfields);
59         $table = array_shift($itemcollection);
60         $this->assertEquals('core_message', $table->get_name());
61         $this->assertEquals('privacy:metadata:core_message', $table->get_summary());
62     }
64     /**
65      * Test for provider::export_groups() to export manual group memberships.
66      */
67     public function test_export_groups() {
68         $this->resetAfterTest();
70         $course = $this->getDataGenerator()->create_course();
71         $user1 = $this->getDataGenerator()->create_user();
72         $user2 = $this->getDataGenerator()->create_user();
73         $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
74         $group2 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
75         $group3 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
76         $group4 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
77         $this->getDataGenerator()->enrol_user($user1->id, $course->id);
78         $this->getDataGenerator()->enrol_user($user2->id, $course->id);
80         // Add user1 to group1 and group2.
81         $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id, 'userid' => $user1->id));
82         $this->getDataGenerator()->create_group_member(array('groupid' => $group2->id, 'userid' => $user1->id));
84         // Add user2 to group2 and group3.
85         $this->getDataGenerator()->create_group_member(array('groupid' => $group2->id, 'userid' => $user2->id));
86         $this->getDataGenerator()->create_group_member(array('groupid' => $group3->id, 'userid' => $user2->id));
88         $context = context_course::instance($course->id);
90         // Retrieve groups for user1.
91         $this->setUser($user1);
92         $writer = writer::with_context($context);
93         provider::export_groups($context, '');
95         $data = $writer->get_data([get_string('groups', 'core_group')]);
96         $exportedgroups = $data->groups;
98         // User1 belongs to group1 and group2.
99         $this->assertEquals(
100                 [$group1->name, $group2->name],
101                 array_column($exportedgroups, 'name'),
102                 '', 0.0, 10, true);
103     }
105     /**
106      * Test for provider::export_groups() to export group memberships of a component.
107      */
108     public function test_export_groups_for_component() {
109         $this->resetAfterTest();
111         $course = $this->getDataGenerator()->create_course();
112         $user1 = $this->getDataGenerator()->create_user();
113         $user2 = $this->getDataGenerator()->create_user();
114         $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
115         $group2 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
116         $group3 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
117         $group4 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
118         $group5 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
119         $this->getDataGenerator()->enrol_user($user1->id, $course->id, null, 'self');
120         $this->getDataGenerator()->enrol_user($user2->id, $course->id, null, 'self');
122         // Add user1 to group1 (via enrol_self) and group2 and group3.
123         $this->getDataGenerator()->create_group_member(
124                 array('groupid' => $group1->id, 'userid' => $user1->id, 'component' => 'enrol_self'));
125         $this->getDataGenerator()->create_group_member(array('groupid' => $group2->id, 'userid' => $user1->id));
126         $this->getDataGenerator()->create_group_member(array('groupid' => $group3->id, 'userid' => $user1->id));
128         // Add user2 to group3 (via enrol_self) and group4.
129         $this->getDataGenerator()->create_group_member(
130                 array('groupid' => $group3->id, 'userid' => $user2->id, 'component' => 'enrol_self'));
131         $this->getDataGenerator()->create_group_member(array('groupid' => $group4->id, 'userid' => $user2->id));
133         $context = context_course::instance($course->id);
135         // Retrieve groups for user1.
136         $this->setUser($user1);
137         $writer = writer::with_context($context);
138         provider::export_groups($context, 'enrol_self');
140         $data = $writer->get_data([get_string('groups', 'core_group')]);
141         $exportedgroups = $data->groups;
143         // User1 only belongs to group1 via enrol_self.
144         $this->assertCount(1, $exportedgroups);
145         $exportedgroup = reset($exportedgroups);
146         $this->assertEquals($group1->name, $exportedgroup->name);
147     }
149     /**
150      * Test for provider::delete_groups_for_all_users() to delete manual group memberships.
151      */
152     public function test_delete_groups_for_all_users() {
153         global $DB;
155         $this->resetAfterTest();
157         $course1 = $this->getDataGenerator()->create_course();
158         $course2 = $this->getDataGenerator()->create_course();
160         $group1a = $this->getDataGenerator()->create_group(array('courseid' => $course1->id));
161         $group1b = $this->getDataGenerator()->create_group(array('courseid' => $course1->id));
162         $group2a = $this->getDataGenerator()->create_group(array('courseid' => $course2->id));
163         $group2b = $this->getDataGenerator()->create_group(array('courseid' => $course2->id));
165         $user1 = $this->getDataGenerator()->create_user();
166         $user2 = $this->getDataGenerator()->create_user();
168         $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
169         $this->getDataGenerator()->enrol_user($user1->id, $course2->id);
170         $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
171         $this->getDataGenerator()->enrol_user($user2->id, $course2->id);
173         $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user1->id));
174         $this->getDataGenerator()->create_group_member(array('groupid' => $group1b->id, 'userid' => $user2->id));
175         $this->getDataGenerator()->create_group_member(array('groupid' => $group2a->id, 'userid' => $user1->id));
176         $this->getDataGenerator()->create_group_member(array('groupid' => $group2b->id, 'userid' => $user2->id));
178         $this->assertEquals(
179                 2,
180                 $DB->count_records_sql("SELECT COUNT(gm.id)
181                                       FROM {groups_members} gm
182                                       JOIN {groups} g ON gm.groupid = g.id
183                                      WHERE g.courseid = ?", [$course1->id])
184         );
185         $this->assertEquals(
186                 2,
187                 $DB->count_records_sql("SELECT COUNT(gm.id)
188                                       FROM {groups_members} gm
189                                       JOIN {groups} g ON gm.groupid = g.id
190                                      WHERE g.courseid = ?", [$course2->id])
191         );
193         $coursecontext1 = context_course::instance($course1->id);
194         provider::delete_groups_for_all_users($coursecontext1, '');
196         $this->assertEquals(
197             0,
198             $DB->count_records_sql("SELECT COUNT(gm.id)
199                                       FROM {groups_members} gm
200                                       JOIN {groups} g ON gm.groupid = g.id
201                                      WHERE g.courseid = ?", [$course1->id])
202         );
203         $this->assertEquals(
204             2,
205             $DB->count_records_sql("SELECT COUNT(gm.id)
206                                       FROM {groups_members} gm
207                                       JOIN {groups} g ON gm.groupid = g.id
208                                      WHERE g.courseid = ?", [$course2->id])
209         );
210     }
212     /**
213      * Test for provider::delete_groups_for_all_users() to delete group memberships of a component.
214      */
215     public function test_delete_groups_for_all_users_for_component() {
216         global $DB;
218         $this->resetAfterTest();
220         $course1 = $this->getDataGenerator()->create_course();
221         $course2 = $this->getDataGenerator()->create_course();
223         $group1a = $this->getDataGenerator()->create_group(array('courseid' => $course1->id));
224         $group1b = $this->getDataGenerator()->create_group(array('courseid' => $course1->id));
225         $group2a = $this->getDataGenerator()->create_group(array('courseid' => $course2->id));
226         $group2b = $this->getDataGenerator()->create_group(array('courseid' => $course2->id));
228         $user1 = $this->getDataGenerator()->create_user();
229         $user2 = $this->getDataGenerator()->create_user();
231         $this->getDataGenerator()->enrol_user($user1->id, $course1->id, null, 'self');
232         $this->getDataGenerator()->enrol_user($user1->id, $course2->id, null, 'self');
233         $this->getDataGenerator()->enrol_user($user2->id, $course1->id, null, 'self');
234         $this->getDataGenerator()->enrol_user($user2->id, $course2->id, null, 'self');
236         $this->getDataGenerator()->create_group_member(
237                 array('groupid' => $group1a->id, 'userid' => $user1->id, 'component' => 'enrol_self'));
238         $this->getDataGenerator()->create_group_member(array('groupid' => $group1b->id, 'userid' => $user2->id));
239         $this->getDataGenerator()->create_group_member(
240                 array('groupid' => $group2a->id, 'userid' => $user1->id, 'component' => 'enrol_self'));
241         $this->getDataGenerator()->create_group_member(array('groupid' => $group2b->id, 'userid' => $user2->id));
243         $this->assertEquals(
244                 2,
245                 $DB->count_records_sql("SELECT COUNT(gm.id)
246                                       FROM {groups_members} gm
247                                       JOIN {groups} g ON gm.groupid = g.id
248                                      WHERE g.courseid = ?", [$course1->id])
249         );
250         $this->assertEquals(
251                 2,
252                 $DB->count_records_sql("SELECT COUNT(gm.id)
253                                       FROM {groups_members} gm
254                                       JOIN {groups} g ON gm.groupid = g.id
255                                      WHERE g.courseid = ?", [$course2->id])
256         );
258         $coursecontext1 = context_course::instance($course1->id);
259         provider::delete_groups_for_all_users($coursecontext1, 'enrol_self');
261         $this->assertEquals(
262             1,
263             $DB->count_records_sql("SELECT COUNT(gm.id)
264                                       FROM {groups_members} gm
265                                       JOIN {groups} g ON gm.groupid = g.id
266                                      WHERE g.courseid = ?", [$course1->id])
267         );
268         $this->assertEquals(
269             2,
270             $DB->count_records_sql("SELECT COUNT(gm.id)
271                                       FROM {groups_members} gm
272                                       JOIN {groups} g ON gm.groupid = g.id
273                                      WHERE g.courseid = ?", [$course2->id])
274         );
275     }
277     /**
278      * Test for provider::delete_groups_for_all_users() to check deleting from cache.
279      */
280     public function test_delete_groups_for_all_users_deletes_cache() {
281         $this->resetAfterTest();
283         $course = $this->getDataGenerator()->create_course();
285         $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
286         $group2 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
288         $user1 = $this->getDataGenerator()->create_user();
289         $user2 = $this->getDataGenerator()->create_user();
291         $this->getDataGenerator()->enrol_user($user1->id, $course->id);
292         $this->getDataGenerator()->enrol_user($user2->id, $course->id);
294         $this->getDataGenerator()->create_group_member(array('userid' => $user1->id, 'groupid' => $group1->id));
295         $this->getDataGenerator()->create_group_member(array('userid' => $user1->id, 'groupid' => $group2->id));
296         $this->getDataGenerator()->create_group_member(array('userid' => $user2->id, 'groupid' => $group1->id));
298         $this->assertEquals([[$group1->id, $group2->id]], groups_get_user_groups($course->id, $user1->id), '', 0.0, 10, true);
299         $this->assertEquals([[$group1->id]], groups_get_user_groups($course->id, $user2->id));
301         $coursecontext = context_course::instance($course->id);
302         provider::delete_groups_for_all_users($coursecontext, '');
304         $this->assertEquals([[]], groups_get_user_groups($course->id, $user1->id));
305         $this->assertEquals([[]], groups_get_user_groups($course->id, $user2->id));
306     }
308     /**
309      * Test for provider::delete_groups_for_user() to delete manual group memberships.
310      */
311     public function test_delete_groups_for_user() {
312         global $DB;
314         $this->resetAfterTest();
316         $course1 = $this->getDataGenerator()->create_course();
317         $course2 = $this->getDataGenerator()->create_course();
318         $course3 = $this->getDataGenerator()->create_course();
320         $group1a = $this->getDataGenerator()->create_group(array('courseid' => $course1->id));
321         $group1b = $this->getDataGenerator()->create_group(array('courseid' => $course1->id));
322         $group2a = $this->getDataGenerator()->create_group(array('courseid' => $course2->id));
323         $group2b = $this->getDataGenerator()->create_group(array('courseid' => $course2->id));
324         $group3a = $this->getDataGenerator()->create_group(array('courseid' => $course3->id));
325         $group3b = $this->getDataGenerator()->create_group(array('courseid' => $course3->id));
327         $user1 = $this->getDataGenerator()->create_user();
328         $user2 = $this->getDataGenerator()->create_user();
330         $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
331         $this->getDataGenerator()->enrol_user($user1->id, $course2->id);
332         $this->getDataGenerator()->enrol_user($user1->id, $course3->id);
333         $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
334         $this->getDataGenerator()->enrol_user($user2->id, $course2->id);
335         $this->getDataGenerator()->enrol_user($user2->id, $course3->id);
337         $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user1->id));
338         $this->getDataGenerator()->create_group_member(array('groupid' => $group1b->id, 'userid' => $user2->id));
339         $this->getDataGenerator()->create_group_member(array('groupid' => $group2a->id, 'userid' => $user1->id));
340         $this->getDataGenerator()->create_group_member(array('groupid' => $group2b->id, 'userid' => $user2->id));
341         $this->getDataGenerator()->create_group_member(array('groupid' => $group3a->id, 'userid' => $user1->id));
342         $this->getDataGenerator()->create_group_member(array('groupid' => $group3b->id, 'userid' => $user2->id));
344         $this->assertEquals(
345                 2,
346                 $DB->count_records_sql("SELECT COUNT(gm.id)
347                                           FROM {groups_members} gm
348                                           JOIN {groups} g ON gm.groupid = g.id
349                                          WHERE g.courseid = ?", [$course1->id])
350         );
351         $this->assertEquals(
352                 2,
353                 $DB->count_records_sql("SELECT COUNT(gm.id)
354                                           FROM {groups_members} gm
355                                           JOIN {groups} g ON gm.groupid = g.id
356                                          WHERE g.courseid = ?", [$course2->id])
357         );
358         $this->assertEquals(
359                 2,
360                 $DB->count_records_sql("SELECT COUNT(gm.id)
361                                           FROM {groups_members} gm
362                                           JOIN {groups} g ON gm.groupid = g.id
363                                          WHERE g.courseid = ?", [$course2->id])
364         );
365         $this->assertEquals(
366                 3,
367                 $DB->count_records_sql("SELECT COUNT(gm.id)
368                                           FROM {groups_members} gm
369                                           JOIN {groups} g ON gm.groupid = g.id
370                                          WHERE gm.userid = ?", [$user1->id])
371         );
373         $this->setUser($user1);
374         $coursecontext1 = context_course::instance($course1->id);
375         $coursecontext2 = context_course::instance($course2->id);
376         $approvedcontextlist = new \core_privacy\tests\request\approved_contextlist($user1, 'core_group',
377                 [$coursecontext1->id, $coursecontext2->id]);
378         provider::delete_groups_for_user($approvedcontextlist, '');
380         $this->assertEquals(
381                 1,
382                 $DB->count_records_sql("SELECT COUNT(gm.id)
383                                           FROM {groups_members} gm
384                                           JOIN {groups} g ON gm.groupid = g.id
385                                          WHERE g.courseid = ?", [$course1->id])
386         );
387         $this->assertEquals(
388                 1,
389                 $DB->count_records_sql("SELECT COUNT(gm.id)
390                                           FROM {groups_members} gm
391                                           JOIN {groups} g ON gm.groupid = g.id
392                                          WHERE g.courseid = ?", [$course2->id])
393         );
394         $this->assertEquals(
395                 2,
396                 $DB->count_records_sql("SELECT COUNT(gm.id)
397                                           FROM {groups_members} gm
398                                           JOIN {groups} g ON gm.groupid = g.id
399                                          WHERE g.courseid = ?", [$course3->id])
400         );
401         $this->assertEquals(
402                 1,
403                 $DB->count_records_sql("SELECT COUNT(gm.id)
404                                           FROM {groups_members} gm
405                                           JOIN {groups} g ON gm.groupid = g.id
406                                          WHERE gm.userid = ?", [$user1->id])
407         );
408     }
410     /**
411      * Test for provider::delete_groups_for_user() to delete group memberships of a component.
412      */
413     public function test_delete_groups_for_user_for_component() {
414         global $DB;
416         $this->resetAfterTest();
418         $course1 = $this->getDataGenerator()->create_course();
419         $course2 = $this->getDataGenerator()->create_course();
420         $course3 = $this->getDataGenerator()->create_course();
422         $group1a = $this->getDataGenerator()->create_group(array('courseid' => $course1->id));
423         $group1b = $this->getDataGenerator()->create_group(array('courseid' => $course1->id));
424         $group2a = $this->getDataGenerator()->create_group(array('courseid' => $course2->id));
425         $group2b = $this->getDataGenerator()->create_group(array('courseid' => $course2->id));
426         $group3a = $this->getDataGenerator()->create_group(array('courseid' => $course3->id));
427         $group3b = $this->getDataGenerator()->create_group(array('courseid' => $course3->id));
429         $user1 = $this->getDataGenerator()->create_user();
430         $user2 = $this->getDataGenerator()->create_user();
432         $this->getDataGenerator()->enrol_user($user1->id, $course1->id, null, 'self');
433         $this->getDataGenerator()->enrol_user($user1->id, $course2->id, null, 'self');
434         $this->getDataGenerator()->enrol_user($user1->id, $course3->id, null, 'self');
435         $this->getDataGenerator()->enrol_user($user2->id, $course1->id, null, 'self');
436         $this->getDataGenerator()->enrol_user($user2->id, $course2->id, null, 'self');
437         $this->getDataGenerator()->enrol_user($user2->id, $course3->id, null, 'self');
439         $this->getDataGenerator()->create_group_member(
440                 array('groupid' => $group1a->id, 'userid' => $user1->id, 'component' => 'enrol_self'));
441         $this->getDataGenerator()->create_group_member(
442                 array('groupid' => $group1b->id, 'userid' => $user2->id, 'component' => 'enrol_self'));
443         $this->getDataGenerator()->create_group_member(array('groupid' => $group2a->id, 'userid' => $user1->id));
444         $this->getDataGenerator()->create_group_member(array('groupid' => $group2b->id, 'userid' => $user2->id));
445         $this->getDataGenerator()->create_group_member(array('groupid' => $group3a->id, 'userid' => $user1->id));
446         $this->getDataGenerator()->create_group_member(array('groupid' => $group3b->id, 'userid' => $user2->id));
448         $this->assertEquals(
449                 2,
450                 $DB->count_records_sql("SELECT COUNT(gm.id)
451                                           FROM {groups_members} gm
452                                           JOIN {groups} g ON gm.groupid = g.id
453                                          WHERE g.courseid = ?", [$course1->id])
454         );
455         $this->assertEquals(
456                 2,
457                 $DB->count_records_sql("SELECT COUNT(gm.id)
458                                           FROM {groups_members} gm
459                                           JOIN {groups} g ON gm.groupid = g.id
460                                          WHERE g.courseid = ?", [$course2->id])
461         );
462         $this->assertEquals(
463                 2,
464                 $DB->count_records_sql("SELECT COUNT(gm.id)
465                                           FROM {groups_members} gm
466                                           JOIN {groups} g ON gm.groupid = g.id
467                                          WHERE g.courseid = ?", [$course2->id])
468         );
469         $this->assertEquals(
470                 3,
471                 $DB->count_records_sql("SELECT COUNT(gm.id)
472                                           FROM {groups_members} gm
473                                           JOIN {groups} g ON gm.groupid = g.id
474                                          WHERE gm.userid = ?", [$user1->id])
475         );
477         $this->setUser($user1);
478         $coursecontext1 = context_course::instance($course1->id);
479         $coursecontext2 = context_course::instance($course2->id);
480         $approvedcontextlist = new \core_privacy\tests\request\approved_contextlist($user1, 'core_group',
481                 [$coursecontext1->id, $coursecontext2->id]);
482         provider::delete_groups_for_user($approvedcontextlist, 'enrol_self');
484         $this->assertEquals(
485                 1,
486                 $DB->count_records_sql("SELECT COUNT(gm.id)
487                                           FROM {groups_members} gm
488                                           JOIN {groups} g ON gm.groupid = g.id
489                                          WHERE g.courseid = ?", [$course1->id])
490         );
491         $this->assertEquals(
492                 2,
493                 $DB->count_records_sql("SELECT COUNT(gm.id)
494                                           FROM {groups_members} gm
495                                           JOIN {groups} g ON gm.groupid = g.id
496                                          WHERE g.courseid = ?", [$course2->id])
497         );
498         $this->assertEquals(
499                 2,
500                 $DB->count_records_sql("SELECT COUNT(gm.id)
501                                           FROM {groups_members} gm
502                                           JOIN {groups} g ON gm.groupid = g.id
503                                          WHERE g.courseid = ?", [$course3->id])
504         );
505         $this->assertEquals(
506                 2,
507                 $DB->count_records_sql("SELECT COUNT(gm.id)
508                                           FROM {groups_members} gm
509                                           JOIN {groups} g ON gm.groupid = g.id
510                                          WHERE gm.userid = ?", [$user1->id])
511         );
512     }
514     /**
515      * Test for provider::delete_groups_for_users() to delete group memberships of a component.
516      */
517     public function test_delete_groups_for_users_for_component() {
518         global $DB;
520         $this->resetAfterTest();
522         $course1 = $this->getDataGenerator()->create_course();
523         $course2 = $this->getDataGenerator()->create_course();
524         $course3 = $this->getDataGenerator()->create_course();
526         $group1a = $this->getDataGenerator()->create_group(array('courseid' => $course1->id));
527         $group1b = $this->getDataGenerator()->create_group(array('courseid' => $course1->id));
528         $group2a = $this->getDataGenerator()->create_group(array('courseid' => $course2->id));
529         $group2b = $this->getDataGenerator()->create_group(array('courseid' => $course2->id));
530         $group3a = $this->getDataGenerator()->create_group(array('courseid' => $course3->id));
531         $group3b = $this->getDataGenerator()->create_group(array('courseid' => $course3->id));
533         $user1 = $this->getDataGenerator()->create_user();
534         $user2 = $this->getDataGenerator()->create_user();
536         $this->getDataGenerator()->enrol_user($user1->id, $course1->id, null, 'self');
537         $this->getDataGenerator()->enrol_user($user1->id, $course2->id, null, 'self');
538         $this->getDataGenerator()->enrol_user($user1->id, $course3->id, null, 'self');
539         $this->getDataGenerator()->enrol_user($user2->id, $course1->id, null, 'self');
540         $this->getDataGenerator()->enrol_user($user2->id, $course2->id, null, 'self');
541         $this->getDataGenerator()->enrol_user($user2->id, $course3->id, null, 'self');
543         $this->getDataGenerator()->create_group_member(
544                 array('groupid' => $group1a->id, 'userid' => $user1->id, 'component' => 'enrol_self'));
545         $this->getDataGenerator()->create_group_member(
546                 array('groupid' => $group1b->id, 'userid' => $user2->id, 'component' => 'enrol_self'));
547         $this->getDataGenerator()->create_group_member(
548                 array('groupid' => $group2a->id, 'userid' => $user1->id, 'component' => 'enrol_self'));
549         $this->getDataGenerator()->create_group_member(
550                 array('groupid' => $group2b->id, 'userid' => $user2->id, 'component' => 'enrol_self'));
551         $this->getDataGenerator()->create_group_member(array('groupid' => $group3a->id, 'userid' => $user1->id));
552         $this->getDataGenerator()->create_group_member(array('groupid' => $group3b->id, 'userid' => $user2->id));
554         $this->assertEquals(
555                 2,
556                 $DB->count_records_sql("SELECT COUNT(gm.id)
557                                           FROM {groups_members} gm
558                                           JOIN {groups} g ON gm.groupid = g.id
559                                          WHERE g.courseid = ?", [$course1->id])
560         );
561         $this->assertEquals(
562                 2,
563                 $DB->count_records_sql("SELECT COUNT(gm.id)
564                                           FROM {groups_members} gm
565                                           JOIN {groups} g ON gm.groupid = g.id
566                                          WHERE g.courseid = ?", [$course2->id])
567         );
568         $this->assertEquals(
569                 2,
570                 $DB->count_records_sql("SELECT COUNT(gm.id)
571                                           FROM {groups_members} gm
572                                           JOIN {groups} g ON gm.groupid = g.id
573                                          WHERE g.courseid = ?", [$course2->id])
574         );
575         $this->assertEquals(
576                 3,
577                 $DB->count_records_sql("SELECT COUNT(gm.id)
578                                           FROM {groups_members} gm
579                                           JOIN {groups} g ON gm.groupid = g.id
580                                          WHERE gm.userid = ?", [$user1->id])
581         );
583         // Delete user1 and user2 from groups in course1.
584         $coursecontext1 = context_course::instance($course1->id);
585         $approveduserlist = new \core_privacy\local\request\approved_userlist($coursecontext1, 'core_group',
586                 [$user1->id, $user2->id]);
587         provider::delete_groups_for_users($approveduserlist, 'enrol_self');
589         $this->assertEquals(
590                 0,
591                 $DB->count_records_sql("SELECT COUNT(gm.id)
592                                           FROM {groups_members} gm
593                                           JOIN {groups} g ON gm.groupid = g.id
594                                          WHERE g.courseid = ?", [$course1->id])
595         );
596         $this->assertEquals(
597                 2,
598                 $DB->count_records_sql("SELECT COUNT(gm.id)
599                                           FROM {groups_members} gm
600                                           JOIN {groups} g ON gm.groupid = g.id
601                                          WHERE g.courseid = ?", [$course2->id])
602         );
603         $this->assertEquals(
604                 2,
605                 $DB->count_records_sql("SELECT COUNT(gm.id)
606                                           FROM {groups_members} gm
607                                           JOIN {groups} g ON gm.groupid = g.id
608                                          WHERE g.courseid = ?", [$course3->id])
609         );
610         $this->assertEquals(
611                 2,
612                 $DB->count_records_sql("SELECT COUNT(gm.id)
613                                           FROM {groups_members} gm
614                                           JOIN {groups} g ON gm.groupid = g.id
615                                          WHERE gm.userid = ?", [$user1->id])
616         );
618         // Delete user1 and user2 from course3.
619         $coursecontext3 = context_course::instance($course3->id);
620         $approveduserlist = new \core_privacy\local\request\approved_userlist($coursecontext3, 'core_group',
621                 [$user1->id, $user2->id]);
622         provider::delete_groups_for_users($approveduserlist, 'enrol_self');
623         $this->assertEquals(
624                 0,
625                 $DB->count_records_sql("SELECT COUNT(gm.id)
626                                           FROM {groups_members} gm
627                                           JOIN {groups} g ON gm.groupid = g.id
628                                          WHERE g.courseid = ?", [$course1->id])
629         );
630         $this->assertEquals(
631                 2,
632                 $DB->count_records_sql("SELECT COUNT(gm.id)
633                                           FROM {groups_members} gm
634                                           JOIN {groups} g ON gm.groupid = g.id
635                                          WHERE g.courseid = ?", [$course2->id])
636         );
637         $this->assertEquals(
638                 2,
639                 $DB->count_records_sql("SELECT COUNT(gm.id)
640                                           FROM {groups_members} gm
641                                           JOIN {groups} g ON gm.groupid = g.id
642                                          WHERE g.courseid = ?", [$course3->id])
643         );
644         $this->assertEquals(
645                 2,
646                 $DB->count_records_sql("SELECT COUNT(gm.id)
647                                           FROM {groups_members} gm
648                                           JOIN {groups} g ON gm.groupid = g.id
649                                          WHERE gm.userid = ?", [$user1->id])
650         );
651     }
653     /**
654      * Test for provider::delete_groups_for_user() to check deleting from cache.
655      */
656     public function test_delete_groups_for_user_deletes_cache() {
657         $this->resetAfterTest();
659         $course = $this->getDataGenerator()->create_course();
661         $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
662         $group2 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
664         $user = $this->getDataGenerator()->create_user();
666         $this->getDataGenerator()->enrol_user($user->id, $course->id);
668         $this->getDataGenerator()->create_group_member(array('userid' => $user->id, 'groupid' => $group1->id));
669         $this->getDataGenerator()->create_group_member(array('userid' => $user->id, 'groupid' => $group2->id));
671         $this->assertEquals([[$group1->id, $group2->id]], groups_get_user_groups($course->id, $user->id), '', 0.0, 10, true);
673         $this->setUser($user);
674         $coursecontext = context_course::instance($course->id);
675         $approvedcontextlist = new \core_privacy\tests\request\approved_contextlist($user, 'core_group', [$coursecontext->id]);
676         provider::delete_groups_for_user($approvedcontextlist, '');
678         $this->assertEquals([[]], groups_get_user_groups($course->id, $user->id));
679     }
681     /**
682      * Test for provider::get_contexts_for_userid().
683      */
684     public function test_get_contexts_for_userid() {
685         $this->resetAfterTest();
687         $course1 = $this->getDataGenerator()->create_course();
688         $course2 = $this->getDataGenerator()->create_course();
689         $course3 = $this->getDataGenerator()->create_course();
691         $group1a = $this->getDataGenerator()->create_group(array('courseid' => $course1->id));
692         $group1b = $this->getDataGenerator()->create_group(array('courseid' => $course1->id));
693         $group2a = $this->getDataGenerator()->create_group(array('courseid' => $course2->id));
694         $group2b = $this->getDataGenerator()->create_group(array('courseid' => $course2->id));
695         $group3a = $this->getDataGenerator()->create_group(array('courseid' => $course3->id));
696         $group3b = $this->getDataGenerator()->create_group(array('courseid' => $course3->id));
698         $user1 = $this->getDataGenerator()->create_user();
699         $user2 = $this->getDataGenerator()->create_user();
701         $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
702         $this->getDataGenerator()->enrol_user($user1->id, $course2->id);
703         $this->getDataGenerator()->enrol_user($user1->id, $course3->id);
704         $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
705         $this->getDataGenerator()->enrol_user($user2->id, $course2->id);
706         $this->getDataGenerator()->enrol_user($user2->id, $course3->id);
708         $this->getDataGenerator()->create_group_member(array('userid' => $user1->id, 'groupid' => $group1a->id));
709         $this->getDataGenerator()->create_group_member(array('userid' => $user1->id, 'groupid' => $group2a->id));
710         $this->getDataGenerator()->create_group_member(array('userid' => $user2->id, 'groupid' => $group1b->id));
711         $this->getDataGenerator()->create_group_member(array('userid' => $user2->id, 'groupid' => $group2b->id));
712         $this->getDataGenerator()->create_group_member(array('userid' => $user2->id, 'groupid' => $group3b->id));
714         $coursecontext1 = context_course::instance($course1->id);
715         $coursecontext2 = context_course::instance($course2->id);
717         // User1 is member of some groups in course1 and course2.
718         $contextlist = provider::get_contexts_for_userid($user1->id);
719         $this->assertCount(2, $contextlist);
720         $this->assertEquals(
721                 [$coursecontext1->id, $coursecontext2->id],
722                 $contextlist->get_contextids(),
723                 '', 0.0, 10, true);
724     }
726     /**
727      * Test for provider::get_contexts_for_userid() when there are group memberships from other components.
728      */
729     public function test_get_contexts_for_userid_component() {
730         $this->resetAfterTest();
732         $course1 = $this->getDataGenerator()->create_course();
733         $course2 = $this->getDataGenerator()->create_course();
735         $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course1->id));
736         $group2 = $this->getDataGenerator()->create_group(array('courseid' => $course2->id));
738         $user = $this->getDataGenerator()->create_user();
740         $this->getDataGenerator()->enrol_user($user->id, $course1->id);
741         $this->getDataGenerator()->enrol_user($user->id, $course2->id);
743         $this->getDataGenerator()->create_group_member(
744                 array(
745                     'userid' => $user->id,
746                     'groupid' => $group1->id
747                 ));
748         $this->getDataGenerator()->create_group_member(
749                 array(
750                     'userid' => $user->id,
751                     'groupid' => $group2->id,
752                     'component' => 'enrol_meta'
753                 ));
755         $coursecontext1 = context_course::instance($course1->id);
757         // User is member of some groups in course1 and course2,
758         // but only the membership in course1 is directly managed by core_group.
759         $contextlist = provider::get_contexts_for_userid($user->id);
760         $this->assertEquals([$coursecontext1->id], $contextlist->get_contextids());
761     }
763     /**
764      * Test for provider::export_user_data().
765      */
766     public function test_export_user_data() {
767         $this->resetAfterTest();
769         $course = $this->getDataGenerator()->create_course();
770         $user1 = $this->getDataGenerator()->create_user();
771         $user2 = $this->getDataGenerator()->create_user();
772         $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
773         $group2 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
774         $group3 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
775         $group4 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
776         $this->getDataGenerator()->enrol_user($user1->id, $course->id);
777         $this->getDataGenerator()->enrol_user($user2->id, $course->id);
779         // Add user1 to group1 and group2.
780         $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id, 'userid' => $user1->id));
781         $this->getDataGenerator()->create_group_member(array('groupid' => $group2->id, 'userid' => $user1->id));
783         // Add user2 to group2 and group3.
784         $this->getDataGenerator()->create_group_member(array('groupid' => $group2->id, 'userid' => $user2->id));
785         $this->getDataGenerator()->create_group_member(array('groupid' => $group3->id, 'userid' => $user2->id));
787         $context = context_course::instance($course->id);
789         $this->setUser($user1);
791         // Export all of the data for the context.
792         $this->export_context_data_for_user($user1->id, $context, 'core_group');
794         $writer = writer::with_context($context);
795         $this->assertTrue($writer->has_any_data());
797         $data = $writer->get_data([get_string('groups', 'core_group')]);
798         $exportedgroups = $data->groups;
800         // User1 belongs to group1 and group2.
801         $this->assertEquals(
802                 [$group1->name, $group2->name],
803                 array_column($exportedgroups, 'name'),
804                 '', 0.0, 10, true);
805     }
807     /**
808      * Test for provider::delete_data_for_all_users_in_context().
809      */
810     public function test_delete_data_for_all_users_in_context() {
811         global $DB;
813         $this->resetAfterTest();
815         $course1 = $this->getDataGenerator()->create_course();
816         $course2 = $this->getDataGenerator()->create_course();
818         $group1a = $this->getDataGenerator()->create_group(array('courseid' => $course1->id));
819         $group1b = $this->getDataGenerator()->create_group(array('courseid' => $course1->id));
820         $group2a = $this->getDataGenerator()->create_group(array('courseid' => $course2->id));
821         $group2b = $this->getDataGenerator()->create_group(array('courseid' => $course2->id));
823         $user1 = $this->getDataGenerator()->create_user();
824         $user2 = $this->getDataGenerator()->create_user();
826         $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
827         $this->getDataGenerator()->enrol_user($user1->id, $course2->id);
828         $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
829         $this->getDataGenerator()->enrol_user($user2->id, $course2->id);
831         $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user1->id));
832         $this->getDataGenerator()->create_group_member(array('groupid' => $group1b->id, 'userid' => $user2->id));
833         $this->getDataGenerator()->create_group_member(array('groupid' => $group2a->id, 'userid' => $user1->id));
834         $this->getDataGenerator()->create_group_member(array('groupid' => $group2b->id, 'userid' => $user2->id));
836         $this->assertEquals(
837                 2,
838                 $DB->count_records_sql("SELECT COUNT(gm.id)
839                                           FROM {groups_members} gm
840                                           JOIN {groups} g ON gm.groupid = g.id
841                                          WHERE g.courseid = ?", [$course1->id])
842         );
843         $this->assertEquals(
844                 2,
845                 $DB->count_records_sql("SELECT COUNT(gm.id)
846                                           FROM {groups_members} gm
847                                           JOIN {groups} g ON gm.groupid = g.id
848                                          WHERE g.courseid = ?", [$course2->id])
849         );
851         $coursecontext1 = context_course::instance($course1->id);
852         provider::delete_data_for_all_users_in_context($coursecontext1);
854         $this->assertEquals(
855                 0,
856                 $DB->count_records_sql("SELECT COUNT(gm.id)
857                                           FROM {groups_members} gm
858                                           JOIN {groups} g ON gm.groupid = g.id
859                                          WHERE g.courseid = ?", [$course1->id])
860         );
861         $this->assertEquals(
862                 2,
863                 $DB->count_records_sql("SELECT COUNT(gm.id)
864                                           FROM {groups_members} gm
865                                           JOIN {groups} g ON gm.groupid = g.id
866                                          WHERE g.courseid = ?", [$course2->id])
867         );
868     }
870     /**
871      * Test for provider::delete_data_for_user().
872      */
873     public function test_delete_data_for_user() {
874         global $DB;
876         $this->resetAfterTest();
878         $course1 = $this->getDataGenerator()->create_course();
879         $course2 = $this->getDataGenerator()->create_course();
880         $course3 = $this->getDataGenerator()->create_course();
882         $group1a = $this->getDataGenerator()->create_group(array('courseid' => $course1->id));
883         $group1b = $this->getDataGenerator()->create_group(array('courseid' => $course1->id));
884         $group2a = $this->getDataGenerator()->create_group(array('courseid' => $course2->id));
885         $group2b = $this->getDataGenerator()->create_group(array('courseid' => $course2->id));
886         $group3a = $this->getDataGenerator()->create_group(array('courseid' => $course3->id));
887         $group3b = $this->getDataGenerator()->create_group(array('courseid' => $course3->id));
889         $user1 = $this->getDataGenerator()->create_user();
890         $user2 = $this->getDataGenerator()->create_user();
892         $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
893         $this->getDataGenerator()->enrol_user($user1->id, $course2->id);
894         $this->getDataGenerator()->enrol_user($user1->id, $course3->id);
895         $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
896         $this->getDataGenerator()->enrol_user($user2->id, $course2->id);
897         $this->getDataGenerator()->enrol_user($user2->id, $course3->id);
899         $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user1->id));
900         $this->getDataGenerator()->create_group_member(array('groupid' => $group1b->id, 'userid' => $user2->id));
901         $this->getDataGenerator()->create_group_member(array('groupid' => $group2a->id, 'userid' => $user1->id));
902         $this->getDataGenerator()->create_group_member(array('groupid' => $group2b->id, 'userid' => $user2->id));
903         $this->getDataGenerator()->create_group_member(array('groupid' => $group3a->id, 'userid' => $user1->id));
904         $this->getDataGenerator()->create_group_member(array('groupid' => $group3b->id, 'userid' => $user2->id));
906         $this->assertEquals(
907                 2,
908                 $DB->count_records_sql("SELECT COUNT(gm.id)
909                                           FROM {groups_members} gm
910                                           JOIN {groups} g ON gm.groupid = g.id
911                                          WHERE g.courseid = ?", [$course1->id])
912         );
913         $this->assertEquals(
914                 2,
915                 $DB->count_records_sql("SELECT COUNT(gm.id)
916                                           FROM {groups_members} gm
917                                           JOIN {groups} g ON gm.groupid = g.id
918                                          WHERE g.courseid = ?", [$course2->id])
919         );
920         $this->assertEquals(
921                 2,
922                 $DB->count_records_sql("SELECT COUNT(gm.id)
923                                           FROM {groups_members} gm
924                                           JOIN {groups} g ON gm.groupid = g.id
925                                          WHERE g.courseid = ?", [$course2->id])
926         );
927         $this->assertEquals(
928                 3,
929                 $DB->count_records_sql("SELECT COUNT(gm.id)
930                                           FROM {groups_members} gm
931                                           JOIN {groups} g ON gm.groupid = g.id
932                                          WHERE gm.userid = ?", [$user1->id])
933         );
935         $this->setUser($user1);
936         $coursecontext1 = context_course::instance($course1->id);
937         $coursecontext2 = context_course::instance($course2->id);
938         $approvedcontextlist = new \core_privacy\tests\request\approved_contextlist($user1, 'core_group',
939                 [$coursecontext1->id, $coursecontext2->id]);
940         provider::delete_data_for_user($approvedcontextlist);
942         $this->assertEquals(
943                 1,
944                 $DB->count_records_sql("SELECT COUNT(gm.id)
945                                           FROM {groups_members} gm
946                                           JOIN {groups} g ON gm.groupid = g.id
947                                          WHERE g.courseid = ?", [$course1->id])
948         );
949         $this->assertEquals(
950                 1,
951                 $DB->count_records_sql("SELECT COUNT(gm.id)
952                                           FROM {groups_members} gm
953                                           JOIN {groups} g ON gm.groupid = g.id
954                                          WHERE g.courseid = ?", [$course2->id])
955         );
956         $this->assertEquals(
957                 2,
958                 $DB->count_records_sql("SELECT COUNT(gm.id)
959                                           FROM {groups_members} gm
960                                           JOIN {groups} g ON gm.groupid = g.id
961                                          WHERE g.courseid = ?", [$course3->id])
962         );
963         $this->assertEquals(
964                 1,
965                 $DB->count_records_sql("SELECT COUNT(gm.id)
966                                           FROM {groups_members} gm
967                                           JOIN {groups} g ON gm.groupid = g.id
968                                          WHERE gm.userid = ?", [$user1->id])
969         );
970     }
972     /**
973      * Test for provider::delete_data_for_users().
974      */
975     public function test_delete_data_for_users() {
976         global $DB;
978         $this->resetAfterTest();
980         $course1 = $this->getDataGenerator()->create_course();
981         $course2 = $this->getDataGenerator()->create_course();
983         $group1a = $this->getDataGenerator()->create_group(array('courseid' => $course1->id));
984         $group1b = $this->getDataGenerator()->create_group(array('courseid' => $course1->id));
985         $group1c = $this->getDataGenerator()->create_group(array('courseid' => $course1->id));
986         $group2a = $this->getDataGenerator()->create_group(array('courseid' => $course2->id));
987         $group2b = $this->getDataGenerator()->create_group(array('courseid' => $course2->id));
988         $group2c = $this->getDataGenerator()->create_group(array('courseid' => $course2->id));
990         $user1 = $this->getDataGenerator()->create_user();
991         $user2 = $this->getDataGenerator()->create_user();
992         $user3 = $this->getDataGenerator()->create_user();
994         $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
995         $this->getDataGenerator()->enrol_user($user1->id, $course2->id);
996         $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
997         $this->getDataGenerator()->enrol_user($user2->id, $course2->id);
998         $this->getDataGenerator()->enrol_user($user3->id, $course1->id);
999         $this->getDataGenerator()->enrol_user($user3->id, $course2->id);
1001         $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user1->id));
1002         $this->getDataGenerator()->create_group_member(array('groupid' => $group1b->id, 'userid' => $user2->id));
1003         $this->getDataGenerator()->create_group_member(array('groupid' => $group1c->id, 'userid' => $user3->id));
1004         $this->getDataGenerator()->create_group_member(array('groupid' => $group2a->id, 'userid' => $user1->id));
1005         $this->getDataGenerator()->create_group_member(array('groupid' => $group2b->id, 'userid' => $user2->id));
1006         $this->getDataGenerator()->create_group_member(array('groupid' => $group2c->id, 'userid' => $user3->id));
1008         $this->assertEquals(
1009                 3,
1010                 $DB->count_records_sql("SELECT COUNT(gm.id)
1011                                           FROM {groups_members} gm
1012                                           JOIN {groups} g ON gm.groupid = g.id
1013                                          WHERE g.courseid = ?", [$course1->id])
1014         );
1015         $this->assertEquals(
1016                 3,
1017                 $DB->count_records_sql("SELECT COUNT(gm.id)
1018                                           FROM {groups_members} gm
1019                                           JOIN {groups} g ON gm.groupid = g.id
1020                                          WHERE g.courseid = ?", [$course2->id])
1021         );
1023         $coursecontext1 = context_course::instance($course1->id);
1024         $approveduserlist = new \core_privacy\local\request\approved_userlist($coursecontext1, 'core_group',
1025                 [$user1->id, $user2->id]);
1026         provider::delete_data_for_users($approveduserlist);
1028         $this->assertEquals(
1029                 [$user3->id],
1030                 $DB->get_fieldset_sql("SELECT gm.userid
1031                                          FROM {groups_members} gm
1032                                          JOIN {groups} g ON gm.groupid = g.id
1033                                         WHERE g.courseid = ?", [$course1->id])
1034         );
1035         $this->assertEquals(
1036                 3,
1037                 $DB->count_records_sql("SELECT COUNT(gm.id)
1038                                           FROM {groups_members} gm
1039                                           JOIN {groups} g ON gm.groupid = g.id
1040                                          WHERE g.courseid = ?", [$course2->id])
1041         );
1042     }
1044     /**
1045      * Test for provider::get_users_in_context().
1046      */
1047     public function test_get_users_in_context() {
1048         $this->resetAfterTest();
1050         $course1 = $this->getDataGenerator()->create_course();
1051         $course2 = $this->getDataGenerator()->create_course();
1053         $group1a = $this->getDataGenerator()->create_group(array('courseid' => $course1->id));
1054         $group1b = $this->getDataGenerator()->create_group(array('courseid' => $course1->id));
1055         $group2a = $this->getDataGenerator()->create_group(array('courseid' => $course2->id));
1056         $group2b = $this->getDataGenerator()->create_group(array('courseid' => $course2->id));
1058         $user1 = $this->getDataGenerator()->create_user();
1059         $user2 = $this->getDataGenerator()->create_user();
1060         $user3 = $this->getDataGenerator()->create_user();
1062         $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
1063         $this->getDataGenerator()->enrol_user($user1->id, $course2->id);
1064         $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
1065         $this->getDataGenerator()->enrol_user($user2->id, $course2->id);
1066         $this->getDataGenerator()->enrol_user($user3->id, $course1->id);
1067         $this->getDataGenerator()->enrol_user($user3->id, $course2->id);
1069         $this->getDataGenerator()->create_group_member(array('userid' => $user1->id, 'groupid' => $group1a->id));
1070         $this->getDataGenerator()->create_group_member(array('userid' => $user1->id, 'groupid' => $group2a->id));
1071         $this->getDataGenerator()->create_group_member(array('userid' => $user2->id, 'groupid' => $group1b->id));
1072         $this->getDataGenerator()->create_group_member(array('userid' => $user2->id, 'groupid' => $group2b->id));
1073         $this->getDataGenerator()->create_group_member(array('userid' => $user3->id, 'groupid' => $group2a->id));
1075         $coursecontext1 = context_course::instance($course1->id);
1077         $userlist = new \core_privacy\local\request\userlist($coursecontext1, 'core_group');
1078         \core_group\privacy\provider::get_users_in_context($userlist);
1080         // Only user1 and user2. User3 is not member of any group in course1.
1081         $this->assertCount(2, $userlist);
1082         $this->assertEquals(
1083                 [$user1->id, $user2->id],
1084                 $userlist->get_userids(),
1085                 '', 0.0, 10, true);
1086     }