MDL-40354 groups: Add unit tests for the api groups_group_visible()
This commit is contained in:
@@ -405,4 +405,187 @@ class grouplib_testcase extends advanced_testcase {
|
||||
$this->assertContains($group3->id, $groupkeys);
|
||||
$this->assertContains($group4->id, $groupkeys);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests for groups_group_visible.
|
||||
*/
|
||||
public function test_groups_group_visible() {
|
||||
global $CFG, $DB;
|
||||
|
||||
$generator = $this->getDataGenerator();
|
||||
$this->resetAfterTest();
|
||||
$this->setAdminUser();
|
||||
|
||||
// Create a course category, course and groups.
|
||||
$cat = $generator->create_category(array('parent' => 0));
|
||||
$course = $generator->create_course(array('category' => $cat->id));
|
||||
$coursecontext = context_course::instance($course->id);
|
||||
$group1 = $generator->create_group(array('courseid' => $course->id, 'name' => 'Group 1'));
|
||||
$group2 = $generator->create_group(array('courseid' => $course->id, 'name' => 'Group 2'));
|
||||
$group3 = $generator->create_group(array('courseid' => $course->id, 'name' => 'Group 3'));
|
||||
$group4 = $generator->create_group(array('courseid' => $course->id, 'name' => 'Group 4'));
|
||||
|
||||
// Create cm.
|
||||
$assign = $generator->create_module("assign", array('course' => $course->id));
|
||||
$cm = get_coursemodule_from_instance("assign", $assign->id);
|
||||
|
||||
// Create users.
|
||||
$user1 = $generator->create_user();
|
||||
$user2 = $generator->create_user();
|
||||
$user3 = $generator->create_user();
|
||||
|
||||
// Enrol users into the course.
|
||||
$generator->enrol_user($user1->id, $course->id);
|
||||
$generator->enrol_user($user2->id, $course->id);
|
||||
|
||||
// Assign groups.
|
||||
groups_add_member($group1, $user2);
|
||||
|
||||
// Give capability at course level to the user to access all groups.
|
||||
$role = $DB->get_field("role", "id", array("shortname" => "manager"));
|
||||
$generator->enrol_user($user3->id, $course->id, $role);
|
||||
// Make sure the user has the capability.
|
||||
assign_capability('moodle/site:accessallgroups', CAP_ALLOW, $role, $coursecontext->id);
|
||||
|
||||
// No groups , not forced.
|
||||
$result = groups_group_visible($group1->id, $course, null, $user1->id);
|
||||
$this->assertTrue($result);
|
||||
$result = groups_group_visible(0, $course, null, $user1->id);
|
||||
$this->assertTrue($result); // Requesting all groups.
|
||||
|
||||
$result = groups_group_visible($group1->id, $course, $cm, $user1->id);
|
||||
$this->assertTrue($result); // Cm with no groups.
|
||||
|
||||
$cm->groupmode = SEPARATEGROUPS;
|
||||
$result = groups_group_visible($group1->id, $course, $cm, $user1->id);
|
||||
$this->assertFalse($result); // Cm with separate groups.
|
||||
$result = groups_group_visible($group1->id, $course, $cm, $user2->id);
|
||||
$this->assertTrue($result); // Cm with separate groups.
|
||||
|
||||
$cm->groupmode = VISIBLEGROUPS;
|
||||
$result = groups_group_visible($group1->id, $course, $cm, $user1->id);
|
||||
$this->assertTrue($result); // Cm with visible groups.
|
||||
|
||||
// No groups, forced.
|
||||
$course->groupmode = NOGROUPS;
|
||||
$course->groupmodeforce = true;
|
||||
update_course($course);
|
||||
$result = groups_group_visible($group1->id, $course, null, $user1->id);
|
||||
$this->assertTrue($result);
|
||||
$result = groups_group_visible(0, $course, null, $user1->id);
|
||||
$this->assertTrue($result); // Requesting all groups.
|
||||
|
||||
$result = groups_group_visible($group1->id, $course, $cm, $user1->id);
|
||||
$this->assertTrue($result); // Cm with no groups.
|
||||
|
||||
$cm->groupmode = SEPARATEGROUPS;
|
||||
$result = groups_group_visible($group1->id, $course, $cm, $user1->id);
|
||||
$this->assertTrue($result); // Cm with separate groups.
|
||||
$result = groups_group_visible($group1->id, $course, $cm, $user2->id);
|
||||
$this->assertTrue($result); // Cm with separate groups.
|
||||
|
||||
$cm->groupmode = SEPARATEGROUPS;
|
||||
$result = groups_group_visible($group1->id, $course, $cm, $user1->id);
|
||||
$this->assertTrue($result); // Cm with visible groups.
|
||||
|
||||
// Visible groups, forced.
|
||||
$course->groupmode = VISIBLEGROUPS;
|
||||
$course->groupmodeforce = true;
|
||||
update_course($course);
|
||||
$result = groups_group_visible($group1->id, $course, null, $user1->id);
|
||||
$this->assertTrue($result);
|
||||
$result = groups_group_visible(0, $course, null, $user1->id);
|
||||
$this->assertTrue($result); // Requesting all groups.
|
||||
|
||||
$cm->groupmode = NOGROUPS;
|
||||
$result = groups_group_visible($group1->id, $course, $cm, $user1->id);
|
||||
$this->assertTrue($result); // Cm with no groups.
|
||||
|
||||
$cm->groupmode = SEPARATEGROUPS;
|
||||
$result = groups_group_visible($group1->id, $course, $cm, $user1->id);
|
||||
$this->assertTrue($result); // Cm with separate groups.
|
||||
$result = groups_group_visible($group1->id, $course, $cm, $user2->id);
|
||||
$this->assertTrue($result); // Cm with separate groups.
|
||||
|
||||
$cm->groupmode = VISIBLEGROUPS;
|
||||
$result = groups_group_visible($group1->id, $course, $cm, $user1->id);
|
||||
$this->assertTrue($result); // Cm with visible groups.
|
||||
|
||||
// Visible groups, not forced.
|
||||
$course->groupmode = VISIBLEGROUPS;
|
||||
$course->groupmodeforce = false;
|
||||
update_course($course);
|
||||
$result = groups_group_visible($group1->id, $course, null, $user1->id);
|
||||
$this->assertTrue($result);
|
||||
$result = groups_group_visible(0, $course, null, $user1->id);
|
||||
$this->assertTrue($result); // Requesting all groups.
|
||||
|
||||
$cm->groupmode = NOGROUPS;
|
||||
$result = groups_group_visible($group1->id, $course, $cm, $user1->id);
|
||||
$this->assertTrue($result); // Cm with no groups.
|
||||
|
||||
$cm->groupmode = SEPARATEGROUPS;
|
||||
$result = groups_group_visible($group1->id, $course, $cm, $user1->id);
|
||||
$this->assertFalse($result); // Cm with separate groups.
|
||||
$result = groups_group_visible($group1->id, $course, $cm, $user2->id);
|
||||
$this->assertTrue($result); // Cm with separate groups.
|
||||
|
||||
$cm->groupmode = VISIBLEGROUPS;
|
||||
$result = groups_group_visible($group1->id, $course, $cm, $user1->id);
|
||||
$this->assertTrue($result); // Cm with visible groups.
|
||||
|
||||
// Separate groups, forced.
|
||||
$course->groupmode = SEPARATEGROUPS;
|
||||
$course->groupmodeforce = true;
|
||||
update_course($course);
|
||||
$result = groups_group_visible($group1->id, $course, null, $user1->id);
|
||||
$this->assertFalse($result);
|
||||
$result = groups_group_visible($group1->id, $course, null, $user2->id);
|
||||
$this->assertTrue($result);
|
||||
$result = groups_group_visible(0, $course, null, $user2->id);
|
||||
$this->assertFalse($result); // Requesting all groups.
|
||||
$result = groups_group_visible(0, $course, null, $user3->id);
|
||||
$this->assertTrue($result); // Requesting all groups.
|
||||
|
||||
$cm->groupmode = NOGROUPS;
|
||||
$result = groups_group_visible($group1->id, $course, $cm, $user1->id);
|
||||
$this->assertFalse($result); // Cm with no groups.
|
||||
|
||||
$cm->groupmode = SEPARATEGROUPS;
|
||||
$result = groups_group_visible($group1->id, $course, $cm, $user1->id);
|
||||
$this->assertFalse($result); // Cm with separate groups.
|
||||
$result = groups_group_visible($group1->id, $course, $cm, $user2->id);
|
||||
$this->assertTrue($result); // Cm with separate groups.
|
||||
|
||||
$cm->groupmode = VISIBLEGROUPS;
|
||||
$result = groups_group_visible($group1->id, $course, $cm, $user1->id);
|
||||
$this->assertFalse($result); // Cm with visible groups.
|
||||
|
||||
// Separate groups, not forced.
|
||||
$course->groupmode = SEPARATEGROUPS;
|
||||
$course->groupmodeforce = false;
|
||||
update_course($course);
|
||||
$result = groups_group_visible($group1->id, $course, null, $user1->id);
|
||||
$this->assertFalse($result);
|
||||
$result = groups_group_visible($group1->id, $course, null, $user2->id);
|
||||
$this->assertTrue($result);
|
||||
$result = groups_group_visible(0, $course, null, $user2->id);
|
||||
$this->assertFalse($result); // Requesting all groups.
|
||||
$result = groups_group_visible(0, $course, null, $user3->id);
|
||||
$this->assertTrue($result); // Requesting all groups.
|
||||
|
||||
$cm->groupmode = NOGROUPS;
|
||||
$result = groups_group_visible($group1->id, $course, $cm, $user1->id);
|
||||
$this->assertTrue($result); // Cm with no groups.
|
||||
|
||||
$cm->groupmode = SEPARATEGROUPS;
|
||||
$result = groups_group_visible($group1->id, $course, $cm, $user1->id);
|
||||
$this->assertFalse($result); // Cm with separate groups.
|
||||
$result = groups_group_visible($group1->id, $course, $cm, $user2->id);
|
||||
$this->assertTrue($result); // Cm with separate groups.
|
||||
|
||||
$cm->groupmode = VISIBLEGROUPS;
|
||||
$result = groups_group_visible($group1->id, $course, $cm, $user1->id);
|
||||
$this->assertTrue($result); // Cm with visible groups.
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user