MDL-59382 calendar: add modal to create and update events

This commit is contained in:
Ryan Wyllie 2017-07-24 08:01:14 +00:00
parent 6103fd2efe
commit aa0912258d
23 changed files with 2392 additions and 157 deletions

View file

@ -409,4 +409,267 @@ class core_calendar_lib_testcase extends advanced_testcase {
$events = calendar_get_legacy_events($timestart, $timeend, true, true, true);
$this->assertCount(3, $events);
}
}
public function test_calendar_get_all_allowed_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_all_allowed_types();
$this->assertEmpty($types);
}
public function test_calendar_get_all_allowed_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_all_allowed_types();
$this->assertTrue($types['user']);
assign_capability('moodle/calendar:manageownentries', CAP_PROHIBIT, $roleid, $context, true);
$types = calendar_get_all_allowed_types();
$this->assertArrayNotHasKey('user', $types);
}
public function test_calendar_get_all_allowed_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_all_allowed_types();
$this->assertTrue($types['site']);
assign_capability('moodle/calendar:manageentries', CAP_PROHIBIT, $roleid, $context, true);
$types = calendar_get_all_allowed_types();
$this->assertArrayNotHasKey('site', $types);
}
public function test_calendar_get_all_allowed_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_all_allowed_types();
$typecourses = $types['course'];
$this->assertCount(1, $typecourses);
$this->assertEquals($course1->id, $typecourses[0]->id);
assign_capability('moodle/calendar:manageentries', CAP_ALLOW, $roleid, $context2, 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_all_allowed_types();
$typecourses = $types['course'];
// Sort the results by id ascending to ensure the test is consistent
// and repeatable.
usort($typecourses, function($a, $b) {
$aid = $a->id;
$bid = $b->id;
if ($aid == $bid) {
return 0;
}
return ($aid < $bid) ? -1 : 1;
});
$this->assertCount(2, $typecourses);
$this->assertEquals($course1->id, $typecourses[0]->id);
$this->assertEquals($course2->id, $typecourses[1]->id);
}
public function test_calendar_get_all_allowed_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_all_allowed_types();
$typecourses = $types['course'];
$this->assertCount(1, $typecourses);
$this->assertEquals($course->id, $typecourses[0]->id);
$this->assertArrayNotHasKey('group', $types);
$this->assertArrayNotHasKey('groupcourses', $types);
}
public function test_calendar_get_all_allowed_types_group_no_acces_to_diff_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);
$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_all_allowed_types();
$typecourses = $types['course'];
$this->assertCount(1, $typecourses);
$this->assertEquals($course->id, $typecourses[0]->id);
$this->assertArrayNotHasKey('group', $types);
$this->assertArrayNotHasKey('groupcourses', $types);
}
public function test_calendar_get_all_allowed_types_group_access_all_groups() {
$generator = $this->getDataGenerator();
$user = $generator->create_user();
$course1 = $generator->create_course();
$course2 = $generator->create_course();
$context1 = context_course::instance($course1->id);
$context2 = context_course::instance($course2->id);
$group1 = $generator->create_group(array('courseid' => $course1->id));
$group2 = $generator->create_group(array('courseid' => $course1->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_all_allowed_types();
$typecourses = $types['course'];
$typegroups = $types['group'];
$typegroupcourses = $types['groupcourses'];
$idascfunc = function($a, $b) {
$aid = $a->id;
$bid = $b->id;
if ($aid == $bid) {
return 0;
}
return ($aid < $bid) ? -1 : 1;
};
// Sort the results by id ascending to ensure the test is consistent
// and repeatable.
usort($typecourses, $idascfunc);
usort($typegroups, $idascfunc);
$this->assertCount(2, $typecourses);
$this->assertEquals($course1->id, $typecourses[0]->id);
$this->assertEquals($course2->id, $typecourses[1]->id);
$this->assertCount(1, $typegroupcourses);
$this->assertEquals($course1->id, $typegroupcourses[0]->id);
$this->assertCount(2, $typegroups);
$this->assertEquals($group1->id, $typegroups[0]->id);
$this->assertEquals($group2->id, $typegroups[1]->id);
}
public function test_calendar_get_all_allowed_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));
$group3 = $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/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 can't access
// groups that they are not a member of.
$types = calendar_get_all_allowed_types();
$typegroups = $types['group'];
$typegroupcourses = $types['groupcourses'];
$idascfunc = function($a, $b) {
$aid = $a->id;
$bid = $b->id;
if ($aid == $bid) {
return 0;
}
return ($aid < $bid) ? -1 : 1;
};
// Sort the results by id ascending to ensure the test is consistent
// and repeatable.
usort($typegroups, $idascfunc);
$this->assertCount(1, $typegroupcourses);
$this->assertEquals($course->id, $typegroupcourses[0]->id);
$this->assertCount(2, $typegroups);
$this->assertEquals($group1->id, $typegroups[0]->id);
$this->assertEquals($group2->id, $typegroups[1]->id);
}
}