See Release Notes
Long Term Support Release
Differences Between: [Versions 39 and 310] [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 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 * Tests groups subsystems. 19 * 20 * @package core_group 21 * @category phpunit 22 * @copyright 2007 onwards Martin Dougiamas (http://dougiamas.com) 23 * @author Andrew Nicols 24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 25 */ 26 27 defined('MOODLE_INTERNAL') || die(); 28 29 /** 30 * Unit tests for lib/grouplib.php 31 * @group core_group 32 */ 33 class core_grouplib_testcase extends advanced_testcase { 34 35 public function test_groups_get_group_by_idnumber() { 36 $this->resetAfterTest(true); 37 38 $generator = $this->getDataGenerator(); 39 40 // Create a course category and course. 41 $cat = $generator->create_category(array('parent' => 0)); 42 $course = $generator->create_course(array('category' => $cat->id)); 43 44 $idnumber1 = 'idnumber1'; 45 $idnumber2 = 'idnumber2'; 46 47 /* 48 * Test with an empty and a null idnumber. 49 */ 50 // An empty idnumber should always return a false value. 51 $this->assertFalse(groups_get_group_by_idnumber($course->id, '')); 52 $this->assertFalse(groups_get_group_by_idnumber($course->id, null)); 53 54 // Even when a group exists which also has an empty idnumber. 55 $generator->create_group(array('courseid' => $course->id)); 56 $this->assertFalse(groups_get_group_by_idnumber($course->id, '')); 57 $this->assertFalse(groups_get_group_by_idnumber($course->id, null)); 58 59 /* 60 * Test with a valid idnumber. 61 */ 62 // There is no matching idnumber at present. 63 $this->assertFalse(groups_get_group_by_idnumber($course->id, $idnumber1)); 64 65 // We should now have a valid group returned by the idnumber search. 66 $group = $generator->create_group(array('courseid' => $course->id, 'idnumber' => $idnumber1)); 67 $this->assertEquals($group, groups_get_group_by_idnumber($course->id, $idnumber1)); 68 69 // An empty idnumber should still return false. 70 $this->assertFalse(groups_get_group_by_idnumber($course->id, '')); 71 $this->assertFalse(groups_get_group_by_idnumber($course->id, null)); 72 73 /* 74 * Test with another idnumber. 75 */ 76 // There is no matching idnumber at present. 77 $this->assertFalse(groups_get_group_by_idnumber($course->id, $idnumber2)); 78 79 // We should now have a valid group returned by the idnumber search. 80 $group = $generator->create_group(array('courseid' => $course->id, 'idnumber' => $idnumber2)); 81 $this->assertEquals($group, groups_get_group_by_idnumber($course->id, $idnumber2)); 82 83 /* 84 * Group idnumbers are unique within a course so test that we don't 85 * retrieve groups for the first course. 86 */ 87 88 // Create a second course. 89 $course = $generator->create_course(array('category' => $cat->id)); 90 91 // An empty idnumber should always return a false value. 92 $this->assertFalse(groups_get_group_by_idnumber($course->id, '')); 93 $this->assertFalse(groups_get_group_by_idnumber($course->id, null)); 94 95 // Our existing idnumbers shouldn't be returned here as we're in a different course. 96 $this->assertFalse(groups_get_group_by_idnumber($course->id, $idnumber1)); 97 $this->assertFalse(groups_get_group_by_idnumber($course->id, $idnumber2)); 98 99 // We should be able to reuse the idnumbers again since this is a different course. 100 $group = $generator->create_group(array('courseid' => $course->id, 'idnumber' => $idnumber1)); 101 $this->assertEquals($group, groups_get_group_by_idnumber($course->id, $idnumber1)); 102 103 $group = $generator->create_group(array('courseid' => $course->id, 'idnumber' => $idnumber2)); 104 $this->assertEquals($group, groups_get_group_by_idnumber($course->id, $idnumber2)); 105 } 106 107 public function test_groups_get_grouping_by_idnumber() { 108 $this->resetAfterTest(true); 109 110 $generator = $this->getDataGenerator(); 111 112 // Create a course category and course. 113 $cat = $generator->create_category(array('parent' => 0)); 114 $course = $generator->create_course(array('category' => $cat->id)); 115 116 $idnumber1 = 'idnumber1'; 117 $idnumber2 = 'idnumber2'; 118 119 /* 120 * Test with an empty and a null idnumber. 121 */ 122 // An empty idnumber should always return a false value. 123 $this->assertFalse(groups_get_grouping_by_idnumber($course->id, '')); 124 $this->assertFalse(groups_get_grouping_by_idnumber($course->id, null)); 125 126 // Even when a grouping exists which also has an empty idnumber. 127 $generator->create_grouping(array('courseid' => $course->id)); 128 $this->assertFalse(groups_get_grouping_by_idnumber($course->id, '')); 129 $this->assertFalse(groups_get_grouping_by_idnumber($course->id, null)); 130 131 /* 132 * Test with a valid idnumber 133 */ 134 // There is no matching idnumber at present. 135 $this->assertFalse(groups_get_grouping_by_idnumber($course->id, $idnumber1)); 136 137 // We should now have a valid group returned by the idnumber search. 138 $grouping = $generator->create_grouping(array('courseid' => $course->id, 'idnumber' => $idnumber1)); 139 $this->assertEquals($grouping, groups_get_grouping_by_idnumber($course->id, $idnumber1)); 140 141 // An empty idnumber should still return false. 142 $this->assertFalse(groups_get_grouping_by_idnumber($course->id, '')); 143 $this->assertFalse(groups_get_grouping_by_idnumber($course->id, null)); 144 145 /* 146 * Test with another idnumber. 147 */ 148 // There is no matching idnumber at present. 149 $this->assertFalse(groups_get_grouping_by_idnumber($course->id, $idnumber2)); 150 151 // We should now have a valid grouping returned by the idnumber search. 152 $grouping = $generator->create_grouping(array('courseid' => $course->id, 'idnumber' => $idnumber2)); 153 $this->assertEquals($grouping, groups_get_grouping_by_idnumber($course->id, $idnumber2)); 154 155 /* 156 * Grouping idnumbers are unique within a course so test that we don't 157 * retrieve groupings for the first course. 158 */ 159 160 // Create a second course. 161 $course = $generator->create_course(array('category' => $cat->id)); 162 163 // An empty idnumber should always return a false value. 164 $this->assertFalse(groups_get_grouping_by_idnumber($course->id, '')); 165 $this->assertFalse(groups_get_grouping_by_idnumber($course->id, null)); 166 167 // Our existing idnumbers shouldn't be returned here as we're in a different course. 168 $this->assertFalse(groups_get_grouping_by_idnumber($course->id, $idnumber1)); 169 $this->assertFalse(groups_get_grouping_by_idnumber($course->id, $idnumber2)); 170 171 // We should be able to reuse the idnumbers again since this is a different course. 172 $grouping = $generator->create_grouping(array('courseid' => $course->id, 'idnumber' => $idnumber1)); 173 $this->assertEquals($grouping, groups_get_grouping_by_idnumber($course->id, $idnumber1)); 174 175 $grouping = $generator->create_grouping(array('courseid' => $course->id, 'idnumber' => $idnumber2)); 176 $this->assertEquals($grouping, groups_get_grouping_by_idnumber($course->id, $idnumber2)); 177 } 178 179 public function test_groups_get_members_ids_sql() { 180 global $DB; 181 182 $this->resetAfterTest(true); 183 184 $generator = $this->getDataGenerator(); 185 186 $course = $generator->create_course(); 187 $coursecontext = context_course::instance($course->id); 188 $student1 = $generator->create_user(); 189 $student2 = $generator->create_user(); 190 $plugin = enrol_get_plugin('manual'); 191 $role = $DB->get_record('role', array('shortname' => 'student')); 192 $group = $generator->create_group(array('courseid' => $course->id)); 193 $instance = $DB->get_record('enrol', array( 194 'courseid' => $course->id, 195 'enrol' => 'manual', 196 )); 197 198 $this->assertNotEquals($instance, false); 199 200 // Enrol users in the course. 201 $plugin->enrol_user($instance, $student1->id, $role->id); 202 $plugin->enrol_user($instance, $student2->id, $role->id); 203 204 list($sql, $params) = groups_get_members_ids_sql($group->id); 205 206 // Test an empty group. 207 $users = $DB->get_records_sql($sql, $params); 208 $this->assertFalse(array_key_exists($student1->id, $users)); 209 210 // Test with a group member. 211 groups_add_member($group->id, $student1->id); 212 $users = $DB->get_records_sql($sql, $params); 213 $this->assertTrue(array_key_exists($student1->id, $users)); 214 } 215 216 public function test_groups_get_members_ids_sql_multiple_groups() { 217 global $DB; 218 219 $this->resetAfterTest(true); 220 221 $generator = $this->getDataGenerator(); 222 223 $course = $generator->create_course(); 224 $student1 = $generator->create_user(); 225 $student2 = $generator->create_user(); 226 $plugin = enrol_get_plugin('manual'); 227 $role = $DB->get_record('role', array('shortname' => 'student')); 228 $group1 = $generator->create_group(array('courseid' => $course->id)); 229 $group2 = $generator->create_group(array('courseid' => $course->id)); 230 $groupids = [ 231 $group1->id, 232 $group2->id, 233 ]; 234 $instance = $DB->get_record('enrol', array( 235 'courseid' => $course->id, 236 'enrol' => 'manual', 237 )); 238 239 $this->assertNotEquals($instance, false); 240 241 // Enrol users in the course. 242 $plugin->enrol_user($instance, $student1->id, $role->id); 243 $plugin->enrol_user($instance, $student2->id, $role->id); 244 245 list($sql, $params) = groups_get_members_ids_sql($groupids); 246 247 // Test an empty group. 248 $users = $DB->get_records_sql($sql, $params); 249 $this->assertFalse(array_key_exists($student1->id, $users)); 250 251 // Test with a member of one of the two group. 252 groups_add_member($group1->id, $student1->id); 253 $users = $DB->get_records_sql($sql, $params); 254 $this->assertTrue(array_key_exists($student1->id, $users)); 255 256 // Test with members of two groups. 257 groups_add_member($group2->id, $student2->id); 258 $users = $DB->get_records_sql($sql, $params); 259 $this->assertTrue(array_key_exists($student1->id, $users)); 260 $this->assertTrue(array_key_exists($student2->id, $users)); 261 } 262 263 public function test_groups_get_members_ids_sql_multiple_groups_join_types() { 264 global $DB; 265 266 $this->resetAfterTest(true); 267 268 $generator = $this->getDataGenerator(); 269 270 $course = $generator->create_course(); 271 $student1 = $generator->create_user(); 272 $student2 = $generator->create_user(); 273 $student3 = $generator->create_user(); 274 $student4 = $generator->create_user(); 275 $student5 = $generator->create_user(); 276 $student6 = $generator->create_user(); 277 $plugin = enrol_get_plugin('manual'); 278 $role = $DB->get_record('role', array('shortname' => 'student')); 279 $group1 = $generator->create_group(array('courseid' => $course->id)); 280 $group2 = $generator->create_group(array('courseid' => $course->id)); 281 $group3 = $generator->create_group(array('courseid' => $course->id)); 282 // Only groups 1 and 2 specified in SQL (group 3 helps cover the None case). 283 $groupids = [ 284 $group1->id, 285 $group2->id, 286 ]; 287 $instance = $DB->get_record('enrol', array( 288 'courseid' => $course->id, 289 'enrol' => 'manual', 290 )); 291 292 $this->assertNotEquals($instance, false); 293 294 // Enrol users in the course. 295 $plugin->enrol_user($instance, $student1->id, $role->id); 296 $plugin->enrol_user($instance, $student2->id, $role->id); 297 $plugin->enrol_user($instance, $student3->id, $role->id); 298 $plugin->enrol_user($instance, $student4->id, $role->id); 299 $plugin->enrol_user($instance, $student5->id, $role->id); 300 $plugin->enrol_user($instance, $student6->id, $role->id); 301 302 // Generate SQL with the different groups join types for members of group1 and group2. 303 list($sqlany, $paramsany) = groups_get_members_ids_sql($groupids, null, GROUPS_JOIN_ANY); 304 list($sqlall, $paramsall) = groups_get_members_ids_sql($groupids, null, GROUPS_JOIN_ALL); 305 list($sqlnone, $paramsnone) = groups_get_members_ids_sql($groupids, null, GROUPS_JOIN_NONE); 306 307 // Any - Test empty groups, no matches. 308 $users = $DB->get_records_sql($sqlany, $paramsany); 309 $this->assertFalse(array_key_exists($student1->id, $users)); 310 $this->assertFalse(array_key_exists($student2->id, $users)); 311 $this->assertFalse(array_key_exists($student3->id, $users)); 312 $this->assertFalse(array_key_exists($student4->id, $users)); 313 $this->assertFalse(array_key_exists($student5->id, $users)); 314 $this->assertFalse(array_key_exists($student6->id, $users)); 315 316 // All - Test empty groups, no matches. 317 $users = $DB->get_records_sql($sqlall, $paramsall); 318 $this->assertFalse(array_key_exists($student1->id, $users)); 319 $this->assertFalse(array_key_exists($student2->id, $users)); 320 $this->assertFalse(array_key_exists($student3->id, $users)); 321 $this->assertFalse(array_key_exists($student4->id, $users)); 322 $this->assertFalse(array_key_exists($student5->id, $users)); 323 $this->assertFalse(array_key_exists($student6->id, $users)); 324 325 // None - Test empty groups, all match. 326 $users = $DB->get_records_sql($sqlnone, $paramsnone); 327 $this->assertTrue(array_key_exists($student1->id, $users)); 328 $this->assertTrue(array_key_exists($student2->id, $users)); 329 $this->assertTrue(array_key_exists($student3->id, $users)); 330 $this->assertTrue(array_key_exists($student4->id, $users)); 331 $this->assertTrue(array_key_exists($student5->id, $users)); 332 $this->assertTrue(array_key_exists($student6->id, $users)); 333 334 // Assign various group member combinations. 335 groups_add_member($group1->id, $student1->id); 336 groups_add_member($group1->id, $student2->id); 337 groups_add_member($group1->id, $student3->id); 338 groups_add_member($group2->id, $student2->id); 339 groups_add_member($group2->id, $student3->id); 340 groups_add_member($group2->id, $student4->id); 341 groups_add_member($group3->id, $student5->id); 342 343 // Any - Test students in one or both of groups 1 and 2 matched. 344 $users = $DB->get_records_sql($sqlany, $paramsany); 345 $this->assertTrue(array_key_exists($student1->id, $users)); 346 $this->assertTrue(array_key_exists($student2->id, $users)); 347 $this->assertTrue(array_key_exists($student3->id, $users)); 348 $this->assertTrue(array_key_exists($student4->id, $users)); 349 $this->assertFalse(array_key_exists($student5->id, $users)); 350 $this->assertFalse(array_key_exists($student6->id, $users)); 351 352 // All - Test only students in both groups 1 and 2 matched. 353 $users = $DB->get_records_sql($sqlall, $paramsall); 354 $this->assertTrue(array_key_exists($student2->id, $users)); 355 $this->assertTrue(array_key_exists($student3->id, $users)); 356 $this->assertFalse(array_key_exists($student1->id, $users)); 357 $this->assertFalse(array_key_exists($student4->id, $users)); 358 $this->assertFalse(array_key_exists($student5->id, $users)); 359 $this->assertFalse(array_key_exists($student6->id, $users)); 360 361 // None - Test only students not in group 1 or 2 matched. 362 $users = $DB->get_records_sql($sqlnone, $paramsnone); 363 $this->assertTrue(array_key_exists($student5->id, $users)); 364 $this->assertTrue(array_key_exists($student6->id, $users)); 365 $this->assertFalse(array_key_exists($student1->id, $users)); 366 $this->assertFalse(array_key_exists($student2->id, $users)); 367 $this->assertFalse(array_key_exists($student3->id, $users)); 368 $this->assertFalse(array_key_exists($student4->id, $users)); 369 } 370 371 public function test_groups_get_members_ids_sql_valid_context() { 372 global $DB; 373 374 $this->resetAfterTest(true); 375 376 $generator = $this->getDataGenerator(); 377 378 $course = $generator->create_course(); 379 $coursecontext = context_course::instance($course->id); 380 $student1 = $generator->create_user(); 381 $student2 = $generator->create_user(); 382 $plugin = enrol_get_plugin('manual'); 383 $role = $DB->get_record('role', array('shortname' => 'student')); 384 $group = $generator->create_group(array('courseid' => $course->id)); 385 $instance = $DB->get_record('enrol', array( 386 'courseid' => $course->id, 387 'enrol' => 'manual', 388 )); 389 390 $this->assertNotEquals($instance, false); 391 392 // Enrol users in the course. 393 $plugin->enrol_user($instance, $student1->id, $role->id); 394 $plugin->enrol_user($instance, $student2->id, $role->id); 395 396 // Add student1 to the group. 397 groups_add_member($group->id, $student1->id); 398 399 // Test with members at any group and with a valid $context. 400 list($sql, $params) = groups_get_members_ids_sql(USERSWITHOUTGROUP, $coursecontext); 401 $users = $DB->get_records_sql($sql, $params); 402 $this->assertFalse(array_key_exists($student1->id, $users)); 403 $this->assertTrue(array_key_exists($student2->id, $users)); 404 } 405 406 public function test_groups_get_members_ids_sql_empty_context() { 407 global $DB; 408 409 $this->resetAfterTest(true); 410 411 $generator = $this->getDataGenerator(); 412 413 $course = $generator->create_course(); 414 $coursecontext = context_course::instance($course->id); 415 $student1 = $generator->create_user(); 416 $student2 = $generator->create_user(); 417 $plugin = enrol_get_plugin('manual'); 418 $role = $DB->get_record('role', array('shortname' => 'student')); 419 $group = $generator->create_group(array('courseid' => $course->id)); 420 $instance = $DB->get_record('enrol', array( 421 'courseid' => $course->id, 422 'enrol' => 'manual', 423 )); 424 425 $this->assertNotEquals($instance, false); 426 427 // Enrol users in the course. 428 $plugin->enrol_user($instance, $student1->id, $role->id); 429 $plugin->enrol_user($instance, $student2->id, $role->id); 430 431 // Add student1 to the group. 432 groups_add_member($group->id, $student1->id); 433 434 // Test with members at any group and without the $context. 435 $this->expectException('coding_exception'); 436 list($sql, $params) = groups_get_members_ids_sql(USERSWITHOUTGROUP); 437 } 438 439 public function test_groups_get_members_ids_sql_invalid_context() { 440 global $DB; 441 442 $this->resetAfterTest(true); 443 444 $generator = $this->getDataGenerator(); 445 446 $course = $generator->create_course(); 447 $coursecontext = context_course::instance($course->id); 448 $student1 = $generator->create_user(); 449 $student2 = $generator->create_user(); 450 $plugin = enrol_get_plugin('manual'); 451 $role = $DB->get_record('role', array('shortname' => 'student')); 452 $group = $generator->create_group(array('courseid' => $course->id)); 453 $instance = $DB->get_record('enrol', array( 454 'courseid' => $course->id, 455 'enrol' => 'manual', 456 )); 457 458 $this->assertNotEquals($instance, false); 459 460 // Enrol users in the course. 461 $plugin->enrol_user($instance, $student1->id, $role->id); 462 $plugin->enrol_user($instance, $student2->id, $role->id); 463 464 // Add student1 to the group. 465 groups_add_member($group->id, $student1->id); 466 467 // Test with members at any group and with an invalid $context. 468 $syscontext = context_system::instance(); 469 $this->expectException('coding_exception'); 470 list($sql, $params) = groups_get_members_ids_sql(USERSWITHOUTGROUP, $syscontext); 471 } 472 473 public function test_groups_get_group_by_name() { 474 $this->resetAfterTest(true); 475 476 $generator = $this->getDataGenerator(); 477 478 // Create a course category and course. 479 $cat = $generator->create_category(array('parent' => 0)); 480 $course = $generator->create_course(array('category' => $cat->id)); 481 482 $name1 = 'Name 1'; 483 $name2 = 'Name 2'; 484 485 // Test with an empty and a null idnumber. 486 $this->assertFalse(groups_get_group_by_name($course->id, '')); 487 $this->assertFalse(groups_get_group_by_name($course->id, null)); 488 489 // Even when a group exists. 490 $generator->create_group(array('courseid' => $course->id)); 491 $this->assertFalse(groups_get_group_by_name($course->id, '')); 492 $this->assertFalse(groups_get_group_by_name($course->id, null)); 493 494 // Test with a valid name, but one that doesn't exist yet. 495 $this->assertFalse(groups_get_group_by_name($course->id, $name1)); 496 $this->assertFalse(groups_get_group_by_name($course->id, $name2)); 497 498 // We should now have a valid group returned by the name search. 499 $group1 = $generator->create_group(array('courseid' => $course->id, 'name' => $name1)); 500 $this->assertEquals($group1->id, groups_get_group_by_name($course->id, $name1)); 501 $this->assertFalse(groups_get_group_by_name($course->id, $name2)); 502 503 // We should now have a two valid groups returned by the name search. 504 $group2 = $generator->create_group(array('courseid' => $course->id, 'name' => $name2)); 505 $this->assertEquals($group1->id, groups_get_group_by_name($course->id, $name1)); 506 $this->assertEquals($group2->id, groups_get_group_by_name($course->id, $name2)); 507 508 // Delete a group. 509 $this->assertTrue(groups_delete_group($group1)); 510 $this->assertFalse(groups_get_group_by_name($course->id, $name1)); 511 $this->assertEquals($group2->id, groups_get_group_by_name($course->id, $name2)); 512 513 /* 514 * Group idnumbers are unique within a course so test that we don't 515 * retrieve groups for the first course. 516 */ 517 518 // Create a second course. 519 $course = $generator->create_course(array('category' => $cat->id)); 520 521 // An empty name should always return a false value. 522 $this->assertFalse(groups_get_group_by_name($course->id, '')); 523 $this->assertFalse(groups_get_group_by_name($course->id, null)); 524 525 // Our existing names shouldn't be returned here as we're in a different course. 526 $this->assertFalse(groups_get_group_by_name($course->id, $name1)); 527 $this->assertFalse(groups_get_group_by_name($course->id, $name2)); 528 529 // We should be able to reuse the idnumbers again since this is a different course. 530 $group1 = $generator->create_group(array('courseid' => $course->id, 'name' => $name1)); 531 $this->assertEquals($group1->id, groups_get_group_by_name($course->id, $name1)); 532 533 $group2 = $generator->create_group(array('courseid' => $course->id, 'name' => $name2)); 534 $this->assertEquals($group2->id, groups_get_group_by_name($course->id, $name2)); 535 } 536 537 public function test_groups_get_grouping() { 538 $this->resetAfterTest(true); 539 540 $generator = $this->getDataGenerator(); 541 542 // Create a course category and course. 543 $cat = $generator->create_category(array('parent' => 0)); 544 $course = $generator->create_course(array('category' => $cat->id)); 545 546 $name1 = 'Grouping 1'; 547 $name2 = 'Grouping 2'; 548 549 // Test with an empty and a null idnumber. 550 $this->assertFalse(groups_get_grouping_by_name($course->id, '')); 551 $this->assertFalse(groups_get_grouping_by_name($course->id, null)); 552 553 // Even when a group exists. 554 $generator->create_group(array('courseid' => $course->id)); 555 $this->assertFalse(groups_get_grouping_by_name($course->id, '')); 556 $this->assertFalse(groups_get_grouping_by_name($course->id, null)); 557 558 // Test with a valid name, but one that doesn't exist yet. 559 $this->assertFalse(groups_get_grouping_by_name($course->id, $name1)); 560 $this->assertFalse(groups_get_grouping_by_name($course->id, $name2)); 561 562 // We should now have a valid group returned by the name search. 563 $group1 = $generator->create_grouping(array('courseid' => $course->id, 'name' => $name1)); 564 $this->assertEquals($group1->id, groups_get_grouping_by_name($course->id, $name1)); 565 $this->assertFalse(groups_get_grouping_by_name($course->id, $name2)); 566 567 // We should now have a two valid groups returned by the name search. 568 $group2 = $generator->create_grouping(array('courseid' => $course->id, 'name' => $name2)); 569 $this->assertEquals($group1->id, groups_get_grouping_by_name($course->id, $name1)); 570 $this->assertEquals($group2->id, groups_get_grouping_by_name($course->id, $name2)); 571 572 // Delete a group. 573 $this->assertTrue(groups_delete_grouping($group1)); 574 $this->assertFalse(groups_get_grouping_by_name($course->id, $name1)); 575 $this->assertEquals($group2->id, groups_get_grouping_by_name($course->id, $name2)); 576 577 /* 578 * Group idnumbers are unique within a course so test that we don't 579 * retrieve groups for the first course. 580 */ 581 582 // Create a second course. 583 $course = $generator->create_course(array('category' => $cat->id)); 584 585 // An empty name should always return a false value. 586 $this->assertFalse(groups_get_grouping_by_name($course->id, '')); 587 $this->assertFalse(groups_get_grouping_by_name($course->id, null)); 588 589 // Our existing names shouldn't be returned here as we're in a different course. 590 $this->assertFalse(groups_get_grouping_by_name($course->id, $name1)); 591 $this->assertFalse(groups_get_grouping_by_name($course->id, $name2)); 592 593 // We should be able to reuse the idnumbers again since this is a different course. 594 $group1 = $generator->create_grouping(array('courseid' => $course->id, 'name' => $name1)); 595 $this->assertEquals($group1->id, groups_get_grouping_by_name($course->id, $name1)); 596 597 $group2 = $generator->create_grouping(array('courseid' => $course->id, 'name' => $name2)); 598 $this->assertEquals($group2->id, groups_get_grouping_by_name($course->id, $name2)); 599 } 600 601 public function test_groups_get_course_data() { 602 $this->resetAfterTest(true); 603 604 $generator = $this->getDataGenerator(); 605 606 // Create a course category and course. 607 $cat = $generator->create_category(array('parent' => 0)); 608 $course = $generator->create_course(array('category' => $cat->id)); 609 $grouping1 = $generator->create_grouping(array('courseid' => $course->id, 'name' => 'Grouping 1')); 610 $grouping2 = $generator->create_grouping(array('courseid' => $course->id, 'name' => 'Grouping 2')); 611 $group1 = $generator->create_group(array('courseid' => $course->id, 'name' => 'Group 1')); 612 $group2 = $generator->create_group(array('courseid' => $course->id, 'name' => 'Group 2')); 613 $group3 = $generator->create_group(array('courseid' => $course->id, 'name' => 'Group 3')); 614 $group4 = $generator->create_group(array('courseid' => $course->id, 'name' => 'Group 4')); 615 616 // Assign the groups to groupings. 617 $this->assertTrue(groups_assign_grouping($grouping1->id, $group1->id)); 618 $this->assertTrue(groups_assign_grouping($grouping1->id, $group2->id)); 619 $this->assertTrue(groups_assign_grouping($grouping2->id, $group3->id)); 620 $this->assertTrue(groups_assign_grouping($grouping2->id, $group4->id)); 621 622 // Get the data. 623 $data = groups_get_course_data($course->id); 624 $this->assertInstanceOf('stdClass', $data); 625 $this->assertObjectHasAttribute('groups', $data); 626 $this->assertObjectHasAttribute('groupings', $data); 627 $this->assertObjectHasAttribute('mappings', $data); 628 629 // Test we have the expected items returns. 630 $this->assertCount(4, $data->groups); 631 $this->assertCount(2, $data->groupings); 632 $this->assertCount(4, $data->mappings); 633 634 // Check we have the expected groups. 635 $this->assertArrayHasKey($group1->id, $data->groups); 636 $this->assertArrayHasKey($group2->id, $data->groups); 637 $this->assertArrayHasKey($group3->id, $data->groups); 638 $this->assertArrayHasKey($group4->id, $data->groups); 639 640 // Test a group-id is mapped correctly. 641 $this->assertSame($group3->name, $data->groups[$group3->id]->name); 642 643 // Check we have the expected number of groupings. 644 $this->assertContains($grouping1->id, array_keys($data->groupings)); 645 $this->assertContains($grouping2->id, array_keys($data->groupings)); 646 647 // Test a grouping-id is mapped correctly. 648 $this->assertEquals($grouping2->name, $data->groupings[$grouping2->id]->name); 649 650 // Test that all of the mappings are correct. 651 $grouping1maps = 0; 652 $grouping2maps = 0; 653 $group1maps = 0; 654 $group2maps = 0; 655 $group3maps = 0; 656 $group4maps = 0; 657 foreach ($data->mappings as $mapping) { 658 if ($mapping->groupingid === $grouping1->id) { 659 $grouping1maps++; 660 $this->assertContains($mapping->groupid, array($group1->id, $group2->id)); 661 } else if ($mapping->groupingid === $grouping2->id) { 662 $grouping2maps++; 663 $this->assertContains($mapping->groupid, array($group3->id, $group4->id)); 664 } else { 665 $this->fail('Unexpected groupingid'); 666 } 667 switch ($mapping->groupid) { 668 case $group1->id : $group1maps++; break; 669 case $group2->id : $group2maps++; break; 670 case $group3->id : $group3maps++; break; 671 case $group4->id : $group4maps++; break; 672 } 673 } 674 $this->assertEquals(2, $grouping1maps); 675 $this->assertEquals(2, $grouping2maps); 676 $this->assertEquals(1, $group1maps); 677 $this->assertEquals(1, $group2maps); 678 $this->assertEquals(1, $group3maps); 679 $this->assertEquals(1, $group4maps); 680 681 // Test the groups_get_all_groups which uses this functionality. 682 $groups = groups_get_all_groups($course->id); 683 $groupkeys = array_keys($groups); 684 $this->assertCount(4, $groups); 685 $this->assertContains($group1->id, $groupkeys); 686 $this->assertContains($group2->id, $groupkeys); 687 $this->assertContains($group3->id, $groupkeys); 688 $this->assertContains($group4->id, $groupkeys); 689 690 $groups = groups_get_all_groups($course->id, null, $grouping1->id); 691 $groupkeys = array_keys($groups); 692 $this->assertCount(2, $groups); 693 $this->assertContains($group1->id, $groupkeys); 694 $this->assertContains($group2->id, $groupkeys); 695 $this->assertNotContains($group3->id, $groupkeys); 696 $this->assertNotContains($group4->id, $groupkeys); 697 698 $groups = groups_get_all_groups($course->id, null, $grouping2->id); 699 $groupkeys = array_keys($groups); 700 $this->assertCount(2, $groups); 701 $this->assertNotContains($group1->id, $groupkeys); 702 $this->assertNotContains($group2->id, $groupkeys); 703 $this->assertContains($group3->id, $groupkeys); 704 $this->assertContains($group4->id, $groupkeys); 705 706 // Test this function using an alternate column for the result index 707 $groups = groups_get_all_groups($course->id, null, $grouping2->id, 'g.name, g.id'); 708 $groupkeys = array_keys($groups); 709 $this->assertCount(2, $groups); 710 $this->assertNotContains($group3->id, $groupkeys); 711 $this->assertContains($group3->name, $groupkeys); 712 $this->assertEquals($group3->id, $groups[$group3->name]->id); 713 } 714 715 /** 716 * Tests for groups_group_visible. 717 */ 718 public function test_groups_group_visible() { 719 global $CFG, $DB; 720 721 $generator = $this->getDataGenerator(); 722 $this->resetAfterTest(); 723 $this->setAdminUser(); 724 725 // Create a course category, course and groups. 726 $cat = $generator->create_category(array('parent' => 0)); 727 $course = $generator->create_course(array('category' => $cat->id)); 728 $coursecontext = context_course::instance($course->id); 729 $group1 = $generator->create_group(array('courseid' => $course->id, 'name' => 'Group 1')); 730 $group2 = $generator->create_group(array('courseid' => $course->id, 'name' => 'Group 2')); 731 $group3 = $generator->create_group(array('courseid' => $course->id, 'name' => 'Group 3')); 732 $group4 = $generator->create_group(array('courseid' => $course->id, 'name' => 'Group 4')); 733 734 // Create cm. 735 $assign = $generator->create_module("assign", array('course' => $course->id)); 736 $cm = get_coursemodule_from_instance("assign", $assign->id); 737 738 // Create users. 739 $user1 = $generator->create_user(); 740 $user2 = $generator->create_user(); 741 $user3 = $generator->create_user(); 742 743 // Enrol users into the course. 744 $generator->enrol_user($user1->id, $course->id); 745 $generator->enrol_user($user2->id, $course->id); 746 747 // Assign groups. 748 groups_add_member($group1, $user2); 749 750 // Give capability at course level to the user to access all groups. 751 $role = $DB->get_field("role", "id", array("shortname" => "manager")); 752 $generator->enrol_user($user3->id, $course->id, $role); 753 // Make sure the user has the capability. 754 assign_capability('moodle/site:accessallgroups', CAP_ALLOW, $role, $coursecontext->id); 755 756 // No groups , not forced. 757 $result = groups_group_visible($group1->id, $course, null, $user1->id); 758 $this->assertTrue($result); 759 $result = groups_group_visible(0, $course, null, $user1->id); 760 $this->assertTrue($result); // Requesting all groups. 761 762 $result = groups_group_visible($group1->id, $course, $cm, $user1->id); 763 $this->assertTrue($result); // Cm with no groups. 764 765 $cm->groupmode = SEPARATEGROUPS; 766 $result = groups_group_visible($group1->id, $course, $cm, $user1->id); 767 $this->assertFalse($result); // Cm with separate groups. 768 $result = groups_group_visible($group1->id, $course, $cm, $user2->id); 769 $this->assertTrue($result); // Cm with separate groups. 770 771 $cm->groupmode = VISIBLEGROUPS; 772 $result = groups_group_visible($group1->id, $course, $cm, $user1->id); 773 $this->assertTrue($result); // Cm with visible groups. 774 775 // No groups, forced. 776 $course->groupmode = NOGROUPS; 777 $course->groupmodeforce = true; 778 update_course($course); 779 $result = groups_group_visible($group1->id, $course, null, $user1->id); 780 $this->assertTrue($result); 781 $result = groups_group_visible(0, $course, null, $user1->id); 782 $this->assertTrue($result); // Requesting all groups. 783 784 $result = groups_group_visible($group1->id, $course, $cm, $user1->id); 785 $this->assertTrue($result); // Cm with no groups. 786 787 $cm->groupmode = SEPARATEGROUPS; 788 $result = groups_group_visible($group1->id, $course, $cm, $user1->id); 789 $this->assertTrue($result); // Cm with separate groups. 790 $result = groups_group_visible($group1->id, $course, $cm, $user2->id); 791 $this->assertTrue($result); // Cm with separate groups. 792 793 $cm->groupmode = SEPARATEGROUPS; 794 $result = groups_group_visible($group1->id, $course, $cm, $user1->id); 795 $this->assertTrue($result); // Cm with visible groups. 796 797 // Visible groups, forced. 798 $course->groupmode = VISIBLEGROUPS; 799 $course->groupmodeforce = true; 800 update_course($course); 801 $result = groups_group_visible($group1->id, $course, null, $user1->id); 802 $this->assertTrue($result); 803 $result = groups_group_visible(0, $course, null, $user1->id); 804 $this->assertTrue($result); // Requesting all groups. 805 806 $cm->groupmode = NOGROUPS; 807 $result = groups_group_visible($group1->id, $course, $cm, $user1->id); 808 $this->assertTrue($result); // Cm with no groups. 809 810 $cm->groupmode = SEPARATEGROUPS; 811 $result = groups_group_visible($group1->id, $course, $cm, $user1->id); 812 $this->assertTrue($result); // Cm with separate groups. 813 $result = groups_group_visible($group1->id, $course, $cm, $user2->id); 814 $this->assertTrue($result); // Cm with separate groups. 815 816 $cm->groupmode = VISIBLEGROUPS; 817 $result = groups_group_visible($group1->id, $course, $cm, $user1->id); 818 $this->assertTrue($result); // Cm with visible groups. 819 820 // Visible groups, not forced. 821 $course->groupmode = VISIBLEGROUPS; 822 $course->groupmodeforce = false; 823 update_course($course); 824 $result = groups_group_visible($group1->id, $course, null, $user1->id); 825 $this->assertTrue($result); 826 $result = groups_group_visible(0, $course, null, $user1->id); 827 $this->assertTrue($result); // Requesting all groups. 828 829 $cm->groupmode = NOGROUPS; 830 $result = groups_group_visible($group1->id, $course, $cm, $user1->id); 831 $this->assertTrue($result); // Cm with no groups. 832 833 $cm->groupmode = SEPARATEGROUPS; 834 $result = groups_group_visible($group1->id, $course, $cm, $user1->id); 835 $this->assertFalse($result); // Cm with separate groups. 836 $result = groups_group_visible($group1->id, $course, $cm, $user2->id); 837 $this->assertTrue($result); // Cm with separate groups. 838 839 $cm->groupmode = VISIBLEGROUPS; 840 $result = groups_group_visible($group1->id, $course, $cm, $user1->id); 841 $this->assertTrue($result); // Cm with visible groups. 842 843 // Separate groups, forced. 844 $course->groupmode = SEPARATEGROUPS; 845 $course->groupmodeforce = true; 846 update_course($course); 847 $result = groups_group_visible($group1->id, $course, null, $user1->id); 848 $this->assertFalse($result); 849 $result = groups_group_visible($group1->id, $course, null, $user2->id); 850 $this->assertTrue($result); 851 $result = groups_group_visible(0, $course, null, $user2->id); 852 $this->assertFalse($result); // Requesting all groups. 853 $result = groups_group_visible(0, $course, null, $user3->id); 854 $this->assertTrue($result); // Requesting all groups. 855 $result = groups_group_visible($group1->id, $course, null, $user3->id); 856 $this->assertTrue($result); // Make sure user with access to all groups can see any group. 857 858 $cm->groupmode = NOGROUPS; 859 $result = groups_group_visible($group1->id, $course, $cm, $user1->id); 860 $this->assertFalse($result); // Cm with no groups. 861 862 $cm->groupmode = SEPARATEGROUPS; 863 $result = groups_group_visible($group1->id, $course, $cm, $user1->id); 864 $this->assertFalse($result); // Cm with separate groups. 865 $result = groups_group_visible($group1->id, $course, $cm, $user2->id); 866 $this->assertTrue($result); // Cm with separate groups. 867 $result = groups_group_visible($group1->id, $course, $cm, $user3->id); 868 $this->assertTrue($result); // Make sure user with access to all groups can see any group. 869 870 $cm->groupmode = VISIBLEGROUPS; 871 $result = groups_group_visible($group1->id, $course, $cm, $user1->id); 872 $this->assertFalse($result); // Cm with visible groups. 873 874 // Separate groups, not forced. 875 $course->groupmode = SEPARATEGROUPS; 876 $course->groupmodeforce = false; 877 update_course($course); 878 $result = groups_group_visible($group1->id, $course, null, $user1->id); 879 $this->assertFalse($result); 880 $result = groups_group_visible($group1->id, $course, null, $user2->id); 881 $this->assertTrue($result); 882 $result = groups_group_visible(0, $course, null, $user2->id); 883 $this->assertFalse($result); // Requesting all groups. 884 $result = groups_group_visible(0, $course, null, $user3->id); 885 $this->assertTrue($result); // Requesting all groups. 886 887 $cm->groupmode = NOGROUPS; 888 $result = groups_group_visible($group1->id, $course, $cm, $user1->id); 889 $this->assertTrue($result); // Cm with no groups. 890 891 $cm->groupmode = SEPARATEGROUPS; 892 $result = groups_group_visible($group1->id, $course, $cm, $user1->id); 893 $this->assertFalse($result); // Cm with separate groups. 894 $result = groups_group_visible($group1->id, $course, $cm, $user2->id); 895 $this->assertTrue($result); // Cm with separate groups. 896 897 $cm->groupmode = VISIBLEGROUPS; 898 $result = groups_group_visible($group1->id, $course, $cm, $user1->id); 899 $this->assertTrue($result); // Cm with visible groups. 900 } 901 902 function test_groups_get_groupmode() { 903 global $DB; 904 $generator = $this->getDataGenerator(); 905 $this->resetAfterTest(); 906 $this->setAdminUser(); 907 908 // Create a course with no groups forcing. 909 $course1 = $generator->create_course(); 910 911 // Create cm1 with no groups, cm1 with visible groups, cm2 with separate groups and cm3 with visible groups. 912 $assign1 = $generator->create_module("assign", array('course' => $course1->id)); 913 $assign2 = $generator->create_module("assign", array('course' => $course1->id), 914 array('groupmode' => SEPARATEGROUPS)); 915 $assign3 = $generator->create_module("assign", array('course' => $course1->id), 916 array('groupmode' => VISIBLEGROUPS)); 917 918 // Request data for tests. 919 $cm1 = get_coursemodule_from_instance("assign", $assign1->id); 920 $cm2 = get_coursemodule_from_instance("assign", $assign2->id); 921 $cm3 = get_coursemodule_from_instance("assign", $assign3->id); 922 $modinfo = get_fast_modinfo($course1->id); 923 924 // Assert that any method of getting activity groupmode returns the correct result. 925 $this->assertEquals(NOGROUPS, groups_get_activity_groupmode($cm1)); 926 $this->assertEquals(NOGROUPS, groups_get_activity_groupmode($cm1, $course1)); 927 $this->assertEquals(NOGROUPS, groups_get_activity_groupmode($modinfo->cms[$cm1->id])); 928 $this->assertEquals(SEPARATEGROUPS, groups_get_activity_groupmode($cm2)); 929 $this->assertEquals(SEPARATEGROUPS, groups_get_activity_groupmode($cm2, $course1)); 930 $this->assertEquals(SEPARATEGROUPS, groups_get_activity_groupmode($modinfo->cms[$cm2->id])); 931 $this->assertEquals(VISIBLEGROUPS, groups_get_activity_groupmode($cm3)); 932 $this->assertEquals(VISIBLEGROUPS, groups_get_activity_groupmode($cm3, $course1)); 933 $this->assertEquals(VISIBLEGROUPS, groups_get_activity_groupmode($modinfo->cms[$cm3->id])); 934 935 // Update the course set the groupmode SEPARATEGROUPS but not forced. 936 update_course((object)array('id' => $course1->id, 'groupmode' => SEPARATEGROUPS)); 937 // Re-request the data from DB. 938 $course1 = $DB->get_record('course', array('id' => $course1->id)); 939 $modinfo = get_fast_modinfo($course1->id); 940 941 // Existing activities are not changed. 942 $this->assertEquals(NOGROUPS, groups_get_activity_groupmode($cm1)); 943 $this->assertEquals(NOGROUPS, groups_get_activity_groupmode($cm1, $course1)); 944 $this->assertEquals(NOGROUPS, groups_get_activity_groupmode($modinfo->cms[$cm1->id])); 945 $this->assertEquals(SEPARATEGROUPS, groups_get_activity_groupmode($cm2)); 946 $this->assertEquals(SEPARATEGROUPS, groups_get_activity_groupmode($cm2, $course1)); 947 $this->assertEquals(SEPARATEGROUPS, groups_get_activity_groupmode($modinfo->cms[$cm2->id])); 948 $this->assertEquals(VISIBLEGROUPS, groups_get_activity_groupmode($cm3)); 949 $this->assertEquals(VISIBLEGROUPS, groups_get_activity_groupmode($cm3, $course1)); 950 $this->assertEquals(VISIBLEGROUPS, groups_get_activity_groupmode($modinfo->cms[$cm3->id])); 951 952 // Update the course set the groupmode SEPARATEGROUPS and forced. 953 update_course((object)array('id' => $course1->id, 'groupmode' => SEPARATEGROUPS, 'groupmodeforce' => true)); 954 // Re-request the data from DB. 955 $course1 = $DB->get_record('course', array('id' => $course1->id)); 956 $modinfo = get_fast_modinfo($course1->id); 957 958 // Make sure all activities have separate groups mode now. 959 $this->assertEquals(SEPARATEGROUPS, groups_get_activity_groupmode($cm1)); 960 $this->assertEquals(SEPARATEGROUPS, groups_get_activity_groupmode($cm1, $course1)); 961 $this->assertEquals(SEPARATEGROUPS, groups_get_activity_groupmode($modinfo->cms[$cm1->id])); 962 $this->assertEquals(SEPARATEGROUPS, groups_get_activity_groupmode($cm2)); 963 $this->assertEquals(SEPARATEGROUPS, groups_get_activity_groupmode($cm2, $course1)); 964 $this->assertEquals(SEPARATEGROUPS, groups_get_activity_groupmode($modinfo->cms[$cm2->id])); 965 $this->assertEquals(SEPARATEGROUPS, groups_get_activity_groupmode($cm3)); 966 $this->assertEquals(SEPARATEGROUPS, groups_get_activity_groupmode($cm3, $course1)); 967 $this->assertEquals(SEPARATEGROUPS, groups_get_activity_groupmode($modinfo->cms[$cm3->id])); 968 } 969 970 /** 971 * Tests for groups_allgroups_course_menu() . 972 */ 973 public function test_groups_allgroups_course_menu() { 974 global $SESSION; 975 976 $this->resetAfterTest(); 977 978 // Generate data. 979 $course = $this->getDataGenerator()->create_course(); 980 $record = new stdClass(); 981 $record->courseid = $course->id; 982 $group1 = $this->getDataGenerator()->create_group($record); 983 $group2 = $this->getDataGenerator()->create_group($record); 984 $user = $this->getDataGenerator()->create_user(); 985 $this->getDataGenerator()->enrol_user($user->id, $course->id); 986 $this->setUser($user); 987 988 $html = groups_allgroups_course_menu($course, 'someurl.php'); 989 // Since user is not a part of this group and doesn't have accessallgroups permission, 990 // the html should be empty. 991 $this->assertEmpty($html); 992 993 groups_add_member($group1->id, $user); 994 // Now user can access one of the group. We can't assert an exact match here because of random ids generated by yui. So do 995 // partial match to see if all groups are listed or not. 996 $html = groups_allgroups_course_menu($course, 'someurl.php'); 997 $this->assertContains(format_string($group1->name), $html); 998 $this->assertNotContains(format_string($group2->name), $html); 999 1000 $this->setAdminUser(); 1001 1002 // Now user can access everything. 1003 $html = groups_allgroups_course_menu($course, 'someurl.php'); 1004 $this->assertContains(format_string($group1->name), $html); 1005 $this->assertContains(format_string($group2->name), $html); 1006 1007 // Make sure separate groups mode, doesn't change anything. 1008 $course->groupmode = SEPARATEGROUPS; 1009 update_course($course); 1010 $html = groups_allgroups_course_menu($course, 'someurl.php'); 1011 $this->assertContains(format_string($group1->name), $html); 1012 $this->assertContains(format_string($group2->name), $html); 1013 1014 // Make sure Visible groups mode, doesn't change anything. 1015 $course->groupmode = VISIBLEGROUPS; 1016 update_course($course); 1017 $html = groups_allgroups_course_menu($course, 'someurl.php'); 1018 $this->assertContains(format_string($group1->name), $html); 1019 $this->assertContains(format_string($group2->name), $html); 1020 1021 // Let us test activegroup changes now. 1022 $this->setUser($user); 1023 $SESSION->activegroup[$course->id][VISIBLEGROUPS][$course->defaultgroupingid] = 5; 1024 groups_allgroups_course_menu($course, 'someurl.php', false); // Do not update session. 1025 $this->assertSame(5, $SESSION->activegroup[$course->id][VISIBLEGROUPS][$course->defaultgroupingid]); 1026 groups_allgroups_course_menu($course, 'someurl.php', true, $group1->id); // Update session. 1027 $this->assertSame($group1->id, $SESSION->activegroup[$course->id][VISIBLEGROUPS][$course->defaultgroupingid]); 1028 // Try to update session with an invalid groupid. It should not accept the invalid id. 1029 groups_allgroups_course_menu($course, 'someurl.php', true, 256); 1030 $this->assertEquals($group1->id, $SESSION->activegroup[$course->id][VISIBLEGROUPS][$course->defaultgroupingid]); 1031 } 1032 1033 /** 1034 * This unit test checks that groups_get_all_groups returns groups in 1035 * alphabetical order even if they are in a grouping. 1036 */ 1037 public function test_groups_ordering() { 1038 $generator = $this->getDataGenerator(); 1039 $this->resetAfterTest(); 1040 1041 // Create a course category and course. 1042 $cat = $generator->create_category(array('parent' => 0)); 1043 $course = $generator->create_course(array('category' => $cat->id)); 1044 $grouping = $generator->create_grouping(array('courseid' => $course->id, 'name' => 'Grouping')); 1045 1046 // Create groups in reverse order. 1047 $group2 = $generator->create_group(array('courseid' => $course->id, 'name' => 'Group 2')); 1048 $group1 = $generator->create_group(array('courseid' => $course->id, 'name' => 'Group 1')); 1049 1050 // Assign the groups to the grouping in reverse order. 1051 $this->assertTrue(groups_assign_grouping($grouping->id, $group2->id)); 1052 $this->assertTrue(groups_assign_grouping($grouping->id, $group1->id)); 1053 1054 // Get all groups and check they are alphabetical. 1055 $groups = array_values(groups_get_all_groups($course->id, 0)); 1056 $this->assertEquals('Group 1', $groups[0]->name); 1057 $this->assertEquals('Group 2', $groups[1]->name); 1058 1059 // Now check the same is true when accessed by grouping. 1060 $groups = array_values(groups_get_all_groups($course->id, 0, $grouping->id)); 1061 $this->assertEquals('Group 1', $groups[0]->name); 1062 $this->assertEquals('Group 2', $groups[1]->name); 1063 } 1064 1065 /** 1066 * Tests for groups_get_all_groups when grouping is set and we want members as well. 1067 */ 1068 public function test_groups_get_all_groups_in_grouping_with_members() { 1069 $generator = $this->getDataGenerator(); 1070 $this->resetAfterTest(); 1071 1072 // Create courses. 1073 $course1 = $generator->create_course(); 1074 $course2 = $generator->create_course(); 1075 1076 // Create users. 1077 $c1user1 = $generator->create_user(); 1078 $c12user1 = $generator->create_user(); 1079 $c12user2 = $generator->create_user(); 1080 1081 // Enrol users. 1082 $generator->enrol_user($c1user1->id, $course1->id); 1083 $generator->enrol_user($c12user1->id, $course1->id); 1084 $generator->enrol_user($c12user1->id, $course2->id); 1085 $generator->enrol_user($c12user2->id, $course1->id); 1086 $generator->enrol_user($c12user2->id, $course2->id); 1087 1088 // Create groupings and groups for course1. 1089 $c1grouping1 = $generator->create_grouping(array('courseid' => $course1->id)); 1090 $c1grouping2 = $generator->create_grouping(array('courseid' => $course1->id)); 1091 $c1group1 = $generator->create_group(array('courseid' => $course1->id)); 1092 $c1group2 = $generator->create_group(array('courseid' => $course1->id)); 1093 $c1group3 = $generator->create_group(array('courseid' => $course1->id)); 1094 groups_assign_grouping($c1grouping1->id, $c1group1->id); 1095 groups_assign_grouping($c1grouping1->id, $c1group2->id); 1096 groups_assign_grouping($c1grouping2->id, $c1group3->id); 1097 1098 // Create groupings and groups for course2. 1099 $c2grouping1 = $generator->create_grouping(array('courseid' => $course2->id)); 1100 $c2group1 = $generator->create_group(array('courseid' => $course1->id)); 1101 groups_assign_grouping($c2grouping1->id, $c2group1->id); 1102 1103 // Assign users to groups. 1104 $generator->create_group_member(array('groupid' => $c1group1->id, 'userid' => $c1user1->id)); 1105 $generator->create_group_member(array('groupid' => $c1group1->id, 'userid' => $c12user1->id)); 1106 $generator->create_group_member(array('groupid' => $c1group2->id, 'userid' => $c12user2->id)); 1107 $generator->create_group_member(array('groupid' => $c2group1->id, 'userid' => $c12user2->id)); 1108 1109 // Test without userid. 1110 $groups = groups_get_all_groups($course1->id, null, $c1grouping1->id, 'g.*', true); 1111 1112 $this->assertEquals( 1113 [$c1group1->id, $c1group2->id], 1114 array_keys($groups), 1115 '', 0.0, 10, true 1116 ); 1117 $this->assertEquals( 1118 [$c1user1->id => $c1user1->id, $c12user1->id => $c12user1->id], 1119 $groups[$c1group1->id]->members 1120 ); 1121 $this->assertEquals( 1122 [$c12user2->id => $c12user2->id], 1123 $groups[$c1group2->id]->members 1124 ); 1125 1126 // Test with userid. 1127 $groups = groups_get_all_groups($course1->id, $c1user1->id, $c1grouping1->id, 'g.*', true); 1128 1129 $this->assertEquals([$c1group1->id], array_keys($groups)); 1130 $this->assertEquals( 1131 [$c1user1->id, $c12user1->id], 1132 $groups[$c1group1->id]->members, 1133 '', 0.0, 10, true 1134 ); 1135 } 1136 1137 /** 1138 * Tests for groups_get_user_groups() method. 1139 */ 1140 public function test_groups_get_user_groups() { 1141 $this->resetAfterTest(true); 1142 $generator = $this->getDataGenerator(); 1143 1144 // Create courses. 1145 $course1 = $generator->create_course(); 1146 $course2 = $generator->create_course(); 1147 1148 // Create users. 1149 $user1 = $generator->create_user(); 1150 $user2 = $generator->create_user(); 1151 $user3 = $generator->create_user(); 1152 1153 // Enrol users. 1154 $generator->enrol_user($user1->id, $course1->id); 1155 $generator->enrol_user($user1->id, $course2->id); 1156 $generator->enrol_user($user2->id, $course2->id); 1157 $generator->enrol_user($user3->id, $course2->id); 1158 1159 // Create groups. 1160 $group1 = $generator->create_group(array('courseid' => $course1->id)); 1161 $group2 = $generator->create_group(array('courseid' => $course2->id)); 1162 $group3 = $generator->create_group(array('courseid' => $course2->id)); 1163 1164 // Assign users to groups. 1165 $this->assertTrue($generator->create_group_member(array('groupid' => $group1->id, 'userid' => $user1->id))); 1166 $this->assertTrue($generator->create_group_member(array('groupid' => $group2->id, 'userid' => $user2->id))); 1167 1168 // Get user groups. 1169 $usergroups1 = groups_get_user_groups($course1->id, $user1->id); 1170 $usergroups2 = groups_get_user_groups($course2->id, $user2->id);; 1171 1172 // Assert return data. 1173 $this->assertEquals($group1->id, $usergroups1[0][0]); 1174 $this->assertEquals($group2->id, $usergroups2[0][0]); 1175 1176 // Now, test with groupings. 1177 $grouping1 = $generator->create_grouping(array('courseid' => $course1->id)); 1178 $grouping2 = $generator->create_grouping(array('courseid' => $course2->id)); 1179 1180 // Assign the groups to grouping. 1181 groups_assign_grouping($grouping1->id, $group1->id); 1182 groups_assign_grouping($grouping2->id, $group2->id); 1183 groups_assign_grouping($grouping2->id, $group3->id); 1184 1185 // Test with grouping. 1186 $usergroups1 = groups_get_user_groups($course1->id, $user1->id); 1187 $usergroups2 = groups_get_user_groups($course2->id, $user2->id); 1188 $this->assertArrayHasKey($grouping1->id, $usergroups1); 1189 $this->assertArrayHasKey($grouping2->id, $usergroups2); 1190 1191 // Test user without a group. 1192 $usergroups1 = groups_get_user_groups($course2->id, $user3->id); 1193 $this->assertCount(0, $usergroups1[0]); 1194 1195 // Test with userid = 0. 1196 $usergroups1 = groups_get_user_groups($course1->id, 0); 1197 $usergroups2 = groups_get_user_groups($course2->id, 0); 1198 $this->assertCount(0, $usergroups1[0]); 1199 $this->assertCount(0, $usergroups2[0]); 1200 1201 // Test with courseid = 0. 1202 $usergroups1 = groups_get_user_groups(0, $user1->id); 1203 $usergroups2 = groups_get_user_groups(0, $user2->id); 1204 $this->assertCount(0, $usergroups1[0]); 1205 $this->assertCount(0, $usergroups2[0]); 1206 } 1207 1208 /** 1209 * Create dummy groups array for use in menu tests 1210 * @param int $number 1211 * @return array 1212 */ 1213 protected function make_group_list($number) { 1214 $testgroups = array(); 1215 for ($a = 0; $a < $number; $a++) { 1216 $grp = new stdClass(); 1217 $grp->id = 100 + $a; 1218 $grp->name = 'test group ' . $grp->id; 1219 $testgroups[$grp->id] = $grp; 1220 } 1221 return $testgroups; 1222 } 1223 1224 public function test_groups_sort_menu_options_empty() { 1225 $this->assertEquals(array(), groups_sort_menu_options(array(), array())); 1226 } 1227 1228 public function test_groups_sort_menu_options_allowed_goups_only() { 1229 $this->assertEquals(array( 1230 100 => 'test group 100', 1231 101 => 'test group 101', 1232 ), groups_sort_menu_options($this->make_group_list(2), array())); 1233 } 1234 1235 public function test_groups_sort_menu_options_user_goups_only() { 1236 $this->assertEquals(array( 1237 100 => 'test group 100', 1238 101 => 'test group 101', 1239 ), groups_sort_menu_options(array(), $this->make_group_list(2))); 1240 } 1241 1242 public function test_groups_sort_menu_options_user_both() { 1243 $this->assertEquals(array( 1244 1 => array(get_string('mygroups', 'group') => array( 1245 100 => 'test group 100', 1246 101 => 'test group 101', 1247 )), 1248 2 => array(get_string('othergroups', 'group') => array( 1249 102 => 'test group 102', 1250 103 => 'test group 103', 1251 )), 1252 ), groups_sort_menu_options($this->make_group_list(4), $this->make_group_list(2))); 1253 } 1254 1255 public function test_groups_sort_menu_options_user_both_many_groups() { 1256 $this->assertEquals(array( 1257 1 => array(get_string('mygroups', 'group') => array( 1258 100 => 'test group 100', 1259 101 => 'test group 101', 1260 )), 1261 2 => array (get_string('othergroups', 'group') => array( 1262 102 => 'test group 102', 1263 103 => 'test group 103', 1264 104 => 'test group 104', 1265 105 => 'test group 105', 1266 106 => 'test group 106', 1267 107 => 'test group 107', 1268 108 => 'test group 108', 1269 109 => 'test group 109', 1270 110 => 'test group 110', 1271 111 => 'test group 111', 1272 112 => 'test group 112', 1273 )), 1274 ), groups_sort_menu_options($this->make_group_list(13), $this->make_group_list(2))); 1275 } 1276 1277 /** 1278 * Tests for groups_user_groups_visible. 1279 */ 1280 public function test_groups_user_groups_visible() { 1281 global $DB; 1282 1283 $generator = $this->getDataGenerator(); 1284 $this->resetAfterTest(); 1285 $this->setAdminUser(); 1286 1287 // Create a course category, course and groups. 1288 $cat = $generator->create_category(array('parent' => 0)); 1289 $course = $generator->create_course(array('category' => $cat->id)); 1290 $coursecontext = context_course::instance($course->id); 1291 $group1 = $generator->create_group(array('courseid' => $course->id, 'name' => 'Group 1')); 1292 $group2 = $generator->create_group(array('courseid' => $course->id, 'name' => 'Group 2')); 1293 $group3 = $generator->create_group(array('courseid' => $course->id, 'name' => 'Group 3')); 1294 $group4 = $generator->create_group(array('courseid' => $course->id, 'name' => 'Group 4')); 1295 1296 // Create cm. 1297 $assign = $generator->create_module("assign", array('course' => $course->id)); 1298 $cm = get_coursemodule_from_instance("assign", $assign->id); 1299 1300 // Create users. 1301 $user1 = $generator->create_user(); // Normal user. 1302 $user2 = $generator->create_user(); // Normal user. 1303 $user3 = $generator->create_user(); // Teacher, access all groups. 1304 $user4 = $generator->create_user(); // Normal user. 1305 1306 // Enrol users into the course. 1307 $generator->enrol_user($user1->id, $course->id); 1308 $generator->enrol_user($user2->id, $course->id); 1309 $generator->enrol_user($user4->id, $course->id); 1310 1311 // Assign groups. 1312 // User1 and User4 share groups. 1313 groups_add_member($group1, $user1); 1314 groups_add_member($group2, $user2); 1315 groups_add_member($group1, $user4); 1316 1317 // Give capability at course level to the user to access all groups. 1318 $role = $DB->get_field("role", "id", array("shortname" => "manager")); 1319 $generator->enrol_user($user3->id, $course->id, $role); 1320 // Make sure the user has the capability. 1321 assign_capability('moodle/site:accessallgroups', CAP_ALLOW, $role, $coursecontext->id); 1322 1323 // Normal users in different groups. 1324 $this->setUser($user1); 1325 1326 // No groups , not forced. 1327 $result = groups_user_groups_visible($course, $user2->id); 1328 $this->assertTrue($result); 1329 1330 $cm->groupmode = NOGROUPS; 1331 $result = groups_user_groups_visible($course, $user2->id, $cm); 1332 $this->assertTrue($result); // Cm with no groups. 1333 1334 $cm->groupmode = SEPARATEGROUPS; 1335 $result = groups_user_groups_visible($course, $user2->id, $cm); 1336 $this->assertFalse($result); // Cm with separate groups. 1337 1338 $cm->groupmode = VISIBLEGROUPS; 1339 $result = groups_user_groups_visible($course, $user2->id, $cm); 1340 $this->assertTrue($result); // Cm with visible groups. 1341 1342 // No groups, forced. 1343 $course->groupmode = NOGROUPS; 1344 $course->groupmodeforce = true; 1345 update_course($course); 1346 $result = groups_user_groups_visible($course, $user2->id); 1347 $this->assertTrue($result); 1348 1349 $cm->groupmode = NOGROUPS; 1350 $result = groups_user_groups_visible($course, $user2->id, $cm); 1351 $this->assertTrue($result); // Cm with no groups. 1352 1353 $cm->groupmode = SEPARATEGROUPS; 1354 $result = groups_user_groups_visible($course, $user2->id, $cm); 1355 $this->assertTrue($result); // Cm with separate groups. 1356 1357 $cm->groupmode = SEPARATEGROUPS; 1358 $result = groups_user_groups_visible($course, $user2->id); 1359 $this->assertTrue($result); // Cm with visible groups. 1360 1361 // Visible groups, forced. 1362 $course->groupmode = VISIBLEGROUPS; 1363 $course->groupmodeforce = true; 1364 update_course($course); 1365 $result = groups_user_groups_visible($course, $user2->id); 1366 $this->assertTrue($result); 1367 1368 $cm->groupmode = NOGROUPS; 1369 $result = groups_user_groups_visible($course, $user2->id, $cm); 1370 $this->assertTrue($result); // Cm with no groups. 1371 1372 $cm->groupmode = SEPARATEGROUPS; 1373 $result = groups_user_groups_visible($course, $user2->id, $cm); 1374 $this->assertTrue($result); // Cm with separate groups. 1375 1376 $cm->groupmode = VISIBLEGROUPS; 1377 $result = groups_user_groups_visible($course, $user2->id, $cm); 1378 $this->assertTrue($result); // Cm with visible groups. 1379 1380 // Visible groups, not forced. 1381 $course->groupmode = VISIBLEGROUPS; 1382 $course->groupmodeforce = false; 1383 update_course($course); 1384 $result = groups_user_groups_visible($course, $user2->id); 1385 $this->assertTrue($result); 1386 1387 $cm->groupmode = NOGROUPS; 1388 $result = groups_user_groups_visible($course, $user2->id, $cm); 1389 $this->assertTrue($result); // Cm with no groups. 1390 1391 $cm->groupmode = SEPARATEGROUPS; 1392 $result = groups_user_groups_visible($course, $user2->id, $cm); 1393 $this->assertFalse($result); // Cm with separate groups. 1394 1395 $cm->groupmode = VISIBLEGROUPS; 1396 $result = groups_user_groups_visible($course, $user2->id, $cm); 1397 $this->assertTrue($result); // Cm with visible groups. 1398 1399 // Separate groups, forced. 1400 $course->groupmode = SEPARATEGROUPS; 1401 $course->groupmodeforce = true; 1402 update_course($course); 1403 $result = groups_user_groups_visible($course, $user2->id); 1404 $this->assertFalse($result); 1405 1406 $result = groups_user_groups_visible($course, $user3->id); 1407 $this->assertFalse($result); // Requesting all groups. 1408 1409 $cm->groupmode = NOGROUPS; 1410 $result = groups_user_groups_visible($course, $user2->id, $cm); 1411 $this->assertFalse($result); // Cm with no groups. 1412 1413 $cm->groupmode = SEPARATEGROUPS; 1414 $result = groups_user_groups_visible($course, $user2->id, $cm); 1415 $this->assertFalse($result); // Cm with separate groups. 1416 1417 $result = groups_user_groups_visible($course, $user3->id, $cm); 1418 $this->assertTrue($result); 1419 1420 $cm->groupmode = VISIBLEGROUPS; 1421 $result = groups_user_groups_visible($course, $user2->id, $cm); 1422 $this->assertFalse($result); // Cm with visible groups. 1423 1424 // Separate groups, not forced. 1425 $course->groupmode = SEPARATEGROUPS; 1426 $course->groupmodeforce = false; 1427 update_course($course); 1428 $result = groups_user_groups_visible($course, $user2->id); 1429 $this->assertFalse($result); 1430 1431 $result = groups_user_groups_visible($course, $user3->id); 1432 $this->assertFalse($result); // Requesting all groups. 1433 1434 $cm->groupmode = NOGROUPS; 1435 $result = groups_user_groups_visible($course, $user2->id, $cm); 1436 $this->assertTrue($result); // Cm with no groups. 1437 1438 $cm->groupmode = SEPARATEGROUPS; 1439 $result = groups_user_groups_visible($course, $user2->id, $cm); 1440 $this->assertFalse($result); // Cm with separate groups. 1441 1442 $cm->groupmode = VISIBLEGROUPS; 1443 $result = groups_user_groups_visible($course, $user2->id, $cm); 1444 $this->assertTrue($result); // Cm with visible groups. 1445 1446 // Users sharing groups. 1447 1448 // No groups , not forced. 1449 $course->groupmode = NOGROUPS; 1450 $course->groupmodeforce = false; 1451 update_course($course); 1452 1453 $result = groups_user_groups_visible($course, $user4->id); 1454 $this->assertTrue($result); 1455 1456 $cm->groupmode = NOGROUPS; 1457 $result = groups_user_groups_visible($course, $user4->id, $cm); 1458 $this->assertTrue($result); // Cm with no groups. 1459 1460 $cm->groupmode = SEPARATEGROUPS; 1461 $result = groups_user_groups_visible($course, $user4->id, $cm); 1462 $this->assertTrue($result); // Cm with separate groups. 1463 1464 $cm->groupmode = VISIBLEGROUPS; 1465 $result = groups_user_groups_visible($course, $user4->id, $cm); 1466 $this->assertTrue($result); // Cm with visible groups. 1467 1468 // No groups, forced. 1469 $course->groupmode = NOGROUPS; 1470 $course->groupmodeforce = true; 1471 update_course($course); 1472 $result = groups_user_groups_visible($course, $user4->id); 1473 $this->assertTrue($result); 1474 1475 $cm->groupmode = NOGROUPS; 1476 $result = groups_user_groups_visible($course, $user4->id, $cm); 1477 $this->assertTrue($result); // Cm with no groups. 1478 1479 $cm->groupmode = SEPARATEGROUPS; 1480 $result = groups_user_groups_visible($course, $user4->id, $cm); 1481 $this->assertTrue($result); // Cm with separate groups. 1482 1483 $cm->groupmode = SEPARATEGROUPS; 1484 $result = groups_user_groups_visible($course, $user4->id, $cm); 1485 $this->assertTrue($result); // Cm with visible groups. 1486 1487 // Visible groups, forced. 1488 $course->groupmode = VISIBLEGROUPS; 1489 $course->groupmodeforce = true; 1490 update_course($course); 1491 $result = groups_user_groups_visible($course, $user4->id); 1492 $this->assertTrue($result); 1493 1494 $cm->groupmode = NOGROUPS; 1495 $result = groups_user_groups_visible($course, $user4->id, $cm); 1496 $this->assertTrue($result); // Cm with no groups. 1497 1498 $cm->groupmode = SEPARATEGROUPS; 1499 $result = groups_user_groups_visible($course, $user4->id, $cm); 1500 $this->assertTrue($result); // Cm with separate groups. 1501 1502 $cm->groupmode = VISIBLEGROUPS; 1503 $result = groups_user_groups_visible($course, $user4->id, $cm); 1504 $this->assertTrue($result); // Cm with visible groups. 1505 1506 // Visible groups, not forced. 1507 $course->groupmode = VISIBLEGROUPS; 1508 $course->groupmodeforce = false; 1509 update_course($course); 1510 $result = groups_user_groups_visible($course, $user4->id); 1511 $this->assertTrue($result); 1512 1513 $cm->groupmode = NOGROUPS; 1514 $result = groups_user_groups_visible($course, $user4->id, $cm); 1515 $this->assertTrue($result); // Cm with no groups. 1516 1517 $cm->groupmode = SEPARATEGROUPS; 1518 $result = groups_user_groups_visible($course, $user4->id, $cm); 1519 $this->assertTrue($result); // Cm with separate groups. 1520 1521 $cm->groupmode = VISIBLEGROUPS; 1522 $result = groups_user_groups_visible($course, $user4->id, $cm); 1523 $this->assertTrue($result); // Cm with visible groups. 1524 1525 // Separate groups, forced. 1526 $course->groupmode = SEPARATEGROUPS; 1527 $course->groupmodeforce = true; 1528 update_course($course); 1529 $result = groups_user_groups_visible($course, $user4->id); 1530 $this->assertTrue($result); 1531 1532 $result = groups_user_groups_visible($course, $user3->id); 1533 $this->assertFalse($result); // Requesting all groups. 1534 1535 $cm->groupmode = NOGROUPS; 1536 $result = groups_user_groups_visible($course, $user4->id, $cm); 1537 $this->assertTrue($result); // Cm with no groups. 1538 1539 $cm->groupmode = SEPARATEGROUPS; 1540 $result = groups_user_groups_visible($course, $user4->id, $cm); 1541 $this->assertTrue($result); // Cm with separate groups. 1542 1543 $result = groups_user_groups_visible($course, $user3->id, $cm); 1544 $this->assertTrue($result); 1545 1546 $cm->groupmode = VISIBLEGROUPS; 1547 $result = groups_user_groups_visible($course, $user4->id, $cm); 1548 $this->assertTrue($result); // Cm with visible groups. 1549 1550 // Separate groups, not forced. 1551 $course->groupmode = SEPARATEGROUPS; 1552 $course->groupmodeforce = false; 1553 update_course($course); 1554 $result = groups_user_groups_visible($course, $user4->id); 1555 $this->assertTrue($result); 1556 1557 $result = groups_user_groups_visible($course, $user3->id); 1558 $this->assertFalse($result); // Requesting all groups. 1559 1560 $cm->groupmode = NOGROUPS; 1561 $result = groups_user_groups_visible($course, $user4->id, $cm); 1562 $this->assertTrue($result); // Cm with no groups. 1563 1564 $cm->groupmode = SEPARATEGROUPS; 1565 $result = groups_user_groups_visible($course, $user4->id, $cm); 1566 $this->assertTrue($result); // Cm with separate groups. 1567 1568 $cm->groupmode = VISIBLEGROUPS; 1569 $result = groups_user_groups_visible($course, $user4->id, $cm); 1570 $this->assertTrue($result); // Cm with visible groups. 1571 1572 // For teacher with access all groups. 1573 1574 // No groups , not forced. 1575 $course->groupmode = NOGROUPS; 1576 $course->groupmodeforce = false; 1577 update_course($course); 1578 1579 $this->setUser($user3); 1580 1581 $result = groups_user_groups_visible($course, $user1->id); 1582 $this->assertTrue($result); 1583 $result = groups_user_groups_visible($course, $user1->id); 1584 $this->assertTrue($result); // Requesting all groups. 1585 1586 $cm->groupmode = NOGROUPS; 1587 $result = groups_user_groups_visible($course, $user1->id, $cm); 1588 $this->assertTrue($result); // Cm with no groups. 1589 1590 $cm->groupmode = SEPARATEGROUPS; 1591 $result = groups_user_groups_visible($course, $user1->id, $cm); 1592 $this->assertTrue($result); // Cm with separate groups. 1593 $result = groups_user_groups_visible($course, $user2->id, $cm); 1594 $this->assertTrue($result); // Cm with separate groups. 1595 1596 $cm->groupmode = VISIBLEGROUPS; 1597 $result = groups_user_groups_visible($course, $user1->id, $cm); 1598 $this->assertTrue($result); // Cm with visible groups. 1599 1600 // No groups, forced. 1601 $course->groupmode = NOGROUPS; 1602 $course->groupmodeforce = true; 1603 update_course($course); 1604 $result = groups_user_groups_visible($course, $user1->id); 1605 $this->assertTrue($result); 1606 $result = groups_user_groups_visible($course, $user1->id); 1607 $this->assertTrue($result); // Requesting all groups. 1608 1609 $cm->groupmode = NOGROUPS; 1610 $result = groups_user_groups_visible($course, $user1->id, $cm); 1611 $this->assertTrue($result); // Cm with no groups. 1612 1613 $cm->groupmode = SEPARATEGROUPS; 1614 $result = groups_user_groups_visible($course, $user1->id, $cm); 1615 $this->assertTrue($result); // Cm with separate groups. 1616 $result = groups_user_groups_visible($course, $user2->id, $cm); 1617 $this->assertTrue($result); // Cm with separate groups. 1618 1619 $cm->groupmode = SEPARATEGROUPS; 1620 $result = groups_user_groups_visible($course, $user1->id, $cm); 1621 $this->assertTrue($result); // Cm with visible groups. 1622 1623 // Visible groups, forced. 1624 $course->groupmode = VISIBLEGROUPS; 1625 $course->groupmodeforce = true; 1626 update_course($course); 1627 $result = groups_user_groups_visible($course, $user1->id); 1628 $this->assertTrue($result); 1629 $result = groups_user_groups_visible($course, $user1->id); 1630 $this->assertTrue($result); // Requesting all groups. 1631 1632 $cm->groupmode = NOGROUPS; 1633 $result = groups_user_groups_visible($course, $user1->id, $cm); 1634 $this->assertTrue($result); // Cm with no groups. 1635 1636 $cm->groupmode = SEPARATEGROUPS; 1637 $result = groups_user_groups_visible($course, $user1->id, $cm); 1638 $this->assertTrue($result); // Cm with separate groups. 1639 $result = groups_user_groups_visible($course, $user2->id, $cm); 1640 $this->assertTrue($result); // Cm with separate groups. 1641 1642 $cm->groupmode = VISIBLEGROUPS; 1643 $result = groups_user_groups_visible($course, $user1->id, $cm); 1644 $this->assertTrue($result); // Cm with visible groups. 1645 1646 // Visible groups, not forced. 1647 $course->groupmode = VISIBLEGROUPS; 1648 $course->groupmodeforce = false; 1649 update_course($course); 1650 $result = groups_user_groups_visible($course, $user1->id); 1651 $this->assertTrue($result); 1652 $result = groups_user_groups_visible($course, $user1->id); 1653 $this->assertTrue($result); // Requesting all groups. 1654 1655 $cm->groupmode = NOGROUPS; 1656 $result = groups_user_groups_visible($course, $user1->id, $cm); 1657 $this->assertTrue($result); // Cm with no groups. 1658 1659 $cm->groupmode = SEPARATEGROUPS; 1660 $result = groups_user_groups_visible($course, $user1->id, $cm); 1661 $this->assertTrue($result); // Cm with separate groups. 1662 $result = groups_user_groups_visible($course, $user2->id, $cm); 1663 $this->assertTrue($result); // Cm with separate groups. 1664 1665 $cm->groupmode = VISIBLEGROUPS; 1666 $result = groups_user_groups_visible($course, $user1->id, $cm); 1667 $this->assertTrue($result); // Cm with visible groups. 1668 1669 // Separate groups, forced. 1670 $course->groupmode = SEPARATEGROUPS; 1671 $course->groupmodeforce = true; 1672 update_course($course); 1673 $result = groups_user_groups_visible($course, $user1->id); 1674 $this->assertTrue($result); 1675 $result = groups_user_groups_visible($course, $user2->id); 1676 $this->assertTrue($result); 1677 $result = groups_user_groups_visible($course, $user2->id); 1678 $this->assertTrue($result); // Requesting all groups. 1679 $result = groups_user_groups_visible($course, $user3->id); 1680 $this->assertTrue($result); // Requesting all groups. 1681 $result = groups_user_groups_visible($course, $user3->id); 1682 $this->assertTrue($result); 1683 1684 $cm->groupmode = NOGROUPS; 1685 $result = groups_user_groups_visible($course, $user1->id, $cm); 1686 $this->assertTrue($result); // Cm with no groups. 1687 1688 $cm->groupmode = SEPARATEGROUPS; 1689 $result = groups_user_groups_visible($course, $user1->id, $cm); 1690 $this->assertTrue($result); // Cm with separate groups. 1691 $result = groups_user_groups_visible($course, $user2->id, $cm); 1692 $this->assertTrue($result); // Cm with separate groups. 1693 $result = groups_user_groups_visible($course, $user3->id, $cm); 1694 $this->assertTrue($result); 1695 1696 $cm->groupmode = VISIBLEGROUPS; 1697 $result = groups_user_groups_visible($course, $user1->id, $cm); 1698 $this->assertTrue($result); // Cm with visible groups. 1699 1700 // Separate groups, not forced. 1701 $course->groupmode = SEPARATEGROUPS; 1702 $course->groupmodeforce = false; 1703 update_course($course); 1704 $result = groups_user_groups_visible($course, $user1->id); 1705 $this->assertTrue($result); 1706 $result = groups_user_groups_visible($course, $user2->id); 1707 $this->assertTrue($result); 1708 $result = groups_user_groups_visible($course, $user2->id); 1709 $this->assertTrue($result); // Requesting all groups. 1710 $result = groups_user_groups_visible($course, $user3->id); 1711 $this->assertTrue($result); // Requesting all groups. 1712 1713 $cm->groupmode = NOGROUPS; 1714 $result = groups_user_groups_visible($course, $user1->id, $cm); 1715 $this->assertTrue($result); // Cm with no groups. 1716 1717 $cm->groupmode = SEPARATEGROUPS; 1718 $result = groups_user_groups_visible($course, $user1->id, $cm); 1719 $this->assertTrue($result); // Cm with separate groups. 1720 $result = groups_user_groups_visible($course, $user2->id, $cm); 1721 $this->assertTrue($result); // Cm with separate groups. 1722 1723 $cm->groupmode = VISIBLEGROUPS; 1724 $result = groups_user_groups_visible($course, $user1->id, $cm); 1725 $this->assertTrue($result); // Cm with visible groups. 1726 } 1727 1728 /** 1729 * Tests for groups_get_groups_members() method. 1730 */ 1731 public function test_groups_get_groups_members() { 1732 $this->resetAfterTest(true); 1733 $generator = $this->getDataGenerator(); 1734 1735 // Create courses. 1736 $course1 = $generator->create_course(); 1737 $course2 = $generator->create_course(); 1738 1739 // Create users. 1740 $user1 = $generator->create_user(); 1741 $user2 = $generator->create_user(); 1742 $user3 = $generator->create_user(); 1743 1744 // Enrol users. 1745 $generator->enrol_user($user1->id, $course1->id); 1746 $generator->enrol_user($user1->id, $course2->id); 1747 $generator->enrol_user($user2->id, $course2->id); 1748 $generator->enrol_user($user3->id, $course2->id); 1749 1750 // Create groups. 1751 $group1 = $generator->create_group(array('courseid' => $course1->id)); 1752 $group2 = $generator->create_group(array('courseid' => $course2->id)); 1753 $group3 = $generator->create_group(array('courseid' => $course2->id)); 1754 1755 // Assign users to groups. 1756 $this->assertTrue($generator->create_group_member(array('groupid' => $group1->id, 'userid' => $user1->id))); 1757 $this->assertTrue($generator->create_group_member(array('groupid' => $group2->id, 'userid' => $user1->id))); 1758 $this->assertTrue($generator->create_group_member(array('groupid' => $group2->id, 'userid' => $user2->id))); 1759 1760 // Test get_groups_members (with extra field and ordering). 1761 $members = groups_get_groups_members([$group1->id, $group2->id], ['lastaccess'], 'u.id ASC'); 1762 $this->assertCount(2, $members); 1763 $this->assertEquals([$user1->id, $user2->id], array_keys($members)); 1764 $this->assertTrue(isset($members[$user1->id]->lastaccess)); 1765 $this->assertTrue(isset($members[$user2->id]->lastaccess)); 1766 1767 // Group with just one. 1768 $members = groups_get_groups_members([$group1->id]); 1769 $this->assertCount(1, $members); 1770 $this->assertEquals($user1->id, $members[$user1->id]->id); 1771 1772 // Test the info matches group membership for the entire course. 1773 $groups = groups_get_all_groups($course1->id, 0, 0, 'g.*', true); 1774 $group1withmembers = array_pop($groups); 1775 1776 // Compare the sorted keys of both arrays (should be list of user ids). 1777 $members = array_keys($members); 1778 sort($members); 1779 $group1members = array_keys($group1withmembers->members); 1780 sort($group1members); 1781 $this->assertEquals($members, $group1members); 1782 1783 // Group with just one plus empty group. 1784 $members = groups_get_groups_members([$group1->id, $group3->id]); 1785 $this->assertCount(1, $members); 1786 $this->assertEquals($user1->id, $members[$user1->id]->id); 1787 1788 // Empty group. 1789 $members = groups_get_groups_members([$group3->id]); 1790 $this->assertCount(0, $members); 1791 1792 // Test groups_get_members. 1793 $members = groups_get_members($group2->id, 'u.*', 'u.id ASC'); 1794 $this->assertCount(2, $members); 1795 $this->assertEquals([$user1->id, $user2->id], array_keys($members)); 1796 1797 // Test the info matches group membership for the entire course. 1798 $groups = groups_get_all_groups($course2->id, 0, 0, 'g.*', true); 1799 $group2withmembers = $groups[$group2->id]; 1800 1801 // Compare the sorted keys of both arrays (should be list of user ids). 1802 $members = array_keys($members); 1803 sort($members); 1804 $group2members = array_keys($group2withmembers->members); 1805 sort($group2members); 1806 $this->assertEquals($members, $group2members); 1807 1808 } 1809 1810 /** 1811 * Tests for groups_get_activity_shared_group_members() method. 1812 */ 1813 public function test_groups_get_activity_shared_group_members() { 1814 $this->resetAfterTest(true); 1815 $generator = $this->getDataGenerator(); 1816 1817 // Create courses. 1818 $course = $generator->create_course(); 1819 1820 // Create cm. 1821 $assign = $generator->create_module("assign", array('course' => $course->id)); 1822 $cm = get_coursemodule_from_instance("assign", $assign->id); 1823 1824 // Create users. 1825 $user1 = $generator->create_user(); 1826 $user2 = $generator->create_user(); 1827 $user3 = $generator->create_user(); 1828 $user4 = $generator->create_user(); 1829 1830 // Enrol users. 1831 $generator->enrol_user($user1->id, $course->id); 1832 $generator->enrol_user($user2->id, $course->id); 1833 $generator->enrol_user($user3->id, $course->id); 1834 $generator->enrol_user($user4->id, $course->id); 1835 1836 // Create groups. 1837 $group1 = $generator->create_group(array('courseid' => $course->id)); 1838 $group2 = $generator->create_group(array('courseid' => $course->id)); 1839 $group3 = $generator->create_group(array('courseid' => $course->id)); 1840 1841 // Assign users to groups. 1842 $generator->create_group_member(array('groupid' => $group1->id, 'userid' => $user1->id)); 1843 $generator->create_group_member(array('groupid' => $group2->id, 'userid' => $user1->id)); 1844 $generator->create_group_member(array('groupid' => $group2->id, 'userid' => $user2->id)); 1845 $generator->create_group_member(array('groupid' => $group3->id, 'userid' => $user3->id)); 1846 1847 // Retrieve users sharing groups with user1. 1848 $members = groups_get_activity_shared_group_members($cm, $user1->id); 1849 $this->assertCount(2, $members); 1850 $this->assertEquals([$user1->id, $user2->id], array_keys($members), '', 0.0, 10, true); 1851 1852 // Retrieve users sharing groups with user2. 1853 $members = groups_get_activity_shared_group_members($cm, $user2->id); 1854 $this->assertCount(2, $members); 1855 $this->assertEquals([$user1->id, $user2->id], array_keys($members), '', 0.0, 10, true); 1856 1857 // Retrieve users sharing groups with user3. 1858 $members = groups_get_activity_shared_group_members($cm, $user3->id); 1859 $this->assertCount(1, $members); 1860 $this->assertEquals($user3->id, $members[$user3->id]->id); 1861 1862 // Retrieve users sharing groups with user without groups (user4). 1863 $members = groups_get_activity_shared_group_members($cm, $user4->id); 1864 $this->assertCount(0, $members); 1865 1866 // Now, create a different activity using groupings. 1867 $grouping = $generator->create_grouping(array('courseid' => $course->id, 'name' => 'Grouping 1')); 1868 // Skip group 2. 1869 groups_assign_grouping($grouping->id, $group1->id); 1870 groups_assign_grouping($grouping->id, $group3->id); 1871 1872 $assign = $generator->create_module("assign", array('course' => $course->id, 'groupingid' => $grouping->id)); 1873 $cm = get_coursemodule_from_instance("assign", $assign->id); 1874 1875 // Since the activity is forced to groupings (groups 1 and 3), I don't see members of group 2. 1876 $members = groups_get_activity_shared_group_members($cm, $user1->id); 1877 $this->assertCount(1, $members); 1878 $this->assertEquals($user1->id, $members[$user1->id]->id); 1879 1880 // Add user1 to group 3 (in the grouping). 1881 $generator->create_group_member(array('groupid' => $group3->id, 'userid' => $user1->id)); 1882 $members = groups_get_activity_shared_group_members($cm, $user1->id); 1883 $this->assertCount(2, $members); // Now I see members of group 3. 1884 $this->assertEquals([$user1->id, $user3->id], array_keys($members), '', 0.0, 10, true); 1885 } 1886 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body