2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
19 use core_group\visibility
;
22 * Unit tests for lib/grouplib.php
25 * @copyright 2007 onwards Martin Dougiamas (http://dougiamas.com)
26 * @author Andrew Nicols
27 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29 class grouplib_test
extends \advanced_testcase
{
31 public function test_groups_get_group_by_idnumber() {
32 $this->resetAfterTest(true);
34 $generator = $this->getDataGenerator();
36 // Create a course category and course.
37 $cat = $generator->create_category(array('parent' => 0));
38 $course = $generator->create_course(array('category' => $cat->id
));
40 $idnumber1 = 'idnumber1';
41 $idnumber2 = 'idnumber2';
44 * Test with an empty and a null idnumber.
46 // An empty idnumber should always return a false value.
47 $this->assertFalse(groups_get_group_by_idnumber($course->id
, ''));
48 $this->assertFalse(groups_get_group_by_idnumber($course->id
, null));
50 // Even when a group exists which also has an empty idnumber.
51 $generator->create_group(array('courseid' => $course->id
));
52 $this->assertFalse(groups_get_group_by_idnumber($course->id
, ''));
53 $this->assertFalse(groups_get_group_by_idnumber($course->id
, null));
56 * Test with a valid idnumber.
58 // There is no matching idnumber at present.
59 $this->assertFalse(groups_get_group_by_idnumber($course->id
, $idnumber1));
61 // We should now have a valid group returned by the idnumber search.
62 $group = $generator->create_group(array('courseid' => $course->id
, 'idnumber' => $idnumber1));
63 $this->assertEquals($group, groups_get_group_by_idnumber($course->id
, $idnumber1));
65 // An empty idnumber should still return false.
66 $this->assertFalse(groups_get_group_by_idnumber($course->id
, ''));
67 $this->assertFalse(groups_get_group_by_idnumber($course->id
, null));
70 * Test with another idnumber.
72 // There is no matching idnumber at present.
73 $this->assertFalse(groups_get_group_by_idnumber($course->id
, $idnumber2));
75 // We should now have a valid group returned by the idnumber search.
76 $group = $generator->create_group(array('courseid' => $course->id
, 'idnumber' => $idnumber2));
77 $this->assertEquals($group, groups_get_group_by_idnumber($course->id
, $idnumber2));
80 * Group idnumbers are unique within a course so test that we don't
81 * retrieve groups for the first course.
84 // Create a second course.
85 $course = $generator->create_course(array('category' => $cat->id
));
87 // An empty idnumber should always return a false value.
88 $this->assertFalse(groups_get_group_by_idnumber($course->id
, ''));
89 $this->assertFalse(groups_get_group_by_idnumber($course->id
, null));
91 // Our existing idnumbers shouldn't be returned here as we're in a different course.
92 $this->assertFalse(groups_get_group_by_idnumber($course->id
, $idnumber1));
93 $this->assertFalse(groups_get_group_by_idnumber($course->id
, $idnumber2));
95 // We should be able to reuse the idnumbers again since this is a different course.
96 $group = $generator->create_group(array('courseid' => $course->id
, 'idnumber' => $idnumber1));
97 $this->assertEquals($group, groups_get_group_by_idnumber($course->id
, $idnumber1));
99 $group = $generator->create_group(array('courseid' => $course->id
, 'idnumber' => $idnumber2));
100 $this->assertEquals($group, groups_get_group_by_idnumber($course->id
, $idnumber2));
103 public function test_groups_get_grouping_by_idnumber() {
104 $this->resetAfterTest(true);
106 $generator = $this->getDataGenerator();
108 // Create a course category and course.
109 $cat = $generator->create_category(array('parent' => 0));
110 $course = $generator->create_course(array('category' => $cat->id
));
112 $idnumber1 = 'idnumber1';
113 $idnumber2 = 'idnumber2';
116 * Test with an empty and a null idnumber.
118 // An empty idnumber should always return a false value.
119 $this->assertFalse(groups_get_grouping_by_idnumber($course->id
, ''));
120 $this->assertFalse(groups_get_grouping_by_idnumber($course->id
, null));
122 // Even when a grouping exists which also has an empty idnumber.
123 $generator->create_grouping(array('courseid' => $course->id
));
124 $this->assertFalse(groups_get_grouping_by_idnumber($course->id
, ''));
125 $this->assertFalse(groups_get_grouping_by_idnumber($course->id
, null));
128 * Test with a valid idnumber
130 // There is no matching idnumber at present.
131 $this->assertFalse(groups_get_grouping_by_idnumber($course->id
, $idnumber1));
133 // We should now have a valid group returned by the idnumber search.
134 $grouping = $generator->create_grouping(array('courseid' => $course->id
, 'idnumber' => $idnumber1));
135 $this->assertEquals($grouping, groups_get_grouping_by_idnumber($course->id
, $idnumber1));
137 // An empty idnumber should still return false.
138 $this->assertFalse(groups_get_grouping_by_idnumber($course->id
, ''));
139 $this->assertFalse(groups_get_grouping_by_idnumber($course->id
, null));
142 * Test with another idnumber.
144 // There is no matching idnumber at present.
145 $this->assertFalse(groups_get_grouping_by_idnumber($course->id
, $idnumber2));
147 // We should now have a valid grouping returned by the idnumber search.
148 $grouping = $generator->create_grouping(array('courseid' => $course->id
, 'idnumber' => $idnumber2));
149 $this->assertEquals($grouping, groups_get_grouping_by_idnumber($course->id
, $idnumber2));
152 * Grouping idnumbers are unique within a course so test that we don't
153 * retrieve groupings for the first course.
156 // Create a second course.
157 $course = $generator->create_course(array('category' => $cat->id
));
159 // An empty idnumber should always return a false value.
160 $this->assertFalse(groups_get_grouping_by_idnumber($course->id
, ''));
161 $this->assertFalse(groups_get_grouping_by_idnumber($course->id
, null));
163 // Our existing idnumbers shouldn't be returned here as we're in a different course.
164 $this->assertFalse(groups_get_grouping_by_idnumber($course->id
, $idnumber1));
165 $this->assertFalse(groups_get_grouping_by_idnumber($course->id
, $idnumber2));
167 // We should be able to reuse the idnumbers again since this is a different course.
168 $grouping = $generator->create_grouping(array('courseid' => $course->id
, 'idnumber' => $idnumber1));
169 $this->assertEquals($grouping, groups_get_grouping_by_idnumber($course->id
, $idnumber1));
171 $grouping = $generator->create_grouping(array('courseid' => $course->id
, 'idnumber' => $idnumber2));
172 $this->assertEquals($grouping, groups_get_grouping_by_idnumber($course->id
, $idnumber2));
175 public function test_groups_get_members_ids_sql() {
178 $this->resetAfterTest(true);
180 $generator = $this->getDataGenerator();
182 $course = $generator->create_course();
183 $coursecontext = \context_course
::instance($course->id
);
184 $student1 = $generator->create_user();
185 $student2 = $generator->create_user();
186 $plugin = enrol_get_plugin('manual');
187 $role = $DB->get_record('role', array('shortname' => 'student'));
188 $group = $generator->create_group(array('courseid' => $course->id
));
189 $instance = $DB->get_record('enrol', array(
190 'courseid' => $course->id
,
194 $this->assertNotEquals($instance, false);
196 // Enrol users in the course.
197 $plugin->enrol_user($instance, $student1->id
, $role->id
);
198 $plugin->enrol_user($instance, $student2->id
, $role->id
);
200 list($sql, $params) = groups_get_members_ids_sql($group->id
);
202 // Test an empty group.
203 $users = $DB->get_records_sql($sql, $params);
204 $this->assertFalse(array_key_exists($student1->id
, $users));
206 // Test with a group member.
207 groups_add_member($group->id
, $student1->id
);
208 $users = $DB->get_records_sql($sql, $params);
209 $this->assertTrue(array_key_exists($student1->id
, $users));
212 public function test_groups_get_members_ids_sql_multiple_groups() {
215 $this->resetAfterTest(true);
217 $generator = $this->getDataGenerator();
219 $course = $generator->create_course();
220 $student1 = $generator->create_user();
221 $student2 = $generator->create_user();
222 $plugin = enrol_get_plugin('manual');
223 $role = $DB->get_record('role', array('shortname' => 'student'));
224 $group1 = $generator->create_group(array('courseid' => $course->id
));
225 $group2 = $generator->create_group(array('courseid' => $course->id
));
230 $instance = $DB->get_record('enrol', array(
231 'courseid' => $course->id
,
235 $this->assertNotEquals($instance, false);
237 // Enrol users in the course.
238 $plugin->enrol_user($instance, $student1->id
, $role->id
);
239 $plugin->enrol_user($instance, $student2->id
, $role->id
);
241 list($sql, $params) = groups_get_members_ids_sql($groupids);
243 // Test an empty group.
244 $users = $DB->get_records_sql($sql, $params);
245 $this->assertFalse(array_key_exists($student1->id
, $users));
247 // Test with a member of one of the two group.
248 groups_add_member($group1->id
, $student1->id
);
249 $users = $DB->get_records_sql($sql, $params);
250 $this->assertTrue(array_key_exists($student1->id
, $users));
252 // Test with members of two groups.
253 groups_add_member($group2->id
, $student2->id
);
254 $users = $DB->get_records_sql($sql, $params);
255 $this->assertTrue(array_key_exists($student1->id
, $users));
256 $this->assertTrue(array_key_exists($student2->id
, $users));
259 public function test_groups_get_members_ids_sql_multiple_groups_join_types() {
262 $this->resetAfterTest(true);
264 $generator = $this->getDataGenerator();
266 $course = $generator->create_course();
267 $student1 = $generator->create_user();
268 $student2 = $generator->create_user();
269 $student3 = $generator->create_user();
270 $student4 = $generator->create_user();
271 $student5 = $generator->create_user();
272 $student6 = $generator->create_user();
273 $plugin = enrol_get_plugin('manual');
274 $role = $DB->get_record('role', array('shortname' => 'student'));
275 $group1 = $generator->create_group(array('courseid' => $course->id
));
276 $group2 = $generator->create_group(array('courseid' => $course->id
));
277 $group3 = $generator->create_group(array('courseid' => $course->id
));
278 // Only groups 1 and 2 specified in SQL (group 3 helps cover the None case).
283 $instance = $DB->get_record('enrol', array(
284 'courseid' => $course->id
,
288 $this->assertNotEquals($instance, false);
290 // Enrol users in the course.
291 $plugin->enrol_user($instance, $student1->id
, $role->id
);
292 $plugin->enrol_user($instance, $student2->id
, $role->id
);
293 $plugin->enrol_user($instance, $student3->id
, $role->id
);
294 $plugin->enrol_user($instance, $student4->id
, $role->id
);
295 $plugin->enrol_user($instance, $student5->id
, $role->id
);
296 $plugin->enrol_user($instance, $student6->id
, $role->id
);
298 // Generate SQL with the different groups join types for members of group1 and group2.
299 list($sqlany, $paramsany) = groups_get_members_ids_sql($groupids, null, GROUPS_JOIN_ANY
);
300 list($sqlall, $paramsall) = groups_get_members_ids_sql($groupids, null, GROUPS_JOIN_ALL
);
301 list($sqlnone, $paramsnone) = groups_get_members_ids_sql($groupids, null, GROUPS_JOIN_NONE
);
303 // Any - Test empty groups, no matches.
304 $users = $DB->get_records_sql($sqlany, $paramsany);
305 $this->assertFalse(array_key_exists($student1->id
, $users));
306 $this->assertFalse(array_key_exists($student2->id
, $users));
307 $this->assertFalse(array_key_exists($student3->id
, $users));
308 $this->assertFalse(array_key_exists($student4->id
, $users));
309 $this->assertFalse(array_key_exists($student5->id
, $users));
310 $this->assertFalse(array_key_exists($student6->id
, $users));
312 // All - Test empty groups, no matches.
313 $users = $DB->get_records_sql($sqlall, $paramsall);
314 $this->assertFalse(array_key_exists($student1->id
, $users));
315 $this->assertFalse(array_key_exists($student2->id
, $users));
316 $this->assertFalse(array_key_exists($student3->id
, $users));
317 $this->assertFalse(array_key_exists($student4->id
, $users));
318 $this->assertFalse(array_key_exists($student5->id
, $users));
319 $this->assertFalse(array_key_exists($student6->id
, $users));
321 // None - Test empty groups, all match.
322 $users = $DB->get_records_sql($sqlnone, $paramsnone);
323 $this->assertTrue(array_key_exists($student1->id
, $users));
324 $this->assertTrue(array_key_exists($student2->id
, $users));
325 $this->assertTrue(array_key_exists($student3->id
, $users));
326 $this->assertTrue(array_key_exists($student4->id
, $users));
327 $this->assertTrue(array_key_exists($student5->id
, $users));
328 $this->assertTrue(array_key_exists($student6->id
, $users));
330 // Assign various group member combinations.
331 groups_add_member($group1->id
, $student1->id
);
332 groups_add_member($group1->id
, $student2->id
);
333 groups_add_member($group1->id
, $student3->id
);
334 groups_add_member($group2->id
, $student2->id
);
335 groups_add_member($group2->id
, $student3->id
);
336 groups_add_member($group2->id
, $student4->id
);
337 groups_add_member($group3->id
, $student5->id
);
339 // Any - Test students in one or both of groups 1 and 2 matched.
340 $users = $DB->get_records_sql($sqlany, $paramsany);
341 $this->assertTrue(array_key_exists($student1->id
, $users));
342 $this->assertTrue(array_key_exists($student2->id
, $users));
343 $this->assertTrue(array_key_exists($student3->id
, $users));
344 $this->assertTrue(array_key_exists($student4->id
, $users));
345 $this->assertFalse(array_key_exists($student5->id
, $users));
346 $this->assertFalse(array_key_exists($student6->id
, $users));
348 // All - Test only students in both groups 1 and 2 matched.
349 $users = $DB->get_records_sql($sqlall, $paramsall);
350 $this->assertTrue(array_key_exists($student2->id
, $users));
351 $this->assertTrue(array_key_exists($student3->id
, $users));
352 $this->assertFalse(array_key_exists($student1->id
, $users));
353 $this->assertFalse(array_key_exists($student4->id
, $users));
354 $this->assertFalse(array_key_exists($student5->id
, $users));
355 $this->assertFalse(array_key_exists($student6->id
, $users));
357 // None - Test only students not in group 1 or 2 matched.
358 $users = $DB->get_records_sql($sqlnone, $paramsnone);
359 $this->assertTrue(array_key_exists($student5->id
, $users));
360 $this->assertTrue(array_key_exists($student6->id
, $users));
361 $this->assertFalse(array_key_exists($student1->id
, $users));
362 $this->assertFalse(array_key_exists($student2->id
, $users));
363 $this->assertFalse(array_key_exists($student3->id
, $users));
364 $this->assertFalse(array_key_exists($student4->id
, $users));
367 public function test_groups_get_members_ids_sql_valid_context() {
370 $this->resetAfterTest(true);
372 $generator = $this->getDataGenerator();
374 $course = $generator->create_course();
375 $coursecontext = \context_course
::instance($course->id
);
376 $student1 = $generator->create_user();
377 $student2 = $generator->create_user();
378 $plugin = enrol_get_plugin('manual');
379 $role = $DB->get_record('role', array('shortname' => 'student'));
380 $group = $generator->create_group(array('courseid' => $course->id
));
381 $instance = $DB->get_record('enrol', array(
382 'courseid' => $course->id
,
386 $this->assertNotEquals($instance, false);
388 // Enrol users in the course.
389 $plugin->enrol_user($instance, $student1->id
, $role->id
);
390 $plugin->enrol_user($instance, $student2->id
, $role->id
);
392 // Add student1 to the group.
393 groups_add_member($group->id
, $student1->id
);
395 // Test with members at any group and with a valid $context.
396 list($sql, $params) = groups_get_members_ids_sql(USERSWITHOUTGROUP
, $coursecontext);
397 $users = $DB->get_records_sql($sql, $params);
398 $this->assertFalse(array_key_exists($student1->id
, $users));
399 $this->assertTrue(array_key_exists($student2->id
, $users));
402 public function test_groups_get_members_ids_sql_empty_context() {
405 $this->resetAfterTest(true);
407 $generator = $this->getDataGenerator();
409 $course = $generator->create_course();
410 $coursecontext = \context_course
::instance($course->id
);
411 $student1 = $generator->create_user();
412 $student2 = $generator->create_user();
413 $plugin = enrol_get_plugin('manual');
414 $role = $DB->get_record('role', array('shortname' => 'student'));
415 $group = $generator->create_group(array('courseid' => $course->id
));
416 $instance = $DB->get_record('enrol', array(
417 'courseid' => $course->id
,
421 $this->assertNotEquals($instance, false);
423 // Enrol users in the course.
424 $plugin->enrol_user($instance, $student1->id
, $role->id
);
425 $plugin->enrol_user($instance, $student2->id
, $role->id
);
427 // Add student1 to the group.
428 groups_add_member($group->id
, $student1->id
);
430 // Test with members at any group and without the $context.
431 $this->expectException('coding_exception');
432 list($sql, $params) = groups_get_members_ids_sql(USERSWITHOUTGROUP
);
435 public function test_groups_get_members_ids_sql_invalid_context() {
438 $this->resetAfterTest(true);
440 $generator = $this->getDataGenerator();
442 $course = $generator->create_course();
443 $coursecontext = \context_course
::instance($course->id
);
444 $student1 = $generator->create_user();
445 $student2 = $generator->create_user();
446 $plugin = enrol_get_plugin('manual');
447 $role = $DB->get_record('role', array('shortname' => 'student'));
448 $group = $generator->create_group(array('courseid' => $course->id
));
449 $instance = $DB->get_record('enrol', array(
450 'courseid' => $course->id
,
454 $this->assertNotEquals($instance, false);
456 // Enrol users in the course.
457 $plugin->enrol_user($instance, $student1->id
, $role->id
);
458 $plugin->enrol_user($instance, $student2->id
, $role->id
);
460 // Add student1 to the group.
461 groups_add_member($group->id
, $student1->id
);
463 // Test with members at any group and with an invalid $context.
464 $syscontext = \context_system
::instance();
465 $this->expectException('coding_exception');
466 list($sql, $params) = groups_get_members_ids_sql(USERSWITHOUTGROUP
, $syscontext);
470 * Test retrieving users with concatenated group names from a course
472 public function test_groups_get_names_concat_sql(): void
{
475 $this->resetAfterTest();
477 // Create a course containing two groups.
478 $course = $this->getDataGenerator()->create_course();
479 $group1 = $this->getDataGenerator()->create_group(['courseid' => $course->id
]);
480 $group2 = $this->getDataGenerator()->create_group(['courseid' => $course->id
]);
482 // Create first user, add them to group 1 and group 2.
483 $user1 = $this->getDataGenerator()->create_and_enrol($course, 'student');
484 $this->getDataGenerator()->create_group_member(['userid' => $user1->id
, 'groupid' => $group1->id
]);
485 $this->getDataGenerator()->create_group_member(['userid' => $user1->id
, 'groupid' => $group2->id
]);
487 // Create second user, add them to group 1 only.
488 $user2 = $this->getDataGenerator()->create_and_enrol($course, 'student');
489 $this->getDataGenerator()->create_group_member(['userid' => $user2->id
, 'groupid' => $group1->id
]);
491 // Call our method, and assertion.
492 [$sql, $params] = groups_get_names_concat_sql($course->id
);
493 $records = $DB->get_records_sql($sql, $params);
495 $this->assertEqualsCanonicalizing([
497 'userid' => $user1->id
,
498 'groupnames' => "{$group1->name}, {$group2->name}",
501 'userid' => $user2->id
,
502 'groupnames' => $group1->name
,
507 public function test_groups_get_group_by_name() {
508 $this->resetAfterTest(true);
510 $generator = $this->getDataGenerator();
512 // Create a course category and course.
513 $cat = $generator->create_category(array('parent' => 0));
514 $course = $generator->create_course(array('category' => $cat->id
));
519 // Test with an empty and a null idnumber.
520 $this->assertFalse(groups_get_group_by_name($course->id
, ''));
521 $this->assertFalse(groups_get_group_by_name($course->id
, null));
523 // Even when a group exists.
524 $generator->create_group(array('courseid' => $course->id
));
525 $this->assertFalse(groups_get_group_by_name($course->id
, ''));
526 $this->assertFalse(groups_get_group_by_name($course->id
, null));
528 // Test with a valid name, but one that doesn't exist yet.
529 $this->assertFalse(groups_get_group_by_name($course->id
, $name1));
530 $this->assertFalse(groups_get_group_by_name($course->id
, $name2));
532 // We should now have a valid group returned by the name search.
533 $group1 = $generator->create_group(array('courseid' => $course->id
, 'name' => $name1));
534 $this->assertEquals($group1->id
, groups_get_group_by_name($course->id
, $name1));
535 $this->assertFalse(groups_get_group_by_name($course->id
, $name2));
537 // We should now have a two valid groups returned by the name search.
538 $group2 = $generator->create_group(array('courseid' => $course->id
, 'name' => $name2));
539 $this->assertEquals($group1->id
, groups_get_group_by_name($course->id
, $name1));
540 $this->assertEquals($group2->id
, groups_get_group_by_name($course->id
, $name2));
543 $this->assertTrue(groups_delete_group($group1));
544 $this->assertFalse(groups_get_group_by_name($course->id
, $name1));
545 $this->assertEquals($group2->id
, groups_get_group_by_name($course->id
, $name2));
548 * Group idnumbers are unique within a course so test that we don't
549 * retrieve groups for the first course.
552 // Create a second course.
553 $course = $generator->create_course(array('category' => $cat->id
));
555 // An empty name should always return a false value.
556 $this->assertFalse(groups_get_group_by_name($course->id
, ''));
557 $this->assertFalse(groups_get_group_by_name($course->id
, null));
559 // Our existing names shouldn't be returned here as we're in a different course.
560 $this->assertFalse(groups_get_group_by_name($course->id
, $name1));
561 $this->assertFalse(groups_get_group_by_name($course->id
, $name2));
563 // We should be able to reuse the idnumbers again since this is a different course.
564 $group1 = $generator->create_group(array('courseid' => $course->id
, 'name' => $name1));
565 $this->assertEquals($group1->id
, groups_get_group_by_name($course->id
, $name1));
567 $group2 = $generator->create_group(array('courseid' => $course->id
, 'name' => $name2));
568 $this->assertEquals($group2->id
, groups_get_group_by_name($course->id
, $name2));
571 public function test_groups_get_grouping() {
572 $this->resetAfterTest(true);
574 $generator = $this->getDataGenerator();
576 // Create a course category and course.
577 $cat = $generator->create_category(array('parent' => 0));
578 $course = $generator->create_course(array('category' => $cat->id
));
580 $name1 = 'Grouping 1';
581 $name2 = 'Grouping 2';
583 // Test with an empty and a null idnumber.
584 $this->assertFalse(groups_get_grouping_by_name($course->id
, ''));
585 $this->assertFalse(groups_get_grouping_by_name($course->id
, null));
587 // Even when a group exists.
588 $generator->create_group(array('courseid' => $course->id
));
589 $this->assertFalse(groups_get_grouping_by_name($course->id
, ''));
590 $this->assertFalse(groups_get_grouping_by_name($course->id
, null));
592 // Test with a valid name, but one that doesn't exist yet.
593 $this->assertFalse(groups_get_grouping_by_name($course->id
, $name1));
594 $this->assertFalse(groups_get_grouping_by_name($course->id
, $name2));
596 // We should now have a valid group returned by the name search.
597 $group1 = $generator->create_grouping(array('courseid' => $course->id
, 'name' => $name1));
598 $this->assertEquals($group1->id
, groups_get_grouping_by_name($course->id
, $name1));
599 $this->assertFalse(groups_get_grouping_by_name($course->id
, $name2));
601 // We should now have a two valid groups returned by the name search.
602 $group2 = $generator->create_grouping(array('courseid' => $course->id
, 'name' => $name2));
603 $this->assertEquals($group1->id
, groups_get_grouping_by_name($course->id
, $name1));
604 $this->assertEquals($group2->id
, groups_get_grouping_by_name($course->id
, $name2));
607 $this->assertTrue(groups_delete_grouping($group1));
608 $this->assertFalse(groups_get_grouping_by_name($course->id
, $name1));
609 $this->assertEquals($group2->id
, groups_get_grouping_by_name($course->id
, $name2));
612 * Group idnumbers are unique within a course so test that we don't
613 * retrieve groups for the first course.
616 // Create a second course.
617 $course = $generator->create_course(array('category' => $cat->id
));
619 // An empty name should always return a false value.
620 $this->assertFalse(groups_get_grouping_by_name($course->id
, ''));
621 $this->assertFalse(groups_get_grouping_by_name($course->id
, null));
623 // Our existing names shouldn't be returned here as we're in a different course.
624 $this->assertFalse(groups_get_grouping_by_name($course->id
, $name1));
625 $this->assertFalse(groups_get_grouping_by_name($course->id
, $name2));
627 // We should be able to reuse the idnumbers again since this is a different course.
628 $group1 = $generator->create_grouping(array('courseid' => $course->id
, 'name' => $name1));
629 $this->assertEquals($group1->id
, groups_get_grouping_by_name($course->id
, $name1));
631 $group2 = $generator->create_grouping(array('courseid' => $course->id
, 'name' => $name2));
632 $this->assertEquals($group2->id
, groups_get_grouping_by_name($course->id
, $name2));
635 public function test_groups_get_course_data() {
636 $this->resetAfterTest(true);
638 $generator = $this->getDataGenerator();
640 // Create a course category and course.
641 $cat = $generator->create_category(array('parent' => 0));
642 $course = $generator->create_course(array('category' => $cat->id
));
643 $grouping1 = $generator->create_grouping(array('courseid' => $course->id
, 'name' => 'Grouping 1'));
644 $grouping2 = $generator->create_grouping(array('courseid' => $course->id
, 'name' => 'Grouping 2'));
645 $group1 = $generator->create_group(array('courseid' => $course->id
, 'name' => 'Group 1'));
646 $group2 = $generator->create_group(array('courseid' => $course->id
, 'name' => 'Group 2'));
647 $group3 = $generator->create_group(array('courseid' => $course->id
, 'name' => 'Group 3'));
648 $group4 = $generator->create_group(array('courseid' => $course->id
, 'name' => 'Group 4'));
650 // Assign the groups to groupings.
651 $this->assertTrue(groups_assign_grouping($grouping1->id
, $group1->id
));
652 $this->assertTrue(groups_assign_grouping($grouping1->id
, $group2->id
));
653 $this->assertTrue(groups_assign_grouping($grouping2->id
, $group3->id
));
654 $this->assertTrue(groups_assign_grouping($grouping2->id
, $group4->id
));
657 $data = groups_get_course_data($course->id
);
658 $this->assertInstanceOf('stdClass', $data);
659 $this->assertObjectHasAttribute('groups', $data);
660 $this->assertObjectHasAttribute('groupings', $data);
661 $this->assertObjectHasAttribute('mappings', $data);
663 // Test we have the expected items returns.
664 $this->assertCount(4, $data->groups
);
665 $this->assertCount(2, $data->groupings
);
666 $this->assertCount(4, $data->mappings
);
668 // Check we have the expected groups.
669 $this->assertArrayHasKey($group1->id
, $data->groups
);
670 $this->assertArrayHasKey($group2->id
, $data->groups
);
671 $this->assertArrayHasKey($group3->id
, $data->groups
);
672 $this->assertArrayHasKey($group4->id
, $data->groups
);
674 // Test a group-id is mapped correctly.
675 $this->assertSame($group3->name
, $data->groups
[$group3->id
]->name
);
677 // Check we have the expected number of groupings.
678 $this->assertArrayHasKey($grouping1->id
, $data->groupings
);
679 $this->assertArrayHasKey($grouping2->id
, $data->groupings
);
681 // Test a grouping-id is mapped correctly.
682 $this->assertEquals($grouping2->name
, $data->groupings
[$grouping2->id
]->name
);
684 // Test that all of the mappings are correct.
691 foreach ($data->mappings
as $mapping) {
692 if ($mapping->groupingid
=== $grouping1->id
) {
694 $this->assertContainsEquals($mapping->groupid
, array($group1->id
, $group2->id
));
695 } else if ($mapping->groupingid
=== $grouping2->id
) {
697 $this->assertContainsEquals($mapping->groupid
, array($group3->id
, $group4->id
));
699 $this->fail('Unexpected groupingid');
701 switch ($mapping->groupid
) {
702 case $group1->id
: $group1maps++
; break;
703 case $group2->id
: $group2maps++
; break;
704 case $group3->id
: $group3maps++
; break;
705 case $group4->id
: $group4maps++
; break;
708 $this->assertEquals(2, $grouping1maps);
709 $this->assertEquals(2, $grouping2maps);
710 $this->assertEquals(1, $group1maps);
711 $this->assertEquals(1, $group2maps);
712 $this->assertEquals(1, $group3maps);
713 $this->assertEquals(1, $group4maps);
715 // Test the groups_get_all_groups which uses this functionality.
716 $groups = groups_get_all_groups($course->id
);
717 $this->assertCount(4, $groups);
718 $this->assertArrayHasKey($group1->id
, $groups);
719 $this->assertArrayHasKey($group2->id
, $groups);
720 $this->assertArrayHasKey($group3->id
, $groups);
721 $this->assertArrayHasKey($group4->id
, $groups);
723 $groups = groups_get_all_groups($course->id
, null, $grouping1->id
);
724 $this->assertCount(2, $groups);
725 $this->assertArrayHasKey($group1->id
, $groups);
726 $this->assertArrayHasKey($group2->id
, $groups);
727 $this->assertArrayNotHasKey($group3->id
, $groups);
728 $this->assertArrayNotHasKey($group4->id
, $groups);
730 $groups = groups_get_all_groups($course->id
, null, $grouping2->id
);
731 $this->assertCount(2, $groups);
732 $this->assertArrayNotHasKey($group1->id
, $groups);
733 $this->assertArrayNotHasKey($group2->id
, $groups);
734 $this->assertArrayHasKey($group3->id
, $groups);
735 $this->assertArrayHasKey($group4->id
, $groups);
737 // Test this function using an alternate column for the result index
738 $groups = groups_get_all_groups($course->id
, null, $grouping2->id
, 'g.name, g.id');
739 $this->assertCount(2, $groups);
740 $this->assertArrayNotHasKey($group3->id
, $groups);
741 $this->assertArrayHasKey($group3->name
, $groups);
742 $this->assertEquals($group3->id
, $groups[$group3->name
]->id
);
746 * Tests for groups_group_visible.
748 public function test_groups_group_visible() {
751 $generator = $this->getDataGenerator();
752 $this->resetAfterTest();
753 $this->setAdminUser();
755 // Create a course category, course and groups.
756 $cat = $generator->create_category(array('parent' => 0));
757 $course = $generator->create_course(array('category' => $cat->id
));
758 $coursecontext = \context_course
::instance($course->id
);
759 $group1 = $generator->create_group(array('courseid' => $course->id
, 'name' => 'Group 1'));
760 $group2 = $generator->create_group(array('courseid' => $course->id
, 'name' => 'Group 2'));
761 $group3 = $generator->create_group(array('courseid' => $course->id
, 'name' => 'Group 3'));
762 $group4 = $generator->create_group(array('courseid' => $course->id
, 'name' => 'Group 4'));
765 $assign = $generator->create_module("assign", array('course' => $course->id
));
766 $cm = get_coursemodule_from_instance("assign", $assign->id
);
769 $user1 = $generator->create_user();
770 $user2 = $generator->create_user();
771 $user3 = $generator->create_user();
773 // Enrol users into the course.
774 $generator->enrol_user($user1->id
, $course->id
);
775 $generator->enrol_user($user2->id
, $course->id
);
778 groups_add_member($group1, $user2);
780 // Give capability at course level to the user to access all groups.
781 $role = $DB->get_field("role", "id", array("shortname" => "manager"));
782 $generator->enrol_user($user3->id
, $course->id
, $role);
783 // Make sure the user has the capability.
784 assign_capability('moodle/site:accessallgroups', CAP_ALLOW
, $role, $coursecontext->id
);
786 // No groups , not forced.
787 $result = groups_group_visible($group1->id
, $course, null, $user1->id
);
788 $this->assertTrue($result);
789 $result = groups_group_visible(0, $course, null, $user1->id
);
790 $this->assertTrue($result); // Requesting all groups.
792 $result = groups_group_visible($group1->id
, $course, $cm, $user1->id
);
793 $this->assertTrue($result); // Cm with no groups.
795 $cm->groupmode
= SEPARATEGROUPS
;
796 $result = groups_group_visible($group1->id
, $course, $cm, $user1->id
);
797 $this->assertFalse($result); // Cm with separate groups.
798 $result = groups_group_visible($group1->id
, $course, $cm, $user2->id
);
799 $this->assertTrue($result); // Cm with separate groups.
801 $cm->groupmode
= VISIBLEGROUPS
;
802 $result = groups_group_visible($group1->id
, $course, $cm, $user1->id
);
803 $this->assertTrue($result); // Cm with visible groups.
805 // No groups, forced.
806 $course->groupmode
= NOGROUPS
;
807 $course->groupmodeforce
= true;
808 update_course($course);
809 $result = groups_group_visible($group1->id
, $course, null, $user1->id
);
810 $this->assertTrue($result);
811 $result = groups_group_visible(0, $course, null, $user1->id
);
812 $this->assertTrue($result); // Requesting all groups.
814 $result = groups_group_visible($group1->id
, $course, $cm, $user1->id
);
815 $this->assertTrue($result); // Cm with no groups.
817 $cm->groupmode
= SEPARATEGROUPS
;
818 $result = groups_group_visible($group1->id
, $course, $cm, $user1->id
);
819 $this->assertTrue($result); // Cm with separate groups.
820 $result = groups_group_visible($group1->id
, $course, $cm, $user2->id
);
821 $this->assertTrue($result); // Cm with separate groups.
823 $cm->groupmode
= SEPARATEGROUPS
;
824 $result = groups_group_visible($group1->id
, $course, $cm, $user1->id
);
825 $this->assertTrue($result); // Cm with visible groups.
827 // Visible groups, forced.
828 $course->groupmode
= VISIBLEGROUPS
;
829 $course->groupmodeforce
= true;
830 update_course($course);
831 $result = groups_group_visible($group1->id
, $course, null, $user1->id
);
832 $this->assertTrue($result);
833 $result = groups_group_visible(0, $course, null, $user1->id
);
834 $this->assertTrue($result); // Requesting all groups.
836 $cm->groupmode
= NOGROUPS
;
837 $result = groups_group_visible($group1->id
, $course, $cm, $user1->id
);
838 $this->assertTrue($result); // Cm with no groups.
840 $cm->groupmode
= SEPARATEGROUPS
;
841 $result = groups_group_visible($group1->id
, $course, $cm, $user1->id
);
842 $this->assertTrue($result); // Cm with separate groups.
843 $result = groups_group_visible($group1->id
, $course, $cm, $user2->id
);
844 $this->assertTrue($result); // Cm with separate groups.
846 $cm->groupmode
= VISIBLEGROUPS
;
847 $result = groups_group_visible($group1->id
, $course, $cm, $user1->id
);
848 $this->assertTrue($result); // Cm with visible groups.
850 // Visible groups, not forced.
851 $course->groupmode
= VISIBLEGROUPS
;
852 $course->groupmodeforce
= false;
853 update_course($course);
854 $result = groups_group_visible($group1->id
, $course, null, $user1->id
);
855 $this->assertTrue($result);
856 $result = groups_group_visible(0, $course, null, $user1->id
);
857 $this->assertTrue($result); // Requesting all groups.
859 $cm->groupmode
= NOGROUPS
;
860 $result = groups_group_visible($group1->id
, $course, $cm, $user1->id
);
861 $this->assertTrue($result); // Cm with no groups.
863 $cm->groupmode
= SEPARATEGROUPS
;
864 $result = groups_group_visible($group1->id
, $course, $cm, $user1->id
);
865 $this->assertFalse($result); // Cm with separate groups.
866 $result = groups_group_visible($group1->id
, $course, $cm, $user2->id
);
867 $this->assertTrue($result); // Cm with separate groups.
869 $cm->groupmode
= VISIBLEGROUPS
;
870 $result = groups_group_visible($group1->id
, $course, $cm, $user1->id
);
871 $this->assertTrue($result); // Cm with visible groups.
873 // Separate groups, forced.
874 $course->groupmode
= SEPARATEGROUPS
;
875 $course->groupmodeforce
= true;
876 update_course($course);
877 $result = groups_group_visible($group1->id
, $course, null, $user1->id
);
878 $this->assertFalse($result);
879 $result = groups_group_visible($group1->id
, $course, null, $user2->id
);
880 $this->assertTrue($result);
881 $result = groups_group_visible(0, $course, null, $user2->id
);
882 $this->assertFalse($result); // Requesting all groups.
883 $result = groups_group_visible(0, $course, null, $user3->id
);
884 $this->assertTrue($result); // Requesting all groups.
885 $result = groups_group_visible($group1->id
, $course, null, $user3->id
);
886 $this->assertTrue($result); // Make sure user with access to all groups can see any group.
888 $cm->groupmode
= NOGROUPS
;
889 $result = groups_group_visible($group1->id
, $course, $cm, $user1->id
);
890 $this->assertFalse($result); // Cm with no groups.
892 $cm->groupmode
= SEPARATEGROUPS
;
893 $result = groups_group_visible($group1->id
, $course, $cm, $user1->id
);
894 $this->assertFalse($result); // Cm with separate groups.
895 $result = groups_group_visible($group1->id
, $course, $cm, $user2->id
);
896 $this->assertTrue($result); // Cm with separate groups.
897 $result = groups_group_visible($group1->id
, $course, $cm, $user3->id
);
898 $this->assertTrue($result); // Make sure user with access to all groups can see any group.
900 $cm->groupmode
= VISIBLEGROUPS
;
901 $result = groups_group_visible($group1->id
, $course, $cm, $user1->id
);
902 $this->assertFalse($result); // Cm with visible groups.
904 // Separate groups, not forced.
905 $course->groupmode
= SEPARATEGROUPS
;
906 $course->groupmodeforce
= false;
907 update_course($course);
908 $result = groups_group_visible($group1->id
, $course, null, $user1->id
);
909 $this->assertFalse($result);
910 $result = groups_group_visible($group1->id
, $course, null, $user2->id
);
911 $this->assertTrue($result);
912 $result = groups_group_visible(0, $course, null, $user2->id
);
913 $this->assertFalse($result); // Requesting all groups.
914 $result = groups_group_visible(0, $course, null, $user3->id
);
915 $this->assertTrue($result); // Requesting all groups.
917 $cm->groupmode
= NOGROUPS
;
918 $result = groups_group_visible($group1->id
, $course, $cm, $user1->id
);
919 $this->assertTrue($result); // Cm with no groups.
921 $cm->groupmode
= SEPARATEGROUPS
;
922 $result = groups_group_visible($group1->id
, $course, $cm, $user1->id
);
923 $this->assertFalse($result); // Cm with separate groups.
924 $result = groups_group_visible($group1->id
, $course, $cm, $user2->id
);
925 $this->assertTrue($result); // Cm with separate groups.
927 $cm->groupmode
= VISIBLEGROUPS
;
928 $result = groups_group_visible($group1->id
, $course, $cm, $user1->id
);
929 $this->assertTrue($result); // Cm with visible groups.
932 function test_groups_get_groupmode() {
934 $generator = $this->getDataGenerator();
935 $this->resetAfterTest();
936 $this->setAdminUser();
938 // Create a course with no groups forcing.
939 $course1 = $generator->create_course();
941 // Create cm1 with no groups, cm1 with visible groups, cm2 with separate groups and cm3 with visible groups.
942 $assign1 = $generator->create_module("assign", array('course' => $course1->id
));
943 $assign2 = $generator->create_module("assign", array('course' => $course1->id
),
944 array('groupmode' => SEPARATEGROUPS
));
945 $assign3 = $generator->create_module("assign", array('course' => $course1->id
),
946 array('groupmode' => VISIBLEGROUPS
));
948 // Request data for tests.
949 $cm1 = get_coursemodule_from_instance("assign", $assign1->id
);
950 $cm2 = get_coursemodule_from_instance("assign", $assign2->id
);
951 $cm3 = get_coursemodule_from_instance("assign", $assign3->id
);
952 $modinfo = get_fast_modinfo($course1->id
);
954 // Assert that any method of getting activity groupmode returns the correct result.
955 $this->assertEquals(NOGROUPS
, groups_get_activity_groupmode($cm1));
956 $this->assertEquals(NOGROUPS
, groups_get_activity_groupmode($cm1, $course1));
957 $this->assertEquals(NOGROUPS
, groups_get_activity_groupmode($modinfo->cms
[$cm1->id
]));
958 $this->assertEquals(SEPARATEGROUPS
, groups_get_activity_groupmode($cm2));
959 $this->assertEquals(SEPARATEGROUPS
, groups_get_activity_groupmode($cm2, $course1));
960 $this->assertEquals(SEPARATEGROUPS
, groups_get_activity_groupmode($modinfo->cms
[$cm2->id
]));
961 $this->assertEquals(VISIBLEGROUPS
, groups_get_activity_groupmode($cm3));
962 $this->assertEquals(VISIBLEGROUPS
, groups_get_activity_groupmode($cm3, $course1));
963 $this->assertEquals(VISIBLEGROUPS
, groups_get_activity_groupmode($modinfo->cms
[$cm3->id
]));
965 // Update the course set the groupmode SEPARATEGROUPS but not forced.
966 update_course((object)array('id' => $course1->id
, 'groupmode' => SEPARATEGROUPS
));
967 // Re-request the data from DB.
968 $course1 = $DB->get_record('course', array('id' => $course1->id
));
969 $modinfo = get_fast_modinfo($course1->id
);
971 // Existing activities are not changed.
972 $this->assertEquals(NOGROUPS
, groups_get_activity_groupmode($cm1));
973 $this->assertEquals(NOGROUPS
, groups_get_activity_groupmode($cm1, $course1));
974 $this->assertEquals(NOGROUPS
, groups_get_activity_groupmode($modinfo->cms
[$cm1->id
]));
975 $this->assertEquals(SEPARATEGROUPS
, groups_get_activity_groupmode($cm2));
976 $this->assertEquals(SEPARATEGROUPS
, groups_get_activity_groupmode($cm2, $course1));
977 $this->assertEquals(SEPARATEGROUPS
, groups_get_activity_groupmode($modinfo->cms
[$cm2->id
]));
978 $this->assertEquals(VISIBLEGROUPS
, groups_get_activity_groupmode($cm3));
979 $this->assertEquals(VISIBLEGROUPS
, groups_get_activity_groupmode($cm3, $course1));
980 $this->assertEquals(VISIBLEGROUPS
, groups_get_activity_groupmode($modinfo->cms
[$cm3->id
]));
982 // Update the course set the groupmode SEPARATEGROUPS and forced.
983 update_course((object)array('id' => $course1->id
, 'groupmode' => SEPARATEGROUPS
, 'groupmodeforce' => true));
984 // Re-request the data from DB.
985 $course1 = $DB->get_record('course', array('id' => $course1->id
));
986 $modinfo = get_fast_modinfo($course1->id
);
988 // Make sure all activities have separate groups mode now.
989 $this->assertEquals(SEPARATEGROUPS
, groups_get_activity_groupmode($cm1));
990 $this->assertEquals(SEPARATEGROUPS
, groups_get_activity_groupmode($cm1, $course1));
991 $this->assertEquals(SEPARATEGROUPS
, groups_get_activity_groupmode($modinfo->cms
[$cm1->id
]));
992 $this->assertEquals(SEPARATEGROUPS
, groups_get_activity_groupmode($cm2));
993 $this->assertEquals(SEPARATEGROUPS
, groups_get_activity_groupmode($cm2, $course1));
994 $this->assertEquals(SEPARATEGROUPS
, groups_get_activity_groupmode($modinfo->cms
[$cm2->id
]));
995 $this->assertEquals(SEPARATEGROUPS
, groups_get_activity_groupmode($cm3));
996 $this->assertEquals(SEPARATEGROUPS
, groups_get_activity_groupmode($cm3, $course1));
997 $this->assertEquals(SEPARATEGROUPS
, groups_get_activity_groupmode($modinfo->cms
[$cm3->id
]));
1001 * Tests for groups_allgroups_course_menu() .
1003 public function test_groups_allgroups_course_menu() {
1006 $this->resetAfterTest();
1009 $course = $this->getDataGenerator()->create_course();
1010 $record = new \
stdClass();
1011 $record->courseid
= $course->id
;
1012 $group1 = $this->getDataGenerator()->create_group($record);
1013 $group2 = $this->getDataGenerator()->create_group($record);
1014 $user = $this->getDataGenerator()->create_user();
1015 $this->getDataGenerator()->enrol_user($user->id
, $course->id
);
1016 $this->setUser($user);
1018 $html = groups_allgroups_course_menu($course, 'someurl.php');
1019 // Since user is not a part of this group and doesn't have accessallgroups permission,
1020 // the html should be empty.
1021 $this->assertEmpty($html);
1023 groups_add_member($group1->id
, $user);
1024 // 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
1025 // partial match to see if all groups are listed or not.
1026 $html = groups_allgroups_course_menu($course, 'someurl.php');
1027 $this->assertStringContainsString(format_string($group1->name
), $html);
1028 $this->assertStringNotContainsString(format_string($group2->name
), $html);
1030 $this->setAdminUser();
1032 // Now user can access everything.
1033 $html = groups_allgroups_course_menu($course, 'someurl.php');
1034 $this->assertStringContainsString(format_string($group1->name
), $html);
1035 $this->assertStringContainsString(format_string($group2->name
), $html);
1037 // Make sure separate groups mode, doesn't change anything.
1038 $course->groupmode
= SEPARATEGROUPS
;
1039 update_course($course);
1040 $html = groups_allgroups_course_menu($course, 'someurl.php');
1041 $this->assertStringContainsString(format_string($group1->name
), $html);
1042 $this->assertStringContainsString(format_string($group2->name
), $html);
1044 // Make sure Visible groups mode, doesn't change anything.
1045 $course->groupmode
= VISIBLEGROUPS
;
1046 update_course($course);
1047 $html = groups_allgroups_course_menu($course, 'someurl.php');
1048 $this->assertStringContainsString(format_string($group1->name
), $html);
1049 $this->assertStringContainsString(format_string($group2->name
), $html);
1051 // Let us test activegroup changes now.
1052 $this->setUser($user);
1053 $SESSION->activegroup
[$course->id
][VISIBLEGROUPS
][$course->defaultgroupingid
] = 5;
1054 groups_allgroups_course_menu($course, 'someurl.php', false); // Do not update session.
1055 $this->assertSame(5, $SESSION->activegroup
[$course->id
][VISIBLEGROUPS
][$course->defaultgroupingid
]);
1056 groups_allgroups_course_menu($course, 'someurl.php', true, $group1->id
); // Update session.
1057 $this->assertSame($group1->id
, $SESSION->activegroup
[$course->id
][VISIBLEGROUPS
][$course->defaultgroupingid
]);
1058 // Try to update session with an invalid groupid. It should not accept the invalid id.
1059 groups_allgroups_course_menu($course, 'someurl.php', true, 256);
1060 $this->assertEquals($group1->id
, $SESSION->activegroup
[$course->id
][VISIBLEGROUPS
][$course->defaultgroupingid
]);
1064 * This unit test checks that groups_get_all_groups returns groups in
1065 * alphabetical order even if they are in a grouping.
1067 public function test_groups_ordering() {
1068 $generator = $this->getDataGenerator();
1069 $this->resetAfterTest();
1071 // Create a course category and course.
1072 $cat = $generator->create_category(array('parent' => 0));
1073 $course = $generator->create_course(array('category' => $cat->id
));
1074 $grouping = $generator->create_grouping(array('courseid' => $course->id
, 'name' => 'Grouping'));
1076 // Create groups in reverse order.
1077 $group2 = $generator->create_group(array('courseid' => $course->id
, 'name' => 'Group 2'));
1078 $group1 = $generator->create_group(array('courseid' => $course->id
, 'name' => 'Group 1'));
1080 // Assign the groups to the grouping in reverse order.
1081 $this->assertTrue(groups_assign_grouping($grouping->id
, $group2->id
));
1082 $this->assertTrue(groups_assign_grouping($grouping->id
, $group1->id
));
1084 // Get all groups and check they are alphabetical.
1085 $groups = array_values(groups_get_all_groups($course->id
, 0));
1086 $this->assertEquals('Group 1', $groups[0]->name
);
1087 $this->assertEquals('Group 2', $groups[1]->name
);
1089 // Now check the same is true when accessed by grouping.
1090 $groups = array_values(groups_get_all_groups($course->id
, 0, $grouping->id
));
1091 $this->assertEquals('Group 1', $groups[0]->name
);
1092 $this->assertEquals('Group 2', $groups[1]->name
);
1096 * Tests for groups_get_all_groups when grouping is set and we want members as well.
1098 public function test_groups_get_all_groups_in_grouping_with_members() {
1099 $generator = $this->getDataGenerator();
1100 $this->resetAfterTest();
1103 $course1 = $generator->create_course();
1104 $course2 = $generator->create_course();
1107 $c1user1 = $generator->create_user();
1108 $c12user1 = $generator->create_user();
1109 $c12user2 = $generator->create_user();
1112 $generator->enrol_user($c1user1->id
, $course1->id
);
1113 $generator->enrol_user($c12user1->id
, $course1->id
);
1114 $generator->enrol_user($c12user1->id
, $course2->id
);
1115 $generator->enrol_user($c12user2->id
, $course1->id
);
1116 $generator->enrol_user($c12user2->id
, $course2->id
);
1118 // Create groupings and groups for course1.
1119 $c1grouping1 = $generator->create_grouping(array('courseid' => $course1->id
));
1120 $c1grouping2 = $generator->create_grouping(array('courseid' => $course1->id
));
1121 $c1group1 = $generator->create_group(array('courseid' => $course1->id
));
1122 $c1group2 = $generator->create_group(array('courseid' => $course1->id
));
1123 $c1group3 = $generator->create_group(array('courseid' => $course1->id
));
1124 groups_assign_grouping($c1grouping1->id
, $c1group1->id
);
1125 groups_assign_grouping($c1grouping1->id
, $c1group2->id
);
1126 groups_assign_grouping($c1grouping2->id
, $c1group3->id
);
1128 // Create groupings and groups for course2.
1129 $c2grouping1 = $generator->create_grouping(array('courseid' => $course2->id
));
1130 $c2group1 = $generator->create_group(array('courseid' => $course1->id
));
1131 groups_assign_grouping($c2grouping1->id
, $c2group1->id
);
1133 // Assign users to groups.
1134 $generator->create_group_member(array('groupid' => $c1group1->id
, 'userid' => $c1user1->id
));
1135 $generator->create_group_member(array('groupid' => $c1group1->id
, 'userid' => $c12user1->id
));
1136 $generator->create_group_member(array('groupid' => $c1group2->id
, 'userid' => $c12user2->id
));
1137 $generator->create_group_member(array('groupid' => $c2group1->id
, 'userid' => $c12user2->id
));
1139 // Test without userid.
1140 $groups = groups_get_all_groups($course1->id
, null, $c1grouping1->id
, 'g.*', true);
1142 $this->assertEqualsCanonicalizing(
1143 [$c1group1->id
, $c1group2->id
],
1146 $this->assertEquals(
1147 [$c1user1->id
=> $c1user1->id
, $c12user1->id
=> $c12user1->id
],
1148 $groups[$c1group1->id
]->members
1150 $this->assertEquals(
1151 [$c12user2->id
=> $c12user2->id
],
1152 $groups[$c1group2->id
]->members
1155 // Test with userid.
1156 $groups = groups_get_all_groups($course1->id
, $c1user1->id
, $c1grouping1->id
, 'g.*', true);
1158 $this->assertEquals([$c1group1->id
], array_keys($groups));
1159 $this->assertEqualsCanonicalizing(
1160 [$c1user1->id
, $c12user1->id
],
1161 $groups[$c1group1->id
]->members
1166 * Tests for groups_get_user_groups() method.
1168 public function test_groups_get_user_groups() {
1169 $this->resetAfterTest(true);
1170 $generator = $this->getDataGenerator();
1173 $course1 = $generator->create_course();
1174 $course2 = $generator->create_course();
1177 $user1 = $generator->create_user();
1178 $user2 = $generator->create_user();
1179 $user3 = $generator->create_user();
1182 $generator->enrol_user($user1->id
, $course1->id
);
1183 $generator->enrol_user($user1->id
, $course2->id
);
1184 $generator->enrol_user($user2->id
, $course2->id
);
1185 $generator->enrol_user($user3->id
, $course2->id
);
1188 $group1 = $generator->create_group(array('courseid' => $course1->id
));
1189 $group2 = $generator->create_group(array('courseid' => $course2->id
));
1190 $group3 = $generator->create_group(array('courseid' => $course2->id
));
1192 // Assign users to groups.
1193 $this->assertTrue($generator->create_group_member(array('groupid' => $group1->id
, 'userid' => $user1->id
)));
1194 $this->assertTrue($generator->create_group_member(array('groupid' => $group2->id
, 'userid' => $user2->id
)));
1197 $usergroups1 = groups_get_user_groups($course1->id
, $user1->id
);
1198 $usergroups2 = groups_get_user_groups($course2->id
, $user2->id
);;
1200 // Assert return data.
1201 $this->assertEquals($group1->id
, $usergroups1[0][0]);
1202 $this->assertEquals($group2->id
, $usergroups2[0][0]);
1204 // Now, test with groupings.
1205 $grouping1 = $generator->create_grouping(array('courseid' => $course1->id
));
1206 $grouping2 = $generator->create_grouping(array('courseid' => $course2->id
));
1208 // Assign the groups to grouping.
1209 groups_assign_grouping($grouping1->id
, $group1->id
);
1210 groups_assign_grouping($grouping2->id
, $group2->id
);
1211 groups_assign_grouping($grouping2->id
, $group3->id
);
1213 // Test with grouping.
1214 $usergroups1 = groups_get_user_groups($course1->id
, $user1->id
);
1215 $usergroups2 = groups_get_user_groups($course2->id
, $user2->id
);
1216 $this->assertArrayHasKey($grouping1->id
, $usergroups1);
1217 $this->assertArrayHasKey($grouping2->id
, $usergroups2);
1219 // Test user without a group.
1220 $usergroups1 = groups_get_user_groups($course2->id
, $user3->id
);
1221 $this->assertCount(0, $usergroups1[0]);
1223 // Test with userid = 0.
1224 $usergroups1 = groups_get_user_groups($course1->id
, 0);
1225 $usergroups2 = groups_get_user_groups($course2->id
, 0);
1226 $this->assertCount(0, $usergroups1[0]);
1227 $this->assertCount(0, $usergroups2[0]);
1229 // Test with courseid = 0.
1230 $usergroups1 = groups_get_user_groups(0, $user1->id
);
1231 $usergroups2 = groups_get_user_groups(0, $user2->id
);
1232 $this->assertCount(0, $usergroups1[0]);
1233 $this->assertCount(0, $usergroups2[0]);
1237 * Create dummy groups array for use in menu tests
1238 * @param int $number
1241 protected function make_group_list($number) {
1242 $testgroups = array();
1243 for ($a = 0; $a < $number; $a++
) {
1244 $grp = new \
stdClass();
1245 $grp->id
= 100 +
$a;
1246 $grp->name
= 'test group ' . $grp->id
;
1247 $testgroups[$grp->id
] = $grp;
1252 public function test_groups_sort_menu_options_empty() {
1253 $this->assertEquals(array(), groups_sort_menu_options(array(), array()));
1256 public function test_groups_sort_menu_options_allowed_goups_only() {
1257 $this->assertEquals(array(
1258 100 => 'test group 100',
1259 101 => 'test group 101',
1260 ), groups_sort_menu_options($this->make_group_list(2), array()));
1263 public function test_groups_sort_menu_options_user_goups_only() {
1264 $this->assertEquals(array(
1265 100 => 'test group 100',
1266 101 => 'test group 101',
1267 ), groups_sort_menu_options(array(), $this->make_group_list(2)));
1270 public function test_groups_sort_menu_options_user_both() {
1271 $this->assertEquals(array(
1272 1 => array(get_string('mygroups', 'group') => array(
1273 100 => 'test group 100',
1274 101 => 'test group 101',
1276 2 => array(get_string('othergroups', 'group') => array(
1277 102 => 'test group 102',
1278 103 => 'test group 103',
1280 ), groups_sort_menu_options($this->make_group_list(4), $this->make_group_list(2)));
1283 public function test_groups_sort_menu_options_user_both_many_groups() {
1284 $this->assertEquals(array(
1285 1 => array(get_string('mygroups', 'group') => array(
1286 100 => 'test group 100',
1287 101 => 'test group 101',
1289 2 => array (get_string('othergroups', 'group') => array(
1290 102 => 'test group 102',
1291 103 => 'test group 103',
1292 104 => 'test group 104',
1293 105 => 'test group 105',
1294 106 => 'test group 106',
1295 107 => 'test group 107',
1296 108 => 'test group 108',
1297 109 => 'test group 109',
1298 110 => 'test group 110',
1299 111 => 'test group 111',
1300 112 => 'test group 112',
1302 ), groups_sort_menu_options($this->make_group_list(13), $this->make_group_list(2)));
1306 * Tests for groups_user_groups_visible.
1308 public function test_groups_user_groups_visible() {
1311 $generator = $this->getDataGenerator();
1312 $this->resetAfterTest();
1313 $this->setAdminUser();
1315 // Create a course category, course and groups.
1316 $cat = $generator->create_category(array('parent' => 0));
1317 $course = $generator->create_course(array('category' => $cat->id
));
1318 $coursecontext = \context_course
::instance($course->id
);
1319 $group1 = $generator->create_group(array('courseid' => $course->id
, 'name' => 'Group 1'));
1320 $group2 = $generator->create_group(array('courseid' => $course->id
, 'name' => 'Group 2'));
1321 $group3 = $generator->create_group(array('courseid' => $course->id
, 'name' => 'Group 3'));
1322 $group4 = $generator->create_group(array('courseid' => $course->id
, 'name' => 'Group 4'));
1325 $assign = $generator->create_module("assign", array('course' => $course->id
));
1326 $cm = get_coursemodule_from_instance("assign", $assign->id
);
1329 $user1 = $generator->create_user(); // Normal user.
1330 $user2 = $generator->create_user(); // Normal user.
1331 $user3 = $generator->create_user(); // Teacher, access all groups.
1332 $user4 = $generator->create_user(); // Normal user.
1334 // Enrol users into the course.
1335 $generator->enrol_user($user1->id
, $course->id
);
1336 $generator->enrol_user($user2->id
, $course->id
);
1337 $generator->enrol_user($user4->id
, $course->id
);
1340 // User1 and User4 share groups.
1341 groups_add_member($group1, $user1);
1342 groups_add_member($group2, $user2);
1343 groups_add_member($group1, $user4);
1345 // Give capability at course level to the user to access all groups.
1346 $role = $DB->get_field("role", "id", array("shortname" => "manager"));
1347 $generator->enrol_user($user3->id
, $course->id
, $role);
1348 // Make sure the user has the capability.
1349 assign_capability('moodle/site:accessallgroups', CAP_ALLOW
, $role, $coursecontext->id
);
1351 // Normal users in different groups.
1352 $this->setUser($user1);
1354 // No groups , not forced.
1355 $result = groups_user_groups_visible($course, $user2->id
);
1356 $this->assertTrue($result);
1358 $cm->groupmode
= NOGROUPS
;
1359 $result = groups_user_groups_visible($course, $user2->id
, $cm);
1360 $this->assertTrue($result); // Cm with no groups.
1362 $cm->groupmode
= SEPARATEGROUPS
;
1363 $result = groups_user_groups_visible($course, $user2->id
, $cm);
1364 $this->assertFalse($result); // Cm with separate groups.
1366 $cm->groupmode
= VISIBLEGROUPS
;
1367 $result = groups_user_groups_visible($course, $user2->id
, $cm);
1368 $this->assertTrue($result); // Cm with visible groups.
1370 // No groups, forced.
1371 $course->groupmode
= NOGROUPS
;
1372 $course->groupmodeforce
= true;
1373 update_course($course);
1374 $result = groups_user_groups_visible($course, $user2->id
);
1375 $this->assertTrue($result);
1377 $cm->groupmode
= NOGROUPS
;
1378 $result = groups_user_groups_visible($course, $user2->id
, $cm);
1379 $this->assertTrue($result); // Cm with no groups.
1381 $cm->groupmode
= SEPARATEGROUPS
;
1382 $result = groups_user_groups_visible($course, $user2->id
, $cm);
1383 $this->assertTrue($result); // Cm with separate groups.
1385 $cm->groupmode
= SEPARATEGROUPS
;
1386 $result = groups_user_groups_visible($course, $user2->id
);
1387 $this->assertTrue($result); // Cm with visible groups.
1389 // Visible groups, forced.
1390 $course->groupmode
= VISIBLEGROUPS
;
1391 $course->groupmodeforce
= true;
1392 update_course($course);
1393 $result = groups_user_groups_visible($course, $user2->id
);
1394 $this->assertTrue($result);
1396 $cm->groupmode
= NOGROUPS
;
1397 $result = groups_user_groups_visible($course, $user2->id
, $cm);
1398 $this->assertTrue($result); // Cm with no groups.
1400 $cm->groupmode
= SEPARATEGROUPS
;
1401 $result = groups_user_groups_visible($course, $user2->id
, $cm);
1402 $this->assertTrue($result); // Cm with separate groups.
1404 $cm->groupmode
= VISIBLEGROUPS
;
1405 $result = groups_user_groups_visible($course, $user2->id
, $cm);
1406 $this->assertTrue($result); // Cm with visible groups.
1408 // Visible groups, not forced.
1409 $course->groupmode
= VISIBLEGROUPS
;
1410 $course->groupmodeforce
= false;
1411 update_course($course);
1412 $result = groups_user_groups_visible($course, $user2->id
);
1413 $this->assertTrue($result);
1415 $cm->groupmode
= NOGROUPS
;
1416 $result = groups_user_groups_visible($course, $user2->id
, $cm);
1417 $this->assertTrue($result); // Cm with no groups.
1419 $cm->groupmode
= SEPARATEGROUPS
;
1420 $result = groups_user_groups_visible($course, $user2->id
, $cm);
1421 $this->assertFalse($result); // Cm with separate groups.
1423 $cm->groupmode
= VISIBLEGROUPS
;
1424 $result = groups_user_groups_visible($course, $user2->id
, $cm);
1425 $this->assertTrue($result); // Cm with visible groups.
1427 // Separate groups, forced.
1428 $course->groupmode
= SEPARATEGROUPS
;
1429 $course->groupmodeforce
= true;
1430 update_course($course);
1431 $result = groups_user_groups_visible($course, $user2->id
);
1432 $this->assertFalse($result);
1434 $result = groups_user_groups_visible($course, $user3->id
);
1435 $this->assertFalse($result); // Requesting all groups.
1437 $cm->groupmode
= NOGROUPS
;
1438 $result = groups_user_groups_visible($course, $user2->id
, $cm);
1439 $this->assertFalse($result); // Cm with no groups.
1441 $cm->groupmode
= SEPARATEGROUPS
;
1442 $result = groups_user_groups_visible($course, $user2->id
, $cm);
1443 $this->assertFalse($result); // Cm with separate groups.
1445 $result = groups_user_groups_visible($course, $user3->id
, $cm);
1446 $this->assertTrue($result);
1448 $cm->groupmode
= VISIBLEGROUPS
;
1449 $result = groups_user_groups_visible($course, $user2->id
, $cm);
1450 $this->assertFalse($result); // Cm with visible groups.
1452 // Separate groups, not forced.
1453 $course->groupmode
= SEPARATEGROUPS
;
1454 $course->groupmodeforce
= false;
1455 update_course($course);
1456 $result = groups_user_groups_visible($course, $user2->id
);
1457 $this->assertFalse($result);
1459 $result = groups_user_groups_visible($course, $user3->id
);
1460 $this->assertFalse($result); // Requesting all groups.
1462 $cm->groupmode
= NOGROUPS
;
1463 $result = groups_user_groups_visible($course, $user2->id
, $cm);
1464 $this->assertTrue($result); // Cm with no groups.
1466 $cm->groupmode
= SEPARATEGROUPS
;
1467 $result = groups_user_groups_visible($course, $user2->id
, $cm);
1468 $this->assertFalse($result); // Cm with separate groups.
1470 $cm->groupmode
= VISIBLEGROUPS
;
1471 $result = groups_user_groups_visible($course, $user2->id
, $cm);
1472 $this->assertTrue($result); // Cm with visible groups.
1474 // Users sharing groups.
1476 // No groups , not forced.
1477 $course->groupmode
= NOGROUPS
;
1478 $course->groupmodeforce
= false;
1479 update_course($course);
1481 $result = groups_user_groups_visible($course, $user4->id
);
1482 $this->assertTrue($result);
1484 $cm->groupmode
= NOGROUPS
;
1485 $result = groups_user_groups_visible($course, $user4->id
, $cm);
1486 $this->assertTrue($result); // Cm with no groups.
1488 $cm->groupmode
= SEPARATEGROUPS
;
1489 $result = groups_user_groups_visible($course, $user4->id
, $cm);
1490 $this->assertTrue($result); // Cm with separate groups.
1492 $cm->groupmode
= VISIBLEGROUPS
;
1493 $result = groups_user_groups_visible($course, $user4->id
, $cm);
1494 $this->assertTrue($result); // Cm with visible groups.
1496 // No groups, forced.
1497 $course->groupmode
= NOGROUPS
;
1498 $course->groupmodeforce
= true;
1499 update_course($course);
1500 $result = groups_user_groups_visible($course, $user4->id
);
1501 $this->assertTrue($result);
1503 $cm->groupmode
= NOGROUPS
;
1504 $result = groups_user_groups_visible($course, $user4->id
, $cm);
1505 $this->assertTrue($result); // Cm with no groups.
1507 $cm->groupmode
= SEPARATEGROUPS
;
1508 $result = groups_user_groups_visible($course, $user4->id
, $cm);
1509 $this->assertTrue($result); // Cm with separate groups.
1511 $cm->groupmode
= SEPARATEGROUPS
;
1512 $result = groups_user_groups_visible($course, $user4->id
, $cm);
1513 $this->assertTrue($result); // Cm with visible groups.
1515 // Visible groups, forced.
1516 $course->groupmode
= VISIBLEGROUPS
;
1517 $course->groupmodeforce
= true;
1518 update_course($course);
1519 $result = groups_user_groups_visible($course, $user4->id
);
1520 $this->assertTrue($result);
1522 $cm->groupmode
= NOGROUPS
;
1523 $result = groups_user_groups_visible($course, $user4->id
, $cm);
1524 $this->assertTrue($result); // Cm with no groups.
1526 $cm->groupmode
= SEPARATEGROUPS
;
1527 $result = groups_user_groups_visible($course, $user4->id
, $cm);
1528 $this->assertTrue($result); // Cm with separate groups.
1530 $cm->groupmode
= VISIBLEGROUPS
;
1531 $result = groups_user_groups_visible($course, $user4->id
, $cm);
1532 $this->assertTrue($result); // Cm with visible groups.
1534 // Visible groups, not forced.
1535 $course->groupmode
= VISIBLEGROUPS
;
1536 $course->groupmodeforce
= false;
1537 update_course($course);
1538 $result = groups_user_groups_visible($course, $user4->id
);
1539 $this->assertTrue($result);
1541 $cm->groupmode
= NOGROUPS
;
1542 $result = groups_user_groups_visible($course, $user4->id
, $cm);
1543 $this->assertTrue($result); // Cm with no groups.
1545 $cm->groupmode
= SEPARATEGROUPS
;
1546 $result = groups_user_groups_visible($course, $user4->id
, $cm);
1547 $this->assertTrue($result); // Cm with separate groups.
1549 $cm->groupmode
= VISIBLEGROUPS
;
1550 $result = groups_user_groups_visible($course, $user4->id
, $cm);
1551 $this->assertTrue($result); // Cm with visible groups.
1553 // Separate groups, forced.
1554 $course->groupmode
= SEPARATEGROUPS
;
1555 $course->groupmodeforce
= true;
1556 update_course($course);
1557 $result = groups_user_groups_visible($course, $user4->id
);
1558 $this->assertTrue($result);
1560 $result = groups_user_groups_visible($course, $user3->id
);
1561 $this->assertFalse($result); // Requesting all groups.
1563 $cm->groupmode
= NOGROUPS
;
1564 $result = groups_user_groups_visible($course, $user4->id
, $cm);
1565 $this->assertTrue($result); // Cm with no groups.
1567 $cm->groupmode
= SEPARATEGROUPS
;
1568 $result = groups_user_groups_visible($course, $user4->id
, $cm);
1569 $this->assertTrue($result); // Cm with separate groups.
1571 $result = groups_user_groups_visible($course, $user3->id
, $cm);
1572 $this->assertTrue($result);
1574 $cm->groupmode
= VISIBLEGROUPS
;
1575 $result = groups_user_groups_visible($course, $user4->id
, $cm);
1576 $this->assertTrue($result); // Cm with visible groups.
1578 // Separate groups, not forced.
1579 $course->groupmode
= SEPARATEGROUPS
;
1580 $course->groupmodeforce
= false;
1581 update_course($course);
1582 $result = groups_user_groups_visible($course, $user4->id
);
1583 $this->assertTrue($result);
1585 $result = groups_user_groups_visible($course, $user3->id
);
1586 $this->assertFalse($result); // Requesting all groups.
1588 $cm->groupmode
= NOGROUPS
;
1589 $result = groups_user_groups_visible($course, $user4->id
, $cm);
1590 $this->assertTrue($result); // Cm with no groups.
1592 $cm->groupmode
= SEPARATEGROUPS
;
1593 $result = groups_user_groups_visible($course, $user4->id
, $cm);
1594 $this->assertTrue($result); // Cm with separate groups.
1596 $cm->groupmode
= VISIBLEGROUPS
;
1597 $result = groups_user_groups_visible($course, $user4->id
, $cm);
1598 $this->assertTrue($result); // Cm with visible groups.
1600 // For teacher with access all groups.
1602 // No groups , not forced.
1603 $course->groupmode
= NOGROUPS
;
1604 $course->groupmodeforce
= false;
1605 update_course($course);
1607 $this->setUser($user3);
1609 $result = groups_user_groups_visible($course, $user1->id
);
1610 $this->assertTrue($result);
1611 $result = groups_user_groups_visible($course, $user1->id
);
1612 $this->assertTrue($result); // Requesting all groups.
1614 $cm->groupmode
= NOGROUPS
;
1615 $result = groups_user_groups_visible($course, $user1->id
, $cm);
1616 $this->assertTrue($result); // Cm with no groups.
1618 $cm->groupmode
= SEPARATEGROUPS
;
1619 $result = groups_user_groups_visible($course, $user1->id
, $cm);
1620 $this->assertTrue($result); // Cm with separate groups.
1621 $result = groups_user_groups_visible($course, $user2->id
, $cm);
1622 $this->assertTrue($result); // Cm with separate groups.
1624 $cm->groupmode
= VISIBLEGROUPS
;
1625 $result = groups_user_groups_visible($course, $user1->id
, $cm);
1626 $this->assertTrue($result); // Cm with visible groups.
1628 // No groups, forced.
1629 $course->groupmode
= NOGROUPS
;
1630 $course->groupmodeforce
= true;
1631 update_course($course);
1632 $result = groups_user_groups_visible($course, $user1->id
);
1633 $this->assertTrue($result);
1634 $result = groups_user_groups_visible($course, $user1->id
);
1635 $this->assertTrue($result); // Requesting all groups.
1637 $cm->groupmode
= NOGROUPS
;
1638 $result = groups_user_groups_visible($course, $user1->id
, $cm);
1639 $this->assertTrue($result); // Cm with no groups.
1641 $cm->groupmode
= SEPARATEGROUPS
;
1642 $result = groups_user_groups_visible($course, $user1->id
, $cm);
1643 $this->assertTrue($result); // Cm with separate groups.
1644 $result = groups_user_groups_visible($course, $user2->id
, $cm);
1645 $this->assertTrue($result); // Cm with separate groups.
1647 $cm->groupmode
= SEPARATEGROUPS
;
1648 $result = groups_user_groups_visible($course, $user1->id
, $cm);
1649 $this->assertTrue($result); // Cm with visible groups.
1651 // Visible groups, forced.
1652 $course->groupmode
= VISIBLEGROUPS
;
1653 $course->groupmodeforce
= true;
1654 update_course($course);
1655 $result = groups_user_groups_visible($course, $user1->id
);
1656 $this->assertTrue($result);
1657 $result = groups_user_groups_visible($course, $user1->id
);
1658 $this->assertTrue($result); // Requesting all groups.
1660 $cm->groupmode
= NOGROUPS
;
1661 $result = groups_user_groups_visible($course, $user1->id
, $cm);
1662 $this->assertTrue($result); // Cm with no groups.
1664 $cm->groupmode
= SEPARATEGROUPS
;
1665 $result = groups_user_groups_visible($course, $user1->id
, $cm);
1666 $this->assertTrue($result); // Cm with separate groups.
1667 $result = groups_user_groups_visible($course, $user2->id
, $cm);
1668 $this->assertTrue($result); // Cm with separate groups.
1670 $cm->groupmode
= VISIBLEGROUPS
;
1671 $result = groups_user_groups_visible($course, $user1->id
, $cm);
1672 $this->assertTrue($result); // Cm with visible groups.
1674 // Visible groups, not forced.
1675 $course->groupmode
= VISIBLEGROUPS
;
1676 $course->groupmodeforce
= false;
1677 update_course($course);
1678 $result = groups_user_groups_visible($course, $user1->id
);
1679 $this->assertTrue($result);
1680 $result = groups_user_groups_visible($course, $user1->id
);
1681 $this->assertTrue($result); // Requesting all groups.
1683 $cm->groupmode
= NOGROUPS
;
1684 $result = groups_user_groups_visible($course, $user1->id
, $cm);
1685 $this->assertTrue($result); // Cm with no groups.
1687 $cm->groupmode
= SEPARATEGROUPS
;
1688 $result = groups_user_groups_visible($course, $user1->id
, $cm);
1689 $this->assertTrue($result); // Cm with separate groups.
1690 $result = groups_user_groups_visible($course, $user2->id
, $cm);
1691 $this->assertTrue($result); // Cm with separate groups.
1693 $cm->groupmode
= VISIBLEGROUPS
;
1694 $result = groups_user_groups_visible($course, $user1->id
, $cm);
1695 $this->assertTrue($result); // Cm with visible groups.
1697 // Separate groups, forced.
1698 $course->groupmode
= SEPARATEGROUPS
;
1699 $course->groupmodeforce
= true;
1700 update_course($course);
1701 $result = groups_user_groups_visible($course, $user1->id
);
1702 $this->assertTrue($result);
1703 $result = groups_user_groups_visible($course, $user2->id
);
1704 $this->assertTrue($result);
1705 $result = groups_user_groups_visible($course, $user2->id
);
1706 $this->assertTrue($result); // Requesting all groups.
1707 $result = groups_user_groups_visible($course, $user3->id
);
1708 $this->assertTrue($result); // Requesting all groups.
1709 $result = groups_user_groups_visible($course, $user3->id
);
1710 $this->assertTrue($result);
1712 $cm->groupmode
= NOGROUPS
;
1713 $result = groups_user_groups_visible($course, $user1->id
, $cm);
1714 $this->assertTrue($result); // Cm with no groups.
1716 $cm->groupmode
= SEPARATEGROUPS
;
1717 $result = groups_user_groups_visible($course, $user1->id
, $cm);
1718 $this->assertTrue($result); // Cm with separate groups.
1719 $result = groups_user_groups_visible($course, $user2->id
, $cm);
1720 $this->assertTrue($result); // Cm with separate groups.
1721 $result = groups_user_groups_visible($course, $user3->id
, $cm);
1722 $this->assertTrue($result);
1724 $cm->groupmode
= VISIBLEGROUPS
;
1725 $result = groups_user_groups_visible($course, $user1->id
, $cm);
1726 $this->assertTrue($result); // Cm with visible groups.
1728 // Separate groups, not forced.
1729 $course->groupmode
= SEPARATEGROUPS
;
1730 $course->groupmodeforce
= false;
1731 update_course($course);
1732 $result = groups_user_groups_visible($course, $user1->id
);
1733 $this->assertTrue($result);
1734 $result = groups_user_groups_visible($course, $user2->id
);
1735 $this->assertTrue($result);
1736 $result = groups_user_groups_visible($course, $user2->id
);
1737 $this->assertTrue($result); // Requesting all groups.
1738 $result = groups_user_groups_visible($course, $user3->id
);
1739 $this->assertTrue($result); // Requesting all groups.
1741 $cm->groupmode
= NOGROUPS
;
1742 $result = groups_user_groups_visible($course, $user1->id
, $cm);
1743 $this->assertTrue($result); // Cm with no groups.
1745 $cm->groupmode
= SEPARATEGROUPS
;
1746 $result = groups_user_groups_visible($course, $user1->id
, $cm);
1747 $this->assertTrue($result); // Cm with separate groups.
1748 $result = groups_user_groups_visible($course, $user2->id
, $cm);
1749 $this->assertTrue($result); // Cm with separate groups.
1751 $cm->groupmode
= VISIBLEGROUPS
;
1752 $result = groups_user_groups_visible($course, $user1->id
, $cm);
1753 $this->assertTrue($result); // Cm with visible groups.
1757 * Tests for groups_get_groups_members() method.
1759 public function test_groups_get_groups_members() {
1760 $this->resetAfterTest(true);
1761 $generator = $this->getDataGenerator();
1764 $course1 = $generator->create_course();
1765 $course2 = $generator->create_course();
1768 $user1 = $generator->create_user();
1769 $user2 = $generator->create_user();
1770 $user3 = $generator->create_user();
1773 $generator->enrol_user($user1->id
, $course1->id
);
1774 $generator->enrol_user($user1->id
, $course2->id
);
1775 $generator->enrol_user($user2->id
, $course2->id
);
1776 $generator->enrol_user($user3->id
, $course2->id
);
1779 $group1 = $generator->create_group(array('courseid' => $course1->id
));
1780 $group2 = $generator->create_group(array('courseid' => $course2->id
));
1781 $group3 = $generator->create_group(array('courseid' => $course2->id
));
1783 // Assign users to groups.
1784 $this->assertTrue($generator->create_group_member(array('groupid' => $group1->id
, 'userid' => $user1->id
)));
1785 $this->assertTrue($generator->create_group_member(array('groupid' => $group2->id
, 'userid' => $user1->id
)));
1786 $this->assertTrue($generator->create_group_member(array('groupid' => $group2->id
, 'userid' => $user2->id
)));
1788 // Test get_groups_members (with extra field and ordering).
1789 $members = groups_get_groups_members([$group1->id
, $group2->id
], ['lastaccess'], 'u.id ASC');
1790 $this->assertCount(2, $members);
1791 $this->assertEquals([$user1->id
, $user2->id
], array_keys($members));
1792 $this->assertTrue(isset($members[$user1->id
]->lastaccess
));
1793 $this->assertTrue(isset($members[$user2->id
]->lastaccess
));
1795 // Group with just one.
1796 $members = groups_get_groups_members([$group1->id
]);
1797 $this->assertCount(1, $members);
1798 $this->assertEquals($user1->id
, $members[$user1->id
]->id
);
1800 // Test the info matches group membership for the entire course.
1801 $groups = groups_get_all_groups($course1->id
, 0, 0, 'g.*', true);
1802 $group1withmembers = array_pop($groups);
1804 // Compare the sorted keys of both arrays (should be list of user ids).
1805 $members = array_keys($members);
1807 $group1members = array_keys($group1withmembers->members
);
1808 sort($group1members);
1809 $this->assertEquals($members, $group1members);
1811 // Group with just one plus empty group.
1812 $members = groups_get_groups_members([$group1->id
, $group3->id
]);
1813 $this->assertCount(1, $members);
1814 $this->assertEquals($user1->id
, $members[$user1->id
]->id
);
1817 $members = groups_get_groups_members([$group3->id
]);
1818 $this->assertCount(0, $members);
1820 // Test groups_get_members.
1821 $members = groups_get_members($group2->id
, 'u.*', 'u.id ASC');
1822 $this->assertCount(2, $members);
1823 $this->assertEquals([$user1->id
, $user2->id
], array_keys($members));
1825 // Test the info matches group membership for the entire course.
1826 $groups = groups_get_all_groups($course2->id
, 0, 0, 'g.*', true);
1827 $group2withmembers = $groups[$group2->id
];
1829 // Compare the sorted keys of both arrays (should be list of user ids).
1830 $members = array_keys($members);
1832 $group2members = array_keys($group2withmembers->members
);
1833 sort($group2members);
1834 $this->assertEquals($members, $group2members);
1839 * Tests for groups_get_activity_shared_group_members() method.
1841 public function test_groups_get_activity_shared_group_members() {
1842 $this->resetAfterTest(true);
1843 $generator = $this->getDataGenerator();
1846 $course = $generator->create_course();
1849 $assign = $generator->create_module("assign", array('course' => $course->id
));
1850 $cm = get_coursemodule_from_instance("assign", $assign->id
);
1853 $user1 = $generator->create_user();
1854 $user2 = $generator->create_user();
1855 $user3 = $generator->create_user();
1856 $user4 = $generator->create_user();
1859 $generator->enrol_user($user1->id
, $course->id
);
1860 $generator->enrol_user($user2->id
, $course->id
);
1861 $generator->enrol_user($user3->id
, $course->id
);
1862 $generator->enrol_user($user4->id
, $course->id
);
1865 $group1 = $generator->create_group(array('courseid' => $course->id
));
1866 $group2 = $generator->create_group(array('courseid' => $course->id
));
1867 $group3 = $generator->create_group(array('courseid' => $course->id
));
1869 // Assign users to groups.
1870 $generator->create_group_member(array('groupid' => $group1->id
, 'userid' => $user1->id
));
1871 $generator->create_group_member(array('groupid' => $group2->id
, 'userid' => $user1->id
));
1872 $generator->create_group_member(array('groupid' => $group2->id
, 'userid' => $user2->id
));
1873 $generator->create_group_member(array('groupid' => $group3->id
, 'userid' => $user3->id
));
1875 // Retrieve users sharing groups with user1.
1876 $members = groups_get_activity_shared_group_members($cm, $user1->id
);
1877 $this->assertCount(2, $members);
1878 $this->assertEqualsCanonicalizing([$user1->id
, $user2->id
], array_keys($members));
1880 // Retrieve users sharing groups with user2.
1881 $members = groups_get_activity_shared_group_members($cm, $user2->id
);
1882 $this->assertCount(2, $members);
1883 $this->assertEqualsCanonicalizing([$user1->id
, $user2->id
], array_keys($members));
1885 // Retrieve users sharing groups with user3.
1886 $members = groups_get_activity_shared_group_members($cm, $user3->id
);
1887 $this->assertCount(1, $members);
1888 $this->assertEquals($user3->id
, $members[$user3->id
]->id
);
1890 // Retrieve users sharing groups with user without groups (user4).
1891 $members = groups_get_activity_shared_group_members($cm, $user4->id
);
1892 $this->assertCount(0, $members);
1894 // Now, create a different activity using groupings.
1895 $grouping = $generator->create_grouping(array('courseid' => $course->id
, 'name' => 'Grouping 1'));
1897 groups_assign_grouping($grouping->id
, $group1->id
);
1898 groups_assign_grouping($grouping->id
, $group3->id
);
1900 $assign = $generator->create_module("assign", array('course' => $course->id
, 'groupingid' => $grouping->id
));
1901 $cm = get_coursemodule_from_instance("assign", $assign->id
);
1903 // Since the activity is forced to groupings (groups 1 and 3), I don't see members of group 2.
1904 $members = groups_get_activity_shared_group_members($cm, $user1->id
);
1905 $this->assertCount(1, $members);
1906 $this->assertEquals($user1->id
, $members[$user1->id
]->id
);
1908 // Add user1 to group 3 (in the grouping).
1909 $generator->create_group_member(array('groupid' => $group3->id
, 'userid' => $user1->id
));
1910 $members = groups_get_activity_shared_group_members($cm, $user1->id
);
1911 $this->assertCount(2, $members); // Now I see members of group 3.
1912 $this->assertEqualsCanonicalizing([$user1->id
, $user3->id
], array_keys($members));
1916 * Generate a set of groups with different visibility levels and users to assign to them.
1920 protected function create_groups_with_visibilty(): array {
1921 $this->resetAfterTest(true);
1922 $generator = $this->getDataGenerator();
1925 $course = $generator->create_course();
1929 1 => $generator->create_user(),
1930 2 => $generator->create_user(),
1931 3 => $generator->create_user(),
1932 4 => $generator->create_user(),
1933 5 => $generator->create_user(),
1937 $generator->enrol_user($users[1]->id
, $course->id
);
1938 $generator->enrol_user($users[2]->id
, $course->id
);
1939 $generator->enrol_user($users[3]->id
, $course->id
);
1940 $generator->enrol_user($users[4]->id
, $course->id
);
1941 $generator->enrol_user($users[5]->id
, $course->id
, 'editingteacher');
1945 'all' => $generator->create_group(['courseid' => $course->id
, 'visibility' => GROUPS_VISIBILITY_ALL
]),
1946 'members' => $generator->create_group(['courseid' => $course->id
, 'visibility' => GROUPS_VISIBILITY_MEMBERS
]),
1947 'own' => $generator->create_group(['courseid' => $course->id
, 'visibility' => GROUPS_VISIBILITY_OWN
]),
1948 'none' => $generator->create_group(['courseid' => $course->id
, 'visibility' => GROUPS_VISIBILITY_NONE
]),
1959 * Tests getting groups and group members based on visibility settings.
1961 * This also covers the groupdata cache, since calls without $withmembers = true use the cache.
1963 * @covers \groups_get_all_groups()
1965 public function test_get_all_groups_with_visibility() {
1966 list($users, $groups, $course) = $this->create_groups_with_visibilty();
1968 // Assign users to groups.
1969 $generator = $this->getDataGenerator();
1970 $generator->create_group_member(['groupid' => $groups['all']->id
, 'userid' => $users[1]->id
]);
1971 $generator->create_group_member(['groupid' => $groups['members']->id
, 'userid' => $users[1]->id
]);
1972 $generator->create_group_member(['groupid' => $groups['members']->id
, 'userid' => $users[2]->id
]);
1973 $generator->create_group_member(['groupid' => $groups['own']->id
, 'userid' => $users[1]->id
]);
1974 $generator->create_group_member(['groupid' => $groups['own']->id
, 'userid' => $users[3]->id
]);
1975 $generator->create_group_member(['groupid' => $groups['none']->id
, 'userid' => $users[4]->id
]);
1977 $this->setUser($users[1]);
1978 $groups1 = groups_get_all_groups($course->id
);
1979 // User1 is in groups all, members and own, and can see them.
1980 $this->assertArrayHasKey($groups['all']->id
, $groups1);
1981 $this->assertArrayHasKey($groups['members']->id
, $groups1);
1982 $this->assertArrayHasKey($groups['own']->id
, $groups1);
1983 $this->assertArrayNotHasKey($groups['none']->id
, $groups1);
1984 // User1 can see members of groups all and members, but only themselves in group own.
1985 $groupsmembers1 = groups_get_all_groups($course->id
, 0, 0, 'g.*', true);
1986 $this->assertArrayHasKey($users[1]->id
, $groupsmembers1[$groups['all']->id
]->members
);
1987 $this->assertArrayHasKey($users[1]->id
, $groupsmembers1[$groups['members']->id
]->members
);
1988 $this->assertArrayHasKey($users[2]->id
, $groupsmembers1[$groups['members']->id
]->members
);
1989 $this->assertArrayHasKey($users[1]->id
, $groupsmembers1[$groups['own']->id
]->members
);
1990 $this->assertArrayNotHasKey($users[3]->id
, $groupsmembers1[$groups['own']->id
]->members
);
1992 $this->setUser($users[2]);
1993 $groups2 = groups_get_all_groups($course->id
);
1994 // User2 is in group members, and can see group all as well.
1995 $this->assertArrayHasKey($groups['all']->id
, $groups2);
1996 $this->assertArrayHasKey($groups['members']->id
, $groups2);
1997 $this->assertArrayNotHasKey($groups['own']->id
, $groups2);
1998 $this->assertArrayNotHasKey($groups['none']->id
, $groups2);
1999 // User2 can see members of groups all and members.
2000 $groupsmembers2 = groups_get_all_groups($course->id
, 0, 0, 'g.*', true);
2001 $this->assertArrayHasKey($users[1]->id
, $groupsmembers2[$groups['all']->id
]->members
);
2002 $this->assertArrayHasKey($users[1]->id
, $groupsmembers2[$groups['members']->id
]->members
);
2003 $this->assertArrayHasKey($users[2]->id
, $groupsmembers2[$groups['members']->id
]->members
);
2005 $this->setUser($users[3]);
2006 $groups3 = groups_get_all_groups($course->id
);
2007 // User3 is in group own, and can see group all as well.
2008 $this->assertArrayHasKey($groups['all']->id
, $groups3);
2009 $this->assertArrayNotHasKey($groups['members']->id
, $groups3);
2010 $this->assertArrayHasKey($groups['own']->id
, $groups3);
2011 $this->assertArrayNotHasKey($groups['none']->id
, $groups3);
2012 $groupsmembers3 = groups_get_all_groups($course->id
, 0, 0, 'g.*', true);
2013 // User3 can see members of group all, but only themselves in group own.
2014 $this->assertArrayHasKey($users[1]->id
, $groupsmembers3[$groups['all']->id
]->members
);
2015 $this->assertArrayHasKey($users[3]->id
, $groupsmembers3[$groups['own']->id
]->members
);
2016 $this->assertArrayNotHasKey($users[1]->id
, $groupsmembers3[$groups['own']->id
]->members
);
2018 $this->setUser($users[4]);
2019 $groups4 = groups_get_all_groups($course->id
);
2020 // User4 can see group all and its members. They are in group none but cannot see it.
2021 $this->assertArrayHasKey($groups['all']->id
, $groups4);
2022 $this->assertArrayNotHasKey($groups['members']->id
, $groups4);
2023 $this->assertArrayNotHasKey($groups['own']->id
, $groups4);
2024 $this->assertArrayNotHasKey($groups['none']->id
, $groups4);
2025 // User4 can see members of group all.
2026 $groupsmembers4 = groups_get_all_groups($course->id
, 0, 0, 'g.*', true);
2027 $this->assertArrayHasKey($users[1]->id
, $groupsmembers4[$groups['all']->id
]->members
);
2029 $this->setUser($users[5]);
2030 $groups5 = groups_get_all_groups($course->id
);
2031 // User5 is has viewallgroups, so can see all groups.
2032 $this->assertArrayHasKey($groups['all']->id
, $groups5);
2033 $this->assertArrayHasKey($groups['members']->id
, $groups5);
2034 $this->assertArrayHasKey($groups['own']->id
, $groups5);
2035 $this->assertArrayHasKey($groups['none']->id
, $groups5);
2036 // User5 is has viewallgroups, so can see all members.
2037 $groupsmembers5 = groups_get_all_groups($course->id
, 0, 0, 'g.*', true);
2038 $this->assertArrayHasKey($users[1]->id
, $groupsmembers5[$groups['all']->id
]->members
);
2039 $this->assertArrayHasKey($users[1]->id
, $groupsmembers5[$groups['members']->id
]->members
);
2040 $this->assertArrayHasKey($users[2]->id
, $groupsmembers5[$groups['members']->id
]->members
);
2041 $this->assertArrayHasKey($users[1]->id
, $groupsmembers5[$groups['own']->id
]->members
);
2042 $this->assertArrayHasKey($users[3]->id
, $groupsmembers5[$groups['own']->id
]->members
);
2043 $this->assertArrayHasKey($users[4]->id
, $groupsmembers5[$groups['none']->id
]->members
);
2048 * Tests getting groups the current user is a member of, with visibility settings applied.
2050 * @covers \groups_get_my_groups()
2052 public function test_get_my_groups_with_visibility() {
2053 list($users, $groups) = $this->create_groups_with_visibilty();
2055 // Assign users to groups.
2056 $generator = $this->getDataGenerator();
2057 $generator->create_group_member(['groupid' => $groups['all']->id
, 'userid' => $users[1]->id
]);
2058 $generator->create_group_member(['groupid' => $groups['members']->id
, 'userid' => $users[1]->id
]);
2059 $generator->create_group_member(['groupid' => $groups['own']->id
, 'userid' => $users[1]->id
]);
2060 $generator->create_group_member(['groupid' => $groups['none']->id
, 'userid' => $users[1]->id
]);
2061 $generator->create_group_member(['groupid' => $groups['all']->id
, 'userid' => $users[5]->id
]);
2062 $generator->create_group_member(['groupid' => $groups['members']->id
, 'userid' => $users[5]->id
]);
2063 $generator->create_group_member(['groupid' => $groups['own']->id
, 'userid' => $users[5]->id
]);
2064 $generator->create_group_member(['groupid' => $groups['none']->id
, 'userid' => $users[5]->id
]);
2066 $generator->role_assign('editingteacher', $users[5]->id
, \context_system
::instance());
2068 // User can see all groups they are in, except group with visibility::NONE.
2069 $this->setUser($users[1]);
2070 $groups1 = groups_get_my_groups();
2071 $this->assertCount(3, $groups1);
2072 $groupids1 = array_map(function($groupmember) {
2073 return $groupmember->groupid
;
2076 $this->assertEquals([$groups['all']->id
, $groups['members']->id
, $groups['own']->id
], $groupids1);
2078 $this->setUser($users[5]);
2079 $groups2 = groups_get_my_groups();
2080 $this->assertCount(4, $groups2);
2081 $groupids2 = array_map(function($groupmember) {
2082 return $groupmember->groupid
;
2085 $this->assertEquals([$groups['all']->id
, $groups['members']->id
, $groups['own']->id
, $groups['none']->id
], $groupids2);
2089 * Tests getting groups a user is a member of, with visibility settings applied.
2091 * @covers \groups_get_user_groups()
2093 public function test_get_user_groups_with_visibility() {
2094 list($users, $groups, $course) = $this->create_groups_with_visibilty();
2096 // Assign users to groups.
2097 $generator = $this->getDataGenerator();
2098 $generator->create_group_member(['groupid' => $groups['all']->id
, 'userid' => $users[1]->id
]);
2099 $generator->create_group_member(['groupid' => $groups['members']->id
, 'userid' => $users[1]->id
]);
2100 $generator->create_group_member(['groupid' => $groups['members']->id
, 'userid' => $users[2]->id
]);
2101 $generator->create_group_member(['groupid' => $groups['own']->id
, 'userid' => $users[1]->id
]);
2102 $generator->create_group_member(['groupid' => $groups['own']->id
, 'userid' => $users[3]->id
]);
2103 $generator->create_group_member(['groupid' => $groups['none']->id
, 'userid' => $users[4]->id
]);
2104 $generator->create_group_member(['groupid' => $groups['none']->id
, 'userid' => $users[1]->id
]);
2106 // Run as unprivileged user.
2107 $this->setUser($users[1]);
2108 // Own groups - should see all groups except group with visibility::NONE.
2109 $usergroups1 = groups_get_user_groups($course->id
, $users[1]->id
);
2110 $this->assertEquals([$groups['all']->id
, $groups['members']->id
, $groups['own']->id
], $usergroups1[0]);
2111 // Fellow member of a group with visiblity::MEMBERS. Should see that group.
2112 $usergroups2 = groups_get_user_groups($course->id
, $users[2]->id
);
2113 $this->assertEquals([$groups['members']->id
], $usergroups2[0]);
2114 // Fellow member of a group with visiblity::OWN. Should not see that group.
2115 $usergroups3 = groups_get_user_groups($course->id
, $users[3]->id
);
2116 $this->assertEmpty($usergroups3[0]);
2117 // Fellow member of a group with visiblity::NONE. Should not see that group.
2118 $usergroups4 = groups_get_user_groups($course->id
, $users[4]->id
);
2119 $this->assertEmpty($usergroups4[0]);
2121 // Run as a user with viewhiddengroups. Should see all group memberships for each member.
2122 $this->setUser($users[5]);
2123 $usergroups1 = groups_get_user_groups($course->id
, $users[1]->id
);
2124 $this->assertEquals([$groups['all']->id
, $groups['members']->id
, $groups['own']->id
, $groups['none']->id
], $usergroups1[0]);
2125 // Fellow member of a group with visiblity::MEMBERS. Should see that group.
2126 $usergroups2 = groups_get_user_groups($course->id
, $users[2]->id
);
2127 $this->assertEquals([$groups['members']->id
], $usergroups2[0]);
2128 // Fellow member of a group with visiblity::OWN. Should not see that group.
2129 $usergroups3 = groups_get_user_groups($course->id
, $users[3]->id
);
2130 $this->assertEquals([$groups['own']->id
], $usergroups3[0]);
2131 // Fellow member of a group with visiblity::NONE. Should not see that group.
2132 $usergroups4 = groups_get_user_groups($course->id
, $users[4]->id
);
2133 $this->assertEquals([$groups['none']->id
], $usergroups4[0]);
2138 * Test groups_is_member() using groups with different visibility settings.
2140 * @covers \groups_is_member()
2142 public function test_groups_is_member_with_visibility(): void
{
2143 list($users, $groups) = $this->create_groups_with_visibilty();
2145 // Assign users to groups.
2146 $generator = $this->getDataGenerator();
2147 $generator->create_group_member(['groupid' => $groups['all']->id
, 'userid' => $users[1]->id
]);
2148 $generator->create_group_member(['groupid' => $groups['members']->id
, 'userid' => $users[1]->id
]);
2149 $generator->create_group_member(['groupid' => $groups['members']->id
, 'userid' => $users[2]->id
]);
2150 $generator->create_group_member(['groupid' => $groups['own']->id
, 'userid' => $users[1]->id
]);
2151 $generator->create_group_member(['groupid' => $groups['own']->id
, 'userid' => $users[3]->id
]);
2152 $generator->create_group_member(['groupid' => $groups['none']->id
, 'userid' => $users[4]->id
]);
2153 $generator->create_group_member(['groupid' => $groups['none']->id
, 'userid' => $users[1]->id
]);
2155 // Run as unprivileged user.
2156 $this->setUser($users[1]);
2157 $this->assertTrue(groups_is_member($groups['all']->id
, $users[1]->id
)); // All can see members.
2158 $this->assertTrue(groups_is_member($groups['members']->id
, $users[1]->id
)); // Can see members.
2159 $this->assertTrue(groups_is_member($groups['own']->id
, $users[1]->id
)); // Can see own membership.
2160 $this->assertFalse(groups_is_member($groups['none']->id
, $users[1]->id
)); // Cannot see group.
2162 $this->assertFalse(groups_is_member($groups['all']->id
, $users[2]->id
)); // Not a member.
2163 $this->assertTrue(groups_is_member($groups['members']->id
, $users[2]->id
)); // Can see other members.
2164 $this->assertFalse(groups_is_member($groups['own']->id
, $users[3]->id
)); // Can only see own membership, not others.
2165 $this->assertFalse(groups_is_member($groups['none']->id
, $users[4]->id
)); // Cannot see group.
2167 // Run as a user not in group 1 or 2.
2168 $this->setUser($users[3]);
2169 $this->assertTrue(groups_is_member($groups['all']->id
, $users[1]->id
)); // All can see members.
2170 $this->assertFalse(groups_is_member($groups['members']->id
, $users[2]->id
)); // Cannot see members of the group.
2172 // Run as a user with viewhiddengroups. Should be able to see memberships that exist in any group.
2173 $this->setUser($users[5]);
2174 $this->assertTrue(groups_is_member($groups['all']->id
, $users[1]->id
));
2175 $this->assertTrue(groups_is_member($groups['members']->id
, $users[1]->id
));
2176 $this->assertTrue(groups_is_member($groups['own']->id
, $users[1]->id
));
2177 $this->assertTrue(groups_is_member($groups['none']->id
, $users[1]->id
));
2179 $this->assertFalse(groups_is_member($groups['all']->id
, $users[2]->id
)); // Not a member.
2180 $this->assertTrue(groups_is_member($groups['members']->id
, $users[2]->id
));
2181 $this->assertTrue(groups_is_member($groups['own']->id
, $users[3]->id
));
2182 $this->assertTrue(groups_is_member($groups['none']->id
, $users[4]->id
));
2186 * Test groups_get_members() using groups with different visibility settings.
2188 * @covers \groups_get_members()
2190 public function test_groups_get_members_with_visibility(): void
{
2191 list($users, $groups) = $this->create_groups_with_visibilty();
2193 // Assign users to groups.
2194 $generator = $this->getDataGenerator();
2195 $generator->create_group_member(['groupid' => $groups['all']->id
, 'userid' => $users[1]->id
]);
2196 $generator->create_group_member(['groupid' => $groups['members']->id
, 'userid' => $users[1]->id
]);
2197 $generator->create_group_member(['groupid' => $groups['members']->id
, 'userid' => $users[2]->id
]);
2198 $generator->create_group_member(['groupid' => $groups['own']->id
, 'userid' => $users[1]->id
]);
2199 $generator->create_group_member(['groupid' => $groups['own']->id
, 'userid' => $users[3]->id
]);
2200 $generator->create_group_member(['groupid' => $groups['none']->id
, 'userid' => $users[4]->id
]);
2201 $generator->create_group_member(['groupid' => $groups['none']->id
, 'userid' => $users[1]->id
]);
2203 // Run as unprivileged user.
2204 $this->setUser($users[1]);
2205 $this->assertEquals([$users[1]->id
],
2206 array_keys(groups_get_members($groups['all']->id
, 'u.id', 'u.id'))); // All can see members.
2207 $this->assertEquals([$users[1]->id
, $users[2]->id
],
2208 array_keys(groups_get_members($groups['members']->id
, 'u.id', 'u.id'))); // Can see members.
2209 $this->assertEquals([$users[1]->id
],
2210 array_keys(groups_get_members($groups['own']->id
, 'u.id', 'u.id'))); // Can see own membership.
2211 $this->assertEquals([], array_keys(groups_get_members($groups['none']->id
, 'u.id', 'u.id'))); // Cannot see group.
2213 // Run as a user not in group 1 or 2.
2214 $this->setUser($users[3]);
2215 $this->assertEquals([$users[1]->id
],
2216 array_keys(groups_get_members($groups['all']->id
, 'u.id', 'u.id'))); // All can see members.
2217 $this->assertEquals([], array_keys(groups_get_members($groups['members']->id
, 'u.id', 'u.id'))); // Cannot see members.
2219 // Run as a user with viewhiddengroups. Should be able to see memberships that exist in any group.
2220 $this->setUser($users[5]);
2221 $this->assertEquals([$users[1]->id
], array_keys(groups_get_members($groups['all']->id
, 'u.id', 'u.id')));
2222 $this->assertEquals([$users[1]->id
, $users[2]->id
],
2223 array_keys(groups_get_members($groups['members']->id
, 'u.id', 'u.id')));
2224 $this->assertEquals([$users[1]->id
, $users[3]->id
],
2225 array_keys(groups_get_members($groups['own']->id
, 'u.id', 'u.id')));
2226 $this->assertEquals([$users[1]->id
, $users[4]->id
],
2227 array_keys(groups_get_members($groups['none']->id
, 'u.id', 'u.id')));
2231 * Test groups_get_groups_members() using groups with different visibility settings.
2233 * @covers \groups_get_groups_members()
2235 public function test_groups_get_groups_members_with_visibility(): void
{
2236 list($users, $groups) = $this->create_groups_with_visibilty();
2238 // Assign users to groups.
2239 $generator = $this->getDataGenerator();
2240 $generator->create_group_member(['groupid' => $groups['all']->id
, 'userid' => $users[1]->id
]);
2241 $generator->create_group_member(['groupid' => $groups['members']->id
, 'userid' => $users[1]->id
]);
2242 $generator->create_group_member(['groupid' => $groups['members']->id
, 'userid' => $users[2]->id
]);
2243 $generator->create_group_member(['groupid' => $groups['own']->id
, 'userid' => $users[1]->id
]);
2244 $generator->create_group_member(['groupid' => $groups['own']->id
, 'userid' => $users[3]->id
]);
2245 $generator->create_group_member(['groupid' => $groups['none']->id
, 'userid' => $users[4]->id
]);
2246 $generator->create_group_member(['groupid' => $groups['none']->id
, 'userid' => $users[1]->id
]);
2248 $groupids = [$groups['all']->id
, $groups['members']->id
, $groups['own']->id
, $groups['none']->id
];
2250 $this->setUser($users[1]);
2251 // Can see self in group1/3, other users in group2.
2252 $this->assertEquals([$users[1]->id
, $users[2]->id
], array_keys(groups_get_groups_members($groupids, null, 'id ASC')));
2254 $this->setUser($users[2]);
2255 // Can see self in group2, user1 from group1/2.
2256 $this->assertEquals([$users[1]->id
, $users[2]->id
], array_keys(groups_get_groups_members($groupids, null, 'id ASC')));
2258 $this->setUser($users[3]);
2259 // Can see self in group3, user1 from group1.
2260 $this->assertEquals([$users[1]->id
, $users[3]->id
], array_keys(groups_get_groups_members($groupids, null, 'id ASC')));
2262 $this->setUser($users[4]);
2263 // Can see user1 from group1, cannot see self in group4.
2264 $this->assertEquals([$users[1]->id
], array_keys(groups_get_groups_members($groupids, null, 'id ASC')));
2266 $this->setUser($users[5]);
2267 // Can see all users from all groups.
2268 $this->assertEquals([$users[1]->id
, $users[2]->id
, $users[3]->id
, $users[4]->id
],
2269 array_keys(groups_get_groups_members($groupids, null, 'id ASC')));
2273 * Only groups with participation == true should be returned for an activity.
2275 * @covers \groups_get_activity_allowed_groups()
2277 * @throws \coding_exception
2279 public function test_groups_get_activity_allowed_groups(): void
{
2280 $this->resetAfterTest(true);
2281 $generator = $this->getDataGenerator();
2284 $course = $generator->create_course();
2287 $user = $generator->create_user();
2290 $generator->enrol_user($user->id
, $course->id
);
2294 'all-p' => $generator->create_group(['courseid' => $course->id
, 'visibility' => GROUPS_VISIBILITY_ALL
]),
2295 'members-p' => $generator->create_group(['courseid' => $course->id
, 'visibility' => GROUPS_VISIBILITY_MEMBERS
]),
2296 'all-n' => $generator->create_group([
2297 'courseid' => $course->id
,
2298 'visibility' => GROUPS_VISIBILITY_ALL
,
2299 'participation' => false
2301 'members-n' => $generator->create_group([
2302 'courseid' => $course->id
,
2303 'visibility' => GROUPS_VISIBILITY_MEMBERS
,
2304 'participation' => false
2306 'own' => $generator->create_group(['courseid' => $course->id
, 'visibility' => GROUPS_VISIBILITY_OWN
]),
2307 'none' => $generator->create_group(['courseid' => $course->id
, 'visibility' => GROUPS_VISIBILITY_NONE
]),
2309 // Add user to all groups.
2310 $generator->create_group_member(['groupid' => $groups['all-p']->id
, 'userid' => $user->id
]);
2311 $generator->create_group_member(['groupid' => $groups['members-p']->id
, 'userid' => $user->id
]);
2312 $generator->create_group_member(['groupid' => $groups['all-n']->id
, 'userid' => $user->id
]);
2313 $generator->create_group_member(['groupid' => $groups['members-n']->id
, 'userid' => $user->id
]);
2314 $generator->create_group_member(['groupid' => $groups['own']->id
, 'userid' => $user->id
]);
2315 $generator->create_group_member(['groupid' => $groups['none']->id
, 'userid' => $user->id
]);
2317 $module = $generator->create_module('forum', ['course' => $course->id
]);
2318 $cm = get_fast_modinfo($course)->get_cm($module->cmid
);
2320 $activitygroups = groups_get_activity_allowed_groups($cm, $user->id
);
2322 $this->assertContains((int)$groups['all-p']->id
, array_keys($activitygroups));
2323 $this->assertContains((int)$groups['members-p']->id
, array_keys($activitygroups));
2324 $this->assertNotContains((int)$groups['all-n']->id
, array_keys($activitygroups));
2325 $this->assertNotContains((int)$groups['members-n']->id
, array_keys($activitygroups));
2326 $this->assertNotContains((int)$groups['own']->id
, array_keys($activitygroups));
2327 $this->assertNotContains((int)$groups['none']->id
, array_keys($activitygroups));