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 310 and 402] [Versions 311 and 402] [Versions 39 and 402] [Versions 400 and 402] [Versions 401 and 402] [Versions 402 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   * Unit tests for group lib.
  19   *
  20   * @package    core_group
  21   * @copyright  2013 Frédéric Massart
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  namespace core_group;
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  global $CFG;
  29  require_once($CFG->dirroot . '/group/lib.php');
  30  
  31  /**
  32   * Group lib testcase.
  33   *
  34   * @package    core_group
  35   * @copyright  2013 Frédéric Massart
  36   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   */
  38  class lib_test extends \advanced_testcase {
  39  
  40      public function test_member_added_event() {
  41          $this->resetAfterTest();
  42  
  43          $this->setAdminUser();
  44          $course = $this->getDataGenerator()->create_course();
  45          $user = $this->getDataGenerator()->create_user();
  46          $group = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
  47          $this->getDataGenerator()->enrol_user($user->id, $course->id);
  48  
  49          $sink = $this->redirectEvents();
  50          groups_add_member($group->id, $user->id, 'mod_workshop', '123');
  51          $events = $sink->get_events();
  52          $this->assertCount(1, $events);
  53          $event = reset($events);
  54  
  55          $this->assertInstanceOf('\core\event\group_member_added', $event);
  56          $this->assertEquals($user->id, $event->relateduserid);
  57          $this->assertEquals(\context_course::instance($course->id), $event->get_context());
  58          $this->assertEquals($group->id, $event->objectid);
  59          $url = new \moodle_url('/group/members.php', array('group' => $event->objectid));
  60          $this->assertEquals($url, $event->get_url());
  61      }
  62  
  63      public function test_member_removed_event() {
  64          $this->resetAfterTest();
  65  
  66          $this->setAdminUser();
  67          $course = $this->getDataGenerator()->create_course();
  68          $user = $this->getDataGenerator()->create_user();
  69          $group = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
  70          $this->getDataGenerator()->enrol_user($user->id, $course->id);
  71          $this->getDataGenerator()->create_group_member(array('userid' => $user->id, 'groupid' => $group->id));
  72  
  73          $sink = $this->redirectEvents();
  74          groups_remove_member($group->id, $user->id);
  75          $events = $sink->get_events();
  76          $this->assertCount(1, $events);
  77          $event = reset($events);
  78  
  79          $this->assertInstanceOf('\core\event\group_member_removed', $event);
  80          $this->assertEquals($user->id, $event->relateduserid);
  81          $this->assertEquals(\context_course::instance($course->id), $event->get_context());
  82          $this->assertEquals($group->id, $event->objectid);
  83          $url = new \moodle_url('/group/members.php', array('group' => $event->objectid));
  84          $this->assertEquals($url, $event->get_url());
  85      }
  86  
  87      public function test_group_created_event() {
  88          $this->resetAfterTest();
  89  
  90          $this->setAdminUser();
  91          $course = $this->getDataGenerator()->create_course();
  92  
  93          $sink = $this->redirectEvents();
  94          $group = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
  95          $events = $sink->get_events();
  96          $this->assertCount(1, $events);
  97          $event = reset($events);
  98  
  99          $this->assertInstanceOf('\core\event\group_created', $event);
 100          $this->assertEquals(\context_course::instance($course->id), $event->get_context());
 101          $this->assertEquals($group->id, $event->objectid);
 102          $url = new \moodle_url('/group/index.php', array('id' => $event->courseid));
 103          $this->assertEquals($url, $event->get_url());
 104      }
 105  
 106      public function test_grouping_created_event() {
 107          $this->resetAfterTest();
 108  
 109          $this->setAdminUser();
 110          $course = $this->getDataGenerator()->create_course();
 111  
 112          $sink = $this->redirectEvents();
 113          $group = $this->getDataGenerator()->create_grouping(array('courseid' => $course->id));
 114          $events = $sink->get_events();
 115          $this->assertCount(1, $events);
 116          $event = reset($events);
 117  
 118          $this->assertInstanceOf('\core\event\grouping_created', $event);
 119  
 120          $this->assertEquals(\context_course::instance($course->id), $event->get_context());
 121          $this->assertEquals($group->id, $event->objectid);
 122          $url = new \moodle_url('/group/groupings.php', array('id' => $event->courseid));
 123          $this->assertEquals($url, $event->get_url());
 124      }
 125  
 126      public function test_group_updated_event() {
 127          global $DB;
 128  
 129          $this->resetAfterTest();
 130  
 131          $course = $this->getDataGenerator()->create_course();
 132          $group = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
 133  
 134          $sink = $this->redirectEvents();
 135          $data = new \stdClass();
 136          $data->id = $group->id;
 137          $data->courseid = $course->id;
 138          $data->name = 'Backend team';
 139          $this->setCurrentTimeStart();
 140          groups_update_group($data);
 141          $group = $DB->get_record('groups', array('id'=>$group->id)); // Fetch record with modified timestamp.
 142          $events = $sink->get_events();
 143          $this->assertCount(1, $events);
 144          $event = reset($events);
 145          $this->assertTimeCurrent($group->timemodified);
 146  
 147          $this->assertInstanceOf('\core\event\group_updated', $event);
 148          $group->name = $data->name;
 149          $this->assertEquals(\context_course::instance($course->id), $event->get_context());
 150          $this->assertEquals($group->id, $event->objectid);
 151          $url = new \moodle_url('/group/group.php', array('id' => $event->objectid));
 152          $this->assertEquals($url, $event->get_url());
 153      }
 154  
 155      public function test_group_updated_event_does_not_require_names() {
 156          global $DB;
 157  
 158          $this->resetAfterTest();
 159  
 160          $course = $this->getDataGenerator()->create_course();
 161          $group = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
 162  
 163          $sink = $this->redirectEvents();
 164          $data = new \stdClass();
 165          $data->id = $group->id;
 166          $data->courseid = $course->id;
 167          $this->setCurrentTimeStart();
 168          groups_update_group($data);
 169          $group = $DB->get_record('groups', array('id'=>$group->id)); // Fetch record with modified timestamp.
 170          $events = $sink->get_events();
 171          $this->assertCount(1, $events);
 172          $event = reset($events);
 173          $this->assertTimeCurrent($group->timemodified);
 174  
 175          $this->assertInstanceOf('\core\event\group_updated', $event);
 176          $this->assertEquals(\context_course::instance($course->id), $event->get_context());
 177          $this->assertEquals($group->id, $event->objectid);
 178          $url = new \moodle_url('/group/group.php', array('id' => $event->objectid));
 179          $this->assertEquals($url, $event->get_url());
 180      }
 181  
 182      public function test_grouping_updated_event() {
 183          global $DB;
 184  
 185          $this->resetAfterTest();
 186  
 187          $course = $this->getDataGenerator()->create_course();
 188          $grouping = $this->getDataGenerator()->create_grouping(array('courseid' => $course->id));
 189  
 190          $sink = $this->redirectEvents();
 191          $data = new \stdClass();
 192          $data->id = $grouping->id;
 193          $data->courseid = $course->id;
 194          $data->name = 'Backend team';
 195          $this->setCurrentTimeStart();
 196          groups_update_grouping($data);
 197          $events = $sink->get_events();
 198          $this->assertCount(1, $events);
 199          $event = reset($events);
 200  
 201          $this->assertInstanceOf('\core\event\grouping_updated', $event);
 202  
 203          // Get the timemodified from DB for comparison with snapshot.
 204          $data->timemodified = $DB->get_field('groupings', 'timemodified', array('id'=>$grouping->id));
 205          $this->assertTimeCurrent($data->timemodified);
 206  
 207          $this->assertEquals(\context_course::instance($course->id), $event->get_context());
 208          $this->assertEquals($grouping->id, $event->objectid);
 209          $url = new \moodle_url('/group/grouping.php', array('id' => $event->objectid));
 210          $this->assertEquals($url, $event->get_url());
 211      }
 212  
 213      public function test_grouping_updated_event_does_not_require_names() {
 214          global $DB;
 215  
 216          $this->resetAfterTest();
 217  
 218          $course = $this->getDataGenerator()->create_course();
 219          $grouping = $this->getDataGenerator()->create_grouping(array('courseid' => $course->id));
 220  
 221          $sink = $this->redirectEvents();
 222          $data = new \stdClass();
 223          $data->id = $grouping->id;
 224          $data->courseid = $course->id;
 225          $this->setCurrentTimeStart();
 226          groups_update_grouping($data);
 227          $events = $sink->get_events();
 228          $this->assertCount(1, $events);
 229          $event = reset($events);
 230  
 231          $this->assertInstanceOf('\core\event\grouping_updated', $event);
 232  
 233          // Get the timemodified from DB for comparison with snapshot.
 234          $data->timemodified = $DB->get_field('groupings', 'timemodified', array('id'=>$grouping->id));
 235          $this->assertTimeCurrent($data->timemodified);
 236          // Following fields were not updated so the snapshot should have them the same as in original group.
 237          $data->description = $grouping->description;
 238          $data->descriptionformat = $grouping->descriptionformat;
 239          $data->configdata = $grouping->configdata;
 240          $data->idnumber = $grouping->idnumber;
 241          $data->name = $grouping->name;
 242          $data->timecreated = $grouping->timecreated;
 243  
 244          $this->assertEquals(\context_course::instance($course->id), $event->get_context());
 245          $this->assertEquals($grouping->id, $event->objectid);
 246          $url = new \moodle_url('/group/grouping.php', array('id' => $event->objectid));
 247          $this->assertEquals($url, $event->get_url());
 248      }
 249  
 250      public function test_group_deleted_event() {
 251          $this->resetAfterTest();
 252  
 253          $course = $this->getDataGenerator()->create_course();
 254          $group = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
 255  
 256          $sink = $this->redirectEvents();
 257          groups_delete_group($group->id);
 258          $events = $sink->get_events();
 259          $this->assertCount(1, $events);
 260          $event = reset($events);
 261  
 262          $this->assertEquals(\context_course::instance($course->id), $event->get_context());
 263          $this->assertEquals($group->id, $event->objectid);
 264          $url = new \moodle_url('/group/index.php', array('id' => $event->courseid));
 265          $this->assertEquals($url, $event->get_url());
 266      }
 267  
 268      public function test_grouping_deleted_event() {
 269          $this->resetAfterTest();
 270  
 271          $course = $this->getDataGenerator()->create_course();
 272          $group = $this->getDataGenerator()->create_grouping(array('courseid' => $course->id));
 273  
 274          $sink = $this->redirectEvents();
 275          groups_delete_grouping($group->id);
 276          $events = $sink->get_events();
 277          $this->assertCount(1, $events);
 278          $event = reset($events);
 279  
 280          $this->assertInstanceOf('\core\event\grouping_deleted', $event);
 281          $this->assertEquals(\context_course::instance($course->id), $event->get_context());
 282          $this->assertEquals($group->id, $event->objectid);
 283          $url = new \moodle_url('/group/groupings.php', array('id' => $event->courseid));
 284          $this->assertEquals($url, $event->get_url());
 285      }
 286  
 287      public function test_groups_delete_group_members() {
 288          global $DB;
 289          $this->resetAfterTest();
 290  
 291          $course = $this->getDataGenerator()->create_course();
 292          $user1 = $this->getDataGenerator()->create_user();
 293          $user2 = $this->getDataGenerator()->create_user();
 294          $this->getDataGenerator()->enrol_user($user1->id, $course->id);
 295          $this->getDataGenerator()->enrol_user($user2->id, $course->id);
 296          $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
 297          $group2 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
 298  
 299          // Test deletion of all the users.
 300          $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id, 'userid' => $user1->id));
 301          $this->getDataGenerator()->create_group_member(array('groupid' => $group2->id, 'userid' => $user1->id));
 302          $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id, 'userid' => $user2->id));
 303  
 304          $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $group1->id, 'userid' => $user1->id)));
 305          $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $group2->id, 'userid' => $user1->id)));
 306          $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $group1->id, 'userid' => $user2->id)));
 307          groups_delete_group_members($course->id);
 308          $this->assertFalse($DB->record_exists('groups_members', array('groupid' => $group1->id, 'userid' => $user1->id)));
 309          $this->assertFalse($DB->record_exists('groups_members', array('groupid' => $group2->id, 'userid' => $user1->id)));
 310          $this->assertFalse($DB->record_exists('groups_members', array('groupid' => $group1->id, 'userid' => $user2->id)));
 311  
 312          // Test deletion of a specific user.
 313          $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id, 'userid' => $user1->id));
 314          $this->getDataGenerator()->create_group_member(array('groupid' => $group2->id, 'userid' => $user1->id));
 315          $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id, 'userid' => $user2->id));
 316  
 317          $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $group1->id, 'userid' => $user1->id)));
 318          $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $group2->id, 'userid' => $user1->id)));
 319          $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $group1->id, 'userid' => $user2->id)));
 320          groups_delete_group_members($course->id, $user2->id);
 321          $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $group1->id, 'userid' => $user1->id)));
 322          $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $group2->id, 'userid' => $user1->id)));
 323          $this->assertFalse($DB->record_exists('groups_members', array('groupid' => $group1->id, 'userid' => $user2->id)));
 324      }
 325  
 326      public function test_groups_remove_member() {
 327          global $DB;
 328          $this->resetAfterTest();
 329  
 330          $course = $this->getDataGenerator()->create_course();
 331          $user1 = $this->getDataGenerator()->create_user();
 332          $user2 = $this->getDataGenerator()->create_user();
 333          $this->getDataGenerator()->enrol_user($user1->id, $course->id);
 334          $this->getDataGenerator()->enrol_user($user2->id, $course->id);
 335          $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
 336          $group2 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
 337  
 338          $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id, 'userid' => $user1->id));
 339          $this->getDataGenerator()->create_group_member(array('groupid' => $group2->id, 'userid' => $user1->id));
 340          $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id, 'userid' => $user2->id));
 341  
 342          $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $group1->id, 'userid' => $user1->id)));
 343          $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $group2->id, 'userid' => $user1->id)));
 344          $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $group1->id, 'userid' => $user2->id)));
 345          groups_remove_member($group1->id, $user1->id);
 346          $this->assertFalse($DB->record_exists('groups_members', array('groupid' => $group1->id, 'userid' => $user1->id)));
 347          $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $group2->id, 'userid' => $user1->id)));
 348          $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $group1->id, 'userid' => $user2->id)));
 349          groups_remove_member($group1->id, $user2->id);
 350          $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $group2->id, 'userid' => $user1->id)));
 351          $this->assertFalse($DB->record_exists('groups_members', array('groupid' => $group1->id, 'userid' => $user2->id)));
 352          groups_remove_member($group2->id, $user1->id);
 353          $this->assertFalse($DB->record_exists('groups_members', array('groupid' => $group2->id, 'userid' => $user1->id)));
 354      }
 355  
 356      public function test_groups_delete_groupings_groups() {
 357          global $DB;
 358          $this->resetAfterTest();
 359  
 360          $course = $this->getDataGenerator()->create_course();
 361          $course2 = $this->getDataGenerator()->create_course();
 362          $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
 363          $group2 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
 364          $group1c2 = $this->getDataGenerator()->create_group(array('courseid' => $course2->id));
 365          $grouping1 = $this->getDataGenerator()->create_grouping(array('courseid' => $course->id));
 366          $grouping2 = $this->getDataGenerator()->create_grouping(array('courseid' => $course->id));
 367          $grouping1c2 = $this->getDataGenerator()->create_grouping(array('courseid' => $course2->id));
 368  
 369          $this->getDataGenerator()->create_grouping_group(array('groupingid' => $grouping1->id, 'groupid' => $group1->id));
 370          $this->getDataGenerator()->create_grouping_group(array('groupingid' => $grouping1->id, 'groupid' => $group2->id));
 371          $this->getDataGenerator()->create_grouping_group(array('groupingid' => $grouping2->id, 'groupid' => $group1->id));
 372          $this->getDataGenerator()->create_grouping_group(array('groupingid' => $grouping1c2->id, 'groupid' => $group1c2->id));
 373          $this->assertTrue($DB->record_exists('groupings_groups', array('groupid' => $group1->id, 'groupingid' => $grouping1->id)));
 374          $this->assertTrue($DB->record_exists('groupings_groups', array('groupid' => $group2->id, 'groupingid' => $grouping1->id)));
 375          $this->assertTrue($DB->record_exists('groupings_groups', array('groupid' => $group1->id, 'groupingid' => $grouping2->id)));
 376          $this->assertTrue($DB->record_exists('groupings_groups',
 377              array('groupid' => $group1c2->id, 'groupingid' => $grouping1c2->id)));
 378          groups_delete_groupings_groups($course->id);
 379          $this->assertFalse($DB->record_exists('groupings_groups', array('groupid' => $group1->id, 'groupingid' => $grouping1->id)));
 380          $this->assertFalse($DB->record_exists('groupings_groups', array('groupid' => $group2->id, 'groupingid' => $grouping1->id)));
 381          $this->assertFalse($DB->record_exists('groupings_groups', array('groupid' => $group1->id, 'groupingid' => $grouping2->id)));
 382          $this->assertTrue($DB->record_exists('groupings_groups',
 383              array('groupid' => $group1c2->id, 'groupingid' => $grouping1c2->id)));
 384      }
 385  
 386      public function test_groups_delete_groups() {
 387          global $DB;
 388          $this->resetAfterTest();
 389  
 390          $course = $this->getDataGenerator()->create_course();
 391          $course2 = $this->getDataGenerator()->create_course();
 392          $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
 393          $group2 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
 394          $group1c2 = $this->getDataGenerator()->create_group(array('courseid' => $course2->id));
 395          $grouping1 = $this->getDataGenerator()->create_grouping(array('courseid' => $course->id));
 396          $grouping2 = $this->getDataGenerator()->create_grouping(array('courseid' => $course->id));
 397          $user1 = $this->getDataGenerator()->create_user();
 398          $this->getDataGenerator()->enrol_user($user1->id, $course->id);
 399          $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id, 'userid' => $user1->id));
 400          $this->getDataGenerator()->create_grouping_group(array('groupid' => $group1->id, 'groupingid' => $grouping1->id));
 401  
 402          $this->assertTrue($DB->record_exists('groups', array('id' => $group1->id, 'courseid' => $course->id)));
 403          $this->assertTrue($DB->record_exists('groups', array('id' => $group2->id, 'courseid' => $course->id)));
 404          $this->assertTrue($DB->record_exists('groups', array('id' => $group1c2->id, 'courseid' => $course2->id)));
 405          $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $group1->id, 'userid' => $user1->id)));
 406          $this->assertTrue($DB->record_exists('groupings', array('id' => $grouping1->id, 'courseid' => $course->id)));
 407          $this->assertTrue($DB->record_exists('groupings_groups', array('groupid' => $group1->id, 'groupingid' => $grouping1->id)));
 408          groups_delete_groups($course->id);
 409          $this->assertFalse($DB->record_exists('groups', array('id' => $group1->id, 'courseid' => $course->id)));
 410          $this->assertFalse($DB->record_exists('groups', array('id' => $group2->id, 'courseid' => $course->id)));
 411          $this->assertTrue($DB->record_exists('groups', array('id' => $group1c2->id, 'courseid' => $course2->id)));
 412          $this->assertFalse($DB->record_exists('groups_members', array('groupid' => $group1->id, 'userid' => $user1->id)));
 413          $this->assertTrue($DB->record_exists('groupings', array('id' => $grouping1->id, 'courseid' => $course->id)));
 414          $this->assertFalse($DB->record_exists('groupings_groups', array('groupid' => $group1->id, 'groupingid' => $grouping1->id)));
 415      }
 416  
 417      public function test_groups_delete_groupings() {
 418          global $DB;
 419          $this->resetAfterTest();
 420  
 421          $course = $this->getDataGenerator()->create_course();
 422          $course2 = $this->getDataGenerator()->create_course();
 423          $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
 424          $grouping1 = $this->getDataGenerator()->create_grouping(array('courseid' => $course->id));
 425          $grouping2 = $this->getDataGenerator()->create_grouping(array('courseid' => $course->id));
 426          $grouping1c2 = $this->getDataGenerator()->create_grouping(array('courseid' => $course2->id));
 427          $this->getDataGenerator()->create_grouping_group(array('groupid' => $group1->id, 'groupingid' => $grouping1->id));
 428  
 429          $this->assertTrue($DB->record_exists('groups', array('id' => $group1->id, 'courseid' => $course->id)));
 430          $this->assertTrue($DB->record_exists('groupings', array('id' => $grouping1->id, 'courseid' => $course->id)));
 431          $this->assertTrue($DB->record_exists('groupings', array('id' => $grouping2->id, 'courseid' => $course->id)));
 432          $this->assertTrue($DB->record_exists('groupings', array('id' => $grouping1c2->id, 'courseid' => $course2->id)));
 433          $this->assertTrue($DB->record_exists('groupings_groups', array('groupid' => $group1->id, 'groupingid' => $grouping1->id)));
 434          groups_delete_groupings($course->id);
 435          $this->assertTrue($DB->record_exists('groups', array('id' => $group1->id, 'courseid' => $course->id)));
 436          $this->assertFalse($DB->record_exists('groupings', array('id' => $grouping1->id, 'courseid' => $course->id)));
 437          $this->assertFalse($DB->record_exists('groupings', array('id' => $grouping2->id, 'courseid' => $course->id)));
 438          $this->assertTrue($DB->record_exists('groupings', array('id' => $grouping1c2->id, 'courseid' => $course2->id)));
 439          $this->assertFalse($DB->record_exists('groupings_groups', array('groupid' => $group1->id, 'groupingid' => $grouping1->id)));
 440      }
 441  
 442      public function test_groups_create_autogroups () {
 443          global $DB;
 444          $this->resetAfterTest();
 445  
 446          $course = $this->getDataGenerator()->create_course();
 447          $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
 448          $group2 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
 449          $group3 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
 450          $grouping1 = $this->getDataGenerator()->create_grouping(array('courseid' => $course->id));
 451          $this->getDataGenerator()->create_grouping_group(array('groupid' => $group2->id, 'groupingid' => $grouping1->id));
 452          $this->getDataGenerator()->create_grouping_group(array('groupid' => $group3->id, 'groupingid' => $grouping1->id));
 453          $user1 = $this->getDataGenerator()->create_user();
 454          $user2 = $this->getDataGenerator()->create_user();
 455          $user3 = $this->getDataGenerator()->create_user();
 456          $user4 = $this->getDataGenerator()->create_user();
 457          $this->getDataGenerator()->enrol_user($user1->id, $course->id);
 458          $this->getDataGenerator()->enrol_user($user2->id, $course->id);
 459          $this->getDataGenerator()->enrol_user($user3->id, $course->id);
 460          $this->getDataGenerator()->enrol_user($user4->id, $course->id);
 461          $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id, 'userid' => $user1->id));
 462          $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id, 'userid' => $user2->id));
 463          $this->getDataGenerator()->create_group_member(array('groupid' => $group2->id, 'userid' => $user3->id));
 464          $this->getDataGenerator()->create_group_member(array('groupid' => $group3->id, 'userid' => $user4->id));
 465  
 466          // Test autocreate group based on all course users.
 467          $users = groups_get_potential_members($course->id);
 468          $group4 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
 469          foreach ($users as $user) {
 470              $this->getDataGenerator()->create_group_member(array('groupid' => $group4->id, 'userid' => $user->id));
 471          }
 472          $this->assertEquals(4, $DB->count_records('groups_members', array('groupid' => $group4->id)));
 473  
 474          // Test autocreate group based on existing group.
 475          $source = array();
 476          $source['groupid'] = $group1->id;
 477          $users = groups_get_potential_members($course->id, 0, $source);
 478          $group5 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
 479          foreach ($users as $user) {
 480              $this->getDataGenerator()->create_group_member(array('groupid' => $group5->id, 'userid' => $user->id));
 481          }
 482          $this->assertEquals(2, $DB->count_records('groups_members', array('groupid' => $group5->id)));
 483  
 484          // Test autocreate group based on existing grouping.
 485          $source = array();
 486          $source['groupingid'] = $grouping1->id;
 487          $users = groups_get_potential_members($course->id, 0, $source);
 488          $group6 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
 489          foreach ($users as $user) {
 490              $this->getDataGenerator()->create_group_member(array('groupid' => $group6->id, 'userid' => $user->id));
 491          }
 492          $this->assertEquals(2, $DB->count_records('groups_members', array('groupid' => $group6->id)));
 493      }
 494  
 495      /**
 496       * Test groups_create_group enabling a group conversation.
 497       */
 498      public function test_groups_create_group_with_conversation() {
 499          global $DB;
 500  
 501          $this->resetAfterTest();
 502          $this->setAdminUser();
 503          $course1 = $this->getDataGenerator()->create_course();
 504          $coursecontext1 = \context_course::instance($course1->id);
 505  
 506          // Create two groups and only one group with enablemessaging = 1.
 507          $group1a = $this->getDataGenerator()->create_group(array('courseid' => $course1->id, 'enablemessaging' => 1));
 508          $group1b = $this->getDataGenerator()->create_group(array('courseid' => $course1->id, 'enablemessaging' => 0));
 509  
 510          $conversations = $DB->get_records('message_conversations',
 511              [
 512                  'contextid' => $coursecontext1->id,
 513                  'component' => 'core_group',
 514                  'itemtype' => 'groups',
 515                  'enabled' => \core_message\api::MESSAGE_CONVERSATION_ENABLED
 516              ]
 517          );
 518          $this->assertCount(1, $conversations);
 519  
 520          $conversation = reset($conversations);
 521          // Check groupid was stored in itemid on conversation area.
 522          $this->assertEquals($group1a->id, $conversation->itemid);
 523  
 524          $conversations = $DB->get_records('message_conversations', ['id' => $conversation->id]);
 525          $this->assertCount(1, $conversations);
 526  
 527          $conversation = reset($conversations);
 528  
 529          // Check group name was stored in conversation.
 530          $this->assertEquals($group1a->name, $conversation->name);
 531      }
 532  
 533      /**
 534       * Test groups_update_group enabling and disabling a group conversation.
 535       */
 536      public function test_groups_update_group_conversation() {
 537          global $DB;
 538  
 539          $this->resetAfterTest();
 540          $this->setAdminUser();
 541          $course1 = $this->getDataGenerator()->create_course();
 542          $coursecontext1 = \context_course::instance($course1->id);
 543  
 544          // Create two groups and only one group with enablemessaging = 1.
 545          $group1a = $this->getDataGenerator()->create_group(array('courseid' => $course1->id, 'enablemessaging' => 1));
 546          $group1b = $this->getDataGenerator()->create_group(array('courseid' => $course1->id, 'enablemessaging' => 0));
 547  
 548          $conversations = $DB->get_records('message_conversations',
 549              [
 550                  'contextid' => $coursecontext1->id,
 551                  'component' => 'core_group',
 552                  'itemtype' => 'groups',
 553                  'enabled' => \core_message\api::MESSAGE_CONVERSATION_ENABLED
 554              ]
 555          );
 556          $this->assertCount(1, $conversations);
 557  
 558          // Check that the conversation area is created when group messaging is enabled in the course group.
 559          $group1b->enablemessaging = 1;
 560          groups_update_group($group1b);
 561  
 562          $conversations = $DB->get_records('message_conversations',
 563              [
 564                  'contextid' => $coursecontext1->id,
 565                  'component' => 'core_group',
 566                  'itemtype' => 'groups',
 567                  'enabled' => \core_message\api::MESSAGE_CONVERSATION_ENABLED
 568              ],
 569          'id ASC');
 570          $this->assertCount(2, $conversations);
 571  
 572          $conversation1a = array_shift($conversations);
 573          $conversation1b = array_shift($conversations);
 574  
 575          $conversation1b = $DB->get_record('message_conversations', ['id' => $conversation1b->id]);
 576  
 577          // Check for group1b that group name was stored in conversation.
 578          $this->assertEquals($group1b->name, $conversation1b->name);
 579  
 580          $group1b->enablemessaging = 0;
 581          groups_update_group($group1b);
 582          $this->assertEquals(0, $DB->get_field("message_conversations", "enabled", ['id' => $conversation1b->id]));
 583  
 584          // Check that the name of the conversation is changed when the name of the course group is updated.
 585          $group1b->name = 'New group name';
 586          groups_update_group($group1b);
 587          $conversation1b = $DB->get_record('message_conversations', ['id' => $conversation1b->id]);
 588          $this->assertEquals($group1b->name, $conversation1b->name);
 589      }
 590  
 591      /**
 592       * Test groups_add_member to conversation.
 593       */
 594      public function test_groups_add_member_conversation() {
 595          global $DB;
 596          $this->resetAfterTest();
 597  
 598          $this->setAdminUser();
 599  
 600          $course1 = $this->getDataGenerator()->create_course();
 601          $coursecontext1 = \context_course::instance($course1->id);
 602  
 603          $user1 = $this->getDataGenerator()->create_user();
 604          $user2 = $this->getDataGenerator()->create_user();
 605          $user3 = $this->getDataGenerator()->create_user();
 606  
 607          $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
 608          $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
 609          $this->getDataGenerator()->enrol_user($user3->id, $course1->id);
 610  
 611          $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course1->id, 'enablemessaging' => 1));
 612  
 613          // Add users to group1.
 614          $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id, 'userid' => $user1->id));
 615          $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id, 'userid' => $user2->id));
 616  
 617          $conversation = \core_message\api::get_conversation_by_area(
 618              'core_group',
 619              'groups',
 620              $group1->id,
 621              $coursecontext1->id
 622          );
 623  
 624          // Check if the users has been added to the conversation.
 625          $this->assertEquals(2, $DB->count_records('message_conversation_members', ['conversationid' => $conversation->id]));
 626  
 627          // Check if the user has been added to the conversation when the conversation is disabled.
 628          \core_message\api::disable_conversation($conversation->id);
 629          $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id, 'userid' => $user3->id));
 630          $this->assertEquals(3, $DB->count_records('message_conversation_members', ['conversationid' => $conversation->id]));
 631      }
 632  
 633      /**
 634       * Test groups_remove_member to conversation.
 635       */
 636      public function test_groups_remove_member_conversation() {
 637          global $DB;
 638  
 639          $this->resetAfterTest();
 640  
 641          $this->setAdminUser();
 642  
 643          $course1 = $this->getDataGenerator()->create_course();
 644          $coursecontext1 = \context_course::instance($course1->id);
 645  
 646          $user1 = $this->getDataGenerator()->create_user();
 647          $user2 = $this->getDataGenerator()->create_user();
 648          $user3 = $this->getDataGenerator()->create_user();
 649  
 650          $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
 651          $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
 652          $this->getDataGenerator()->enrol_user($user3->id, $course1->id);
 653  
 654          $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course1->id, 'enablemessaging' => 1));
 655  
 656          $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id, 'userid' => $user1->id));
 657          $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id, 'userid' => $user2->id));
 658          $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id, 'userid' => $user3->id));
 659  
 660          $conversation = \core_message\api::get_conversation_by_area(
 661              'core_group',
 662              'groups',
 663              $group1->id,
 664              $coursecontext1->id
 665          );
 666  
 667          // Check if there are three users in the conversation.
 668          $this->assertEquals(3, $DB->count_records('message_conversation_members', ['conversationid' => $conversation->id]));
 669  
 670          // Check if after removing one member in the conversation there are two members.
 671          groups_remove_member($group1->id, $user1->id);
 672          $this->assertEquals(2, $DB->count_records('message_conversation_members', ['conversationid' => $conversation->id]));
 673  
 674          // Check if the user has been removed from the conversation when the conversation is disabled.
 675          \core_message\api::disable_conversation($conversation->id);
 676          groups_remove_member($group1->id, $user2->id);
 677          $this->assertEquals(1, $DB->count_records('message_conversation_members', ['conversationid' => $conversation->id]));
 678      }
 679  
 680      /**
 681       * Test if you enable group messaging in a group with members these are added to the conversation.
 682       */
 683      public function test_add_members_group_updated_conversation_enabled() {
 684          global $DB;
 685  
 686          $this->resetAfterTest();
 687  
 688          $this->setAdminUser();
 689  
 690          $course1 = $this->getDataGenerator()->create_course();
 691          $coursecontext1 = \context_course::instance($course1->id);
 692  
 693          $user1 = $this->getDataGenerator()->create_user();
 694          $user2 = $this->getDataGenerator()->create_user();
 695          $user3 = $this->getDataGenerator()->create_user();
 696  
 697          $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
 698          $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
 699          $this->getDataGenerator()->enrol_user($user3->id, $course1->id);
 700  
 701          $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course1->id, 'enablemessaging' => 0));
 702  
 703          $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id, 'userid' => $user1->id));
 704          $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id, 'userid' => $user2->id));
 705          $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id, 'userid' => $user3->id));
 706  
 707          $conversation = \core_message\api::get_conversation_by_area(
 708              'core_group',
 709              'groups',
 710              $group1->id,
 711              $coursecontext1->id
 712          );
 713  
 714          // No conversation should exist as 'enablemessaging' was set to 0.
 715          $this->assertFalse($conversation);
 716  
 717          // Check that the three users are in the conversation when group messaging is enabled in the course group.
 718          $group1->enablemessaging = 1;
 719          groups_update_group($group1);
 720  
 721          $conversation = \core_message\api::get_conversation_by_area(
 722              'core_group',
 723              'groups',
 724              $group1->id,
 725              $coursecontext1->id
 726          );
 727  
 728          $this->assertEquals(3, $DB->count_records('message_conversation_members', ['conversationid' => $conversation->id]));
 729      }
 730  
 731      public function test_groups_get_members_by_role(): void {
 732          $this->resetAfterTest();
 733  
 734          $this->setAdminUser();
 735  
 736          $course1 = $this->getDataGenerator()->create_course();
 737  
 738          $user1 = $this->getDataGenerator()->create_user(['username' => 'user1', 'idnumber' => 1]);
 739          $user2 = $this->getDataGenerator()->create_user(['username' => 'user2', 'idnumber' => 2]);
 740          $user3 = $this->getDataGenerator()->create_user(['username' => 'user3', 'idnumber' => 3]);
 741  
 742          $this->getDataGenerator()->enrol_user($user1->id, $course1->id, 0);
 743          $this->getDataGenerator()->enrol_user($user2->id, $course1->id, 1);
 744          $this->getDataGenerator()->enrol_user($user3->id, $course1->id, 1);
 745  
 746          $group1 = $this->getDataGenerator()->create_group(['courseid' => $course1->id]);
 747  
 748          $this->getDataGenerator()->create_group_member(['groupid' => $group1->id, 'userid' => $user1->id]);
 749          $this->getDataGenerator()->create_group_member(['groupid' => $group1->id, 'userid' => $user2->id]);
 750          $this->getDataGenerator()->create_group_member(['groupid' => $group1->id, 'userid' => $user3->id]);
 751  
 752          // Test basic usage.
 753          $result = groups_get_members_by_role($group1->id, $course1->id);
 754          $this->assertEquals(1, count($result[0]->users));
 755          $this->assertEquals(2, count($result[1]->users));
 756          $this->assertEquals($user1->firstname, reset($result[0]->users)->firstname);
 757          $this->assertEquals($user1->username, reset($result[0]->users)->username);
 758  
 759          // Test with specified fields.
 760          $result = groups_get_members_by_role($group1->id, $course1->id, 'u.firstname, u.lastname');
 761          $this->assertEquals(1, count($result[0]->users));
 762          $this->assertEquals($user1->firstname, reset($result[0]->users)->firstname);
 763          $this->assertEquals($user1->lastname, reset($result[0]->users)->lastname);
 764          $this->assertEquals(false, isset(reset($result[0]->users)->username));
 765  
 766          // Test with sorting.
 767          $result = groups_get_members_by_role($group1->id, $course1->id, 'u.username', 'u.username DESC');
 768          $this->assertEquals(1, count($result[0]->users));
 769          $this->assertEquals($user3->username, reset($result[1]->users)->username);
 770          $result = groups_get_members_by_role($group1->id, $course1->id, 'u.username', 'u.username ASC');
 771          $this->assertEquals(1, count($result[0]->users));
 772          $this->assertEquals($user2->username, reset($result[1]->users)->username);
 773  
 774          // Test with extra WHERE.
 775          $result = groups_get_members_by_role(
 776              $group1->id,
 777              $course1->id,
 778              'u.username',
 779              null,
 780              'u.idnumber > :number',
 781              ['number' => 2]);
 782          $this->assertEquals(1, count($result));
 783          $this->assertEquals(1, count($result[1]->users));
 784          $this->assertEquals($user3->username, reset($result[1]->users)->username);
 785  
 786          // Test with join.
 787          set_user_preference('reptile', 'snake', $user1);
 788          $result = groups_get_members_by_role($group1->id, $course1->id, 'u.username, up.value', null, 'up.name = :prefname',
 789                  ['prefname' => 'reptile'], 'JOIN {user_preferences} up ON up.userid = u.id');
 790          $this->assertEquals('snake', reset($result[0]->users)->value);
 791      }
 792  }