Permalink
Browse files

MDL-37100 webservices: PHPunit tests for core_calendar_get_calendar_e…

…vents()
  • Loading branch information...
1 parent 475896b commit 7c30224aeb993a81a0bb73081651fa5363e8e897 @ankitagarwal ankitagarwal committed Jan 18, 2013
Showing with 108 additions and 20 deletions.
  1. +22 −19 calendar/externallib.php
  2. +86 −1 calendar/tests/externallib_tests.php
View
@@ -206,35 +206,38 @@ public static function get_calendar_events($events = array(), $options = array()
$funcparam['courses'][] = $SITE->id;
}
- $events = calendar_get_events($params['options']['timestart'], $params['options']['timeend'], $funcparam['users'], $funcparam['groups'],
+ $eventlist = calendar_get_events($params['options']['timestart'], $params['options']['timeend'], $funcparam['users'], $funcparam['groups'],
$funcparam['courses'], true, $params['options']['ignorehidden']);
+ // WS expects arrays.
+ $events = array();
+ foreach ($eventlist as $id => $event) {
+ $events[$id] = (array) $event;
+ }
// We need to get events asked for eventids.
$eventsbyid = calendar_get_events_by_id($params['events']['eventids']);
- foreach ($eventsbyid as $eventid => $event) {
+ foreach ($eventsbyid as $eventid => $eventobj) {
+ $event = (array) $eventobj;
+ if (isset($events[$eventid])) {
+ continue;
+ }
if ($hassystemcap) {
// User can see everything, no further check is needed.
- if (!in_array($event, $events)) {
- $events[] = (array)$event;
- }
- } else if (!empty($event->modulename)) {
- $cm = get_coursemodule_from_instance($event->modulename, $event->instance);
- if (!groups_course_module_visible($cm)) {
- unset($eventsbyid[$eventid]);
- } else if (!in_array($event, $events)) {
- $events[] = (array)$event;
+ $events[$eventid] = $event;
+ } else if (!empty($eventobj->modulename)) {
+ $cm = get_coursemodule_from_instance($eventobj->modulename, $eventobj->instance);
+ if (groups_course_module_visible($cm)) {
+ $events[$eventid] = $event;
}
} else {
// Can the user actually see this event?
- $eventobj = calendar_event::load($eventid);
- if (!in_array($event, $events) ||
- ($hassystemcap) ||
- ($event->courseid == $SITE->id) ||
- (!empty($event->groupid) && in_array($event->groupid, $groups)) ||
- (!empty($event->courseid) && in_array($event->courseid, $courses)) ||
- ($USER->id == $event->userid) ||
+ $eventobj = calendar_event::load($eventobj);
+ if (($eventobj->courseid == $SITE->id) ||
+ (!empty($eventobj->groupid) && in_array($eventobj->groupid, $groups)) ||
+ (!empty($eventobj->courseid) && in_array($eventobj->courseid, $courses)) ||
+ ($USER->id == $eventobj->userid) ||
(calendar_edit_event_allowed($eventid))) {
- $events[] = (array)$event;
+ $events[$eventid] = $event;
}
}
}
@@ -253,5 +253,90 @@ public function test_delete_calendar_events() {
);
core_calendar_external::delete_calendar_events($events);
}
+}
-}
+ /**
+ * Test delete_courses
+ */
+ public function test_get_calendar_events() {
+ global $DB, $USER;
+
+ $this->resetAfterTest(true);
+ $this->setAdminUser();
+
+ // Create a few stuff to test with.
+ $user = $this->getDataGenerator()->create_user();
+ $course = $this->getDataGenerator()->create_course();
+ $record = new stdClass();
+ $record->courseid = $course->id;
+ $group = $this->getDataGenerator()->create_group($record);
+
+ $beforecount = $DB->count_records('event');
+
+ // Let's create a few events.
+ $siteevent = $this->getDataGenerator()->create_calendar_event('site', $USER->id, 'site');
+ $record = new stdClass();
+ $record->courseid = $course->id;
+ $courseevent = $this->getDataGenerator()->create_calendar_event('course', $USER->id, 'course', 2, time(), $record);
+ $userevent = $this->getDataGenerator()->create_calendar_event('user', $USER->id);
+ $record = new stdClass();
+ $record->courseid = $course->id;
+ $record->groupid = $group->id;
+ $groupevent = $this->getDataGenerator()->create_calendar_event('group', $USER->id, 'group', 0, time(), $record);
+
+ $paramevents = array ('eventids' => array($siteevent->id), 'courseids' => array($course->id), 'groupids' => array($group->id));
+ $options = array ('siteevents' => true, 'userevents' => true);
+ $events = core_calendar_external::get_calendar_events($paramevents, $options);
+
+ // Check to see if we got all events.
+ $this->assertEquals(4, count($events));
+ $options = array ('siteevents' => true, 'userevents' => true, 'timeend' => time() + 7*WEEKSECS);
+ $events = core_calendar_external::get_calendar_events($paramevents, $options);
+ $this->assertEquals(5, count($events));
+
+ // Let's play around with caps.
+ $this->setUser($user);
+ $events = core_calendar_external::get_calendar_events($paramevents, $options);
+ $this->assertEquals(2, count($events)); // site, user.
+
+ $role = $DB->get_record('role', array('shortname' => 'student'));
+ $this->getDataGenerator()->enrol_user($user->id, $course->id, $role->id);
+ $events = core_calendar_external::get_calendar_events($paramevents, $options);
+ $this->assertEquals(4, count($events)); // site, user, both course events.
+
+ $options = array ('siteevents' => true, 'userevents' => true);
+ $events = core_calendar_external::get_calendar_events($paramevents, $options);
+ $this->assertEquals(3, count($events)); // site, user, one course event.
+
+ groups_add_member($group, $user);
+ $events = core_calendar_external::get_calendar_events($paramevents, $options);
+ $this->assertEquals(4, count($events)); // site, user, group, one course event.
+
+ $paramevents = array ('courseids' => array($course->id), 'groupids' => array($group->id));
+ $events = core_calendar_external::get_calendar_events($paramevents, $options);
+ $this->assertEquals(4, count($events)); // site, user, group, one course event.
+
+ $paramevents = array ('groupids' => array($group->id));
+ $events = core_calendar_external::get_calendar_events($paramevents, $options);
+ $this->assertEquals(3, count($events)); // site, user, group.
+
+ $paramevents = array ();
+ $events = core_calendar_external::get_calendar_events($paramevents, $options);
+ $this->assertEquals(2, count($events)); // site, user.
+
+ $paramevents = array ();
+ $options = array ('siteevents' => false, 'userevents' => false, 'timeend' => time() + 7*WEEKSECS);
+ $events = core_calendar_external::get_calendar_events($paramevents, $options);
+ $this->assertEquals(0, count($events)); // nothing returned.
+
+ $paramevents = array ('eventids' => array($siteevent->id, $groupevent->id));
+ $options = array ('siteevents' => false, 'userevents' => false, 'timeend' => time() + 7*WEEKSECS);
+ $events = core_calendar_external::get_calendar_events($paramevents, $options);
+ $this->assertEquals(2, count($events)); // site, group.
+
+ $paramevents = array ('eventids' => array($siteevent->id));
+ $events = core_calendar_external::get_calendar_events($paramevents, $options);
+ $this->assertEquals(1, count($events)); // site.
+
+ }
+}

0 comments on commit 7c30224

Please sign in to comment.