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