Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

Differences Between: [Versions 311 and 402] [Versions 400 and 402] [Versions 401 and 402]

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