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/>.
18 * Tests groups subsystems.
22 * @copyright 2007 onwards Martin Dougiamas (http://dougiamas.com)
23 * @author Andrew Nicols
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 defined('MOODLE_INTERNAL') ||
die();
30 * Unit tests for lib/grouplib.php
33 class core_grouplib_testcase
extends advanced_testcase
{
35 public function test_groups_get_group_by_idnumber() {
36 $this->resetAfterTest(true);
38 $generator = $this->getDataGenerator();
40 // Create a course category and course.
41 $cat = $generator->create_category(array('parent' => 0));
42 $course = $generator->create_course(array('category' => $cat->id
));
44 $idnumber1 = 'idnumber1';
45 $idnumber2 = 'idnumber2';
48 * Test with an empty and a null idnumber.
50 // An empty idnumber should always return a false value.
51 $this->assertFalse(groups_get_group_by_idnumber($course->id
, ''));
52 $this->assertFalse(groups_get_group_by_idnumber($course->id
, null));
54 // Even when a group exists which also has an empty idnumber.
55 $generator->create_group(array('courseid' => $course->id
));
56 $this->assertFalse(groups_get_group_by_idnumber($course->id
, ''));
57 $this->assertFalse(groups_get_group_by_idnumber($course->id
, null));
60 * Test with a valid idnumber.
62 // There is no matching idnumber at present.
63 $this->assertFalse(groups_get_group_by_idnumber($course->id
, $idnumber1));
65 // We should now have a valid group returned by the idnumber search.
66 $group = $generator->create_group(array('courseid' => $course->id
, 'idnumber' => $idnumber1));
67 $this->assertEquals($group, groups_get_group_by_idnumber($course->id
, $idnumber1));
69 // An empty idnumber should still return false.
70 $this->assertFalse(groups_get_group_by_idnumber($course->id
, ''));
71 $this->assertFalse(groups_get_group_by_idnumber($course->id
, null));
74 * Test with another idnumber.
76 // There is no matching idnumber at present.
77 $this->assertFalse(groups_get_group_by_idnumber($course->id
, $idnumber2));
79 // We should now have a valid group returned by the idnumber search.
80 $group = $generator->create_group(array('courseid' => $course->id
, 'idnumber' => $idnumber2));
81 $this->assertEquals($group, groups_get_group_by_idnumber($course->id
, $idnumber2));
84 * Group idnumbers are unique within a course so test that we don't
85 * retrieve groups for the first course.
88 // Create a second course.
89 $course = $generator->create_course(array('category' => $cat->id
));
91 // An empty idnumber should always return a false value.
92 $this->assertFalse(groups_get_group_by_idnumber($course->id
, ''));
93 $this->assertFalse(groups_get_group_by_idnumber($course->id
, null));
95 // Our existing idnumbers shouldn't be returned here as we're in a different course.
96 $this->assertFalse(groups_get_group_by_idnumber($course->id
, $idnumber1));
97 $this->assertFalse(groups_get_group_by_idnumber($course->id
, $idnumber2));
99 // We should be able to reuse the idnumbers again since this is a different course.
100 $group = $generator->create_group(array('courseid' => $course->id
, 'idnumber' => $idnumber1));
101 $this->assertEquals($group, groups_get_group_by_idnumber($course->id
, $idnumber1));
103 $group = $generator->create_group(array('courseid' => $course->id
, 'idnumber' => $idnumber2));
104 $this->assertEquals($group, groups_get_group_by_idnumber($course->id
, $idnumber2));
107 public function test_groups_get_grouping_by_idnumber() {
108 $this->resetAfterTest(true);
110 $generator = $this->getDataGenerator();
112 // Create a course category and course.
113 $cat = $generator->create_category(array('parent' => 0));
114 $course = $generator->create_course(array('category' => $cat->id
));
116 $idnumber1 = 'idnumber1';
117 $idnumber2 = 'idnumber2';
120 * Test with an empty and a null idnumber.
122 // An empty idnumber should always return a false value.
123 $this->assertFalse(groups_get_grouping_by_idnumber($course->id
, ''));
124 $this->assertFalse(groups_get_grouping_by_idnumber($course->id
, null));
126 // Even when a grouping exists which also has an empty idnumber.
127 $generator->create_grouping(array('courseid' => $course->id
));
128 $this->assertFalse(groups_get_grouping_by_idnumber($course->id
, ''));
129 $this->assertFalse(groups_get_grouping_by_idnumber($course->id
, null));
132 * Test with a valid idnumber
134 // There is no matching idnumber at present.
135 $this->assertFalse(groups_get_grouping_by_idnumber($course->id
, $idnumber1));
137 // We should now have a valid group returned by the idnumber search.
138 $grouping = $generator->create_grouping(array('courseid' => $course->id
, 'idnumber' => $idnumber1));
139 $this->assertEquals($grouping, groups_get_grouping_by_idnumber($course->id
, $idnumber1));
141 // An empty idnumber should still return false.
142 $this->assertFalse(groups_get_grouping_by_idnumber($course->id
, ''));
143 $this->assertFalse(groups_get_grouping_by_idnumber($course->id
, null));
146 * Test with another idnumber.
148 // There is no matching idnumber at present.
149 $this->assertFalse(groups_get_grouping_by_idnumber($course->id
, $idnumber2));
151 // We should now have a valid grouping returned by the idnumber search.
152 $grouping = $generator->create_grouping(array('courseid' => $course->id
, 'idnumber' => $idnumber2));
153 $this->assertEquals($grouping, groups_get_grouping_by_idnumber($course->id
, $idnumber2));
156 * Grouping idnumbers are unique within a course so test that we don't
157 * retrieve groupings for the first course.
160 // Create a second course.
161 $course = $generator->create_course(array('category' => $cat->id
));
163 // An empty idnumber should always return a false value.
164 $this->assertFalse(groups_get_grouping_by_idnumber($course->id
, ''));
165 $this->assertFalse(groups_get_grouping_by_idnumber($course->id
, null));
167 // Our existing idnumbers shouldn't be returned here as we're in a different course.
168 $this->assertFalse(groups_get_grouping_by_idnumber($course->id
, $idnumber1));
169 $this->assertFalse(groups_get_grouping_by_idnumber($course->id
, $idnumber2));
171 // We should be able to reuse the idnumbers again since this is a different course.
172 $grouping = $generator->create_grouping(array('courseid' => $course->id
, 'idnumber' => $idnumber1));
173 $this->assertEquals($grouping, groups_get_grouping_by_idnumber($course->id
, $idnumber1));
175 $grouping = $generator->create_grouping(array('courseid' => $course->id
, 'idnumber' => $idnumber2));
176 $this->assertEquals($grouping, groups_get_grouping_by_idnumber($course->id
, $idnumber2));
179 public function test_groups_get_group_by_name() {
180 $this->resetAfterTest(true);
182 $generator = $this->getDataGenerator();
184 // Create a course category and course.
185 $cat = $generator->create_category(array('parent' => 0));
186 $course = $generator->create_course(array('category' => $cat->id
));
191 // Test with an empty and a null idnumber.
192 $this->assertFalse(groups_get_group_by_name($course->id
, ''));
193 $this->assertFalse(groups_get_group_by_name($course->id
, null));
195 // Even when a group exists.
196 $generator->create_group(array('courseid' => $course->id
));
197 $this->assertFalse(groups_get_group_by_name($course->id
, ''));
198 $this->assertFalse(groups_get_group_by_name($course->id
, null));
200 // Test with a valid name, but one that doesn't exist yet.
201 $this->assertFalse(groups_get_group_by_name($course->id
, $name1));
202 $this->assertFalse(groups_get_group_by_name($course->id
, $name2));
204 // We should now have a valid group returned by the name search.
205 $group1 = $generator->create_group(array('courseid' => $course->id
, 'name' => $name1));
206 $this->assertEquals($group1->id
, groups_get_group_by_name($course->id
, $name1));
207 $this->assertFalse(groups_get_group_by_name($course->id
, $name2));
209 // We should now have a two valid groups returned by the name search.
210 $group2 = $generator->create_group(array('courseid' => $course->id
, 'name' => $name2));
211 $this->assertEquals($group1->id
, groups_get_group_by_name($course->id
, $name1));
212 $this->assertEquals($group2->id
, groups_get_group_by_name($course->id
, $name2));
215 $this->assertTrue(groups_delete_group($group1));
216 $this->assertFalse(groups_get_group_by_name($course->id
, $name1));
217 $this->assertEquals($group2->id
, groups_get_group_by_name($course->id
, $name2));
220 * Group idnumbers are unique within a course so test that we don't
221 * retrieve groups for the first course.
224 // Create a second course.
225 $course = $generator->create_course(array('category' => $cat->id
));
227 // An empty name should always return a false value.
228 $this->assertFalse(groups_get_group_by_name($course->id
, ''));
229 $this->assertFalse(groups_get_group_by_name($course->id
, null));
231 // Our existing names shouldn't be returned here as we're in a different course.
232 $this->assertFalse(groups_get_group_by_name($course->id
, $name1));
233 $this->assertFalse(groups_get_group_by_name($course->id
, $name2));
235 // We should be able to reuse the idnumbers again since this is a different course.
236 $group1 = $generator->create_group(array('courseid' => $course->id
, 'name' => $name1));
237 $this->assertEquals($group1->id
, groups_get_group_by_name($course->id
, $name1));
239 $group2 = $generator->create_group(array('courseid' => $course->id
, 'name' => $name2));
240 $this->assertEquals($group2->id
, groups_get_group_by_name($course->id
, $name2));
243 public function test_groups_get_grouping() {
244 $this->resetAfterTest(true);
246 $generator = $this->getDataGenerator();
248 // Create a course category and course.
249 $cat = $generator->create_category(array('parent' => 0));
250 $course = $generator->create_course(array('category' => $cat->id
));
252 $name1 = 'Grouping 1';
253 $name2 = 'Grouping 2';
255 // Test with an empty and a null idnumber.
256 $this->assertFalse(groups_get_grouping_by_name($course->id
, ''));
257 $this->assertFalse(groups_get_grouping_by_name($course->id
, null));
259 // Even when a group exists.
260 $generator->create_group(array('courseid' => $course->id
));
261 $this->assertFalse(groups_get_grouping_by_name($course->id
, ''));
262 $this->assertFalse(groups_get_grouping_by_name($course->id
, null));
264 // Test with a valid name, but one that doesn't exist yet.
265 $this->assertFalse(groups_get_grouping_by_name($course->id
, $name1));
266 $this->assertFalse(groups_get_grouping_by_name($course->id
, $name2));
268 // We should now have a valid group returned by the name search.
269 $group1 = $generator->create_grouping(array('courseid' => $course->id
, 'name' => $name1));
270 $this->assertEquals($group1->id
, groups_get_grouping_by_name($course->id
, $name1));
271 $this->assertFalse(groups_get_grouping_by_name($course->id
, $name2));
273 // We should now have a two valid groups returned by the name search.
274 $group2 = $generator->create_grouping(array('courseid' => $course->id
, 'name' => $name2));
275 $this->assertEquals($group1->id
, groups_get_grouping_by_name($course->id
, $name1));
276 $this->assertEquals($group2->id
, groups_get_grouping_by_name($course->id
, $name2));
279 $this->assertTrue(groups_delete_grouping($group1));
280 $this->assertFalse(groups_get_grouping_by_name($course->id
, $name1));
281 $this->assertEquals($group2->id
, groups_get_grouping_by_name($course->id
, $name2));
284 * Group idnumbers are unique within a course so test that we don't
285 * retrieve groups for the first course.
288 // Create a second course.
289 $course = $generator->create_course(array('category' => $cat->id
));
291 // An empty name should always return a false value.
292 $this->assertFalse(groups_get_grouping_by_name($course->id
, ''));
293 $this->assertFalse(groups_get_grouping_by_name($course->id
, null));
295 // Our existing names shouldn't be returned here as we're in a different course.
296 $this->assertFalse(groups_get_grouping_by_name($course->id
, $name1));
297 $this->assertFalse(groups_get_grouping_by_name($course->id
, $name2));
299 // We should be able to reuse the idnumbers again since this is a different course.
300 $group1 = $generator->create_grouping(array('courseid' => $course->id
, 'name' => $name1));
301 $this->assertEquals($group1->id
, groups_get_grouping_by_name($course->id
, $name1));
303 $group2 = $generator->create_grouping(array('courseid' => $course->id
, 'name' => $name2));
304 $this->assertEquals($group2->id
, groups_get_grouping_by_name($course->id
, $name2));
307 public function test_groups_get_course_data() {
308 $this->resetAfterTest(true);
310 $generator = $this->getDataGenerator();
312 // Create a course category and course.
313 $cat = $generator->create_category(array('parent' => 0));
314 $course = $generator->create_course(array('category' => $cat->id
));
315 $grouping1 = $generator->create_grouping(array('courseid' => $course->id
, 'name' => 'Grouping 1'));
316 $grouping2 = $generator->create_grouping(array('courseid' => $course->id
, 'name' => 'Grouping 2'));
317 $group1 = $generator->create_group(array('courseid' => $course->id
, 'name' => 'Group 1'));
318 $group2 = $generator->create_group(array('courseid' => $course->id
, 'name' => 'Group 2'));
319 $group3 = $generator->create_group(array('courseid' => $course->id
, 'name' => 'Group 3'));
320 $group4 = $generator->create_group(array('courseid' => $course->id
, 'name' => 'Group 4'));
322 // Assign the groups to groupings.
323 $this->assertTrue(groups_assign_grouping($grouping1->id
, $group1->id
));
324 $this->assertTrue(groups_assign_grouping($grouping1->id
, $group2->id
));
325 $this->assertTrue(groups_assign_grouping($grouping2->id
, $group3->id
));
326 $this->assertTrue(groups_assign_grouping($grouping2->id
, $group4->id
));
329 $data = groups_get_course_data($course->id
);
330 $this->assertInstanceOf('stdClass', $data);
331 $this->assertObjectHasAttribute('groups', $data);
332 $this->assertObjectHasAttribute('groupings', $data);
333 $this->assertObjectHasAttribute('mappings', $data);
335 // Test we have the expected items returns.
336 $this->assertCount(4, $data->groups
);
337 $this->assertCount(2, $data->groupings
);
338 $this->assertCount(4, $data->mappings
);
340 // Check we have the expected groups.
341 $this->assertArrayHasKey($group1->id
, $data->groups
);
342 $this->assertArrayHasKey($group2->id
, $data->groups
);
343 $this->assertArrayHasKey($group3->id
, $data->groups
);
344 $this->assertArrayHasKey($group4->id
, $data->groups
);
346 // Test a group-id is mapped correctly.
347 $this->assertSame($group3->name
, $data->groups
[$group3->id
]->name
);
349 // Check we have the expected number of groupings.
350 $this->assertContains($grouping1->id
, array_keys($data->groupings
));
351 $this->assertContains($grouping2->id
, array_keys($data->groupings
));
353 // Test a grouping-id is mapped correctly.
354 $this->assertEquals($grouping2->name
, $data->groupings
[$grouping2->id
]->name
);
356 // Test that all of the mappings are correct.
363 foreach ($data->mappings
as $mapping) {
364 if ($mapping->groupingid
=== $grouping1->id
) {
366 $this->assertContains($mapping->groupid
, array($group1->id
, $group2->id
));
367 } else if ($mapping->groupingid
=== $grouping2->id
) {
369 $this->assertContains($mapping->groupid
, array($group3->id
, $group4->id
));
371 $this->fail('Unexpected groupingid');
373 switch ($mapping->groupid
) {
374 case $group1->id
: $group1maps++
; break;
375 case $group2->id
: $group2maps++
; break;
376 case $group3->id
: $group3maps++
; break;
377 case $group4->id
: $group4maps++
; break;
380 $this->assertEquals(2, $grouping1maps);
381 $this->assertEquals(2, $grouping2maps);
382 $this->assertEquals(1, $group1maps);
383 $this->assertEquals(1, $group2maps);
384 $this->assertEquals(1, $group3maps);
385 $this->assertEquals(1, $group4maps);
387 // Test the groups_get_all_groups which uses this functionality.
388 $groups = groups_get_all_groups($course->id
);
389 $groupkeys = array_keys($groups);
390 $this->assertCount(4, $groups);
391 $this->assertContains($group1->id
, $groupkeys);
392 $this->assertContains($group2->id
, $groupkeys);
393 $this->assertContains($group3->id
, $groupkeys);
394 $this->assertContains($group4->id
, $groupkeys);
396 $groups = groups_get_all_groups($course->id
, null, $grouping1->id
);
397 $groupkeys = array_keys($groups);
398 $this->assertCount(2, $groups);
399 $this->assertContains($group1->id
, $groupkeys);
400 $this->assertContains($group2->id
, $groupkeys);
401 $this->assertNotContains($group3->id
, $groupkeys);
402 $this->assertNotContains($group4->id
, $groupkeys);
404 $groups = groups_get_all_groups($course->id
, null, $grouping2->id
);
405 $groupkeys = array_keys($groups);
406 $this->assertCount(2, $groups);
407 $this->assertNotContains($group1->id
, $groupkeys);
408 $this->assertNotContains($group2->id
, $groupkeys);
409 $this->assertContains($group3->id
, $groupkeys);
410 $this->assertContains($group4->id
, $groupkeys);
412 // Test this function using an alternate column for the result index
413 $groups = groups_get_all_groups($course->id
, null, $grouping2->id
, 'g.name, g.id');
414 $groupkeys = array_keys($groups);
415 $this->assertCount(2, $groups);
416 $this->assertNotContains($group3->id
, $groupkeys);
417 $this->assertContains($group3->name
, $groupkeys);
418 $this->assertEquals($group3->id
, $groups[$group3->name
]->id
);
422 * Tests for groups_group_visible.
424 public function test_groups_group_visible() {
427 $generator = $this->getDataGenerator();
428 $this->resetAfterTest();
429 $this->setAdminUser();
431 // Create a course category, course and groups.
432 $cat = $generator->create_category(array('parent' => 0));
433 $course = $generator->create_course(array('category' => $cat->id
));
434 $coursecontext = context_course
::instance($course->id
);
435 $group1 = $generator->create_group(array('courseid' => $course->id
, 'name' => 'Group 1'));
436 $group2 = $generator->create_group(array('courseid' => $course->id
, 'name' => 'Group 2'));
437 $group3 = $generator->create_group(array('courseid' => $course->id
, 'name' => 'Group 3'));
438 $group4 = $generator->create_group(array('courseid' => $course->id
, 'name' => 'Group 4'));
441 $assign = $generator->create_module("assign", array('course' => $course->id
));
442 $cm = get_coursemodule_from_instance("assign", $assign->id
);
445 $user1 = $generator->create_user();
446 $user2 = $generator->create_user();
447 $user3 = $generator->create_user();
449 // Enrol users into the course.
450 $generator->enrol_user($user1->id
, $course->id
);
451 $generator->enrol_user($user2->id
, $course->id
);
454 groups_add_member($group1, $user2);
456 // Give capability at course level to the user to access all groups.
457 $role = $DB->get_field("role", "id", array("shortname" => "manager"));
458 $generator->enrol_user($user3->id
, $course->id
, $role);
459 // Make sure the user has the capability.
460 assign_capability('moodle/site:accessallgroups', CAP_ALLOW
, $role, $coursecontext->id
);
462 // No groups , not forced.
463 $result = groups_group_visible($group1->id
, $course, null, $user1->id
);
464 $this->assertTrue($result);
465 $result = groups_group_visible(0, $course, null, $user1->id
);
466 $this->assertTrue($result); // Requesting all groups.
468 $result = groups_group_visible($group1->id
, $course, $cm, $user1->id
);
469 $this->assertTrue($result); // Cm with no groups.
471 $cm->groupmode
= SEPARATEGROUPS
;
472 $result = groups_group_visible($group1->id
, $course, $cm, $user1->id
);
473 $this->assertFalse($result); // Cm with separate groups.
474 $result = groups_group_visible($group1->id
, $course, $cm, $user2->id
);
475 $this->assertTrue($result); // Cm with separate groups.
477 $cm->groupmode
= VISIBLEGROUPS
;
478 $result = groups_group_visible($group1->id
, $course, $cm, $user1->id
);
479 $this->assertTrue($result); // Cm with visible groups.
481 // No groups, forced.
482 $course->groupmode
= NOGROUPS
;
483 $course->groupmodeforce
= true;
484 update_course($course);
485 $result = groups_group_visible($group1->id
, $course, null, $user1->id
);
486 $this->assertTrue($result);
487 $result = groups_group_visible(0, $course, null, $user1->id
);
488 $this->assertTrue($result); // Requesting all groups.
490 $result = groups_group_visible($group1->id
, $course, $cm, $user1->id
);
491 $this->assertTrue($result); // Cm with no groups.
493 $cm->groupmode
= SEPARATEGROUPS
;
494 $result = groups_group_visible($group1->id
, $course, $cm, $user1->id
);
495 $this->assertTrue($result); // Cm with separate groups.
496 $result = groups_group_visible($group1->id
, $course, $cm, $user2->id
);
497 $this->assertTrue($result); // Cm with separate groups.
499 $cm->groupmode
= SEPARATEGROUPS
;
500 $result = groups_group_visible($group1->id
, $course, $cm, $user1->id
);
501 $this->assertTrue($result); // Cm with visible groups.
503 // Visible groups, forced.
504 $course->groupmode
= VISIBLEGROUPS
;
505 $course->groupmodeforce
= true;
506 update_course($course);
507 $result = groups_group_visible($group1->id
, $course, null, $user1->id
);
508 $this->assertTrue($result);
509 $result = groups_group_visible(0, $course, null, $user1->id
);
510 $this->assertTrue($result); // Requesting all groups.
512 $cm->groupmode
= NOGROUPS
;
513 $result = groups_group_visible($group1->id
, $course, $cm, $user1->id
);
514 $this->assertTrue($result); // Cm with no groups.
516 $cm->groupmode
= SEPARATEGROUPS
;
517 $result = groups_group_visible($group1->id
, $course, $cm, $user1->id
);
518 $this->assertTrue($result); // Cm with separate groups.
519 $result = groups_group_visible($group1->id
, $course, $cm, $user2->id
);
520 $this->assertTrue($result); // Cm with separate groups.
522 $cm->groupmode
= VISIBLEGROUPS
;
523 $result = groups_group_visible($group1->id
, $course, $cm, $user1->id
);
524 $this->assertTrue($result); // Cm with visible groups.
526 // Visible groups, not forced.
527 $course->groupmode
= VISIBLEGROUPS
;
528 $course->groupmodeforce
= false;
529 update_course($course);
530 $result = groups_group_visible($group1->id
, $course, null, $user1->id
);
531 $this->assertTrue($result);
532 $result = groups_group_visible(0, $course, null, $user1->id
);
533 $this->assertTrue($result); // Requesting all groups.
535 $cm->groupmode
= NOGROUPS
;
536 $result = groups_group_visible($group1->id
, $course, $cm, $user1->id
);
537 $this->assertTrue($result); // Cm with no groups.
539 $cm->groupmode
= SEPARATEGROUPS
;
540 $result = groups_group_visible($group1->id
, $course, $cm, $user1->id
);
541 $this->assertFalse($result); // Cm with separate groups.
542 $result = groups_group_visible($group1->id
, $course, $cm, $user2->id
);
543 $this->assertTrue($result); // Cm with separate groups.
545 $cm->groupmode
= VISIBLEGROUPS
;
546 $result = groups_group_visible($group1->id
, $course, $cm, $user1->id
);
547 $this->assertTrue($result); // Cm with visible groups.
549 // Separate groups, forced.
550 $course->groupmode
= SEPARATEGROUPS
;
551 $course->groupmodeforce
= true;
552 update_course($course);
553 $result = groups_group_visible($group1->id
, $course, null, $user1->id
);
554 $this->assertFalse($result);
555 $result = groups_group_visible($group1->id
, $course, null, $user2->id
);
556 $this->assertTrue($result);
557 $result = groups_group_visible(0, $course, null, $user2->id
);
558 $this->assertFalse($result); // Requesting all groups.
559 $result = groups_group_visible(0, $course, null, $user3->id
);
560 $this->assertTrue($result); // Requesting all groups.
561 $result = groups_group_visible($group1->id
, $course, null, $user3->id
);
562 $this->assertTrue($result); // Make sure user with access to all groups can see any group.
564 $cm->groupmode
= NOGROUPS
;
565 $result = groups_group_visible($group1->id
, $course, $cm, $user1->id
);
566 $this->assertFalse($result); // Cm with no groups.
568 $cm->groupmode
= SEPARATEGROUPS
;
569 $result = groups_group_visible($group1->id
, $course, $cm, $user1->id
);
570 $this->assertFalse($result); // Cm with separate groups.
571 $result = groups_group_visible($group1->id
, $course, $cm, $user2->id
);
572 $this->assertTrue($result); // Cm with separate groups.
573 $result = groups_group_visible($group1->id
, $course, $cm, $user3->id
);
574 $this->assertTrue($result); // Make sure user with access to all groups can see any group.
576 $cm->groupmode
= VISIBLEGROUPS
;
577 $result = groups_group_visible($group1->id
, $course, $cm, $user1->id
);
578 $this->assertFalse($result); // Cm with visible groups.
580 // Separate groups, not forced.
581 $course->groupmode
= SEPARATEGROUPS
;
582 $course->groupmodeforce
= false;
583 update_course($course);
584 $result = groups_group_visible($group1->id
, $course, null, $user1->id
);
585 $this->assertFalse($result);
586 $result = groups_group_visible($group1->id
, $course, null, $user2->id
);
587 $this->assertTrue($result);
588 $result = groups_group_visible(0, $course, null, $user2->id
);
589 $this->assertFalse($result); // Requesting all groups.
590 $result = groups_group_visible(0, $course, null, $user3->id
);
591 $this->assertTrue($result); // Requesting all groups.
593 $cm->groupmode
= NOGROUPS
;
594 $result = groups_group_visible($group1->id
, $course, $cm, $user1->id
);
595 $this->assertTrue($result); // Cm with no groups.
597 $cm->groupmode
= SEPARATEGROUPS
;
598 $result = groups_group_visible($group1->id
, $course, $cm, $user1->id
);
599 $this->assertFalse($result); // Cm with separate groups.
600 $result = groups_group_visible($group1->id
, $course, $cm, $user2->id
);
601 $this->assertTrue($result); // Cm with separate groups.
603 $cm->groupmode
= VISIBLEGROUPS
;
604 $result = groups_group_visible($group1->id
, $course, $cm, $user1->id
);
605 $this->assertTrue($result); // Cm with visible groups.
608 function test_groups_get_groupmode() {
610 $generator = $this->getDataGenerator();
611 $this->resetAfterTest();
612 $this->setAdminUser();
614 // Create a course with no groups forcing.
615 $course1 = $generator->create_course();
617 // Create cm1 with no groups, cm1 with visible groups, cm2 with separate groups and cm3 with visible groups.
618 $assign1 = $generator->create_module("assign", array('course' => $course1->id
));
619 $assign2 = $generator->create_module("assign", array('course' => $course1->id
),
620 array('groupmode' => SEPARATEGROUPS
));
621 $assign3 = $generator->create_module("assign", array('course' => $course1->id
),
622 array('groupmode' => VISIBLEGROUPS
));
624 // Request data for tests.
625 $cm1 = get_coursemodule_from_instance("assign", $assign1->id
);
626 $cm2 = get_coursemodule_from_instance("assign", $assign2->id
);
627 $cm3 = get_coursemodule_from_instance("assign", $assign3->id
);
628 $modinfo = get_fast_modinfo($course1->id
);
630 // Assert that any method of getting activity groupmode returns the correct result.
631 $this->assertEquals(NOGROUPS
, groups_get_activity_groupmode($cm1));
632 $this->assertEquals(NOGROUPS
, groups_get_activity_groupmode($cm1, $course1));
633 $this->assertEquals(NOGROUPS
, groups_get_activity_groupmode($modinfo->cms
[$cm1->id
]));
634 $this->assertEquals(SEPARATEGROUPS
, groups_get_activity_groupmode($cm2));
635 $this->assertEquals(SEPARATEGROUPS
, groups_get_activity_groupmode($cm2, $course1));
636 $this->assertEquals(SEPARATEGROUPS
, groups_get_activity_groupmode($modinfo->cms
[$cm2->id
]));
637 $this->assertEquals(VISIBLEGROUPS
, groups_get_activity_groupmode($cm3));
638 $this->assertEquals(VISIBLEGROUPS
, groups_get_activity_groupmode($cm3, $course1));
639 $this->assertEquals(VISIBLEGROUPS
, groups_get_activity_groupmode($modinfo->cms
[$cm3->id
]));
641 // Update the course set the groupmode SEPARATEGROUPS but not forced.
642 update_course((object)array('id' => $course1->id
, 'groupmode' => SEPARATEGROUPS
));
643 // Re-request the data from DB.
644 $course1 = $DB->get_record('course', array('id' => $course1->id
));
645 $modinfo = get_fast_modinfo($course1->id
);
647 // Existing activities are not changed.
648 $this->assertEquals(NOGROUPS
, groups_get_activity_groupmode($cm1));
649 $this->assertEquals(NOGROUPS
, groups_get_activity_groupmode($cm1, $course1));
650 $this->assertEquals(NOGROUPS
, groups_get_activity_groupmode($modinfo->cms
[$cm1->id
]));
651 $this->assertEquals(SEPARATEGROUPS
, groups_get_activity_groupmode($cm2));
652 $this->assertEquals(SEPARATEGROUPS
, groups_get_activity_groupmode($cm2, $course1));
653 $this->assertEquals(SEPARATEGROUPS
, groups_get_activity_groupmode($modinfo->cms
[$cm2->id
]));
654 $this->assertEquals(VISIBLEGROUPS
, groups_get_activity_groupmode($cm3));
655 $this->assertEquals(VISIBLEGROUPS
, groups_get_activity_groupmode($cm3, $course1));
656 $this->assertEquals(VISIBLEGROUPS
, groups_get_activity_groupmode($modinfo->cms
[$cm3->id
]));
658 // Update the course set the groupmode SEPARATEGROUPS and forced.
659 update_course((object)array('id' => $course1->id
, 'groupmode' => SEPARATEGROUPS
, 'groupmodeforce' => true));
660 // Re-request the data from DB.
661 $course1 = $DB->get_record('course', array('id' => $course1->id
));
662 $modinfo = get_fast_modinfo($course1->id
);
664 // Make sure all activities have separate groups mode now.
665 $this->assertEquals(SEPARATEGROUPS
, groups_get_activity_groupmode($cm1));
666 $this->assertEquals(SEPARATEGROUPS
, groups_get_activity_groupmode($cm1, $course1));
667 $this->assertEquals(SEPARATEGROUPS
, groups_get_activity_groupmode($modinfo->cms
[$cm1->id
]));
668 $this->assertEquals(SEPARATEGROUPS
, groups_get_activity_groupmode($cm2));
669 $this->assertEquals(SEPARATEGROUPS
, groups_get_activity_groupmode($cm2, $course1));
670 $this->assertEquals(SEPARATEGROUPS
, groups_get_activity_groupmode($modinfo->cms
[$cm2->id
]));
671 $this->assertEquals(SEPARATEGROUPS
, groups_get_activity_groupmode($cm3));
672 $this->assertEquals(SEPARATEGROUPS
, groups_get_activity_groupmode($cm3, $course1));
673 $this->assertEquals(SEPARATEGROUPS
, groups_get_activity_groupmode($modinfo->cms
[$cm3->id
]));
677 * Tests for groups_allgroups_course_menu() .
679 public function test_groups_allgroups_course_menu() {
682 $this->resetAfterTest();
685 $course = $this->getDataGenerator()->create_course();
686 $record = new stdClass();
687 $record->courseid
= $course->id
;
688 $group1 = $this->getDataGenerator()->create_group($record);
689 $group2 = $this->getDataGenerator()->create_group($record);
690 $user = $this->getDataGenerator()->create_user();
691 $this->getDataGenerator()->enrol_user($user->id
, $course->id
);
692 $this->setUser($user);
694 $html = groups_allgroups_course_menu($course, 'someurl.php');
695 // Since user is not a part of this group and doesn't have accessallgroups permission,
696 // the html should be empty.
697 $this->assertEmpty($html);
699 groups_add_member($group1->id
, $user);
700 // 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
701 // partial match to see if all groups are listed or not.
702 $html = groups_allgroups_course_menu($course, 'someurl.php');
703 $this->assertContains(format_string($group1->name
), $html);
704 $this->assertNotContains(format_string($group2->name
), $html);
706 $this->setAdminUser();
708 // Now user can access everything.
709 $html = groups_allgroups_course_menu($course, 'someurl.php');
710 $this->assertContains(format_string($group1->name
), $html);
711 $this->assertContains(format_string($group2->name
), $html);
713 // Make sure separate groups mode, doesn't change anything.
714 $course->groupmode
= SEPARATEGROUPS
;
715 update_course($course);
716 $html = groups_allgroups_course_menu($course, 'someurl.php');
717 $this->assertContains(format_string($group1->name
), $html);
718 $this->assertContains(format_string($group2->name
), $html);
720 // Make sure Visible groups mode, doesn't change anything.
721 $course->groupmode
= VISIBLEGROUPS
;
722 update_course($course);
723 $html = groups_allgroups_course_menu($course, 'someurl.php');
724 $this->assertContains(format_string($group1->name
), $html);
725 $this->assertContains(format_string($group2->name
), $html);
727 // Let us test activegroup changes now.
728 $this->setUser($user);
729 $SESSION->activegroup
[$course->id
][VISIBLEGROUPS
][$course->defaultgroupingid
] = 5;
730 groups_allgroups_course_menu($course, 'someurl.php', false); // Do not update session.
731 $this->assertSame(5, $SESSION->activegroup
[$course->id
][VISIBLEGROUPS
][$course->defaultgroupingid
]);
732 groups_allgroups_course_menu($course, 'someurl.php', true, $group1->id
); // Update session.
733 $this->assertSame($group1->id
, $SESSION->activegroup
[$course->id
][VISIBLEGROUPS
][$course->defaultgroupingid
]);
734 // Try to update session with an invalid groupid. It should not accept the invalid id.
735 groups_allgroups_course_menu($course, 'someurl.php', true, 256);
736 $this->assertEquals($group1->id
, $SESSION->activegroup
[$course->id
][VISIBLEGROUPS
][$course->defaultgroupingid
]);
740 * This unit test checks that groups_get_all_groups returns groups in
741 * alphabetical order even if they are in a grouping.
743 public function test_groups_ordering() {
744 $generator = $this->getDataGenerator();
745 $this->resetAfterTest();
747 // Create a course category and course.
748 $cat = $generator->create_category(array('parent' => 0));
749 $course = $generator->create_course(array('category' => $cat->id
));
750 $grouping = $generator->create_grouping(array('courseid' => $course->id
, 'name' => 'Grouping'));
752 // Create groups in reverse order.
753 $group2 = $generator->create_group(array('courseid' => $course->id
, 'name' => 'Group 2'));
754 $group1 = $generator->create_group(array('courseid' => $course->id
, 'name' => 'Group 1'));
756 // Assign the groups to the grouping in reverse order.
757 $this->assertTrue(groups_assign_grouping($grouping->id
, $group2->id
));
758 $this->assertTrue(groups_assign_grouping($grouping->id
, $group1->id
));
760 // Get all groups and check they are alphabetical.
761 $groups = array_values(groups_get_all_groups($course->id
, 0));
762 $this->assertEquals('Group 1', $groups[0]->name
);
763 $this->assertEquals('Group 2', $groups[1]->name
);
765 // Now check the same is true when accessed by grouping.
766 $groups = array_values(groups_get_all_groups($course->id
, 0, $grouping->id
));
767 $this->assertEquals('Group 1', $groups[0]->name
);
768 $this->assertEquals('Group 2', $groups[1]->name
);
772 * Tests for groups_get_user_groups() method.
774 public function test_groups_get_user_groups() {
775 $this->resetAfterTest(true);
776 $generator = $this->getDataGenerator();
779 $course1 = $generator->create_course();
780 $course2 = $generator->create_course();
783 $user1 = $generator->create_user();
784 $user2 = $generator->create_user();
785 $user3 = $generator->create_user();
788 $generator->enrol_user($user1->id
, $course1->id
);
789 $generator->enrol_user($user1->id
, $course2->id
);
790 $generator->enrol_user($user2->id
, $course2->id
);
791 $generator->enrol_user($user3->id
, $course2->id
);
794 $group1 = $generator->create_group(array('courseid' => $course1->id
));
795 $group2 = $generator->create_group(array('courseid' => $course2->id
));
796 $group3 = $generator->create_group(array('courseid' => $course2->id
));
798 // Assign users to groups.
799 $this->assertTrue($generator->create_group_member(array('groupid' => $group1->id
, 'userid' => $user1->id
)));
800 $this->assertTrue($generator->create_group_member(array('groupid' => $group2->id
, 'userid' => $user2->id
)));
803 $usergroups1 = groups_get_user_groups($course1->id
, $user1->id
);
804 $usergroups2 = groups_get_user_groups($course2->id
, $user2->id
);;
806 // Assert return data.
807 $this->assertEquals($group1->id
, $usergroups1[0][0]);
808 $this->assertEquals($group2->id
, $usergroups2[0][0]);
810 // Now, test with groupings.
811 $grouping1 = $generator->create_grouping(array('courseid' => $course1->id
));
812 $grouping2 = $generator->create_grouping(array('courseid' => $course2->id
));
814 // Assign the groups to grouping.
815 groups_assign_grouping($grouping1->id
, $group1->id
);
816 groups_assign_grouping($grouping2->id
, $group2->id
);
817 groups_assign_grouping($grouping2->id
, $group3->id
);
819 // Test with grouping.
820 $usergroups1 = groups_get_user_groups($course1->id
, $user1->id
);
821 $usergroups2 = groups_get_user_groups($course2->id
, $user2->id
);
822 $this->assertArrayHasKey($grouping1->id
, $usergroups1);
823 $this->assertArrayHasKey($grouping2->id
, $usergroups2);
825 // Test user without a group.
826 $usergroups1 = groups_get_user_groups($course2->id
, $user3->id
);
827 $this->assertCount(0, $usergroups1[0]);
829 // Test with userid = 0.
830 $usergroups1 = groups_get_user_groups($course1->id
, 0);
831 $usergroups2 = groups_get_user_groups($course2->id
, 0);
832 $this->assertCount(0, $usergroups1[0]);
833 $this->assertCount(0, $usergroups2[0]);
835 // Test with courseid = 0.
836 $usergroups1 = groups_get_user_groups(0, $user1->id
);
837 $usergroups2 = groups_get_user_groups(0, $user2->id
);
838 $this->assertCount(0, $usergroups1[0]);
839 $this->assertCount(0, $usergroups2[0]);
843 * Create dummy groups array for use in menu tests
847 protected function make_group_list($number) {
848 $testgroups = array();
849 for ($a = 0; $a < $number; $a++
) {
850 $grp = new stdClass();
852 $grp->name
= 'test group ' . $grp->id
;
853 $testgroups[$grp->id
] = $grp;
858 public function test_groups_sort_menu_options_empty() {
859 $this->assertEquals(array(), groups_sort_menu_options(array(), array()));
862 public function test_groups_sort_menu_options_allowed_goups_only() {
863 $this->assertEquals(array(
864 100 => 'test group 100',
865 101 => 'test group 101',
866 ), groups_sort_menu_options($this->make_group_list(2), array()));
869 public function test_groups_sort_menu_options_user_goups_only() {
870 $this->assertEquals(array(
871 100 => 'test group 100',
872 101 => 'test group 101',
873 ), groups_sort_menu_options(array(), $this->make_group_list(2)));
876 public function test_groups_sort_menu_options_user_both() {
877 $this->assertEquals(array(
878 1 => array(get_string('mygroups', 'group') => array(
879 100 => 'test group 100',
880 101 => 'test group 101',
882 2 => array(get_string('othergroups', 'group') => array(
883 102 => 'test group 102',
884 103 => 'test group 103',
886 ), groups_sort_menu_options($this->make_group_list(4), $this->make_group_list(2)));
889 public function test_groups_sort_menu_options_user_both_many_groups() {
890 $this->assertEquals(array(
891 1 => array(get_string('mygroups', 'group') => array(
892 100 => 'test group 100',
893 101 => 'test group 101',
895 2 => array (get_string('othergroups', 'group') => array(
896 102 => 'test group 102',
897 103 => 'test group 103',
898 104 => 'test group 104',
899 105 => 'test group 105',
900 106 => 'test group 106',
901 107 => 'test group 107',
902 108 => 'test group 108',
903 109 => 'test group 109',
904 110 => 'test group 110',
905 111 => 'test group 111',
906 112 => 'test group 112',
908 ), groups_sort_menu_options($this->make_group_list(13), $this->make_group_list(2)));
912 * Tests for groups_user_groups_visible.
914 public function test_groups_user_groups_visible() {
917 $generator = $this->getDataGenerator();
918 $this->resetAfterTest();
919 $this->setAdminUser();
921 // Create a course category, course and groups.
922 $cat = $generator->create_category(array('parent' => 0));
923 $course = $generator->create_course(array('category' => $cat->id
));
924 $coursecontext = context_course
::instance($course->id
);
925 $group1 = $generator->create_group(array('courseid' => $course->id
, 'name' => 'Group 1'));
926 $group2 = $generator->create_group(array('courseid' => $course->id
, 'name' => 'Group 2'));
927 $group3 = $generator->create_group(array('courseid' => $course->id
, 'name' => 'Group 3'));
928 $group4 = $generator->create_group(array('courseid' => $course->id
, 'name' => 'Group 4'));
931 $assign = $generator->create_module("assign", array('course' => $course->id
));
932 $cm = get_coursemodule_from_instance("assign", $assign->id
);
935 $user1 = $generator->create_user(); // Normal user.
936 $user2 = $generator->create_user(); // Normal user.
937 $user3 = $generator->create_user(); // Teacher, access all groups.
938 $user4 = $generator->create_user(); // Normal user.
940 // Enrol users into the course.
941 $generator->enrol_user($user1->id
, $course->id
);
942 $generator->enrol_user($user2->id
, $course->id
);
943 $generator->enrol_user($user4->id
, $course->id
);
946 // User1 and User4 share groups.
947 groups_add_member($group1, $user1);
948 groups_add_member($group2, $user2);
949 groups_add_member($group1, $user4);
951 // Give capability at course level to the user to access all groups.
952 $role = $DB->get_field("role", "id", array("shortname" => "manager"));
953 $generator->enrol_user($user3->id
, $course->id
, $role);
954 // Make sure the user has the capability.
955 assign_capability('moodle/site:accessallgroups', CAP_ALLOW
, $role, $coursecontext->id
);
957 // Normal users in different groups.
958 $this->setUser($user1);
960 // No groups , not forced.
961 $result = groups_user_groups_visible($course, $user2->id
);
962 $this->assertTrue($result);
964 $result = groups_user_groups_visible($course, $user2->id
);
965 $this->assertTrue($result); // Cm with no groups.
967 $cm->groupmode
= SEPARATEGROUPS
;
968 $result = groups_user_groups_visible($course, $user2->id
, $cm);
969 $this->assertFalse($result); // Cm with separate groups.
971 $cm->groupmode
= VISIBLEGROUPS
;
972 $result = groups_user_groups_visible($course, $user2->id
, $cm);
973 $this->assertTrue($result); // Cm with visible groups.
975 // No groups, forced.
976 $course->groupmode
= NOGROUPS
;
977 $course->groupmodeforce
= true;
978 update_course($course);
979 $result = groups_user_groups_visible($course, $user2->id
);
980 $this->assertTrue($result);
982 $result = groups_user_groups_visible($course, $user2->id
);
983 $this->assertTrue($result); // Cm with no groups.
985 $cm->groupmode
= SEPARATEGROUPS
;
986 $result = groups_user_groups_visible($course, $user2->id
, $cm);
987 $this->assertTrue($result); // Cm with separate groups.
989 $cm->groupmode
= SEPARATEGROUPS
;
990 $result = groups_user_groups_visible($course, $user2->id
);
991 $this->assertTrue($result); // Cm with visible groups.
993 // Visible groups, forced.
994 $course->groupmode
= VISIBLEGROUPS
;
995 $course->groupmodeforce
= true;
996 update_course($course);
997 $result = groups_user_groups_visible($course, $user2->id
);
998 $this->assertTrue($result);
1000 $cm->groupmode
= NOGROUPS
;
1001 $result = groups_user_groups_visible($course, $user2->id
, $cm);
1002 $this->assertTrue($result); // Cm with no groups.
1004 $cm->groupmode
= SEPARATEGROUPS
;
1005 $result = groups_user_groups_visible($course, $user2->id
, $cm);
1006 $this->assertTrue($result); // Cm with separate groups.
1008 $cm->groupmode
= VISIBLEGROUPS
;
1009 $result = groups_user_groups_visible($course, $user2->id
, $cm);
1010 $this->assertTrue($result); // Cm with visible groups.
1012 // Visible groups, not forced.
1013 $course->groupmode
= VISIBLEGROUPS
;
1014 $course->groupmodeforce
= false;
1015 update_course($course);
1016 $result = groups_user_groups_visible($course, $user2->id
);
1017 $this->assertTrue($result);
1019 $cm->groupmode
= NOGROUPS
;
1020 $result = groups_user_groups_visible($course, $user2->id
);
1021 $this->assertTrue($result); // Cm with no groups.
1023 $cm->groupmode
= SEPARATEGROUPS
;
1024 $result = groups_user_groups_visible($course, $user2->id
, $cm);
1025 $this->assertFalse($result); // Cm with separate groups.
1027 $cm->groupmode
= VISIBLEGROUPS
;
1028 $result = groups_user_groups_visible($course, $user2->id
, $cm);
1029 $this->assertTrue($result); // Cm with visible groups.
1031 // Separate groups, forced.
1032 $course->groupmode
= SEPARATEGROUPS
;
1033 $course->groupmodeforce
= true;
1034 update_course($course);
1035 $result = groups_user_groups_visible($course, $user2->id
);
1036 $this->assertFalse($result);
1038 $result = groups_user_groups_visible($course, $user3->id
);
1039 $this->assertFalse($result); // Requesting all groups.
1041 $cm->groupmode
= NOGROUPS
;
1042 $result = groups_user_groups_visible($course, $user2->id
, $cm);
1043 $this->assertFalse($result); // Cm with no groups.
1045 $cm->groupmode
= SEPARATEGROUPS
;
1046 $result = groups_user_groups_visible($course, $user2->id
, $cm);
1047 $this->assertFalse($result); // Cm with separate groups.
1049 $result = groups_user_groups_visible($course, $user3->id
, $cm);
1050 $this->assertTrue($result);
1052 $cm->groupmode
= VISIBLEGROUPS
;
1053 $result = groups_user_groups_visible($course, $user2->id
, $cm);
1054 $this->assertFalse($result); // Cm with visible groups.
1056 // Separate groups, not forced.
1057 $course->groupmode
= SEPARATEGROUPS
;
1058 $course->groupmodeforce
= false;
1059 update_course($course);
1060 $result = groups_user_groups_visible($course, $user2->id
);
1061 $this->assertFalse($result);
1063 $result = groups_user_groups_visible($course, $user3->id
);
1064 $this->assertFalse($result); // Requesting all groups.
1066 $cm->groupmode
= NOGROUPS
;
1067 $result = groups_user_groups_visible($course, $user2->id
, $cm);
1068 $this->assertTrue($result); // Cm with no groups.
1070 $cm->groupmode
= SEPARATEGROUPS
;
1071 $result = groups_user_groups_visible($course, $user2->id
, $cm);
1072 $this->assertFalse($result); // Cm with separate groups.
1074 $cm->groupmode
= VISIBLEGROUPS
;
1075 $result = groups_user_groups_visible($course, $user2->id
, $cm);
1076 $this->assertTrue($result); // Cm with visible groups.
1078 // Users sharing groups.
1080 // No groups , not forced.
1081 $course->groupmode
= NOGROUPS
;
1082 $course->groupmodeforce
= false;
1083 update_course($course);
1085 $result = groups_user_groups_visible($course, $user4->id
);
1086 $this->assertTrue($result);
1088 $result = groups_user_groups_visible($course, $user4->id
);
1089 $this->assertTrue($result); // Cm with no groups.
1091 $cm->groupmode
= SEPARATEGROUPS
;
1092 $result = groups_user_groups_visible($course, $user4->id
, $cm);
1093 $this->assertTrue($result); // Cm with separate groups.
1095 $cm->groupmode
= VISIBLEGROUPS
;
1096 $result = groups_user_groups_visible($course, $user4->id
, $cm);
1097 $this->assertTrue($result); // Cm with visible groups.
1099 // No groups, forced.
1100 $course->groupmode
= NOGROUPS
;
1101 $course->groupmodeforce
= true;
1102 update_course($course);
1103 $result = groups_user_groups_visible($course, $user4->id
);
1104 $this->assertTrue($result);
1106 $result = groups_user_groups_visible($course, $user4->id
);
1107 $this->assertTrue($result); // Cm with no groups.
1109 $cm->groupmode
= SEPARATEGROUPS
;
1110 $result = groups_user_groups_visible($course, $user4->id
, $cm);
1111 $this->assertTrue($result); // Cm with separate groups.
1113 $cm->groupmode
= SEPARATEGROUPS
;
1114 $result = groups_user_groups_visible($course, $user4->id
);
1115 $this->assertTrue($result); // Cm with visible groups.
1117 // Visible groups, forced.
1118 $course->groupmode
= VISIBLEGROUPS
;
1119 $course->groupmodeforce
= true;
1120 update_course($course);
1121 $result = groups_user_groups_visible($course, $user4->id
);
1122 $this->assertTrue($result);
1124 $cm->groupmode
= NOGROUPS
;
1125 $result = groups_user_groups_visible($course, $user4->id
, $cm);
1126 $this->assertTrue($result); // Cm with no groups.
1128 $cm->groupmode
= SEPARATEGROUPS
;
1129 $result = groups_user_groups_visible($course, $user4->id
, $cm);
1130 $this->assertTrue($result); // Cm with separate groups.
1132 $cm->groupmode
= VISIBLEGROUPS
;
1133 $result = groups_user_groups_visible($course, $user4->id
, $cm);
1134 $this->assertTrue($result); // Cm with visible groups.
1136 // Visible groups, not forced.
1137 $course->groupmode
= VISIBLEGROUPS
;
1138 $course->groupmodeforce
= false;
1139 update_course($course);
1140 $result = groups_user_groups_visible($course, $user4->id
);
1141 $this->assertTrue($result);
1143 $cm->groupmode
= NOGROUPS
;
1144 $result = groups_user_groups_visible($course, $user4->id
);
1145 $this->assertTrue($result); // Cm with no groups.
1147 $cm->groupmode
= SEPARATEGROUPS
;
1148 $result = groups_user_groups_visible($course, $user4->id
, $cm);
1149 $this->assertTrue($result); // Cm with separate groups.
1151 $cm->groupmode
= VISIBLEGROUPS
;
1152 $result = groups_user_groups_visible($course, $user4->id
, $cm);
1153 $this->assertTrue($result); // Cm with visible groups.
1155 // Separate groups, forced.
1156 $course->groupmode
= SEPARATEGROUPS
;
1157 $course->groupmodeforce
= true;
1158 update_course($course);
1159 $result = groups_user_groups_visible($course, $user4->id
);
1160 $this->assertTrue($result);
1162 $result = groups_user_groups_visible($course, $user3->id
);
1163 $this->assertFalse($result); // Requesting all groups.
1165 $cm->groupmode
= NOGROUPS
;
1166 $result = groups_user_groups_visible($course, $user4->id
, $cm);
1167 $this->assertTrue($result); // Cm with no groups.
1169 $cm->groupmode
= SEPARATEGROUPS
;
1170 $result = groups_user_groups_visible($course, $user4->id
, $cm);
1171 $this->assertTrue($result); // Cm with separate groups.
1173 $result = groups_user_groups_visible($course, $user3->id
, $cm);
1174 $this->assertTrue($result);
1176 $cm->groupmode
= VISIBLEGROUPS
;
1177 $result = groups_user_groups_visible($course, $user4->id
, $cm);
1178 $this->assertTrue($result); // Cm with visible groups.
1180 // Separate groups, not forced.
1181 $course->groupmode
= SEPARATEGROUPS
;
1182 $course->groupmodeforce
= false;
1183 update_course($course);
1184 $result = groups_user_groups_visible($course, $user4->id
);
1185 $this->assertTrue($result);
1187 $result = groups_user_groups_visible($course, $user3->id
);
1188 $this->assertFalse($result); // Requesting all groups.
1190 $cm->groupmode
= NOGROUPS
;
1191 $result = groups_user_groups_visible($course, $user4->id
, $cm);
1192 $this->assertTrue($result); // Cm with no groups.
1194 $cm->groupmode
= SEPARATEGROUPS
;
1195 $result = groups_user_groups_visible($course, $user4->id
, $cm);
1196 $this->assertTrue($result); // Cm with separate groups.
1198 $cm->groupmode
= VISIBLEGROUPS
;
1199 $result = groups_user_groups_visible($course, $user4->id
, $cm);
1200 $this->assertTrue($result); // Cm with visible groups.
1202 // For teacher with access all groups.
1204 // No groups , not forced.
1205 $course->groupmode
= NOGROUPS
;
1206 $course->groupmodeforce
= false;
1207 update_course($course);
1209 $this->setUser($user3);
1211 $result = groups_user_groups_visible($course, $user1->id
);
1212 $this->assertTrue($result);
1213 $result = groups_user_groups_visible($course, $user1->id
);
1214 $this->assertTrue($result); // Requesting all groups.
1216 $result = groups_user_groups_visible($course, $user1->id
);
1217 $this->assertTrue($result); // Cm with no groups.
1219 $cm->groupmode
= SEPARATEGROUPS
;
1220 $result = groups_user_groups_visible($course, $user1->id
, $cm);
1221 $this->assertTrue($result); // Cm with separate groups.
1222 $result = groups_user_groups_visible($course, $user2->id
, $cm);
1223 $this->assertTrue($result); // Cm with separate groups.
1225 $cm->groupmode
= VISIBLEGROUPS
;
1226 $result = groups_user_groups_visible($course, $user1->id
, $cm);
1227 $this->assertTrue($result); // Cm with visible groups.
1229 // No groups, forced.
1230 $course->groupmode
= NOGROUPS
;
1231 $course->groupmodeforce
= true;
1232 update_course($course);
1233 $result = groups_user_groups_visible($course, $user1->id
);
1234 $this->assertTrue($result);
1235 $result = groups_user_groups_visible($course, $user1->id
);
1236 $this->assertTrue($result); // Requesting all groups.
1238 $result = groups_user_groups_visible($course, $user1->id
);
1239 $this->assertTrue($result); // Cm with no groups.
1241 $cm->groupmode
= SEPARATEGROUPS
;
1242 $result = groups_user_groups_visible($course, $user1->id
, $cm);
1243 $this->assertTrue($result); // Cm with separate groups.
1244 $result = groups_user_groups_visible($course, $user2->id
, $cm);
1245 $this->assertTrue($result); // Cm with separate groups.
1247 $cm->groupmode
= SEPARATEGROUPS
;
1248 $result = groups_user_groups_visible($course, $user1->id
);
1249 $this->assertTrue($result); // Cm with visible groups.
1251 // Visible groups, forced.
1252 $course->groupmode
= VISIBLEGROUPS
;
1253 $course->groupmodeforce
= true;
1254 update_course($course);
1255 $result = groups_user_groups_visible($course, $user1->id
);
1256 $this->assertTrue($result);
1257 $result = groups_user_groups_visible($course, $user1->id
);
1258 $this->assertTrue($result); // Requesting all groups.
1260 $cm->groupmode
= NOGROUPS
;
1261 $result = groups_user_groups_visible($course, $user1->id
, $cm);
1262 $this->assertTrue($result); // Cm with no groups.
1264 $cm->groupmode
= SEPARATEGROUPS
;
1265 $result = groups_user_groups_visible($course, $user1->id
, $cm);
1266 $this->assertTrue($result); // Cm with separate groups.
1267 $result = groups_user_groups_visible($course, $user2->id
, $cm);
1268 $this->assertTrue($result); // Cm with separate groups.
1270 $cm->groupmode
= VISIBLEGROUPS
;
1271 $result = groups_user_groups_visible($course, $user1->id
, $cm);
1272 $this->assertTrue($result); // Cm with visible groups.
1274 // Visible groups, not forced.
1275 $course->groupmode
= VISIBLEGROUPS
;
1276 $course->groupmodeforce
= false;
1277 update_course($course);
1278 $result = groups_user_groups_visible($course, $user1->id
);
1279 $this->assertTrue($result);
1280 $result = groups_user_groups_visible($course, $user1->id
);
1281 $this->assertTrue($result); // Requesting all groups.
1283 $cm->groupmode
= NOGROUPS
;
1284 $result = groups_user_groups_visible($course, $user1->id
);
1285 $this->assertTrue($result); // Cm with no groups.
1287 $cm->groupmode
= SEPARATEGROUPS
;
1288 $result = groups_user_groups_visible($course, $user1->id
, $cm);
1289 $this->assertTrue($result); // Cm with separate groups.
1290 $result = groups_user_groups_visible($course, $user2->id
, $cm);
1291 $this->assertTrue($result); // Cm with separate groups.
1293 $cm->groupmode
= VISIBLEGROUPS
;
1294 $result = groups_user_groups_visible($course, $user1->id
, $cm);
1295 $this->assertTrue($result); // Cm with visible groups.
1297 // Separate groups, forced.
1298 $course->groupmode
= SEPARATEGROUPS
;
1299 $course->groupmodeforce
= true;
1300 update_course($course);
1301 $result = groups_user_groups_visible($course, $user1->id
);
1302 $this->assertTrue($result);
1303 $result = groups_user_groups_visible($course, $user2->id
);
1304 $this->assertTrue($result);
1305 $result = groups_user_groups_visible($course, $user2->id
);
1306 $this->assertTrue($result); // Requesting all groups.
1307 $result = groups_user_groups_visible($course, $user3->id
);
1308 $this->assertTrue($result); // Requesting all groups.
1309 $result = groups_user_groups_visible($course, $user3->id
);
1310 $this->assertTrue($result);
1312 $cm->groupmode
= NOGROUPS
;
1313 $result = groups_user_groups_visible($course, $user1->id
, $cm);
1314 $this->assertTrue($result); // Cm with no groups.
1316 $cm->groupmode
= SEPARATEGROUPS
;
1317 $result = groups_user_groups_visible($course, $user1->id
, $cm);
1318 $this->assertTrue($result); // Cm with separate groups.
1319 $result = groups_user_groups_visible($course, $user2->id
, $cm);
1320 $this->assertTrue($result); // Cm with separate groups.
1321 $result = groups_user_groups_visible($course, $user3->id
, $cm);
1322 $this->assertTrue($result);
1324 $cm->groupmode
= VISIBLEGROUPS
;
1325 $result = groups_user_groups_visible($course, $user1->id
, $cm);
1326 $this->assertTrue($result); // Cm with visible groups.
1328 // Separate groups, not forced.
1329 $course->groupmode
= SEPARATEGROUPS
;
1330 $course->groupmodeforce
= false;
1331 update_course($course);
1332 $result = groups_user_groups_visible($course, $user1->id
);
1333 $this->assertTrue($result);
1334 $result = groups_user_groups_visible($course, $user2->id
);
1335 $this->assertTrue($result);
1336 $result = groups_user_groups_visible($course, $user2->id
);
1337 $this->assertTrue($result); // Requesting all groups.
1338 $result = groups_user_groups_visible($course, $user3->id
);
1339 $this->assertTrue($result); // Requesting all groups.
1341 $cm->groupmode
= NOGROUPS
;
1342 $result = groups_user_groups_visible($course, $user1->id
, $cm);
1343 $this->assertTrue($result); // Cm with no groups.
1345 $cm->groupmode
= SEPARATEGROUPS
;
1346 $result = groups_user_groups_visible($course, $user1->id
, $cm);
1347 $this->assertTrue($result); // Cm with separate groups.
1348 $result = groups_user_groups_visible($course, $user2->id
, $cm);
1349 $this->assertTrue($result); // Cm with separate groups.
1351 $cm->groupmode
= VISIBLEGROUPS
;
1352 $result = groups_user_groups_visible($course, $user1->id
, $cm);
1353 $this->assertTrue($result); // Cm with visible groups.