MDL-48495 curl: Limit usage of curl to HTTP and HTTPS protocols
[moodle.git] / lib / tests / grouplib_test.php
blob5c0d0f3c2bf60d87b9cf301e8c33ecac509eefe2
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.
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 groups subsystems.
20 * @package core_group
21 * @category phpunit
22 * @copyright 2007 onwards Martin Dougiamas (http://dougiamas.com)
23 * @author Andrew Nicols
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 defined('MOODLE_INTERNAL') || die();
29 /**
30 * Unit tests for lib/grouplib.php
31 * @group core_group
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));
188 $name1 = 'Name 1';
189 $name2 = 'Name 2';
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));
214 // Delete a group.
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));
278 // Delete a group.
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));
328 // Get the data.
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.
357 $grouping1maps = 0;
358 $grouping2maps = 0;
359 $group1maps = 0;
360 $group2maps = 0;
361 $group3maps = 0;
362 $group4maps = 0;
363 foreach ($data->mappings as $mapping) {
364 if ($mapping->groupingid === $grouping1->id) {
365 $grouping1maps++;
366 $this->assertContains($mapping->groupid, array($group1->id, $group2->id));
367 } else if ($mapping->groupingid === $grouping2->id) {
368 $grouping2maps++;
369 $this->assertContains($mapping->groupid, array($group3->id, $group4->id));
370 } else {
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() {
425 global $CFG, $DB;
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'));
440 // Create cm.
441 $assign = $generator->create_module("assign", array('course' => $course->id));
442 $cm = get_coursemodule_from_instance("assign", $assign->id);
444 // Create users.
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);
453 // Assign groups.
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() {
609 global $DB;
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() {
680 global $SESSION;
682 $this->resetAfterTest();
684 // Generate data.
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();
778 // Create courses.
779 $course1 = $generator->create_course();
780 $course2 = $generator->create_course();
782 // Create users.
783 $user1 = $generator->create_user();
784 $user2 = $generator->create_user();
785 $user3 = $generator->create_user();
787 // Enrol users.
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);
793 // Create groups.
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)));
802 // Get user groups.
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]);