Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403] [Versions 39 and 310]
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->assertStringContainsString(format_string($group1->name), $html); 998 $this->assertStringNotContainsString(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->assertStringContainsString(format_string($group1->name), $html); 1005 $this->assertStringContainsString(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->assertStringContainsString(format_string($group1->name), $html); 1012 $this->assertStringContainsString(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->assertStringContainsString(format_string($group1->name), $html); 1019 $this->assertStringContainsString(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->assertEqualsCanonicalizing( 1113 [$c1group1->id, $c1group2->id], 1114 array_keys($groups) 1115 ); 1116 $this->assertEquals( 1117 [$c1user1->id => $c1user1->id, $c12user1->id => $c12user1->id], 1118 $groups[$c1group1->id]->members 1119 ); 1120 $this->assertEquals( 1121 [$c12user2->id => $c12user2->id], 1122 $groups[$c1group2->id]->members 1123 ); 1124 1125 // Test with userid. 1126 $groups = groups_get_all_groups($course1->id, $c1user1->id, $c1grouping1->id, 'g.*', true); 1127 1128 $this->assertEquals([$c1group1->id], array_keys($groups)); 1129 $this->assertEqualsCanonicalizing( 1130 [$c1user1->id, $c12user1->id], 1131 $groups[$c1group1->id]->members 1132 ); 1133 } 1134 1135 /** 1136 * Tests for groups_get_user_groups() method. 1137 */ 1138 public function test_groups_get_user_groups() { 1139 $this->resetAfterTest(true); 1140 $generator = $this->getDataGenerator(); 1141 1142 // Create courses. 1143 $course1 = $generator->create_course(); 1144 $course2 = $generator->create_course(); 1145 1146 // Create users. 1147 $user1 = $generator->create_user(); 1148 $user2 = $generator->create_user(); 1149 $user3 = $generator->create_user(); 1150 1151 // Enrol users. 1152 $generator->enrol_user($user1->id, $course1->id); 1153 $generator->enrol_user($user1->id, $course2->id); 1154 $generator->enrol_user($user2->id, $course2->id); 1155 $generator->enrol_user($user3->id, $course2->id); 1156 1157 // Create groups. 1158 $group1 = $generator->create_group(array('courseid' => $course1->id)); 1159 $group2 = $generator->create_group(array('courseid' => $course2->id)); 1160 $group3 = $generator->create_group(array('courseid' => $course2->id)); 1161 1162 // Assign users to groups. 1163 $this->assertTrue($generator->create_group_member(array('groupid' => $group1->id, 'userid' => $user1->id))); 1164 $this->assertTrue($generator->create_group_member(array('groupid' => $group2->id, 'userid' => $user2->id))); 1165 1166 // Get user groups. 1167 $usergroups1 = groups_get_user_groups($course1->id, $user1->id); 1168 $usergroups2 = groups_get_user_groups($course2->id, $user2->id);; 1169 1170 // Assert return data. 1171 $this->assertEquals($group1->id, $usergroups1[0][0]); 1172 $this->assertEquals($group2->id, $usergroups2[0][0]); 1173 1174 // Now, test with groupings. 1175 $grouping1 = $generator->create_grouping(array('courseid' => $course1->id)); 1176 $grouping2 = $generator->create_grouping(array('courseid' => $course2->id)); 1177 1178 // Assign the groups to grouping. 1179 groups_assign_grouping($grouping1->id, $group1->id); 1180 groups_assign_grouping($grouping2->id, $group2->id); 1181 groups_assign_grouping($grouping2->id, $group3->id); 1182 1183 // Test with grouping. 1184 $usergroups1 = groups_get_user_groups($course1->id, $user1->id); 1185 $usergroups2 = groups_get_user_groups($course2->id, $user2->id); 1186 $this->assertArrayHasKey($grouping1->id, $usergroups1); 1187 $this->assertArrayHasKey($grouping2->id, $usergroups2); 1188 1189 // Test user without a group. 1190 $usergroups1 = groups_get_user_groups($course2->id, $user3->id); 1191 $this->assertCount(0, $usergroups1[0]); 1192 1193 // Test with userid = 0. 1194 $usergroups1 = groups_get_user_groups($course1->id, 0); 1195 $usergroups2 = groups_get_user_groups($course2->id, 0); 1196 $this->assertCount(0, $usergroups1[0]); 1197 $this->assertCount(0, $usergroups2[0]); 1198 1199 // Test with courseid = 0. 1200 $usergroups1 = groups_get_user_groups(0, $user1->id); 1201 $usergroups2 = groups_get_user_groups(0, $user2->id); 1202 $this->assertCount(0, $usergroups1[0]); 1203 $this->assertCount(0, $usergroups2[0]); 1204 } 1205 1206 /** 1207 * Create dummy groups array for use in menu tests 1208 * @param int $number 1209 * @return array 1210 */ 1211 protected function make_group_list($number) { 1212 $testgroups = array(); 1213 for ($a = 0; $a < $number; $a++) { 1214 $grp = new stdClass(); 1215 $grp->id = 100 + $a; 1216 $grp->name = 'test group ' . $grp->id; 1217 $testgroups[$grp->id] = $grp; 1218 } 1219 return $testgroups; 1220 } 1221 1222 public function test_groups_sort_menu_options_empty() { 1223 $this->assertEquals(array(), groups_sort_menu_options(array(), array())); 1224 } 1225 1226 public function test_groups_sort_menu_options_allowed_goups_only() { 1227 $this->assertEquals(array( 1228 100 => 'test group 100', 1229 101 => 'test group 101', 1230 ), groups_sort_menu_options($this->make_group_list(2), array())); 1231 } 1232 1233 public function test_groups_sort_menu_options_user_goups_only() { 1234 $this->assertEquals(array( 1235 100 => 'test group 100', 1236 101 => 'test group 101', 1237 ), groups_sort_menu_options(array(), $this->make_group_list(2))); 1238 } 1239 1240 public function test_groups_sort_menu_options_user_both() { 1241 $this->assertEquals(array( 1242 1 => array(get_string('mygroups', 'group') => array( 1243 100 => 'test group 100', 1244 101 => 'test group 101', 1245 )), 1246 2 => array(get_string('othergroups', 'group') => array( 1247 102 => 'test group 102', 1248 103 => 'test group 103', 1249 )), 1250 ), groups_sort_menu_options($this->make_group_list(4), $this->make_group_list(2))); 1251 } 1252 1253 public function test_groups_sort_menu_options_user_both_many_groups() { 1254 $this->assertEquals(array( 1255 1 => array(get_string('mygroups', 'group') => array( 1256 100 => 'test group 100', 1257 101 => 'test group 101', 1258 )), 1259 2 => array (get_string('othergroups', 'group') => array( 1260 102 => 'test group 102', 1261 103 => 'test group 103', 1262 104 => 'test group 104', 1263 105 => 'test group 105', 1264 106 => 'test group 106', 1265 107 => 'test group 107', 1266 108 => 'test group 108', 1267 109 => 'test group 109', 1268 110 => 'test group 110', 1269 111 => 'test group 111', 1270 112 => 'test group 112', 1271 )), 1272 ), groups_sort_menu_options($this->make_group_list(13), $this->make_group_list(2))); 1273 } 1274 1275 /** 1276 * Tests for groups_user_groups_visible. 1277 */ 1278 public function test_groups_user_groups_visible() { 1279 global $DB; 1280 1281 $generator = $this->getDataGenerator(); 1282 $this->resetAfterTest(); 1283 $this->setAdminUser(); 1284 1285 // Create a course category, course and groups. 1286 $cat = $generator->create_category(array('parent' => 0)); 1287 $course = $generator->create_course(array('category' => $cat->id)); 1288 $coursecontext = context_course::instance($course->id); 1289 $group1 = $generator->create_group(array('courseid' => $course->id, 'name' => 'Group 1')); 1290 $group2 = $generator->create_group(array('courseid' => $course->id, 'name' => 'Group 2')); 1291 $group3 = $generator->create_group(array('courseid' => $course->id, 'name' => 'Group 3')); 1292 $group4 = $generator->create_group(array('courseid' => $course->id, 'name' => 'Group 4')); 1293 1294 // Create cm. 1295 $assign = $generator->create_module("assign", array('course' => $course->id)); 1296 $cm = get_coursemodule_from_instance("assign", $assign->id); 1297 1298 // Create users. 1299 $user1 = $generator->create_user(); // Normal user. 1300 $user2 = $generator->create_user(); // Normal user. 1301 $user3 = $generator->create_user(); // Teacher, access all groups. 1302 $user4 = $generator->create_user(); // Normal user. 1303 1304 // Enrol users into the course. 1305 $generator->enrol_user($user1->id, $course->id); 1306 $generator->enrol_user($user2->id, $course->id); 1307 $generator->enrol_user($user4->id, $course->id); 1308 1309 // Assign groups. 1310 // User1 and User4 share groups. 1311 groups_add_member($group1, $user1); 1312 groups_add_member($group2, $user2); 1313 groups_add_member($group1, $user4); 1314 1315 // Give capability at course level to the user to access all groups. 1316 $role = $DB->get_field("role", "id", array("shortname" => "manager")); 1317 $generator->enrol_user($user3->id, $course->id, $role); 1318 // Make sure the user has the capability. 1319 assign_capability('moodle/site:accessallgroups', CAP_ALLOW, $role, $coursecontext->id); 1320 1321 // Normal users in different groups. 1322 $this->setUser($user1); 1323 1324 // No groups , not forced. 1325 $result = groups_user_groups_visible($course, $user2->id); 1326 $this->assertTrue($result); 1327 1328 $cm->groupmode = NOGROUPS; 1329 $result = groups_user_groups_visible($course, $user2->id, $cm); 1330 $this->assertTrue($result); // Cm with no groups. 1331 1332 $cm->groupmode = SEPARATEGROUPS; 1333 $result = groups_user_groups_visible($course, $user2->id, $cm); 1334 $this->assertFalse($result); // Cm with separate groups. 1335 1336 $cm->groupmode = VISIBLEGROUPS; 1337 $result = groups_user_groups_visible($course, $user2->id, $cm); 1338 $this->assertTrue($result); // Cm with visible groups. 1339 1340 // No groups, forced. 1341 $course->groupmode = NOGROUPS; 1342 $course->groupmodeforce = true; 1343 update_course($course); 1344 $result = groups_user_groups_visible($course, $user2->id); 1345 $this->assertTrue($result); 1346 1347 $cm->groupmode = NOGROUPS; 1348 $result = groups_user_groups_visible($course, $user2->id, $cm); 1349 $this->assertTrue($result); // Cm with no groups. 1350 1351 $cm->groupmode = SEPARATEGROUPS; 1352 $result = groups_user_groups_visible($course, $user2->id, $cm); 1353 $this->assertTrue($result); // Cm with separate groups. 1354 1355 $cm->groupmode = SEPARATEGROUPS; 1356 $result = groups_user_groups_visible($course, $user2->id); 1357 $this->assertTrue($result); // Cm with visible groups. 1358 1359 // Visible groups, forced. 1360 $course->groupmode = VISIBLEGROUPS; 1361 $course->groupmodeforce = true; 1362 update_course($course); 1363 $result = groups_user_groups_visible($course, $user2->id); 1364 $this->assertTrue($result); 1365 1366 $cm->groupmode = NOGROUPS; 1367 $result = groups_user_groups_visible($course, $user2->id, $cm); 1368 $this->assertTrue($result); // Cm with no groups. 1369 1370 $cm->groupmode = SEPARATEGROUPS; 1371 $result = groups_user_groups_visible($course, $user2->id, $cm); 1372 $this->assertTrue($result); // Cm with separate groups. 1373 1374 $cm->groupmode = VISIBLEGROUPS; 1375 $result = groups_user_groups_visible($course, $user2->id, $cm); 1376 $this->assertTrue($result); // Cm with visible groups. 1377 1378 // Visible groups, not forced. 1379 $course->groupmode = VISIBLEGROUPS; 1380 $course->groupmodeforce = false; 1381 update_course($course); 1382 $result = groups_user_groups_visible($course, $user2->id); 1383 $this->assertTrue($result); 1384 1385 $cm->groupmode = NOGROUPS; 1386 $result = groups_user_groups_visible($course, $user2->id, $cm); 1387 $this->assertTrue($result); // Cm with no groups. 1388 1389 $cm->groupmode = SEPARATEGROUPS; 1390 $result = groups_user_groups_visible($course, $user2->id, $cm); 1391 $this->assertFalse($result); // Cm with separate groups. 1392 1393 $cm->groupmode = VISIBLEGROUPS; 1394 $result = groups_user_groups_visible($course, $user2->id, $cm); 1395 $this->assertTrue($result); // Cm with visible groups. 1396 1397 // Separate groups, forced. 1398 $course->groupmode = SEPARATEGROUPS; 1399 $course->groupmodeforce = true; 1400 update_course($course); 1401 $result = groups_user_groups_visible($course, $user2->id); 1402 $this->assertFalse($result); 1403 1404 $result = groups_user_groups_visible($course, $user3->id); 1405 $this->assertFalse($result); // Requesting all groups. 1406 1407 $cm->groupmode = NOGROUPS; 1408 $result = groups_user_groups_visible($course, $user2->id, $cm); 1409 $this->assertFalse($result); // Cm with no groups. 1410 1411 $cm->groupmode = SEPARATEGROUPS; 1412 $result = groups_user_groups_visible($course, $user2->id, $cm); 1413 $this->assertFalse($result); // Cm with separate groups. 1414 1415 $result = groups_user_groups_visible($course, $user3->id, $cm); 1416 $this->assertTrue($result); 1417 1418 $cm->groupmode = VISIBLEGROUPS; 1419 $result = groups_user_groups_visible($course, $user2->id, $cm); 1420 $this->assertFalse($result); // Cm with visible groups. 1421 1422 // Separate groups, not forced. 1423 $course->groupmode = SEPARATEGROUPS; 1424 $course->groupmodeforce = false; 1425 update_course($course); 1426 $result = groups_user_groups_visible($course, $user2->id); 1427 $this->assertFalse($result); 1428 1429 $result = groups_user_groups_visible($course, $user3->id); 1430 $this->assertFalse($result); // Requesting all groups. 1431 1432 $cm->groupmode = NOGROUPS; 1433 $result = groups_user_groups_visible($course, $user2->id, $cm); 1434 $this->assertTrue($result); // Cm with no groups. 1435 1436 $cm->groupmode = SEPARATEGROUPS; 1437 $result = groups_user_groups_visible($course, $user2->id, $cm); 1438 $this->assertFalse($result); // Cm with separate groups. 1439 1440 $cm->groupmode = VISIBLEGROUPS; 1441 $result = groups_user_groups_visible($course, $user2->id, $cm); 1442 $this->assertTrue($result); // Cm with visible groups. 1443 1444 // Users sharing groups. 1445 1446 // No groups , not forced. 1447 $course->groupmode = NOGROUPS; 1448 $course->groupmodeforce = false; 1449 update_course($course); 1450 1451 $result = groups_user_groups_visible($course, $user4->id); 1452 $this->assertTrue($result); 1453 1454 $cm->groupmode = NOGROUPS; 1455 $result = groups_user_groups_visible($course, $user4->id, $cm); 1456 $this->assertTrue($result); // Cm with no groups. 1457 1458 $cm->groupmode = SEPARATEGROUPS; 1459 $result = groups_user_groups_visible($course, $user4->id, $cm); 1460 $this->assertTrue($result); // Cm with separate groups. 1461 1462 $cm->groupmode = VISIBLEGROUPS; 1463 $result = groups_user_groups_visible($course, $user4->id, $cm); 1464 $this->assertTrue($result); // Cm with visible groups. 1465 1466 // No groups, forced. 1467 $course->groupmode = NOGROUPS; 1468 $course->groupmodeforce = true; 1469 update_course($course); 1470 $result = groups_user_groups_visible($course, $user4->id); 1471 $this->assertTrue($result); 1472 1473 $cm->groupmode = NOGROUPS; 1474 $result = groups_user_groups_visible($course, $user4->id, $cm); 1475 $this->assertTrue($result); // Cm with no groups. 1476 1477 $cm->groupmode = SEPARATEGROUPS; 1478 $result = groups_user_groups_visible($course, $user4->id, $cm); 1479 $this->assertTrue($result); // Cm with separate groups. 1480 1481 $cm->groupmode = SEPARATEGROUPS; 1482 $result = groups_user_groups_visible($course, $user4->id, $cm); 1483 $this->assertTrue($result); // Cm with visible groups. 1484 1485 // Visible groups, forced. 1486 $course->groupmode = VISIBLEGROUPS; 1487 $course->groupmodeforce = true; 1488 update_course($course); 1489 $result = groups_user_groups_visible($course, $user4->id); 1490 $this->assertTrue($result); 1491 1492 $cm->groupmode = NOGROUPS; 1493 $result = groups_user_groups_visible($course, $user4->id, $cm); 1494 $this->assertTrue($result); // Cm with no groups. 1495 1496 $cm->groupmode = SEPARATEGROUPS; 1497 $result = groups_user_groups_visible($course, $user4->id, $cm); 1498 $this->assertTrue($result); // Cm with separate groups. 1499 1500 $cm->groupmode = VISIBLEGROUPS; 1501 $result = groups_user_groups_visible($course, $user4->id, $cm); 1502 $this->assertTrue($result); // Cm with visible groups. 1503 1504 // Visible groups, not forced. 1505 $course->groupmode = VISIBLEGROUPS; 1506 $course->groupmodeforce = false; 1507 update_course($course); 1508 $result = groups_user_groups_visible($course, $user4->id); 1509 $this->assertTrue($result); 1510 1511 $cm->groupmode = NOGROUPS; 1512 $result = groups_user_groups_visible($course, $user4->id, $cm); 1513 $this->assertTrue($result); // Cm with no groups. 1514 1515 $cm->groupmode = SEPARATEGROUPS; 1516 $result = groups_user_groups_visible($course, $user4->id, $cm); 1517 $this->assertTrue($result); // Cm with separate groups. 1518 1519 $cm->groupmode = VISIBLEGROUPS; 1520 $result = groups_user_groups_visible($course, $user4->id, $cm); 1521 $this->assertTrue($result); // Cm with visible groups. 1522 1523 // Separate groups, forced. 1524 $course->groupmode = SEPARATEGROUPS; 1525 $course->groupmodeforce = true; 1526 update_course($course); 1527 $result = groups_user_groups_visible($course, $user4->id); 1528 $this->assertTrue($result); 1529 1530 $result = groups_user_groups_visible($course, $user3->id); 1531 $this->assertFalse($result); // Requesting all groups. 1532 1533 $cm->groupmode = NOGROUPS; 1534 $result = groups_user_groups_visible($course, $user4->id, $cm); 1535 $this->assertTrue($result); // Cm with no groups. 1536 1537 $cm->groupmode = SEPARATEGROUPS; 1538 $result = groups_user_groups_visible($course, $user4->id, $cm); 1539 $this->assertTrue($result); // Cm with separate groups. 1540 1541 $result = groups_user_groups_visible($course, $user3->id, $cm); 1542 $this->assertTrue($result); 1543 1544 $cm->groupmode = VISIBLEGROUPS; 1545 $result = groups_user_groups_visible($course, $user4->id, $cm); 1546 $this->assertTrue($result); // Cm with visible groups. 1547 1548 // Separate groups, not forced. 1549 $course->groupmode = SEPARATEGROUPS; 1550 $course->groupmodeforce = false; 1551 update_course($course); 1552 $result = groups_user_groups_visible($course, $user4->id); 1553 $this->assertTrue($result); 1554 1555 $result = groups_user_groups_visible($course, $user3->id); 1556 $this->assertFalse($result); // Requesting all groups. 1557 1558 $cm->groupmode = NOGROUPS; 1559 $result = groups_user_groups_visible($course, $user4->id, $cm); 1560 $this->assertTrue($result); // Cm with no groups. 1561 1562 $cm->groupmode = SEPARATEGROUPS; 1563 $result = groups_user_groups_visible($course, $user4->id, $cm); 1564 $this->assertTrue($result); // Cm with separate groups. 1565 1566 $cm->groupmode = VISIBLEGROUPS; 1567 $result = groups_user_groups_visible($course, $user4->id, $cm); 1568 $this->assertTrue($result); // Cm with visible groups. 1569 1570 // For teacher with access all groups. 1571 1572 // No groups , not forced. 1573 $course->groupmode = NOGROUPS; 1574 $course->groupmodeforce = false; 1575 update_course($course); 1576 1577 $this->setUser($user3); 1578 1579 $result = groups_user_groups_visible($course, $user1->id); 1580 $this->assertTrue($result); 1581 $result = groups_user_groups_visible($course, $user1->id); 1582 $this->assertTrue($result); // Requesting all groups. 1583 1584 $cm->groupmode = NOGROUPS; 1585 $result = groups_user_groups_visible($course, $user1->id, $cm); 1586 $this->assertTrue($result); // Cm with no groups. 1587 1588 $cm->groupmode = SEPARATEGROUPS; 1589 $result = groups_user_groups_visible($course, $user1->id, $cm); 1590 $this->assertTrue($result); // Cm with separate groups. 1591 $result = groups_user_groups_visible($course, $user2->id, $cm); 1592 $this->assertTrue($result); // Cm with separate groups. 1593 1594 $cm->groupmode = VISIBLEGROUPS; 1595 $result = groups_user_groups_visible($course, $user1->id, $cm); 1596 $this->assertTrue($result); // Cm with visible groups. 1597 1598 // No groups, forced. 1599 $course->groupmode = NOGROUPS; 1600 $course->groupmodeforce = true; 1601 update_course($course); 1602 $result = groups_user_groups_visible($course, $user1->id); 1603 $this->assertTrue($result); 1604 $result = groups_user_groups_visible($course, $user1->id); 1605 $this->assertTrue($result); // Requesting all groups. 1606 1607 $cm->groupmode = NOGROUPS; 1608 $result = groups_user_groups_visible($course, $user1->id, $cm); 1609 $this->assertTrue($result); // Cm with no groups. 1610 1611 $cm->groupmode = SEPARATEGROUPS; 1612 $result = groups_user_groups_visible($course, $user1->id, $cm); 1613 $this->assertTrue($result); // Cm with separate groups. 1614 $result = groups_user_groups_visible($course, $user2->id, $cm); 1615 $this->assertTrue($result); // Cm with separate groups. 1616 1617 $cm->groupmode = SEPARATEGROUPS; 1618 $result = groups_user_groups_visible($course, $user1->id, $cm); 1619 $this->assertTrue($result); // Cm with visible groups. 1620 1621 // Visible groups, forced. 1622 $course->groupmode = VISIBLEGROUPS; 1623 $course->groupmodeforce = true; 1624 update_course($course); 1625 $result = groups_user_groups_visible($course, $user1->id); 1626 $this->assertTrue($result); 1627 $result = groups_user_groups_visible($course, $user1->id); 1628 $this->assertTrue($result); // Requesting all groups. 1629 1630 $cm->groupmode = NOGROUPS; 1631 $result = groups_user_groups_visible($course, $user1->id, $cm); 1632 $this->assertTrue($result); // Cm with no groups. 1633 1634 $cm->groupmode = SEPARATEGROUPS; 1635 $result = groups_user_groups_visible($course, $user1->id, $cm); 1636 $this->assertTrue($result); // Cm with separate groups. 1637 $result = groups_user_groups_visible($course, $user2->id, $cm); 1638 $this->assertTrue($result); // Cm with separate groups. 1639 1640 $cm->groupmode = VISIBLEGROUPS; 1641 $result = groups_user_groups_visible($course, $user1->id, $cm); 1642 $this->assertTrue($result); // Cm with visible groups. 1643 1644 // Visible groups, not forced. 1645 $course->groupmode = VISIBLEGROUPS; 1646 $course->groupmodeforce = false; 1647 update_course($course); 1648 $result = groups_user_groups_visible($course, $user1->id); 1649 $this->assertTrue($result); 1650 $result = groups_user_groups_visible($course, $user1->id); 1651 $this->assertTrue($result); // Requesting all groups. 1652 1653 $cm->groupmode = NOGROUPS; 1654 $result = groups_user_groups_visible($course, $user1->id, $cm); 1655 $this->assertTrue($result); // Cm with no groups. 1656 1657 $cm->groupmode = SEPARATEGROUPS; 1658 $result = groups_user_groups_visible($course, $user1->id, $cm); 1659 $this->assertTrue($result); // Cm with separate groups. 1660 $result = groups_user_groups_visible($course, $user2->id, $cm); 1661 $this->assertTrue($result); // Cm with separate groups. 1662 1663 $cm->groupmode = VISIBLEGROUPS; 1664 $result = groups_user_groups_visible($course, $user1->id, $cm); 1665 $this->assertTrue($result); // Cm with visible groups. 1666 1667 // Separate groups, forced. 1668 $course->groupmode = SEPARATEGROUPS; 1669 $course->groupmodeforce = true; 1670 update_course($course); 1671 $result = groups_user_groups_visible($course, $user1->id); 1672 $this->assertTrue($result); 1673 $result = groups_user_groups_visible($course, $user2->id); 1674 $this->assertTrue($result); 1675 $result = groups_user_groups_visible($course, $user2->id); 1676 $this->assertTrue($result); // Requesting all groups. 1677 $result = groups_user_groups_visible($course, $user3->id); 1678 $this->assertTrue($result); // Requesting all groups. 1679 $result = groups_user_groups_visible($course, $user3->id); 1680 $this->assertTrue($result); 1681 1682 $cm->groupmode = NOGROUPS; 1683 $result = groups_user_groups_visible($course, $user1->id, $cm); 1684 $this->assertTrue($result); // Cm with no groups. 1685 1686 $cm->groupmode = SEPARATEGROUPS; 1687 $result = groups_user_groups_visible($course, $user1->id, $cm); 1688 $this->assertTrue($result); // Cm with separate groups. 1689 $result = groups_user_groups_visible($course, $user2->id, $cm); 1690 $this->assertTrue($result); // Cm with separate groups. 1691 $result = groups_user_groups_visible($course, $user3->id, $cm); 1692 $this->assertTrue($result); 1693 1694 $cm->groupmode = VISIBLEGROUPS; 1695 $result = groups_user_groups_visible($course, $user1->id, $cm); 1696 $this->assertTrue($result); // Cm with visible groups. 1697 1698 // Separate groups, not forced. 1699 $course->groupmode = SEPARATEGROUPS; 1700 $course->groupmodeforce = false; 1701 update_course($course); 1702 $result = groups_user_groups_visible($course, $user1->id); 1703 $this->assertTrue($result); 1704 $result = groups_user_groups_visible($course, $user2->id); 1705 $this->assertTrue($result); 1706 $result = groups_user_groups_visible($course, $user2->id); 1707 $this->assertTrue($result); // Requesting all groups. 1708 $result = groups_user_groups_visible($course, $user3->id); 1709 $this->assertTrue($result); // Requesting all groups. 1710 1711 $cm->groupmode = NOGROUPS; 1712 $result = groups_user_groups_visible($course, $user1->id, $cm); 1713 $this->assertTrue($result); // Cm with no groups. 1714 1715 $cm->groupmode = SEPARATEGROUPS; 1716 $result = groups_user_groups_visible($course, $user1->id, $cm); 1717 $this->assertTrue($result); // Cm with separate groups. 1718 $result = groups_user_groups_visible($course, $user2->id, $cm); 1719 $this->assertTrue($result); // Cm with separate groups. 1720 1721 $cm->groupmode = VISIBLEGROUPS; 1722 $result = groups_user_groups_visible($course, $user1->id, $cm); 1723 $this->assertTrue($result); // Cm with visible groups. 1724 } 1725 1726 /** 1727 * Tests for groups_get_groups_members() method. 1728 */ 1729 public function test_groups_get_groups_members() { 1730 $this->resetAfterTest(true); 1731 $generator = $this->getDataGenerator(); 1732 1733 // Create courses. 1734 $course1 = $generator->create_course(); 1735 $course2 = $generator->create_course(); 1736 1737 // Create users. 1738 $user1 = $generator->create_user(); 1739 $user2 = $generator->create_user(); 1740 $user3 = $generator->create_user(); 1741 1742 // Enrol users. 1743 $generator->enrol_user($user1->id, $course1->id); 1744 $generator->enrol_user($user1->id, $course2->id); 1745 $generator->enrol_user($user2->id, $course2->id); 1746 $generator->enrol_user($user3->id, $course2->id); 1747 1748 // Create groups. 1749 $group1 = $generator->create_group(array('courseid' => $course1->id)); 1750 $group2 = $generator->create_group(array('courseid' => $course2->id)); 1751 $group3 = $generator->create_group(array('courseid' => $course2->id)); 1752 1753 // Assign users to groups. 1754 $this->assertTrue($generator->create_group_member(array('groupid' => $group1->id, 'userid' => $user1->id))); 1755 $this->assertTrue($generator->create_group_member(array('groupid' => $group2->id, 'userid' => $user1->id))); 1756 $this->assertTrue($generator->create_group_member(array('groupid' => $group2->id, 'userid' => $user2->id))); 1757 1758 // Test get_groups_members (with extra field and ordering). 1759 $members = groups_get_groups_members([$group1->id, $group2->id], ['lastaccess'], 'u.id ASC'); 1760 $this->assertCount(2, $members); 1761 $this->assertEquals([$user1->id, $user2->id], array_keys($members)); 1762 $this->assertTrue(isset($members[$user1->id]->lastaccess)); 1763 $this->assertTrue(isset($members[$user2->id]->lastaccess)); 1764 1765 // Group with just one. 1766 $members = groups_get_groups_members([$group1->id]); 1767 $this->assertCount(1, $members); 1768 $this->assertEquals($user1->id, $members[$user1->id]->id); 1769 1770 // Test the info matches group membership for the entire course. 1771 $groups = groups_get_all_groups($course1->id, 0, 0, 'g.*', true); 1772 $group1withmembers = array_pop($groups); 1773 1774 // Compare the sorted keys of both arrays (should be list of user ids). 1775 $members = array_keys($members); 1776 sort($members); 1777 $group1members = array_keys($group1withmembers->members); 1778 sort($group1members); 1779 $this->assertEquals($members, $group1members); 1780 1781 // Group with just one plus empty group. 1782 $members = groups_get_groups_members([$group1->id, $group3->id]); 1783 $this->assertCount(1, $members); 1784 $this->assertEquals($user1->id, $members[$user1->id]->id); 1785 1786 // Empty group. 1787 $members = groups_get_groups_members([$group3->id]); 1788 $this->assertCount(0, $members); 1789 1790 // Test groups_get_members. 1791 $members = groups_get_members($group2->id, 'u.*', 'u.id ASC'); 1792 $this->assertCount(2, $members); 1793 $this->assertEquals([$user1->id, $user2->id], array_keys($members)); 1794 1795 // Test the info matches group membership for the entire course. 1796 $groups = groups_get_all_groups($course2->id, 0, 0, 'g.*', true); 1797 $group2withmembers = $groups[$group2->id]; 1798 1799 // Compare the sorted keys of both arrays (should be list of user ids). 1800 $members = array_keys($members); 1801 sort($members); 1802 $group2members = array_keys($group2withmembers->members); 1803 sort($group2members); 1804 $this->assertEquals($members, $group2members); 1805 1806 } 1807 1808 /** 1809 * Tests for groups_get_activity_shared_group_members() method. 1810 */ 1811 public function test_groups_get_activity_shared_group_members() { 1812 $this->resetAfterTest(true); 1813 $generator = $this->getDataGenerator(); 1814 1815 // Create courses. 1816 $course = $generator->create_course(); 1817 1818 // Create cm. 1819 $assign = $generator->create_module("assign", array('course' => $course->id)); 1820 $cm = get_coursemodule_from_instance("assign", $assign->id); 1821 1822 // Create users. 1823 $user1 = $generator->create_user(); 1824 $user2 = $generator->create_user(); 1825 $user3 = $generator->create_user(); 1826 $user4 = $generator->create_user(); 1827 1828 // Enrol users. 1829 $generator->enrol_user($user1->id, $course->id); 1830 $generator->enrol_user($user2->id, $course->id); 1831 $generator->enrol_user($user3->id, $course->id); 1832 $generator->enrol_user($user4->id, $course->id); 1833 1834 // Create groups. 1835 $group1 = $generator->create_group(array('courseid' => $course->id)); 1836 $group2 = $generator->create_group(array('courseid' => $course->id)); 1837 $group3 = $generator->create_group(array('courseid' => $course->id)); 1838 1839 // Assign users to groups. 1840 $generator->create_group_member(array('groupid' => $group1->id, 'userid' => $user1->id)); 1841 $generator->create_group_member(array('groupid' => $group2->id, 'userid' => $user1->id)); 1842 $generator->create_group_member(array('groupid' => $group2->id, 'userid' => $user2->id)); 1843 $generator->create_group_member(array('groupid' => $group3->id, 'userid' => $user3->id)); 1844 1845 // Retrieve users sharing groups with user1. 1846 $members = groups_get_activity_shared_group_members($cm, $user1->id); 1847 $this->assertCount(2, $members); 1848 $this->assertEqualsCanonicalizing([$user1->id, $user2->id], array_keys($members)); 1849 1850 // Retrieve users sharing groups with user2. 1851 $members = groups_get_activity_shared_group_members($cm, $user2->id); 1852 $this->assertCount(2, $members); 1853 $this->assertEqualsCanonicalizing([$user1->id, $user2->id], array_keys($members)); 1854 1855 // Retrieve users sharing groups with user3. 1856 $members = groups_get_activity_shared_group_members($cm, $user3->id); 1857 $this->assertCount(1, $members); 1858 $this->assertEquals($user3->id, $members[$user3->id]->id); 1859 1860 // Retrieve users sharing groups with user without groups (user4). 1861 $members = groups_get_activity_shared_group_members($cm, $user4->id); 1862 $this->assertCount(0, $members); 1863 1864 // Now, create a different activity using groupings. 1865 $grouping = $generator->create_grouping(array('courseid' => $course->id, 'name' => 'Grouping 1')); 1866 // Skip group 2. 1867 groups_assign_grouping($grouping->id, $group1->id); 1868 groups_assign_grouping($grouping->id, $group3->id); 1869 1870 $assign = $generator->create_module("assign", array('course' => $course->id, 'groupingid' => $grouping->id)); 1871 $cm = get_coursemodule_from_instance("assign", $assign->id); 1872 1873 // Since the activity is forced to groupings (groups 1 and 3), I don't see members of group 2. 1874 $members = groups_get_activity_shared_group_members($cm, $user1->id); 1875 $this->assertCount(1, $members); 1876 $this->assertEquals($user1->id, $members[$user1->id]->id); 1877 1878 // Add user1 to group 3 (in the grouping). 1879 $generator->create_group_member(array('groupid' => $group3->id, 'userid' => $user1->id)); 1880 $members = groups_get_activity_shared_group_members($cm, $user1->id); 1881 $this->assertCount(2, $members); // Now I see members of group 3. 1882 $this->assertEqualsCanonicalizing([$user1->id, $user3->id], array_keys($members)); 1883 } 1884 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body