Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 401 and 402] [Versions 401 and 403]

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  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   */
  25  namespace core_group\privacy;
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  use core_privacy\tests\provider_testcase;
  30  use core_privacy\local\metadata\collection;
  31  use core_group\privacy\provider;
  32  use core_privacy\local\request\writer;
  33  
  34  /**
  35   * Privacy provider test for core_group.
  36   *
  37   * @copyright  2018 Shamim Rezaie <shamim@moodle.com>
  38   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  39   */
  40  class provider_test extends provider_testcase {
  41  
  42      /**
  43       * Test for provider::get_metadata().
  44       */
  45      public function test_get_metadata() {
  46          $collection = new collection('core_group');
  47          $newcollection = provider::get_metadata($collection);
  48          $itemcollection = $newcollection->get_collection();
  49          $this->assertCount(2, $itemcollection);
  50  
  51          $table = array_shift($itemcollection);
  52          $this->assertEquals('groups_members', $table->get_name());
  53          $this->assertEquals('privacy:metadata:groups', $table->get_summary());
  54  
  55          $privacyfields = $table->get_privacy_fields();
  56          $this->assertArrayHasKey('groupid', $privacyfields);
  57          $this->assertArrayHasKey('userid', $privacyfields);
  58          $this->assertArrayHasKey('timeadded', $privacyfields);
  59  
  60          $table = array_shift($itemcollection);
  61          $this->assertEquals('core_message', $table->get_name());
  62          $this->assertEquals('privacy:metadata:core_message', $table->get_summary());
  63      }
  64  
  65      /**
  66       * Test for provider::export_groups() to export manual group memberships.
  67       */
  68      public function test_export_groups() {
  69          $this->resetAfterTest();
  70  
  71          $course = $this->getDataGenerator()->create_course();
  72          $user1 = $this->getDataGenerator()->create_user();
  73          $user2 = $this->getDataGenerator()->create_user();
  74          $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
  75          $group2 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
  76          $group3 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
  77          $group4 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
  78          $this->getDataGenerator()->enrol_user($user1->id, $course->id);
  79          $this->getDataGenerator()->enrol_user($user2->id, $course->id);
  80  
  81          // Add user1 to group1 and group2.
  82          $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id, 'userid' => $user1->id));
  83          $this->getDataGenerator()->create_group_member(array('groupid' => $group2->id, 'userid' => $user1->id));
  84  
  85          // Add user2 to group2 and group3.
  86          $this->getDataGenerator()->create_group_member(array('groupid' => $group2->id, 'userid' => $user2->id));
  87          $this->getDataGenerator()->create_group_member(array('groupid' => $group3->id, 'userid' => $user2->id));
  88  
  89          $context = \context_course::instance($course->id);
  90  
  91          // Retrieve groups for user1.
  92          $this->setUser($user1);
  93          $writer = writer::with_context($context);
  94          provider::export_groups($context, '');
  95  
  96          $data = $writer->get_data([get_string('groups', 'core_group')]);
  97          $exportedgroups = $data->groups;
  98  
  99          // User1 belongs to group1 and group2.
 100          $this->assertEqualsCanonicalizing(
 101                  [$group1->name, $group2->name],
 102                  array_column($exportedgroups, 'name'));
 103      }
 104  
 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();
 110  
 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');
 121  
 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));
 127  
 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));
 132  
 133          $context = \context_course::instance($course->id);
 134  
 135          // Retrieve groups for user1.
 136          $this->setUser($user1);
 137          $writer = writer::with_context($context);
 138          provider::export_groups($context, 'enrol_self');
 139  
 140          $data = $writer->get_data([get_string('groups', 'core_group')]);
 141          $exportedgroups = $data->groups;
 142  
 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      }
 148  
 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;
 154  
 155          $this->resetAfterTest();
 156  
 157          $course1 = $this->getDataGenerator()->create_course();
 158          $course2 = $this->getDataGenerator()->create_course();
 159  
 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));
 164  
 165          $user1 = $this->getDataGenerator()->create_user();
 166          $user2 = $this->getDataGenerator()->create_user();
 167  
 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);
 172  
 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));
 177  
 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          );
 192  
 193          $coursecontext1 = \context_course::instance($course1->id);
 194          provider::delete_groups_for_all_users($coursecontext1, '');
 195  
 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      }
 211  
 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;
 217  
 218          $this->resetAfterTest();
 219  
 220          $course1 = $this->getDataGenerator()->create_course();
 221          $course2 = $this->getDataGenerator()->create_course();
 222  
 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));
 227  
 228          $user1 = $this->getDataGenerator()->create_user();
 229          $user2 = $this->getDataGenerator()->create_user();
 230  
 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');
 235  
 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));
 242  
 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          );
 257  
 258          $coursecontext1 = \context_course::instance($course1->id);
 259          provider::delete_groups_for_all_users($coursecontext1, 'enrol_self');
 260  
 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      }
 276  
 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();
 282  
 283          $course = $this->getDataGenerator()->create_course();
 284  
 285          $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
 286          $group2 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
 287  
 288          $user1 = $this->getDataGenerator()->create_user();
 289          $user2 = $this->getDataGenerator()->create_user();
 290  
 291          $this->getDataGenerator()->enrol_user($user1->id, $course->id);
 292          $this->getDataGenerator()->enrol_user($user2->id, $course->id);
 293  
 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));
 297  
 298          $this->assertEqualsCanonicalizing([[$group1->id, $group2->id]], groups_get_user_groups($course->id, $user1->id));
 299          $this->assertEquals([[$group1->id]], groups_get_user_groups($course->id, $user2->id));
 300  
 301          $coursecontext = \context_course::instance($course->id);
 302          provider::delete_groups_for_all_users($coursecontext, '');
 303  
 304          $this->assertEquals([[]], groups_get_user_groups($course->id, $user1->id));
 305          $this->assertEquals([[]], groups_get_user_groups($course->id, $user2->id));
 306      }
 307  
 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;
 313  
 314          $this->resetAfterTest();
 315  
 316          $course1 = $this->getDataGenerator()->create_course();
 317          $course2 = $this->getDataGenerator()->create_course();
 318          $course3 = $this->getDataGenerator()->create_course();
 319  
 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));
 326  
 327          $user1 = $this->getDataGenerator()->create_user();
 328          $user2 = $this->getDataGenerator()->create_user();
 329  
 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);
 336  
 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));
 343  
 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          );
 372  
 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, '');
 379  
 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      }
 409  
 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;
 415  
 416          $this->resetAfterTest();
 417  
 418          $course1 = $this->getDataGenerator()->create_course();
 419          $course2 = $this->getDataGenerator()->create_course();
 420          $course3 = $this->getDataGenerator()->create_course();
 421  
 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));
 428  
 429          $user1 = $this->getDataGenerator()->create_user();
 430          $user2 = $this->getDataGenerator()->create_user();
 431  
 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');
 438  
 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));
 447  
 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          );
 476  
 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');
 483  
 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      }
 513  
 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;
 519  
 520          $this->resetAfterTest();
 521  
 522          $course1 = $this->getDataGenerator()->create_course();
 523          $course2 = $this->getDataGenerator()->create_course();
 524          $course3 = $this->getDataGenerator()->create_course();
 525  
 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));
 532  
 533          $user1 = $this->getDataGenerator()->create_user();
 534          $user2 = $this->getDataGenerator()->create_user();
 535  
 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');
 542  
 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));
 553  
 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          );
 582  
 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');
 588  
 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          );
 617  
 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      }
 652  
 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();
 658  
 659          $course = $this->getDataGenerator()->create_course();
 660  
 661          $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
 662          $group2 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
 663  
 664          $user = $this->getDataGenerator()->create_user();
 665  
 666          $this->getDataGenerator()->enrol_user($user->id, $course->id);
 667  
 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));
 670  
 671          $this->assertEqualsCanonicalizing([[$group1->id, $group2->id]], groups_get_user_groups($course->id, $user->id));
 672  
 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, '');
 677  
 678          $this->assertEquals([[]], groups_get_user_groups($course->id, $user->id));
 679      }
 680  
 681      /**
 682       * Test for provider::get_contexts_for_userid().
 683       */
 684      public function test_get_contexts_for_userid() {
 685          $this->resetAfterTest();
 686  
 687          $course1 = $this->getDataGenerator()->create_course();
 688          $course2 = $this->getDataGenerator()->create_course();
 689          $course3 = $this->getDataGenerator()->create_course();
 690  
 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));
 697  
 698          $user1 = $this->getDataGenerator()->create_user();
 699          $user2 = $this->getDataGenerator()->create_user();
 700  
 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);
 707  
 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));
 713  
 714          $coursecontext1 = \context_course::instance($course1->id);
 715          $coursecontext2 = \context_course::instance($course2->id);
 716  
 717          // User1 is member of some groups in course1 and course2 + self-conversation.
 718          $contextlist = provider::get_contexts_for_userid($user1->id);
 719          $contextids = array_values($contextlist->get_contextids());
 720  
 721          $this->assertCount(3, $contextlist);
 722          // One of the user context is the one related to self-conversation. Let's test group contexts.
 723          $this->assertContainsEquals($coursecontext1->id, $contextids);
 724          $this->assertContainsEquals($coursecontext2->id, $contextids);
 725      }
 726  
 727      /**
 728       * Test for provider::get_contexts_for_userid() when there are group memberships from other components.
 729       */
 730      public function test_get_contexts_for_userid_component() {
 731          $this->resetAfterTest();
 732  
 733          $course1 = $this->getDataGenerator()->create_course();
 734          $course2 = $this->getDataGenerator()->create_course();
 735  
 736          $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course1->id));
 737          $group2 = $this->getDataGenerator()->create_group(array('courseid' => $course2->id));
 738  
 739          $user = $this->getDataGenerator()->create_user();
 740  
 741          $this->getDataGenerator()->enrol_user($user->id, $course1->id);
 742          $this->getDataGenerator()->enrol_user($user->id, $course2->id);
 743  
 744          $this->getDataGenerator()->create_group_member(
 745                  array(
 746                      'userid' => $user->id,
 747                      'groupid' => $group1->id
 748                  ));
 749          $this->getDataGenerator()->create_group_member(
 750                  array(
 751                      'userid' => $user->id,
 752                      'groupid' => $group2->id,
 753                      'component' => 'enrol_meta'
 754                  ));
 755  
 756          $coursecontext1 = \context_course::instance($course1->id);
 757  
 758          // User is member of some groups in course1 and course2,
 759          // but only the membership in course1 is directly managed by core_group.
 760          $contextlist = provider::get_contexts_for_userid($user->id);
 761          $this->assertEquals($coursecontext1->id, $contextlist->get_contextids()[0]);
 762      }
 763  
 764      /**
 765       * Test for provider::export_user_data().
 766       */
 767      public function test_export_user_data() {
 768          $this->resetAfterTest();
 769  
 770          $course = $this->getDataGenerator()->create_course();
 771          $user1 = $this->getDataGenerator()->create_user();
 772          $user2 = $this->getDataGenerator()->create_user();
 773          $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
 774          $group2 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
 775          $group3 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
 776          $group4 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
 777          $this->getDataGenerator()->enrol_user($user1->id, $course->id);
 778          $this->getDataGenerator()->enrol_user($user2->id, $course->id);
 779  
 780          // Add user1 to group1 and group2.
 781          $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id, 'userid' => $user1->id));
 782          $this->getDataGenerator()->create_group_member(array('groupid' => $group2->id, 'userid' => $user1->id));
 783  
 784          // Add user2 to group2 and group3.
 785          $this->getDataGenerator()->create_group_member(array('groupid' => $group2->id, 'userid' => $user2->id));
 786          $this->getDataGenerator()->create_group_member(array('groupid' => $group3->id, 'userid' => $user2->id));
 787  
 788          $context = \context_course::instance($course->id);
 789  
 790          $this->setUser($user1);
 791  
 792          // Export all of the data for the context.
 793          $this->export_context_data_for_user($user1->id, $context, 'core_group');
 794  
 795          $writer = writer::with_context($context);
 796          $this->assertTrue($writer->has_any_data());
 797  
 798          $data = $writer->get_data([get_string('groups', 'core_group')]);
 799          $exportedgroups = $data->groups;
 800  
 801          // User1 belongs to group1 and group2.
 802          $this->assertEqualsCanonicalizing(
 803                  [$group1->name, $group2->name],
 804                  array_column($exportedgroups, 'name'));
 805      }
 806  
 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;
 812  
 813          $this->resetAfterTest();
 814  
 815          $course1 = $this->getDataGenerator()->create_course();
 816          $course2 = $this->getDataGenerator()->create_course();
 817  
 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));
 822  
 823          $user1 = $this->getDataGenerator()->create_user();
 824          $user2 = $this->getDataGenerator()->create_user();
 825  
 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);
 830  
 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));
 835  
 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          );
 850  
 851          $coursecontext1 = \context_course::instance($course1->id);
 852          provider::delete_data_for_all_users_in_context($coursecontext1);
 853  
 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      }
 869  
 870      /**
 871       * Test for provider::delete_data_for_user().
 872       */
 873      public function test_delete_data_for_user() {
 874          global $DB;
 875  
 876          $this->resetAfterTest();
 877  
 878          $course1 = $this->getDataGenerator()->create_course();
 879          $course2 = $this->getDataGenerator()->create_course();
 880          $course3 = $this->getDataGenerator()->create_course();
 881  
 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));
 888  
 889          $user1 = $this->getDataGenerator()->create_user();
 890          $user2 = $this->getDataGenerator()->create_user();
 891  
 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);
 898  
 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));
 905  
 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          );
 934  
 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);
 941  
 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      }
 971  
 972      /**
 973       * Test for provider::delete_data_for_users().
 974       */
 975      public function test_delete_data_for_users() {
 976          global $DB;
 977  
 978          $this->resetAfterTest();
 979  
 980          $course1 = $this->getDataGenerator()->create_course();
 981          $course2 = $this->getDataGenerator()->create_course();
 982  
 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));
 989  
 990          $user1 = $this->getDataGenerator()->create_user();
 991          $user2 = $this->getDataGenerator()->create_user();
 992          $user3 = $this->getDataGenerator()->create_user();
 993  
 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);
1000  
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));
1007  
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          );
1022  
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);
1027  
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      }
1043  
1044      /**
1045       * Test for provider::get_users_in_context().
1046       */
1047      public function test_get_users_in_context() {
1048          $this->resetAfterTest();
1049  
1050          $course1 = $this->getDataGenerator()->create_course();
1051          $course2 = $this->getDataGenerator()->create_course();
1052  
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));
1057  
1058          $user1 = $this->getDataGenerator()->create_user();
1059          $user2 = $this->getDataGenerator()->create_user();
1060          $user3 = $this->getDataGenerator()->create_user();
1061  
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);
1068  
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));
1074  
1075          $coursecontext1 = \context_course::instance($course1->id);
1076  
1077          $userlist = new \core_privacy\local\request\userlist($coursecontext1, 'core_group');
1078          \core_group\privacy\provider::get_users_in_context($userlist);
1079  
1080          // Only user1 and user2. User3 is not member of any group in course1.
1081          $this->assertCount(2, $userlist);
1082          $this->assertEqualsCanonicalizing(
1083                  [$user1->id, $user2->id],
1084                  $userlist->get_userids());
1085      }
1086  }