Search moodle.org's
Developer Documentation

See Release Notes

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

Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [Versions 400 and 403] [Versions 401 and 403] [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  require_once($CFG->dirroot . '/lib/grouplib.php');
  31  
  32  /**
  33   * Group lib testcase.
  34   *
  35   * @package    core_group
  36   * @copyright  2013 Frédéric Massart
  37   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  38   */
  39  class lib_test extends \advanced_testcase {
  40  
  41      public function test_member_added_event() {
  42          $this->resetAfterTest();
  43  
  44          $this->setAdminUser();
  45          $course = $this->getDataGenerator()->create_course();
  46          $user = $this->getDataGenerator()->create_user();
  47          $group = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
  48          $this->getDataGenerator()->enrol_user($user->id, $course->id);
  49  
  50          $sink = $this->redirectEvents();
  51          groups_add_member($group->id, $user->id, 'mod_workshop', '123');
  52          $events = $sink->get_events();
  53          $this->assertCount(1, $events);
  54          $event = reset($events);
  55  
  56          $this->assertInstanceOf('\core\event\group_member_added', $event);
  57          $this->assertEquals($user->id, $event->relateduserid);
  58          $this->assertEquals(\context_course::instance($course->id), $event->get_context());
  59          $this->assertEquals($group->id, $event->objectid);
  60          $url = new \moodle_url('/group/members.php', array('group' => $event->objectid));
  61          $this->assertEquals($url, $event->get_url());
  62      }
  63  
  64      public function test_member_removed_event() {
  65          $this->resetAfterTest();
  66  
  67          $this->setAdminUser();
  68          $course = $this->getDataGenerator()->create_course();
  69          $user = $this->getDataGenerator()->create_user();
  70          $group = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
  71          $this->getDataGenerator()->enrol_user($user->id, $course->id);
  72          $this->getDataGenerator()->create_group_member(array('userid' => $user->id, 'groupid' => $group->id));
  73  
  74          $sink = $this->redirectEvents();
  75          groups_remove_member($group->id, $user->id);
  76          $events = $sink->get_events();
  77          $this->assertCount(1, $events);
  78          $event = reset($events);
  79  
  80          $this->assertInstanceOf('\core\event\group_member_removed', $event);
  81          $this->assertEquals($user->id, $event->relateduserid);
  82          $this->assertEquals(\context_course::instance($course->id), $event->get_context());
  83          $this->assertEquals($group->id, $event->objectid);
  84          $url = new \moodle_url('/group/members.php', array('group' => $event->objectid));
  85          $this->assertEquals($url, $event->get_url());
  86      }
  87  
  88      public function test_group_created_event() {
  89          $this->resetAfterTest();
  90  
  91          $this->setAdminUser();
  92          $course = $this->getDataGenerator()->create_course();
  93  
  94          $sink = $this->redirectEvents();
  95          $group = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
  96          $events = $sink->get_events();
  97          $this->assertCount(1, $events);
  98          $event = reset($events);
  99  
 100          $this->assertInstanceOf('\core\event\group_created', $event);
 101          $this->assertEquals(\context_course::instance($course->id), $event->get_context());
 102          $this->assertEquals($group->id, $event->objectid);
 103          $url = new \moodle_url('/group/index.php', array('id' => $event->courseid));
 104          $this->assertEquals($url, $event->get_url());
 105      }
 106  
 107      public function test_grouping_created_event() {
 108          $this->resetAfterTest();
 109  
 110          $this->setAdminUser();
 111          $course = $this->getDataGenerator()->create_course();
 112  
 113          $sink = $this->redirectEvents();
 114          $group = $this->getDataGenerator()->create_grouping(array('courseid' => $course->id));
 115          $events = $sink->get_events();
 116          $this->assertCount(1, $events);
 117          $event = reset($events);
 118  
 119          $this->assertInstanceOf('\core\event\grouping_created', $event);
 120  
 121          $this->assertEquals(\context_course::instance($course->id), $event->get_context());
 122          $this->assertEquals($group->id, $event->objectid);
 123          $url = new \moodle_url('/group/groupings.php', array('id' => $event->courseid));
 124          $this->assertEquals($url, $event->get_url());
 125      }
 126  
 127      public function test_group_updated_event() {
 128          global $DB;
 129  
 130          $this->resetAfterTest();
 131  
 132          $course = $this->getDataGenerator()->create_course();
 133          $group = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
 134  
 135          $sink = $this->redirectEvents();
 136          $data = new \stdClass();
 137          $data->id = $group->id;
 138          $data->courseid = $course->id;
 139          $data->name = 'Backend team';
 140          $this->setCurrentTimeStart();
 141          groups_update_group($data);
 142          $group = $DB->get_record('groups', array('id'=>$group->id)); // Fetch record with modified timestamp.
 143          $events = $sink->get_events();
 144          $this->assertCount(1, $events);
 145          $event = reset($events);
 146          $this->assertTimeCurrent($group->timemodified);
 147  
 148          $this->assertInstanceOf('\core\event\group_updated', $event);
 149          $group->name = $data->name;
 150          $this->assertEquals(\context_course::instance($course->id), $event->get_context());
 151          $this->assertEquals($group->id, $event->objectid);
 152          $url = new \moodle_url('/group/group.php', array('id' => $event->objectid));
 153          $this->assertEquals($url, $event->get_url());
 154      }
 155  
 156      public function test_group_updated_event_does_not_require_names() {
 157          global $DB;
 158  
 159          $this->resetAfterTest();
 160  
 161          $course = $this->getDataGenerator()->create_course();
 162          $group = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
 163  
 164          $sink = $this->redirectEvents();
 165          $data = new \stdClass();
 166          $data->id = $group->id;
 167          $data->courseid = $course->id;
 168          $this->setCurrentTimeStart();
 169          groups_update_group($data);
 170          $group = $DB->get_record('groups', array('id'=>$group->id)); // Fetch record with modified timestamp.
 171          $events = $sink->get_events();
 172          $this->assertCount(1, $events);
 173          $event = reset($events);
 174          $this->assertTimeCurrent($group->timemodified);
 175  
 176          $this->assertInstanceOf('\core\event\group_updated', $event);
 177          $this->assertEquals(\context_course::instance($course->id), $event->get_context());
 178          $this->assertEquals($group->id, $event->objectid);
 179          $url = new \moodle_url('/group/group.php', array('id' => $event->objectid));
 180          $this->assertEquals($url, $event->get_url());
 181      }
 182  
 183      public function test_grouping_updated_event() {
 184          global $DB;
 185  
 186          $this->resetAfterTest();
 187  
 188          $course = $this->getDataGenerator()->create_course();
 189          $grouping = $this->getDataGenerator()->create_grouping(array('courseid' => $course->id));
 190  
 191          $sink = $this->redirectEvents();
 192          $data = new \stdClass();
 193          $data->id = $grouping->id;
 194          $data->courseid = $course->id;
 195          $data->name = 'Backend team';
 196          $this->setCurrentTimeStart();
 197          groups_update_grouping($data);
 198          $events = $sink->get_events();
 199          $this->assertCount(1, $events);
 200          $event = reset($events);
 201  
 202          $this->assertInstanceOf('\core\event\grouping_updated', $event);
 203  
 204          // Get the timemodified from DB for comparison with snapshot.
 205          $data->timemodified = $DB->get_field('groupings', 'timemodified', array('id'=>$grouping->id));
 206          $this->assertTimeCurrent($data->timemodified);
 207  
 208          $this->assertEquals(\context_course::instance($course->id), $event->get_context());
 209          $this->assertEquals($grouping->id, $event->objectid);
 210          $url = new \moodle_url('/group/grouping.php', array('id' => $event->objectid));
 211          $this->assertEquals($url, $event->get_url());
 212      }
 213  
 214      public function test_grouping_updated_event_does_not_require_names() {
 215          global $DB;
 216  
 217          $this->resetAfterTest();
 218  
 219          $course = $this->getDataGenerator()->create_course();
 220          $grouping = $this->getDataGenerator()->create_grouping(array('courseid' => $course->id));
 221  
 222          $sink = $this->redirectEvents();
 223          $data = new \stdClass();
 224          $data->id = $grouping->id;
 225          $data->courseid = $course->id;
 226          $this->setCurrentTimeStart();
 227          groups_update_grouping($data);
 228          $events = $sink->get_events();
 229          $this->assertCount(1, $events);
 230          $event = reset($events);
 231  
 232          $this->assertInstanceOf('\core\event\grouping_updated', $event);
 233  
 234          // Get the timemodified from DB for comparison with snapshot.
 235          $data->timemodified = $DB->get_field('groupings', 'timemodified', array('id'=>$grouping->id));
 236          $this->assertTimeCurrent($data->timemodified);
 237          // Following fields were not updated so the snapshot should have them the same as in original group.
 238          $data->description = $grouping->description;
 239          $data->descriptionformat = $grouping->descriptionformat;
 240          $data->configdata = $grouping->configdata;
 241          $data->idnumber = $grouping->idnumber;
 242          $data->name = $grouping->name;
 243          $data->timecreated = $grouping->timecreated;
 244  
 245          $this->assertEquals(\context_course::instance($course->id), $event->get_context());
 246          $this->assertEquals($grouping->id, $event->objectid);
 247          $url = new \moodle_url('/group/grouping.php', array('id' => $event->objectid));
 248          $this->assertEquals($url, $event->get_url());
 249      }
 250  
 251      public function test_group_deleted_event() {
 252          $this->resetAfterTest();
 253  
 254          $course = $this->getDataGenerator()->create_course();
 255          $group = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
 256  
 257          $sink = $this->redirectEvents();
 258          groups_delete_group($group->id);
 259          $events = $sink->get_events();
 260          $this->assertCount(1, $events);
 261          $event = reset($events);
 262  
 263          $this->assertEquals(\context_course::instance($course->id), $event->get_context());
 264          $this->assertEquals($group->id, $event->objectid);
 265          $url = new \moodle_url('/group/index.php', array('id' => $event->courseid));
 266          $this->assertEquals($url, $event->get_url());
 267      }
 268  
 269      public function test_grouping_deleted_event() {
 270          $this->resetAfterTest();
 271  
 272          $course = $this->getDataGenerator()->create_course();
 273          $group = $this->getDataGenerator()->create_grouping(array('courseid' => $course->id));
 274  
 275          $sink = $this->redirectEvents();
 276          groups_delete_grouping($group->id);
 277          $events = $sink->get_events();
 278          $this->assertCount(1, $events);
 279          $event = reset($events);
 280  
 281          $this->assertInstanceOf('\core\event\grouping_deleted', $event);
 282          $this->assertEquals(\context_course::instance($course->id), $event->get_context());
 283          $this->assertEquals($group->id, $event->objectid);
 284          $url = new \moodle_url('/group/groupings.php', array('id' => $event->courseid));
 285          $this->assertEquals($url, $event->get_url());
 286      }
 287  
 288      public function test_groups_delete_group_members() {
 289          global $DB;
 290          $this->resetAfterTest();
 291  
 292          $course = $this->getDataGenerator()->create_course();
 293          $user1 = $this->getDataGenerator()->create_user();
 294          $user2 = $this->getDataGenerator()->create_user();
 295          $this->getDataGenerator()->enrol_user($user1->id, $course->id);
 296          $this->getDataGenerator()->enrol_user($user2->id, $course->id);
 297          $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
 298          $group2 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
 299  
 300          // Test deletion of all the users.
 301          $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id, 'userid' => $user1->id));
 302          $this->getDataGenerator()->create_group_member(array('groupid' => $group2->id, 'userid' => $user1->id));
 303          $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id, 'userid' => $user2->id));
 304  
 305          $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $group1->id, 'userid' => $user1->id)));
 306          $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $group2->id, 'userid' => $user1->id)));
 307          $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $group1->id, 'userid' => $user2->id)));
 308          groups_delete_group_members($course->id);
 309          $this->assertFalse($DB->record_exists('groups_members', array('groupid' => $group1->id, 'userid' => $user1->id)));
 310          $this->assertFalse($DB->record_exists('groups_members', array('groupid' => $group2->id, 'userid' => $user1->id)));
 311          $this->assertFalse($DB->record_exists('groups_members', array('groupid' => $group1->id, 'userid' => $user2->id)));
 312  
 313          // Test deletion of a specific user.
 314          $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id, 'userid' => $user1->id));
 315          $this->getDataGenerator()->create_group_member(array('groupid' => $group2->id, 'userid' => $user1->id));
 316          $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id, 'userid' => $user2->id));
 317  
 318          $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $group1->id, 'userid' => $user1->id)));
 319          $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $group2->id, 'userid' => $user1->id)));
 320          $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $group1->id, 'userid' => $user2->id)));
 321          groups_delete_group_members($course->id, $user2->id);
 322          $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $group1->id, 'userid' => $user1->id)));
 323          $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $group2->id, 'userid' => $user1->id)));
 324          $this->assertFalse($DB->record_exists('groups_members', array('groupid' => $group1->id, 'userid' => $user2->id)));
 325      }
 326  
 327      public function test_groups_remove_member() {
 328          global $DB;
 329          $this->resetAfterTest();
 330  
 331          $course = $this->getDataGenerator()->create_course();
 332          $user1 = $this->getDataGenerator()->create_user();
 333          $user2 = $this->getDataGenerator()->create_user();
 334          $this->getDataGenerator()->enrol_user($user1->id, $course->id);
 335          $this->getDataGenerator()->enrol_user($user2->id, $course->id);
 336          $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
 337          $group2 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
 338  
 339          $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id, 'userid' => $user1->id));
 340          $this->getDataGenerator()->create_group_member(array('groupid' => $group2->id, 'userid' => $user1->id));
 341          $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id, 'userid' => $user2->id));
 342  
 343          $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $group1->id, 'userid' => $user1->id)));
 344          $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $group2->id, 'userid' => $user1->id)));
 345          $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $group1->id, 'userid' => $user2->id)));
 346          groups_remove_member($group1->id, $user1->id);
 347          $this->assertFalse($DB->record_exists('groups_members', array('groupid' => $group1->id, 'userid' => $user1->id)));
 348          $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $group2->id, 'userid' => $user1->id)));
 349          $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $group1->id, 'userid' => $user2->id)));
 350          groups_remove_member($group1->id, $user2->id);
 351          $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $group2->id, 'userid' => $user1->id)));
 352          $this->assertFalse($DB->record_exists('groups_members', array('groupid' => $group1->id, 'userid' => $user2->id)));
 353          groups_remove_member($group2->id, $user1->id);
 354          $this->assertFalse($DB->record_exists('groups_members', array('groupid' => $group2->id, 'userid' => $user1->id)));
 355      }
 356  
 357      public function test_groups_delete_groupings_groups() {
 358          global $DB;
 359          $this->resetAfterTest();
 360  
 361          $course = $this->getDataGenerator()->create_course();
 362          $course2 = $this->getDataGenerator()->create_course();
 363          $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
 364          $group2 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
 365          $group1c2 = $this->getDataGenerator()->create_group(array('courseid' => $course2->id));
 366          $grouping1 = $this->getDataGenerator()->create_grouping(array('courseid' => $course->id));
 367          $grouping2 = $this->getDataGenerator()->create_grouping(array('courseid' => $course->id));
 368          $grouping1c2 = $this->getDataGenerator()->create_grouping(array('courseid' => $course2->id));
 369  
 370          $this->getDataGenerator()->create_grouping_group(array('groupingid' => $grouping1->id, 'groupid' => $group1->id));
 371          $this->getDataGenerator()->create_grouping_group(array('groupingid' => $grouping1->id, 'groupid' => $group2->id));
 372          $this->getDataGenerator()->create_grouping_group(array('groupingid' => $grouping2->id, 'groupid' => $group1->id));
 373          $this->getDataGenerator()->create_grouping_group(array('groupingid' => $grouping1c2->id, 'groupid' => $group1c2->id));
 374          $this->assertTrue($DB->record_exists('groupings_groups', array('groupid' => $group1->id, 'groupingid' => $grouping1->id)));
 375          $this->assertTrue($DB->record_exists('groupings_groups', array('groupid' => $group2->id, 'groupingid' => $grouping1->id)));
 376          $this->assertTrue($DB->record_exists('groupings_groups', array('groupid' => $group1->id, 'groupingid' => $grouping2->id)));
 377          $this->assertTrue($DB->record_exists('groupings_groups',
 378              array('groupid' => $group1c2->id, 'groupingid' => $grouping1c2->id)));
 379          groups_delete_groupings_groups($course->id);
 380          $this->assertFalse($DB->record_exists('groupings_groups', array('groupid' => $group1->id, 'groupingid' => $grouping1->id)));
 381          $this->assertFalse($DB->record_exists('groupings_groups', array('groupid' => $group2->id, 'groupingid' => $grouping1->id)));
 382          $this->assertFalse($DB->record_exists('groupings_groups', array('groupid' => $group1->id, 'groupingid' => $grouping2->id)));
 383          $this->assertTrue($DB->record_exists('groupings_groups',
 384              array('groupid' => $group1c2->id, 'groupingid' => $grouping1c2->id)));
 385      }
 386  
 387      public function test_groups_delete_groups() {
 388          global $DB;
 389          $this->resetAfterTest();
 390  
 391          $course = $this->getDataGenerator()->create_course();
 392          $course2 = $this->getDataGenerator()->create_course();
 393          $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
 394          $group2 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
 395          $group1c2 = $this->getDataGenerator()->create_group(array('courseid' => $course2->id));
 396          $grouping1 = $this->getDataGenerator()->create_grouping(array('courseid' => $course->id));
 397          $grouping2 = $this->getDataGenerator()->create_grouping(array('courseid' => $course->id));
 398          $user1 = $this->getDataGenerator()->create_user();
 399          $this->getDataGenerator()->enrol_user($user1->id, $course->id);
 400          $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id, 'userid' => $user1->id));
 401          $this->getDataGenerator()->create_grouping_group(array('groupid' => $group1->id, 'groupingid' => $grouping1->id));
 402  
 403          $this->assertTrue($DB->record_exists('groups', array('id' => $group1->id, 'courseid' => $course->id)));
 404          $this->assertTrue($DB->record_exists('groups', array('id' => $group2->id, 'courseid' => $course->id)));
 405          $this->assertTrue($DB->record_exists('groups', array('id' => $group1c2->id, 'courseid' => $course2->id)));
 406          $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $group1->id, 'userid' => $user1->id)));
 407          $this->assertTrue($DB->record_exists('groupings', array('id' => $grouping1->id, 'courseid' => $course->id)));
 408          $this->assertTrue($DB->record_exists('groupings_groups', array('groupid' => $group1->id, 'groupingid' => $grouping1->id)));
 409          groups_delete_groups($course->id);
 410          $this->assertFalse($DB->record_exists('groups', array('id' => $group1->id, 'courseid' => $course->id)));
 411          $this->assertFalse($DB->record_exists('groups', array('id' => $group2->id, 'courseid' => $course->id)));
 412          $this->assertTrue($DB->record_exists('groups', array('id' => $group1c2->id, 'courseid' => $course2->id)));
 413          $this->assertFalse($DB->record_exists('groups_members', array('groupid' => $group1->id, 'userid' => $user1->id)));
 414          $this->assertTrue($DB->record_exists('groupings', array('id' => $grouping1->id, 'courseid' => $course->id)));
 415          $this->assertFalse($DB->record_exists('groupings_groups', array('groupid' => $group1->id, 'groupingid' => $grouping1->id)));
 416      }
 417  
 418      public function test_groups_delete_groupings() {
 419          global $DB;
 420          $this->resetAfterTest();
 421  
 422          $course = $this->getDataGenerator()->create_course();
 423          $course2 = $this->getDataGenerator()->create_course();
 424          $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
 425          $grouping1 = $this->getDataGenerator()->create_grouping(array('courseid' => $course->id));
 426          $grouping2 = $this->getDataGenerator()->create_grouping(array('courseid' => $course->id));
 427          $grouping1c2 = $this->getDataGenerator()->create_grouping(array('courseid' => $course2->id));
 428          $this->getDataGenerator()->create_grouping_group(array('groupid' => $group1->id, 'groupingid' => $grouping1->id));
 429  
 430          $this->assertTrue($DB->record_exists('groups', array('id' => $group1->id, 'courseid' => $course->id)));
 431          $this->assertTrue($DB->record_exists('groupings', array('id' => $grouping1->id, 'courseid' => $course->id)));
 432          $this->assertTrue($DB->record_exists('groupings', array('id' => $grouping2->id, 'courseid' => $course->id)));
 433          $this->assertTrue($DB->record_exists('groupings', array('id' => $grouping1c2->id, 'courseid' => $course2->id)));
 434          $this->assertTrue($DB->record_exists('groupings_groups', array('groupid' => $group1->id, 'groupingid' => $grouping1->id)));
 435          groups_delete_groupings($course->id);
 436          $this->assertTrue($DB->record_exists('groups', array('id' => $group1->id, 'courseid' => $course->id)));
 437          $this->assertFalse($DB->record_exists('groupings', array('id' => $grouping1->id, 'courseid' => $course->id)));
 438          $this->assertFalse($DB->record_exists('groupings', array('id' => $grouping2->id, 'courseid' => $course->id)));
 439          $this->assertTrue($DB->record_exists('groupings', array('id' => $grouping1c2->id, 'courseid' => $course2->id)));
 440          $this->assertFalse($DB->record_exists('groupings_groups', array('groupid' => $group1->id, 'groupingid' => $grouping1->id)));
 441      }
 442  
 443      /**
 444       * Test custom field for group.
 445       * @covers ::groups_create_group
 446       * @covers ::groups_get_group
 447       */
 448      public function test_groups_with_customfield() {
 449          $this->resetAfterTest();
 450          $this->setAdminUser();
 451  
 452          $course1 = self::getDataGenerator()->create_course();
 453          $course2 = self::getDataGenerator()->create_course();
 454  
 455          $groupfieldcategory = self::getDataGenerator()->create_custom_field_category([
 456              'component' => 'core_group',
 457              'area' => 'group',
 458          ]);
 459          $groupcustomfield = self::getDataGenerator()->create_custom_field([
 460              'shortname' => 'testgroupcustomfield1',
 461              'type' => 'text',
 462              'categoryid' => $groupfieldcategory->get('id'),
 463          ]);
 464          $groupingfieldcategory = self::getDataGenerator()->create_custom_field_category([
 465              'component' => 'core_group',
 466              'area' => 'grouping',
 467          ]);
 468          $groupingcustomfield = self::getDataGenerator()->create_custom_field([
 469              'shortname' => 'testgroupingcustomfield1',
 470              'type' => 'text',
 471              'categoryid' => $groupingfieldcategory->get('id'),
 472          ]);
 473  
 474          $group1 = self::getDataGenerator()->create_group([
 475              'courseid' => $course1->id,
 476              'customfield_testgroupcustomfield1' => 'Custom input for group1',
 477          ]);
 478          $group2 = self::getDataGenerator()->create_group([
 479              'courseid' => $course2->id,
 480              'customfield_testgroupcustomfield1' => 'Custom input for group2',
 481          ]);
 482          $grouping1 = self::getDataGenerator()->create_grouping([
 483              'courseid' => $course1->id,
 484              'customfield_testgroupingcustomfield1' => 'Custom input for grouping1',
 485          ]);
 486          $grouping2 = self::getDataGenerator()->create_grouping([
 487              'courseid' => $course2->id,
 488              'customfield_testgroupingcustomfield1' => 'Custom input for grouping2',
 489          ]);
 490  
 491          $grouphandler = \core_group\customfield\group_handler::create();
 492          $data = $grouphandler->export_instance_data_object($group1->id);
 493          $this->assertSame('Custom input for group1', $data->testgroupcustomfield1);
 494          $data = $grouphandler->export_instance_data_object($group2->id);
 495          $this->assertSame('Custom input for group2', $data->testgroupcustomfield1);
 496  
 497          $groupinghandler = \core_group\customfield\grouping_handler::create();
 498          $data = $groupinghandler->export_instance_data_object($grouping1->id);
 499          $this->assertSame('Custom input for grouping1', $data->testgroupingcustomfield1);
 500          $data = $groupinghandler->export_instance_data_object($grouping2->id);
 501          $this->assertSame('Custom input for grouping2', $data->testgroupingcustomfield1);
 502  
 503          $group1->customfield_testgroupcustomfield1 = 'Updated input for group1';
 504          $group2->customfield_testgroupcustomfield1 = 'Updated input for group2';
 505          groups_update_group($group1);
 506          groups_update_group($group2);
 507          $data = $grouphandler->export_instance_data_object($group1->id);
 508          $this->assertSame('Updated input for group1', $data->testgroupcustomfield1);
 509          $data = $grouphandler->export_instance_data_object($group2->id);
 510          $this->assertSame('Updated input for group2', $data->testgroupcustomfield1);
 511  
 512          $group = groups_get_group($group1->id, '*', IGNORE_MISSING, true);
 513          $this->assertCount(1, $group->customfields);
 514          $customfield = reset($group->customfields);
 515          $this->assertSame('Updated input for group1', $customfield['value']);
 516  
 517          $grouping1->customfield_testgroupingcustomfield1 = 'Updated input for grouping1';
 518          $grouping2->customfield_testgroupingcustomfield1 = 'Updated input for grouping2';
 519          groups_update_grouping($grouping1);
 520          groups_update_grouping($grouping2);
 521          $data = $groupinghandler->export_instance_data_object($grouping1->id);
 522          $this->assertSame('Updated input for grouping1', $data->testgroupingcustomfield1);
 523          $data = $groupinghandler->export_instance_data_object($grouping2->id);
 524          $this->assertSame('Updated input for grouping2', $data->testgroupingcustomfield1);
 525  
 526          $grouping = groups_get_grouping($grouping1->id, '*', IGNORE_MISSING, true);
 527          $this->assertCount(1, $grouping->customfields);
 528          $customfield = reset($grouping->customfields);
 529          $this->assertSame('Updated input for grouping1', $customfield['value']);
 530      }
 531  
 532      public function test_groups_create_autogroups () {
 533          global $DB;
 534          $this->resetAfterTest();
 535  
 536          $course = $this->getDataGenerator()->create_course();
 537          $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
 538          $group2 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
 539          $group3 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
 540          $grouping1 = $this->getDataGenerator()->create_grouping(array('courseid' => $course->id));
 541          $this->getDataGenerator()->create_grouping_group(array('groupid' => $group2->id, 'groupingid' => $grouping1->id));
 542          $this->getDataGenerator()->create_grouping_group(array('groupid' => $group3->id, 'groupingid' => $grouping1->id));
 543          $user1 = $this->getDataGenerator()->create_user();
 544          $user2 = $this->getDataGenerator()->create_user();
 545          $user3 = $this->getDataGenerator()->create_user();
 546          $user4 = $this->getDataGenerator()->create_user();
 547          $this->getDataGenerator()->enrol_user($user1->id, $course->id);
 548          $this->getDataGenerator()->enrol_user($user2->id, $course->id);
 549          $this->getDataGenerator()->enrol_user($user3->id, $course->id);
 550          $this->getDataGenerator()->enrol_user($user4->id, $course->id);
 551          $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id, 'userid' => $user1->id));
 552          $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id, 'userid' => $user2->id));
 553          $this->getDataGenerator()->create_group_member(array('groupid' => $group2->id, 'userid' => $user3->id));
 554          $this->getDataGenerator()->create_group_member(array('groupid' => $group3->id, 'userid' => $user4->id));
 555  
 556          // Test autocreate group based on all course users.
 557          $users = groups_get_potential_members($course->id);
 558          $group4 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
 559          foreach ($users as $user) {
 560              $this->getDataGenerator()->create_group_member(array('groupid' => $group4->id, 'userid' => $user->id));
 561          }
 562          $this->assertEquals(4, $DB->count_records('groups_members', array('groupid' => $group4->id)));
 563  
 564          // Test autocreate group based on existing group.
 565          $source = array();
 566          $source['groupid'] = $group1->id;
 567          $users = groups_get_potential_members($course->id, 0, $source);
 568          $group5 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
 569          foreach ($users as $user) {
 570              $this->getDataGenerator()->create_group_member(array('groupid' => $group5->id, 'userid' => $user->id));
 571          }
 572          $this->assertEquals(2, $DB->count_records('groups_members', array('groupid' => $group5->id)));
 573  
 574          // Test autocreate group based on existing grouping.
 575          $source = array();
 576          $source['groupingid'] = $grouping1->id;
 577          $users = groups_get_potential_members($course->id, 0, $source);
 578          $group6 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
 579          foreach ($users as $user) {
 580              $this->getDataGenerator()->create_group_member(array('groupid' => $group6->id, 'userid' => $user->id));
 581          }
 582          $this->assertEquals(2, $DB->count_records('groups_members', array('groupid' => $group6->id)));
 583      }
 584  
 585      /**
 586       * Test groups_create_group enabling a group conversation.
 587       */
 588      public function test_groups_create_group_with_conversation() {
 589          global $DB;
 590  
 591          $this->resetAfterTest();
 592          $this->setAdminUser();
 593          $course1 = $this->getDataGenerator()->create_course();
 594          $coursecontext1 = \context_course::instance($course1->id);
 595  
 596          // Create two groups and only one group with enablemessaging = 1.
 597          $group1a = $this->getDataGenerator()->create_group(array('courseid' => $course1->id, 'enablemessaging' => 1));
 598          $group1b = $this->getDataGenerator()->create_group(array('courseid' => $course1->id, 'enablemessaging' => 0));
 599  
 600          $conversations = $DB->get_records('message_conversations',
 601              [
 602                  'contextid' => $coursecontext1->id,
 603                  'component' => 'core_group',
 604                  'itemtype' => 'groups',
 605                  'enabled' => \core_message\api::MESSAGE_CONVERSATION_ENABLED
 606              ]
 607          );
 608          $this->assertCount(1, $conversations);
 609  
 610          $conversation = reset($conversations);
 611          // Check groupid was stored in itemid on conversation area.
 612          $this->assertEquals($group1a->id, $conversation->itemid);
 613  
 614          $conversations = $DB->get_records('message_conversations', ['id' => $conversation->id]);
 615          $this->assertCount(1, $conversations);
 616  
 617          $conversation = reset($conversations);
 618  
 619          // Check group name was stored in conversation.
 620          $this->assertEquals($group1a->name, $conversation->name);
 621      }
 622  
 623      /**
 624       * Test groups_update_group enabling and disabling a group conversation.
 625       */
 626      public function test_groups_update_group_conversation() {
 627          global $DB;
 628  
 629          $this->resetAfterTest();
 630          $this->setAdminUser();
 631          $course1 = $this->getDataGenerator()->create_course();
 632          $coursecontext1 = \context_course::instance($course1->id);
 633  
 634          // Create two groups and only one group with enablemessaging = 1.
 635          $group1a = $this->getDataGenerator()->create_group(array('courseid' => $course1->id, 'enablemessaging' => 1));
 636          $group1b = $this->getDataGenerator()->create_group(array('courseid' => $course1->id, 'enablemessaging' => 0));
 637  
 638          $conversations = $DB->get_records('message_conversations',
 639              [
 640                  'contextid' => $coursecontext1->id,
 641                  'component' => 'core_group',
 642                  'itemtype' => 'groups',
 643                  'enabled' => \core_message\api::MESSAGE_CONVERSATION_ENABLED
 644              ]
 645          );
 646          $this->assertCount(1, $conversations);
 647  
 648          // Check that the conversation area is created when group messaging is enabled in the course group.
 649          $group1b->enablemessaging = 1;
 650          groups_update_group($group1b);
 651  
 652          $conversations = $DB->get_records('message_conversations',
 653              [
 654                  'contextid' => $coursecontext1->id,
 655                  'component' => 'core_group',
 656                  'itemtype' => 'groups',
 657                  'enabled' => \core_message\api::MESSAGE_CONVERSATION_ENABLED
 658              ],
 659          'id ASC');
 660          $this->assertCount(2, $conversations);
 661  
 662          $conversation1a = array_shift($conversations);
 663          $conversation1b = array_shift($conversations);
 664  
 665          $conversation1b = $DB->get_record('message_conversations', ['id' => $conversation1b->id]);
 666  
 667          // Check for group1b that group name was stored in conversation.
 668          $this->assertEquals($group1b->name, $conversation1b->name);
 669  
 670          $group1b->enablemessaging = 0;
 671          groups_update_group($group1b);
 672          $this->assertEquals(0, $DB->get_field("message_conversations", "enabled", ['id' => $conversation1b->id]));
 673  
 674          // Check that the name of the conversation is changed when the name of the course group is updated.
 675          $group1b->name = 'New group name';
 676          groups_update_group($group1b);
 677          $conversation1b = $DB->get_record('message_conversations', ['id' => $conversation1b->id]);
 678          $this->assertEquals($group1b->name, $conversation1b->name);
 679      }
 680  
 681      /**
 682       * Test groups_add_member to conversation.
 683       */
 684      public function test_groups_add_member_conversation() {
 685          global $DB;
 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' => 1));
 702  
 703          // Add users to group1.
 704          $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id, 'userid' => $user1->id));
 705          $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id, 'userid' => $user2->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          // Check if the users has been added to the conversation.
 715          $this->assertEquals(2, $DB->count_records('message_conversation_members', ['conversationid' => $conversation->id]));
 716  
 717          // Check if the user has been added to the conversation when the conversation is disabled.
 718          \core_message\api::disable_conversation($conversation->id);
 719          $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id, 'userid' => $user3->id));
 720          $this->assertEquals(3, $DB->count_records('message_conversation_members', ['conversationid' => $conversation->id]));
 721      }
 722  
 723      /**
 724       * Test groups_remove_member to conversation.
 725       */
 726      public function test_groups_remove_member_conversation() {
 727          global $DB;
 728  
 729          $this->resetAfterTest();
 730  
 731          $this->setAdminUser();
 732  
 733          $course1 = $this->getDataGenerator()->create_course();
 734          $coursecontext1 = \context_course::instance($course1->id);
 735  
 736          $user1 = $this->getDataGenerator()->create_user();
 737          $user2 = $this->getDataGenerator()->create_user();
 738          $user3 = $this->getDataGenerator()->create_user();
 739  
 740          $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
 741          $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
 742          $this->getDataGenerator()->enrol_user($user3->id, $course1->id);
 743  
 744          $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course1->id, 'enablemessaging' => 1));
 745  
 746          $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id, 'userid' => $user1->id));
 747          $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id, 'userid' => $user2->id));
 748          $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id, 'userid' => $user3->id));
 749  
 750          $conversation = \core_message\api::get_conversation_by_area(
 751              'core_group',
 752              'groups',
 753              $group1->id,
 754              $coursecontext1->id
 755          );
 756  
 757          // Check if there are three users in the conversation.
 758          $this->assertEquals(3, $DB->count_records('message_conversation_members', ['conversationid' => $conversation->id]));
 759  
 760          // Check if after removing one member in the conversation there are two members.
 761          groups_remove_member($group1->id, $user1->id);
 762          $this->assertEquals(2, $DB->count_records('message_conversation_members', ['conversationid' => $conversation->id]));
 763  
 764          // Check if the user has been removed from the conversation when the conversation is disabled.
 765          \core_message\api::disable_conversation($conversation->id);
 766          groups_remove_member($group1->id, $user2->id);
 767          $this->assertEquals(1, $DB->count_records('message_conversation_members', ['conversationid' => $conversation->id]));
 768      }
 769  
 770      /**
 771       * Test if you enable group messaging in a group with members these are added to the conversation.
 772       */
 773      public function test_add_members_group_updated_conversation_enabled() {
 774          global $DB;
 775  
 776          $this->resetAfterTest();
 777  
 778          $this->setAdminUser();
 779  
 780          $course1 = $this->getDataGenerator()->create_course();
 781          $coursecontext1 = \context_course::instance($course1->id);
 782  
 783          $user1 = $this->getDataGenerator()->create_user();
 784          $user2 = $this->getDataGenerator()->create_user();
 785          $user3 = $this->getDataGenerator()->create_user();
 786  
 787          $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
 788          $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
 789          $this->getDataGenerator()->enrol_user($user3->id, $course1->id);
 790  
 791          $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course1->id, 'enablemessaging' => 0));
 792  
 793          $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id, 'userid' => $user1->id));
 794          $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id, 'userid' => $user2->id));
 795          $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id, 'userid' => $user3->id));
 796  
 797          $conversation = \core_message\api::get_conversation_by_area(
 798              'core_group',
 799              'groups',
 800              $group1->id,
 801              $coursecontext1->id
 802          );
 803  
 804          // No conversation should exist as 'enablemessaging' was set to 0.
 805          $this->assertFalse($conversation);
 806  
 807          // Check that the three users are in the conversation when group messaging is enabled in the course group.
 808          $group1->enablemessaging = 1;
 809          groups_update_group($group1);
 810  
 811          $conversation = \core_message\api::get_conversation_by_area(
 812              'core_group',
 813              'groups',
 814              $group1->id,
 815              $coursecontext1->id
 816          );
 817  
 818          $this->assertEquals(3, $DB->count_records('message_conversation_members', ['conversationid' => $conversation->id]));
 819      }
 820  
 821      public function test_groups_get_members_by_role(): void {
 822          $this->resetAfterTest();
 823  
 824          $this->setAdminUser();
 825  
 826          $course1 = $this->getDataGenerator()->create_course();
 827  
 828          $user1 = $this->getDataGenerator()->create_user(['username' => 'user1', 'idnumber' => 1]);
 829          $user2 = $this->getDataGenerator()->create_user(['username' => 'user2', 'idnumber' => 2]);
 830          $user3 = $this->getDataGenerator()->create_user(['username' => 'user3', 'idnumber' => 3]);
 831  
 832          $this->getDataGenerator()->enrol_user($user1->id, $course1->id, 0);
 833          $this->getDataGenerator()->enrol_user($user2->id, $course1->id, 1);
 834          $this->getDataGenerator()->enrol_user($user3->id, $course1->id, 1);
 835  
 836          $group1 = $this->getDataGenerator()->create_group(['courseid' => $course1->id]);
 837  
 838          $this->getDataGenerator()->create_group_member(['groupid' => $group1->id, 'userid' => $user1->id]);
 839          $this->getDataGenerator()->create_group_member(['groupid' => $group1->id, 'userid' => $user2->id]);
 840          $this->getDataGenerator()->create_group_member(['groupid' => $group1->id, 'userid' => $user3->id]);
 841  
 842          // Test basic usage.
 843          $result = groups_get_members_by_role($group1->id, $course1->id);
 844          $this->assertEquals(1, count($result[0]->users));
 845          $this->assertEquals(2, count($result[1]->users));
 846          $this->assertEquals($user1->firstname, reset($result[0]->users)->firstname);
 847          $this->assertEquals($user1->username, reset($result[0]->users)->username);
 848  
 849          // Test with specified fields.
 850          $result = groups_get_members_by_role($group1->id, $course1->id, 'u.firstname, u.lastname');
 851          $this->assertEquals(1, count($result[0]->users));
 852          $this->assertEquals($user1->firstname, reset($result[0]->users)->firstname);
 853          $this->assertEquals($user1->lastname, reset($result[0]->users)->lastname);
 854          $this->assertEquals(false, isset(reset($result[0]->users)->username));
 855  
 856          // Test with sorting.
 857          $result = groups_get_members_by_role($group1->id, $course1->id, 'u.username', 'u.username DESC');
 858          $this->assertEquals(1, count($result[0]->users));
 859          $this->assertEquals($user3->username, reset($result[1]->users)->username);
 860          $result = groups_get_members_by_role($group1->id, $course1->id, 'u.username', 'u.username ASC');
 861          $this->assertEquals(1, count($result[0]->users));
 862          $this->assertEquals($user2->username, reset($result[1]->users)->username);
 863  
 864          // Test with extra WHERE.
 865          $result = groups_get_members_by_role(
 866              $group1->id,
 867              $course1->id,
 868              'u.username',
 869              null,
 870              'u.idnumber > :number',
 871              ['number' => 2]);
 872          $this->assertEquals(1, count($result));
 873          $this->assertEquals(1, count($result[1]->users));
 874          $this->assertEquals($user3->username, reset($result[1]->users)->username);
 875  
 876          // Test with join.
 877          set_user_preference('reptile', 'snake', $user1);
 878          $result = groups_get_members_by_role($group1->id, $course1->id, 'u.username, up.value', null, 'up.name = :prefname',
 879                  ['prefname' => 'reptile'], 'JOIN {user_preferences} up ON up.userid = u.id');
 880          $this->assertEquals('snake', reset($result[0]->users)->value);
 881      }
 882  
 883      /**
 884       * Tests set_groups_messaging
 885       *
 886       * @covers \core_group::set_groups_messaging
 887       */
 888      public function test_set_groups_messaging() {
 889          $this->resetAfterTest();
 890          $this->setAdminUser();
 891          $dg = $this->getDataGenerator();
 892          $course = $dg->create_course();
 893  
 894          // Create some groups in the course.
 895          $groupids = [];
 896          for ($i = 0; $i < 5; $i++) {
 897              $group = new \stdClass();
 898              $group->courseid = $course->id;
 899              $group->name = 'group-'.$i;
 900              $group->enablemessaging = 0;
 901              $groupids[] = groups_create_group($group);
 902          }
 903  
 904          // They should all initially be disabled.
 905          $alldisabledinitially = $this->check_groups_messaging_status_is($groupids, $course->id, false);
 906          $this->assertTrue($alldisabledinitially);
 907  
 908          // Enable messaging for all the groups.
 909          set_groups_messaging($groupids, true);
 910  
 911          // Check they were all enabled.
 912          $allenabled = $this->check_groups_messaging_status_is($groupids, $course->id, true);
 913          $this->assertTrue($allenabled);
 914  
 915          // Disable messaging for all the groups.
 916          set_groups_messaging($groupids, false);
 917  
 918          // Check they were all disabled.
 919          $alldisabled = $this->check_groups_messaging_status_is($groupids, $course->id, false);
 920          $this->assertTrue($alldisabled);
 921      }
 922  
 923      /**
 924       * Tests set group messaging where it doesn't exist
 925       *
 926       * @covers \core_group::set_groups_messaging
 927       */
 928      public function test_set_groups_messaging_doesnt_exist() {
 929          $this->resetAfterTest();
 930          $this->setAdminUser();
 931  
 932          $groupids = [-1];
 933  
 934          $this->expectException('dml_exception');
 935          set_groups_messaging($groupids, false);
 936      }
 937  
 938      /**
 939       * Checks the given list of groups to verify their messaging settings.
 940       *
 941       * @param array $groupids array of group ids
 942       * @param int $courseid the course the groups are in
 943       * @param bool $desired the desired setting value
 944       * @return bool true if all groups $enablemessaging setting matches the given $desired value, else false
 945       */
 946      private function check_groups_messaging_status_is(array $groupids, int $courseid, bool $desired) {
 947          $context = \context_course::instance($courseid);
 948  
 949          foreach ($groupids as $groupid) {
 950              $conversation = \core_message\api::get_conversation_by_area(
 951                  'core_group',
 952                  'groups',
 953                  $groupid,
 954                  $context->id
 955              );
 956  
 957              // An empty conversation means it has not been enabled yet.
 958              if (empty($conversation)) {
 959                  $conversation = (object) [
 960                      'enabled' => 0
 961                  ];
 962              }
 963  
 964              if ($desired !== boolval($conversation->enabled)) {
 965                  return false;
 966              }
 967          }
 968  
 969          return true;
 970      }
 971  }