MDL-60826 calendar: tests for calendar_get_allowed_event_types

This commit is contained in:
Simey Lameze 2018-07-16 10:06:31 +08:00
parent 4be55b06da
commit 633c5ffbfe

View File

@ -765,4 +765,162 @@ class core_calendar_lib_testcase extends advanced_testcase {
$this->assertEquals($event->id, $data->events[0]->id);
}
public function test_calendar_get_allowed_event_types_no_types() {
$generator = $this->getDataGenerator();
$user = $generator->create_user();
$systemcontext = context_system::instance();
$sitecontext = context_course::instance(SITEID);
$roleid = $generator->create_role();
$generator->role_assign($roleid, $user->id, $systemcontext->id);
$generator->role_assign($roleid, $user->id, $sitecontext->id);
$this->setUser($user);
assign_capability('moodle/calendar:manageentries', CAP_PROHIBIT, $roleid, $sitecontext, true);
assign_capability('moodle/calendar:manageownentries', CAP_PROHIBIT, $roleid, $systemcontext, true);
$types = calendar_get_allowed_event_types();
foreach ($types as $allowed) {
$this->assertFalse($allowed);
}
}
public function test_calendar_get_allowed_event_types_user() {
$generator = $this->getDataGenerator();
$user = $generator->create_user();
$context = context_system::instance();
$roleid = $generator->create_role();
$generator->role_assign($roleid, $user->id, $context->id);
$this->setUser($user);
assign_capability('moodle/calendar:manageownentries', CAP_ALLOW, $roleid, $context, true);
$types = calendar_get_allowed_event_types();
$this->assertTrue($types['user']);
assign_capability('moodle/calendar:manageownentries', CAP_PROHIBIT, $roleid, $context, true);
$types = calendar_get_allowed_event_types();
$this->assertFalse($types['user']);
}
public function test_calendar_get_allowed_event_types_site() {
$generator = $this->getDataGenerator();
$user = $generator->create_user();
$context = context_course::instance(SITEID);
$roleid = $generator->create_role();
$generator->role_assign($roleid, $user->id, $context->id);
$this->setUser($user);
assign_capability('moodle/calendar:manageentries', CAP_ALLOW, $roleid, $context, true);
$types = calendar_get_allowed_event_types();
$this->assertTrue($types['site']);
assign_capability('moodle/calendar:manageentries', CAP_PROHIBIT, $roleid, $context, true);
$types = calendar_get_allowed_event_types();
$this->assertFalse($types['site']);
}
public function test_calendar_get_allowed_event_types_course() {
$generator = $this->getDataGenerator();
$user = $generator->create_user();
$course1 = $generator->create_course(); // Has capability.
$course2 = $generator->create_course(); // Doesn't have capability.
$course3 = $generator->create_course(); // Not enrolled.
$context1 = context_course::instance($course1->id);
$context2 = context_course::instance($course2->id);
$context3 = context_course::instance($course3->id);
$roleid = $generator->create_role();
$contexts = [$context1, $context2, $context3];
$enrolledcourses = [$course1, $course2];
foreach ($enrolledcourses as $course) {
$generator->enrol_user($user->id, $course->id, 'student');
}
foreach ($contexts as $context) {
$generator->role_assign($roleid, $user->id, $context->id);
}
$this->setUser($user);
assign_capability('moodle/calendar:manageentries', CAP_ALLOW, $roleid, $context1, true);
assign_capability('moodle/calendar:manageentries', CAP_PROHIBIT, $roleid, $context2, true);
// The user only has the correct capability in course 1 so that is the only
// one that should be in the results.
$types = calendar_get_allowed_event_types($course1->id);
$this->assertTrue($types['course']);
assign_capability('moodle/calendar:manageentries', CAP_PROHIBIT, $roleid, $context1, true);
// The user only now has the correct capability in both course 1 and 2 so we
// expect both to be in the results.
$types = calendar_get_allowed_event_types($course1->id);
$this->assertFalse($types['course']);
}
public function test_calendar_get_allowed_event_types_group_no_groups() {
$generator = $this->getDataGenerator();
$user = $generator->create_user();
$course = $generator->create_course();
$context = context_course::instance($course->id);
$roleid = $generator->create_role();
$generator->enrol_user($user->id, $course->id, 'student');
$generator->role_assign($roleid, $user->id, $context->id);
$this->setUser($user);
assign_capability('moodle/calendar:manageentries', CAP_ALLOW, $roleid, $context, true);
// The user has the correct capability in the course but there are
// no groups so we shouldn't see a group type.
$types = calendar_get_allowed_event_types($course->id);
$this->assertTrue($types['course']);
}
public function test_calendar_get_allowed_event_types_group_no_acces_to_diff_groups() {
$generator = $this->getDataGenerator();
$user = $generator->create_user();
$course = $generator->create_course();
$context = context_course::instance($course->id);
$roleid = $generator->create_role();
$generator->enrol_user($user->id, $course->id, 'student');
$generator->role_assign($roleid, $user->id, $context->id);
$this->setUser($user);
assign_capability('moodle/calendar:manageentries', CAP_ALLOW, $roleid, $context, true);
assign_capability('moodle/site:accessallgroups', CAP_PROHIBIT, $roleid, $context, true);
// The user has the correct capability in the course but they aren't a member
// of any of the groups and don't have the accessallgroups capability.
$types = calendar_get_allowed_event_types($course->id);
$this->assertTrue($types['course']);
$this->assertFalse($types['group']);
}
public function test_calendar_get_allowed_event_types_group_access_all_groups() {
$generator = $this->getDataGenerator();
$user = $generator->create_user();
$course1 = $generator->create_course();
$course2 = $generator->create_course();
$generator->create_group(array('courseid' => $course1->id));
$generator->create_group(array('courseid' => $course2->id));
$context1 = context_course::instance($course1->id);
$context2 = context_course::instance($course2->id);
$roleid = $generator->create_role();
$generator->enrol_user($user->id, $course1->id, 'student');
$generator->enrol_user($user->id, $course2->id, 'student');
$generator->role_assign($roleid, $user->id, $context1->id);
$generator->role_assign($roleid, $user->id, $context2->id);
$this->setUser($user);
assign_capability('moodle/calendar:manageentries', CAP_ALLOW, $roleid, $context1, true);
assign_capability('moodle/calendar:manageentries', CAP_ALLOW, $roleid, $context2, true);
assign_capability('moodle/site:accessallgroups', CAP_ALLOW, $roleid, $context1, true);
assign_capability('moodle/site:accessallgroups', CAP_ALLOW, $roleid, $context2, true);
// The user has the correct capability in the course and has
// the accessallgroups capability.
$types = calendar_get_allowed_event_types($course1->id);
$this->assertTrue($types['group']);
}
public function test_calendar_get_allowed_event_types_group_no_access_all_groups() {
$generator = $this->getDataGenerator();
$user = $generator->create_user();
$course = $generator->create_course();
$context = context_course::instance($course->id);
$group1 = $generator->create_group(array('courseid' => $course->id));
$group2 = $generator->create_group(array('courseid' => $course->id));
$roleid = $generator->create_role();
$generator->enrol_user($user->id, $course->id, 'student');
$generator->role_assign($roleid, $user->id, $context->id);
$generator->create_group_member(array('groupid' => $group1->id, 'userid' => $user->id));
$generator->create_group_member(array('groupid' => $group2->id, 'userid' => $user->id));
$this->setUser($user);
assign_capability('moodle/site:accessallgroups', CAP_PROHIBIT, $roleid, $context, true);
// The user has the correct capability in the course but can't access
// groups that they are not a member of.
$types = calendar_get_allowed_event_types($course->id);
$this->assertFalse($types['group']);
assign_capability('moodle/calendar:manageentries', CAP_ALLOW, $roleid, $context, true);
assign_capability('moodle/site:accessallgroups', CAP_ALLOW, $roleid, $context, true);
$types = calendar_get_allowed_event_types($course->id);
$this->assertTrue($types['group']);
}
}