Skip to content

Commit

Permalink
MDL-61450 calendar: Unit test for calendar_view_event_allowed()
Browse files Browse the repository at this point in the history
* Unit test for calendar_view_event_allowed() when dealing with course
events.
  • Loading branch information
junpataleta authored and lameze committed Oct 3, 2019
1 parent ec48255 commit ec72061
Showing 1 changed file with 68 additions and 0 deletions.
68 changes: 68 additions & 0 deletions calendar/tests/lib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -772,4 +772,72 @@ public function test_calendar_set_filters_logged_in_another_user() {
$this->assertEquals(array($coursegroups[$courses[0]->id][1]->id), $groupids);
$this->assertEquals($users[1]->id, $userid);
}

/**
* Test for calendar_view_event_allowed for course event types.
*/
public function test_calendar_view_event_allowed_course_event() {
global $USER;

$this->setAdminUser();

$generator = $this->getDataGenerator();

// A student in a course.
$student = $generator->create_user();
// Some user not enrolled in any course.
$someuser = $generator->create_user();

// A course with manual enrolments.
$manualcourse = $generator->create_course();

// Enrol the student to the manual enrolment course.
$generator->enrol_user($student->id, $manualcourse->id);

// A course that allows guest access.
$guestcourse = $generator->create_course(
(object)[
'shortname' => 'guestcourse',
'enrol_guest_status_0' => ENROL_INSTANCE_ENABLED,
'enrol_guest_password_0' => ''
]);

$manualevent = (object)[
'name' => 'Manual course event',
'description' => '',
'format' => 1,
'categoryid' => 0,
'courseid' => $manualcourse->id,
'groupid' => 0,
'userid' => $USER->id,
'modulename' => 0,
'instance' => 0,
'eventtype' => 'course',
'timestart' => time(),
'timeduration' => 86400,
'visible' => 1
];
$caleventmanual = calendar_event::create($manualevent, false);

// Create a course event for the course with guest access.
$guestevent = clone $manualevent;
$guestevent->name = 'Guest course event';
$guestevent->courseid = $guestcourse->id;
$caleventguest = calendar_event::create($guestevent, false);

// Viewing as admin.
$this->assertTrue(calendar_view_event_allowed($caleventmanual));
$this->assertTrue(calendar_view_event_allowed($caleventguest));

// Viewing as someone enrolled in a course.
$this->setUser($student);
$this->assertTrue(calendar_view_event_allowed($caleventmanual));

// Viewing as someone not enrolled in any course.
$this->setUser($someuser);
// Viewing as someone not enrolled in a course without guest access on.
$this->assertFalse(calendar_view_event_allowed($caleventmanual));
// Viewing as someone not enrolled in a course with guest access on.
$this->assertTrue(calendar_view_event_allowed($caleventguest));
}
}

0 comments on commit ec72061

Please sign in to comment.