MDL-40897 avoid extra DB queries in groups_get_activity_groupmode()
[moodle.git] / lib / tests / grouplib_test.php
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
17 /**
18  * Tests events subsystems
19  *
20  * @package    core
21  * @subpackage group
22  * @copyright  2007 onwards Martin Dougiamas (http://dougiamas.com)
23  * @author     Andrew Nicols
24  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25  */
27 defined('MOODLE_INTERNAL') || die();
29 class grouplib_testcase 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';
43         /**
44          * Test with an empty and a null idnumber
45          */
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));
55         /**
56          * Test with a valid idnumber
57          */
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(groups_get_group_by_idnumber($course->id, $idnumber1), $group);
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));
69         /**
70          * Test with another idnumber
71          */
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(groups_get_group_by_idnumber($course->id, $idnumber2), $group);
79         /**
80          * Group idnumbers are unique within a course so test that we don't
81          * retrieve groups for the first course
82          */
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(groups_get_group_by_idnumber($course->id, $idnumber1), $group);
99         $group = $generator->create_group(array('courseid' => $course->id, 'idnumber' => $idnumber2));
100         $this->assertEquals(groups_get_group_by_idnumber($course->id, $idnumber2), $group);
101     }
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';
115         /**
116          * Test with an empty and a null idnumber
117          */
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));
127         /**
128          * Test with a valid idnumber
129          */
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(groups_get_grouping_by_idnumber($course->id, $idnumber1), $grouping);
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));
141         /**
142          * Test with another idnumber
143          */
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(groups_get_grouping_by_idnumber($course->id, $idnumber2), $grouping);
151         /**
152          * Grouping idnumbers are unique within a course so test that we don't
153          * retrieve groupings for the first course
154          */
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(groups_get_grouping_by_idnumber($course->id, $idnumber1), $grouping);
171         $grouping = $generator->create_grouping(array('courseid' => $course->id, 'idnumber' => $idnumber2));
172         $this->assertEquals(groups_get_grouping_by_idnumber($course->id, $idnumber2), $grouping);
173     }
175     public function test_groups_get_group_by_name() {
176         $this->resetAfterTest(true);
178         $generator = $this->getDataGenerator();
180         // Create a course category and course
181         $cat = $generator->create_category(array('parent' => 0));
182         $course = $generator->create_course(array('category' => $cat->id));
184         $name1 = 'Name 1';
185         $name2 = 'Name 2';
187         // Test with an empty and a null idnumber
188         $this->assertFalse(groups_get_group_by_name($course->id, ''));
189         $this->assertFalse(groups_get_group_by_name($course->id, null));
191         // Even when a group exists.
192         $generator->create_group(array('courseid' => $course->id));
193         $this->assertFalse(groups_get_group_by_name($course->id, ''));
194         $this->assertFalse(groups_get_group_by_name($course->id, null));
196         // Test with a valid name, but one that doesn't exist yet.
197         $this->assertFalse(groups_get_group_by_name($course->id, $name1));
198         $this->assertFalse(groups_get_group_by_name($course->id, $name2));
200         // We should now have a valid group returned by the name search.
201         $group1 = $generator->create_group(array('courseid' => $course->id, 'name' => $name1));
202         $this->assertEquals(groups_get_group_by_name($course->id, $name1), $group1->id);
203         $this->assertFalse(groups_get_group_by_name($course->id, $name2));
205         // We should now have a two valid groups returned by the name search.
206         $group2 = $generator->create_group(array('courseid' => $course->id, 'name' => $name2));
207         $this->assertEquals(groups_get_group_by_name($course->id, $name1), $group1->id);
208         $this->assertEquals(groups_get_group_by_name($course->id, $name2), $group2->id);
210         // Delete a group.
211         $this->assertTrue(groups_delete_group($group1));
212         $this->assertFalse(groups_get_group_by_name($course->id, $name1));
213         $this->assertEquals(groups_get_group_by_name($course->id, $name2), $group2->id);
215         /**
216          * Group idnumbers are unique within a course so test that we don't
217          * retrieve groups for the first course
218          */
220         // Create a second course
221         $course = $generator->create_course(array('category' => $cat->id));
223         // An empty name should always return a false value
224         $this->assertFalse(groups_get_group_by_name($course->id, ''));
225         $this->assertFalse(groups_get_group_by_name($course->id, null));
227         // Our existing names shouldn't be returned here as we're in a different course
228         $this->assertFalse(groups_get_group_by_name($course->id, $name1));
229         $this->assertFalse(groups_get_group_by_name($course->id, $name2));
231         // We should be able to reuse the idnumbers again since this is a different course
232         $group1 = $generator->create_group(array('courseid' => $course->id, 'name' => $name1));
233         $this->assertEquals(groups_get_group_by_name($course->id, $name1), $group1->id);
235         $group2 = $generator->create_group(array('courseid' => $course->id, 'name' => $name2));
236         $this->assertEquals(groups_get_group_by_name($course->id, $name2), $group2->id);
237     }
239     public function test_groups_get_grouping() {
240         $this->resetAfterTest(true);
242         $generator = $this->getDataGenerator();
244         // Create a course category and course
245         $cat = $generator->create_category(array('parent' => 0));
246         $course = $generator->create_course(array('category' => $cat->id));
248         $name1 = 'Grouping 1';
249         $name2 = 'Grouping 2';
251         // Test with an empty and a null idnumber
252         $this->assertFalse(groups_get_grouping_by_name($course->id, ''));
253         $this->assertFalse(groups_get_grouping_by_name($course->id, null));
255         // Even when a group exists.
256         $generator->create_group(array('courseid' => $course->id));
257         $this->assertFalse(groups_get_grouping_by_name($course->id, ''));
258         $this->assertFalse(groups_get_grouping_by_name($course->id, null));
260         // Test with a valid name, but one that doesn't exist yet.
261         $this->assertFalse(groups_get_grouping_by_name($course->id, $name1));
262         $this->assertFalse(groups_get_grouping_by_name($course->id, $name2));
264         // We should now have a valid group returned by the name search.
265         $group1 = $generator->create_grouping(array('courseid' => $course->id, 'name' => $name1));
266         $this->assertEquals(groups_get_grouping_by_name($course->id, $name1), $group1->id);
267         $this->assertFalse(groups_get_grouping_by_name($course->id, $name2));
269         // We should now have a two valid groups returned by the name search.
270         $group2 = $generator->create_grouping(array('courseid' => $course->id, 'name' => $name2));
271         $this->assertEquals(groups_get_grouping_by_name($course->id, $name1), $group1->id);
272         $this->assertEquals(groups_get_grouping_by_name($course->id, $name2), $group2->id);
274         // Delete a group.
275         $this->assertTrue(groups_delete_grouping($group1));
276         $this->assertFalse(groups_get_grouping_by_name($course->id, $name1));
277         $this->assertEquals(groups_get_grouping_by_name($course->id, $name2), $group2->id);
279         /**
280          * Group idnumbers are unique within a course so test that we don't
281          * retrieve groups for the first course
282          */
284         // Create a second course
285         $course = $generator->create_course(array('category' => $cat->id));
287         // An empty name should always return a false value
288         $this->assertFalse(groups_get_grouping_by_name($course->id, ''));
289         $this->assertFalse(groups_get_grouping_by_name($course->id, null));
291         // Our existing names shouldn't be returned here as we're in a different course
292         $this->assertFalse(groups_get_grouping_by_name($course->id, $name1));
293         $this->assertFalse(groups_get_grouping_by_name($course->id, $name2));
295         // We should be able to reuse the idnumbers again since this is a different course
296         $group1 = $generator->create_grouping(array('courseid' => $course->id, 'name' => $name1));
297         $this->assertEquals(groups_get_grouping_by_name($course->id, $name1), $group1->id);
299         $group2 = $generator->create_grouping(array('courseid' => $course->id, 'name' => $name2));
300         $this->assertEquals(groups_get_grouping_by_name($course->id, $name2), $group2->id);
301     }
303     public function test_groups_get_course_data() {
304         $this->resetAfterTest(true);
306         $generator = $this->getDataGenerator();
308         // Create a course category and course
309         $cat = $generator->create_category(array('parent' => 0));
310         $course = $generator->create_course(array('category' => $cat->id));
311         $grouping1 = $generator->create_grouping(array('courseid' => $course->id, 'name' => 'Grouping 1'));
312         $grouping2 = $generator->create_grouping(array('courseid' => $course->id, 'name' => 'Grouping 2'));
313         $group1 = $generator->create_group(array('courseid' => $course->id, 'name' => 'Group 1'));
314         $group2 = $generator->create_group(array('courseid' => $course->id, 'name' => 'Group 2'));
315         $group3 = $generator->create_group(array('courseid' => $course->id, 'name' => 'Group 3'));
316         $group4 = $generator->create_group(array('courseid' => $course->id, 'name' => 'Group 4'));
318         // Assign the groups to groupings.
319         $this->assertTrue(groups_assign_grouping($grouping1->id, $group1->id));
320         $this->assertTrue(groups_assign_grouping($grouping1->id, $group2->id));
321         $this->assertTrue(groups_assign_grouping($grouping2->id, $group3->id));
322         $this->assertTrue(groups_assign_grouping($grouping2->id, $group4->id));
324         // Get the data.
325         $data = groups_get_course_data($course->id);
326         $this->assertInstanceOf('stdClass', $data);
327         $this->assertObjectHasAttribute('groups', $data);
328         $this->assertObjectHasAttribute('groupings', $data);
329         $this->assertObjectHasAttribute('mappings', $data);
331         // Test we have the expected items returns.
332         $this->assertCount(4, $data->groups);
333         $this->assertCount(2, $data->groupings);
334         $this->assertCount(4, $data->mappings);
336         // Check we have the expected groups.
337         $this->assertContains($group1->id, array_keys($data->groups));
338         $this->assertContains($group2->id, array_keys($data->groups));
339         $this->assertContains($group3->id, array_keys($data->groups));
340         $this->assertContains($group4->id, array_keys($data->groups));
342         // Test a group-id is mapped correctly.
343         $this->assertEquals($group3->name, $data->groups[$group3->id]->name);
345         // Check we have the expected number of groupings.
346         $this->assertContains($grouping1->id, array_keys($data->groupings));
347         $this->assertContains($grouping2->id, array_keys($data->groupings));
349         // Test a grouping-id is mapped correctly.
350         $this->assertEquals($grouping2->name, $data->groupings[$grouping2->id]->name);
352         // Test that all of the mappings are correct.
353         $grouping1maps = 0;
354         $grouping2maps = 0;
355         $group1maps = 0;
356         $group2maps = 0;
357         $group3maps = 0;
358         $group4maps = 0;
359         foreach ($data->mappings as $mapping) {
360             if ($mapping->groupingid === $grouping1->id) {
361                 $grouping1maps++;
362                 $this->assertContains($mapping->groupid, array($group1->id, $group2->id));
363             } else if ($mapping->groupingid === $grouping2->id) {
364                 $grouping2maps++;
365                 $this->assertContains($mapping->groupid, array($group3->id, $group4->id));
366             } else {
367                 $this->fail('Unexpected groupingid');
368             }
369             switch ($mapping->groupid) {
370                 case $group1->id : $group1maps++; break;
371                 case $group2->id : $group2maps++; break;
372                 case $group3->id : $group3maps++; break;
373                 case $group4->id : $group4maps++; break;
374             }
375         }
376         $this->assertEquals(2, $grouping1maps);
377         $this->assertEquals(2, $grouping2maps);
378         $this->assertEquals(1, $group1maps);
379         $this->assertEquals(1, $group2maps);
380         $this->assertEquals(1, $group3maps);
381         $this->assertEquals(1, $group4maps);
383         // Test the groups_get_all_groups which uses this functionality.
384         $groups  = groups_get_all_groups($course->id);
385         $groupkeys = array_keys($groups);
386         $this->assertCount(4, $groups);
387         $this->assertContains($group1->id, $groupkeys);
388         $this->assertContains($group2->id, $groupkeys);
389         $this->assertContains($group3->id, $groupkeys);
390         $this->assertContains($group4->id, $groupkeys);
392         $groups  = groups_get_all_groups($course->id, null, $grouping1->id);
393         $groupkeys = array_keys($groups);
394         $this->assertCount(2, $groups);
395         $this->assertContains($group1->id, $groupkeys);
396         $this->assertContains($group2->id, $groupkeys);
397         $this->assertNotContains($group3->id, $groupkeys);
398         $this->assertNotContains($group4->id, $groupkeys);
400         $groups  = groups_get_all_groups($course->id, null, $grouping2->id);
401         $groupkeys = array_keys($groups);
402         $this->assertCount(2, $groups);
403         $this->assertNotContains($group1->id, $groupkeys);
404         $this->assertNotContains($group2->id, $groupkeys);
405         $this->assertContains($group3->id, $groupkeys);
406         $this->assertContains($group4->id, $groupkeys);
407     }
409     /**
410      * Tests for groups_group_visible.
411      */
412     public function test_groups_group_visible() {
413         global $CFG, $DB;
415         $generator = $this->getDataGenerator();
416         $this->resetAfterTest();
417         $this->setAdminUser();
419         // Create a course category, course and groups.
420         $cat = $generator->create_category(array('parent' => 0));
421         $course = $generator->create_course(array('category' => $cat->id));
422         $coursecontext = context_course::instance($course->id);
423         $group1 = $generator->create_group(array('courseid' => $course->id, 'name' => 'Group 1'));
424         $group2 = $generator->create_group(array('courseid' => $course->id, 'name' => 'Group 2'));
425         $group3 = $generator->create_group(array('courseid' => $course->id, 'name' => 'Group 3'));
426         $group4 = $generator->create_group(array('courseid' => $course->id, 'name' => 'Group 4'));
428         // Create cm.
429         $assign = $generator->create_module("assign", array('course' => $course->id));
430         $cm = get_coursemodule_from_instance("assign", $assign->id);
432         // Create users.
433         $user1 = $generator->create_user();
434         $user2 = $generator->create_user();
435         $user3 = $generator->create_user();
437         // Enrol users into the course.
438         $generator->enrol_user($user1->id, $course->id);
439         $generator->enrol_user($user2->id, $course->id);
441         // Assign groups.
442         groups_add_member($group1, $user2);
444         // Give capability at course level to the user to access all groups.
445         $role = $DB->get_field("role", "id", array("shortname" => "manager"));
446         $generator->enrol_user($user3->id, $course->id, $role);
447         // Make sure the user has the capability.
448         assign_capability('moodle/site:accessallgroups', CAP_ALLOW, $role, $coursecontext->id);
450         // No groups , not forced.
451         $result = groups_group_visible($group1->id, $course, null, $user1->id);
452         $this->assertTrue($result);
453         $result = groups_group_visible(0, $course, null, $user1->id);
454         $this->assertTrue($result); // Requesting all groups.
456         $result = groups_group_visible($group1->id, $course, $cm, $user1->id);
457         $this->assertTrue($result); // Cm with no groups.
459         $cm->groupmode = SEPARATEGROUPS;
460         $result = groups_group_visible($group1->id, $course, $cm, $user1->id);
461         $this->assertFalse($result); // Cm with separate groups.
462         $result = groups_group_visible($group1->id, $course, $cm, $user2->id);
463         $this->assertTrue($result); // Cm with separate groups.
465         $cm->groupmode = VISIBLEGROUPS;
466         $result = groups_group_visible($group1->id, $course, $cm, $user1->id);
467         $this->assertTrue($result); // Cm with visible groups.
469         // No groups, forced.
470         $course->groupmode = NOGROUPS;
471         $course->groupmodeforce = true;
472         update_course($course);
473         $result = groups_group_visible($group1->id, $course, null, $user1->id);
474         $this->assertTrue($result);
475         $result = groups_group_visible(0, $course, null, $user1->id);
476         $this->assertTrue($result); // Requesting all groups.
478         $result = groups_group_visible($group1->id, $course, $cm, $user1->id);
479         $this->assertTrue($result); // Cm with no groups.
481         $cm->groupmode = SEPARATEGROUPS;
482         $result = groups_group_visible($group1->id, $course, $cm, $user1->id);
483         $this->assertTrue($result); // Cm with separate groups.
484         $result = groups_group_visible($group1->id, $course, $cm, $user2->id);
485         $this->assertTrue($result); // Cm with separate groups.
487         $cm->groupmode = SEPARATEGROUPS;
488         $result = groups_group_visible($group1->id, $course, $cm, $user1->id);
489         $this->assertTrue($result); // Cm with visible groups.
491         // Visible groups, forced.
492         $course->groupmode = VISIBLEGROUPS;
493         $course->groupmodeforce = true;
494         update_course($course);
495         $result = groups_group_visible($group1->id, $course, null, $user1->id);
496         $this->assertTrue($result);
497         $result = groups_group_visible(0, $course, null, $user1->id);
498         $this->assertTrue($result); // Requesting all groups.
500         $cm->groupmode = NOGROUPS;
501         $result = groups_group_visible($group1->id, $course, $cm, $user1->id);
502         $this->assertTrue($result); // Cm with no groups.
504         $cm->groupmode = SEPARATEGROUPS;
505         $result = groups_group_visible($group1->id, $course, $cm, $user1->id);
506         $this->assertTrue($result); // Cm with separate groups.
507         $result = groups_group_visible($group1->id, $course, $cm, $user2->id);
508         $this->assertTrue($result); // Cm with separate groups.
510         $cm->groupmode = VISIBLEGROUPS;
511         $result = groups_group_visible($group1->id, $course, $cm, $user1->id);
512         $this->assertTrue($result); // Cm with visible groups.
514         // Visible groups, not forced.
515         $course->groupmode = VISIBLEGROUPS;
516         $course->groupmodeforce = false;
517         update_course($course);
518         $result = groups_group_visible($group1->id, $course, null, $user1->id);
519         $this->assertTrue($result);
520         $result = groups_group_visible(0, $course, null, $user1->id);
521         $this->assertTrue($result); // Requesting all groups.
523         $cm->groupmode = NOGROUPS;
524         $result = groups_group_visible($group1->id, $course, $cm, $user1->id);
525         $this->assertTrue($result); // Cm with no groups.
527         $cm->groupmode = SEPARATEGROUPS;
528         $result = groups_group_visible($group1->id, $course, $cm, $user1->id);
529         $this->assertFalse($result); // Cm with separate groups.
530         $result = groups_group_visible($group1->id, $course, $cm, $user2->id);
531         $this->assertTrue($result); // Cm with separate groups.
533         $cm->groupmode = VISIBLEGROUPS;
534         $result = groups_group_visible($group1->id, $course, $cm, $user1->id);
535         $this->assertTrue($result); // Cm with visible groups.
537         // Separate groups, forced.
538         $course->groupmode = SEPARATEGROUPS;
539         $course->groupmodeforce = true;
540         update_course($course);
541         $result = groups_group_visible($group1->id, $course, null, $user1->id);
542         $this->assertFalse($result);
543         $result = groups_group_visible($group1->id, $course, null, $user2->id);
544         $this->assertTrue($result);
545         $result = groups_group_visible(0, $course, null, $user2->id);
546         $this->assertFalse($result); // Requesting all groups.
547         $result = groups_group_visible(0, $course, null, $user3->id);
548         $this->assertTrue($result); // Requesting all groups.
549         $result = groups_group_visible($group1->id, $course, null, $user3->id);
550         $this->assertTrue($result); // Make sure user with access to all groups can see any group.
552         $cm->groupmode = NOGROUPS;
553         $result = groups_group_visible($group1->id, $course, $cm, $user1->id);
554         $this->assertFalse($result); // Cm with no groups.
556         $cm->groupmode = SEPARATEGROUPS;
557         $result = groups_group_visible($group1->id, $course, $cm, $user1->id);
558         $this->assertFalse($result); // Cm with separate groups.
559         $result = groups_group_visible($group1->id, $course, $cm, $user2->id);
560         $this->assertTrue($result); // Cm with separate groups.
561         $result = groups_group_visible($group1->id, $course, $cm, $user3->id);
562         $this->assertTrue($result); // Make sure user with access to all groups can see any group.
564         $cm->groupmode = VISIBLEGROUPS;
565         $result = groups_group_visible($group1->id, $course, $cm, $user1->id);
566         $this->assertFalse($result); // Cm with visible groups.
568         // Separate groups, not forced.
569         $course->groupmode = SEPARATEGROUPS;
570         $course->groupmodeforce = false;
571         update_course($course);
572         $result = groups_group_visible($group1->id, $course, null, $user1->id);
573         $this->assertFalse($result);
574         $result = groups_group_visible($group1->id, $course, null, $user2->id);
575         $this->assertTrue($result);
576         $result = groups_group_visible(0, $course, null, $user2->id);
577         $this->assertFalse($result); // Requesting all groups.
578         $result = groups_group_visible(0, $course, null, $user3->id);
579         $this->assertTrue($result); // Requesting all groups.
581         $cm->groupmode = NOGROUPS;
582         $result = groups_group_visible($group1->id, $course, $cm, $user1->id);
583         $this->assertTrue($result); // Cm with no groups.
585         $cm->groupmode = SEPARATEGROUPS;
586         $result = groups_group_visible($group1->id, $course, $cm, $user1->id);
587         $this->assertFalse($result); // Cm with separate groups.
588         $result = groups_group_visible($group1->id, $course, $cm, $user2->id);
589         $this->assertTrue($result); // Cm with separate groups.
591         $cm->groupmode = VISIBLEGROUPS;
592         $result = groups_group_visible($group1->id, $course, $cm, $user1->id);
593         $this->assertTrue($result); // Cm with visible groups.
594     }
596     function test_groups_get_groupmode() {
597         global $DB;
598         $generator = $this->getDataGenerator();
599         $this->resetAfterTest();
600         $this->setAdminUser();
602         // Create a course with no groups forcing.
603         $course1 = $generator->create_course();
605         // Create cm1 with no groups, cm1 with visible groups, cm2 with separate groups and cm3 with visible groups.
606         $assign1 = $generator->create_module("assign", array('course' => $course1->id));
607         $assign2 = $generator->create_module("assign", array('course' => $course1->id),
608                 array('groupmode' => SEPARATEGROUPS));
609         $assign3 = $generator->create_module("assign", array('course' => $course1->id),
610                 array('groupmode' => VISIBLEGROUPS));
612         // Request data for tests.
613         $cm1 = get_coursemodule_from_instance("assign", $assign1->id);
614         $cm2 = get_coursemodule_from_instance("assign", $assign2->id);
615         $cm3 = get_coursemodule_from_instance("assign", $assign3->id);
616         $modinfo = get_fast_modinfo($course1->id);
618         // Assert that any method of getting activity groupmode returns the correct result.
619         $this->assertEquals(NOGROUPS, groups_get_activity_groupmode($cm1));
620         $this->assertEquals(NOGROUPS, groups_get_activity_groupmode($cm1, $course1));
621         $this->assertEquals(NOGROUPS, groups_get_activity_groupmode($modinfo->cms[$cm1->id]));
622         $this->assertEquals(SEPARATEGROUPS, groups_get_activity_groupmode($cm2));
623         $this->assertEquals(SEPARATEGROUPS, groups_get_activity_groupmode($cm2, $course1));
624         $this->assertEquals(SEPARATEGROUPS, groups_get_activity_groupmode($modinfo->cms[$cm2->id]));
625         $this->assertEquals(VISIBLEGROUPS, groups_get_activity_groupmode($cm3));
626         $this->assertEquals(VISIBLEGROUPS, groups_get_activity_groupmode($cm3, $course1));
627         $this->assertEquals(VISIBLEGROUPS, groups_get_activity_groupmode($modinfo->cms[$cm3->id]));
629         // Update the course set the groupmode SEPARATEGROUPS but not forced.
630         update_course((object)array('id' => $course1->id, 'groupmode' => SEPARATEGROUPS));
631         // Re-request the data from DB.
632         $course1 = $DB->get_record('course', array('id' => $course1->id));
633         $modinfo = get_fast_modinfo($course1->id);
635         // Existing activities are not changed.
636         $this->assertEquals(NOGROUPS, groups_get_activity_groupmode($cm1));
637         $this->assertEquals(NOGROUPS, groups_get_activity_groupmode($cm1, $course1));
638         $this->assertEquals(NOGROUPS, groups_get_activity_groupmode($modinfo->cms[$cm1->id]));
639         $this->assertEquals(SEPARATEGROUPS, groups_get_activity_groupmode($cm2));
640         $this->assertEquals(SEPARATEGROUPS, groups_get_activity_groupmode($cm2, $course1));
641         $this->assertEquals(SEPARATEGROUPS, groups_get_activity_groupmode($modinfo->cms[$cm2->id]));
642         $this->assertEquals(VISIBLEGROUPS, groups_get_activity_groupmode($cm3));
643         $this->assertEquals(VISIBLEGROUPS, groups_get_activity_groupmode($cm3, $course1));
644         $this->assertEquals(VISIBLEGROUPS, groups_get_activity_groupmode($modinfo->cms[$cm3->id]));
646         // Update the course set the groupmode SEPARATEGROUPS and forced.
647         update_course((object)array('id' => $course1->id, 'groupmode' => SEPARATEGROUPS, 'groupmodeforce' => true));
648         // Re-request the data from DB.
649         $course1 = $DB->get_record('course', array('id' => $course1->id));
650         $modinfo = get_fast_modinfo($course1->id);
652         // Make sure all activities have separate groups mode now.
653         $this->assertEquals(SEPARATEGROUPS, groups_get_activity_groupmode($cm1));
654         $this->assertEquals(SEPARATEGROUPS, groups_get_activity_groupmode($cm1, $course1));
655         $this->assertEquals(SEPARATEGROUPS, groups_get_activity_groupmode($modinfo->cms[$cm1->id]));
656         $this->assertEquals(SEPARATEGROUPS, groups_get_activity_groupmode($cm2));
657         $this->assertEquals(SEPARATEGROUPS, groups_get_activity_groupmode($cm2, $course1));
658         $this->assertEquals(SEPARATEGROUPS, groups_get_activity_groupmode($modinfo->cms[$cm2->id]));
659         $this->assertEquals(SEPARATEGROUPS, groups_get_activity_groupmode($cm3));
660         $this->assertEquals(SEPARATEGROUPS, groups_get_activity_groupmode($cm3, $course1));
661         $this->assertEquals(SEPARATEGROUPS, groups_get_activity_groupmode($modinfo->cms[$cm3->id]));
662     }