diff --git a/calendar/lib.php b/calendar/lib.php index 43e9de89abc..f378c102b0a 100644 --- a/calendar/lib.php +++ b/calendar/lib.php @@ -3016,7 +3016,7 @@ function calendar_get_icalendar($url) { * Import events from an iCalendar object into a course calendar. * * @param iCalendar $ical The iCalendar object. - * @param int $courseid The course ID for the calendar. + * @param int $unused Deprecated * @param int $subscriptionid The subscription ID. * @return string A log of the import progress, including errors. */ @@ -3066,7 +3066,8 @@ function calendar_import_icalendar_events($ical, $unused = null, $subscriptionid } } - if (!empty($subscriptionid)) { + $existing = $DB->get_field('event_subscriptions', 'lastupdated', ['id' => $subscriptionid]); + if (!empty($existing)) { $eventsuuids = $DB->get_records_menu('event', ['subscriptionid' => $subscriptionid], '', 'id, uuid'); $icaleventscount = count($icaluuids); diff --git a/calendar/tests/lib_test.php b/calendar/tests/lib_test.php index 96af6075ba8..94a3cb4c9f4 100644 --- a/calendar/tests/lib_test.php +++ b/calendar/tests/lib_test.php @@ -227,6 +227,24 @@ class core_calendar_lib_testcase extends advanced_testcase { calendar_import_icalendar_events($ical, null, $sub->id); $count = $DB->count_records('event', array('subscriptionid' => $sub->id)); $this->assertEquals($count, 1); + + // Test for ICS file with repeated events. + $subscription = new stdClass(); + $subscription->name = 'Repeated events'; + $subscription->importfrom = CALENDAR_IMPORT_FROM_FILE; + $subscription->eventtype = 'site'; + $id = calendar_add_subscription($subscription); + $calendar = file_get_contents($CFG->dirroot . '/lib/tests/fixtures/repeated_events.ics'); + $ical = new iCalendar(); + $ical->unserialize($calendar); + $this->assertEquals($ical->parser_errors, []); + + $sub = calendar_get_subscription($id); + $output = calendar_import_icalendar_events($ical, null, $sub->id); + $this->assertStringNotContainsString('Events deleted: 17', $output); + $this->assertStringContainsString('Events imported: 1', $output); + $this->assertStringContainsString('Events skipped: 0', $output); + $this->assertStringContainsString('Events updated: 0', $output); } /** diff --git a/lib/tests/fixtures/repeated_events.ics b/lib/tests/fixtures/repeated_events.ics new file mode 100644 index 00000000000..7157007a5a2 --- /dev/null +++ b/lib/tests/fixtures/repeated_events.ics @@ -0,0 +1,34 @@ +BEGIN:VCALENDAR +PRODID:QIS-LSF HIS eG +VERSION:2.0 +BEGIN:VTIMEZONE +TZID:Europe/Berlin +X-LIC-LOCATION:Europe/Berlin +BEGIN:DAYLIGHT +TZOFFSETFROM:+0100 +TZOFFSETTO:+0200 +TZNAME:CEST +DTSTART:19700329T020000 +RRULE:FREQ=YEARLY;BYDAY=-1SU;BYMONTH=3 +END:DAYLIGHT +BEGIN:STANDARD +TZOFFSETFROM:+0200 +TZOFFSETTO:+0100 +TZNAME:CET +DTSTART:19701025T030000 +RRULE:FREQ=YEARLY;BYDAY=-1SU;BYMONTH=10 +END:STANDARD +END:VTIMEZONE +METHOD:PUBLISH +BEGIN:VEVENT +DTSTART;TZID=Europe/Berlin:20191015T160000 +DTEND;TZID=Europe/Berlin:20191015T180000 +RRULE:FREQ=WEEKLY;UNTIL=20200211T235900Z;INTERVAL=1;BYDAY=TU +LOCATION:O27 - 122 +DTSTAMP:20191002T151421Z +UID:115808212972 +DESCRIPTION: +SUMMARY:CS6307.000 - Introduction to Computer Science (for Non-Computer Scientists) +CATEGORIES:Vorlesung/ Übung +END:VEVENT +END:VCALENDAR