From 3b99bd66896b7d3283cba0be33c43a29542229a3 Mon Sep 17 00:00:00 2001 From: Oleg Demeshev Date: Fri, 10 Oct 2014 16:31:06 +1300 Subject: [PATCH] T-13119 general: Fixed Ical import Change-Id: Ic65f0c1bf4992522c78a3fdc13d1a6d58c0836aa --- calendar/lib.php | 4 ++- calendar/tests/calendarical_test.php | 59 ++++++++++++++++++++++++++++++++++ lib/bennu/iCalendar_components.php | 7 ++-- lib/bennu/iCalendar_parameters.php | 8 ++--- lib/bennu/iCalendar_properties.php | 2 ++ lib/tests/fixtures/google_gmail.ics | 21 ++++++++++++ lib/tests/fixtures/ms_outlook_2010.ics | 42 ++++++++++++++++++++++++ lib/tests/fixtures/osx_yosemite.ics | 36 +++++++++++++++++++++ 8 files changed, 170 insertions(+), 9 deletions(-) create mode 100644 lib/tests/fixtures/google_gmail.ics create mode 100644 lib/tests/fixtures/ms_outlook_2010.ics create mode 100644 lib/tests/fixtures/osx_yosemite.ics diff --git a/calendar/lib.php b/calendar/lib.php index 5057f52..ab21050 100644 --- a/calendar/lib.php +++ b/calendar/lib.php @@ -3083,7 +3083,9 @@ function calendar_import_icalendar_events($ical, $courseid, $subscriptionid = nu $updatecount = 0; // Large calendars take a while... - set_time_limit(300); + if (!CLI_SCRIPT) { + set_time_limit(300); + } // Mark all events in a subscription with a zero timestamp. if (!empty($subscriptionid)) { diff --git a/calendar/tests/calendarical_test.php b/calendar/tests/calendarical_test.php index 7b4e117..2f4b61f 100644 --- a/calendar/tests/calendarical_test.php +++ b/calendar/tests/calendarical_test.php @@ -74,4 +74,63 @@ class core_calendar_ical_testcase extends advanced_testcase { $this->setExpectedException('coding_exception'); calendar_update_subscription($subscription); } + + public function test_calendar_add_subscription() { + global $DB, $CFG; + + require_once($CFG->dirroot . '/lib/bennu/bennu.inc.php'); + + $this->resetAfterTest(true); + + // Test for Microsoft Outlook 2010. + $subscription = new stdClass(); + $subscription->eventtype = 'test'; + $subscription->name = 'Microsoft Outlook 2010'; + $subscription->importfrom = CALENDAR_IMPORT_FROM_FILE; + $id = calendar_add_subscription($subscription); + + $calendar = file_get_contents($CFG->dirroot . '/lib/tests/fixtures/ms_outlook_2010.ics'); + $ical = new iCalendar(); + $ical->unserialize($calendar); + $this->assertEquals($ical->parser_errors, array()); + + $sub = calendar_get_subscription($id); + $result = calendar_import_icalendar_events($ical, $sub->courseid, $sub->id); + $count = $DB->count_records('event', array('subscriptionid' => $sub->id)); + $this->assertEquals($count, 1); + + // Test for OSX Yosemite + $subscription = new stdClass(); + $subscription->eventtype = 'test'; + $subscription->name = 'Microsoft Outlook 2010'; + $subscription->importfrom = CALENDAR_IMPORT_FROM_FILE; + $id = calendar_add_subscription($subscription); + + $calendar = file_get_contents($CFG->dirroot . '/lib/tests/fixtures/osx_yosemite.ics'); + $ical = new iCalendar(); + $ical->unserialize($calendar); + $this->assertEquals($ical->parser_errors, array()); + + $sub = calendar_get_subscription($id); + $result = calendar_import_icalendar_events($ical, $sub->courseid, $sub->id); + $count = $DB->count_records('event', array('subscriptionid' => $sub->id)); + $this->assertEquals($count, 1); + + // Test for Google Gmail + $subscription = new stdClass(); + $subscription->eventtype = 'test'; + $subscription->name = 'Microsoft Outlook 2010'; + $subscription->importfrom = CALENDAR_IMPORT_FROM_FILE; + $id = calendar_add_subscription($subscription); + + $calendar = file_get_contents($CFG->dirroot . '/lib/tests/fixtures/google_gmail.ics'); + $ical = new iCalendar(); + $ical->unserialize($calendar); + $this->assertEquals($ical->parser_errors, array()); + + $sub = calendar_get_subscription($id); + $result = calendar_import_icalendar_events($ical, $sub->courseid, $sub->id); + $count = $DB->count_records('event', array('subscriptionid' => $sub->id)); + $this->assertEquals($count, 1); + } } diff --git a/lib/bennu/iCalendar_components.php b/lib/bennu/iCalendar_components.php index 1f6bf3d..73f362f 100644 --- a/lib/bennu/iCalendar_components.php +++ b/lib/bennu/iCalendar_components.php @@ -287,8 +287,10 @@ class iCalendar_component { if($parent_component == null) { $parent_component = $this; // If there's no components on the stack, use the iCalendar object } - if ($parent_component->add_component($component) === false) { - $this->parser_error("Failed to add component on line $key"); + if ($component !== null) { + if ($parent_component->add_component($component) === false) { + $this->parser_error("Failed to add component on line $key"); + } } if ($parent_component != $this) { // If we're not using the iCalendar array_push($components, $parent_component); // Put the component back on the stack @@ -312,7 +314,6 @@ class iCalendar_component { } } - } function clear_errors() { diff --git a/lib/bennu/iCalendar_parameters.php b/lib/bennu/iCalendar_parameters.php index d264083..af3f5ad 100644 --- a/lib/bennu/iCalendar_parameters.php +++ b/lib/bennu/iCalendar_parameters.php @@ -104,9 +104,7 @@ class iCalendar_parameter { 'VIDEO' => array('MPEG', 'QUICKTIME', 'VND.VIVO', 'VND.MOTOROLA.VIDEO', 'VND.MOTOROLA.VIDEOP') ); $value = strtoupper($value); - if(rfc2445_is_xname($value)) { - return true; - } + // Mimetype is enumerated above and anything else results in false. @list($type, $subtype) = explode('/', $value); if(empty($type) || empty($subtype)) { return false; @@ -178,7 +176,7 @@ class iCalendar_parameter { if(empty($value)) { return false; } - return (strcspn($value, '";:,') == strlen($value)); + return (strcspn($value, ';:,') == strlen($value)); break; case 'VALUE': @@ -227,7 +225,7 @@ class iCalendar_parameter { // Parameters we shouldn't be messing with case 'TZID': - return $value; + return str_replace('"', '', $value); break; } } diff --git a/lib/bennu/iCalendar_properties.php b/lib/bennu/iCalendar_properties.php index 8a557a4..260c42a 100644 --- a/lib/bennu/iCalendar_properties.php +++ b/lib/bennu/iCalendar_properties.php @@ -1227,6 +1227,8 @@ class iCalendar_property_x extends iCalendar_property { function __construct() { parent::__construct(); $this->valid_parameters = array( + // X-ALT-DESC (Description) can have FMTTYPE declaration of text/html is a property for HTML content. + 'FMTTYPE' => RFC2445_OPTIONAL | RFC2445_ONCE, 'LANGUAGE' => RFC2445_OPTIONAL | RFC2445_ONCE, RFC2445_XNAME => RFC2445_OPTIONAL ); diff --git a/lib/tests/fixtures/google_gmail.ics b/lib/tests/fixtures/google_gmail.ics new file mode 100644 index 0000000..e9d7d08 --- /dev/null +++ b/lib/tests/fixtures/google_gmail.ics @@ -0,0 +1,21 @@ +BEGIN:VCALENDAR +PRODID:-//Google Inc//Google Calendar 70.9054//EN +VERSION:2.0 +CALSCALE:GREGORIAN +METHOD:REQUEST +BEGIN:VEVENT +DTSTART:20141028T213000Z +DTEND:20141028T223000Z +DTSTAMP:20141028T211302Z +ORGANIZER;CN=John Smith:mailto:john.smith@gmail.com +UID:hjlv3v1lcerpi629s5gpfuijk0@google.com +CREATED:20141028T210927Z +DESCRIPTION:Lorem ipsum dolor sit amet\, consectetur adipisicing elit. +LAST-MODIFIED:20141028T211302Z +LOCATION:150 Willis St\, Te Aro\, Wellington 6011\, New Zealand +SEQUENCE:0 +STATUS:CONFIRMED +SUMMARY:Google calendar +TRANSP:OPAQUE +END:VEVENT +END:VCALENDAR diff --git a/lib/tests/fixtures/ms_outlook_2010.ics b/lib/tests/fixtures/ms_outlook_2010.ics new file mode 100644 index 0000000..69d85ab --- /dev/null +++ b/lib/tests/fixtures/ms_outlook_2010.ics @@ -0,0 +1,42 @@ +BEGIN:VCALENDAR +PRODID:-//Microsoft Corporation//Outlook 14.0 MIMEDIR//EN +VERSION:2.0 +METHOD:PUBLISH +X-MS-OLK-FORCEINSPECTOROPEN:TRUE +BEGIN:VTIMEZONE +TZID:Central Standard Time +BEGIN:STANDARD +DTSTART:16011104T020000 +RRULE:FREQ=YEARLY;BYDAY=1SU;BYMONTH=11 +TZOFFSETFROM:-0500 +TZOFFSETTO:-0600 +END:STANDARD +BEGIN:DAYLIGHT +DTSTART:16010311T020000 +RRULE:FREQ=YEARLY;BYDAY=2SU;BYMONTH=3 +TZOFFSETFROM:-0600 +TZOFFSETTO:-0500 +END:DAYLIGHT +END:VTIMEZONE +BEGIN:VEVENT +CLASS:PUBLIC +CREATED:20140916T144857Z +DESCRIPTION:\n +DTSTART;TZID="Central Standard Time":20140916T090000 +DTSTAMP:20140916T144857Z +DTEND;TZID="Central Standard Time":20140916T093000 +LAST-MODIFIED:20140916T144857Z +PRIORITY:5 +SEQUENCE:0 +SUMMARY;LANGUAGE=en-us:test +TRANSP:OPAQUE +UID:040000008200E00074C5B7101A82E0080000000000D9F06393D1CF010000000000000000100000007B710E58C9CE8146B1403BF7E84162FB +X-ALT-DESC;FMTTYPE=text/html:\n\n\n\n\n\n\n\n

\n\n\n +X-MICROSOFT-CDO-BUSYSTATUS:BUSY +X-MICROSOFT-CDO-IMPORTANCE:1 +X-MICROSOFT-DISALLOW-COUNTER:FALSE +X-MS-OLK-AUTOFILLLOCATION:TRUE +X-MS-OLK-AUTOSTARTCHECK:FALSE +X-MS-OLK-CONFTYPE:0 +END:VEVENT +END:VCALENDAR \ No newline at end of file diff --git a/lib/tests/fixtures/osx_yosemite.ics b/lib/tests/fixtures/osx_yosemite.ics new file mode 100644 index 0000000..fe0a2e7 --- /dev/null +++ b/lib/tests/fixtures/osx_yosemite.ics @@ -0,0 +1,36 @@ +BEGIN:VCALENDAR +CALSCALE:GREGORIAN +VERSION:2.0 +METHOD:PUBLISH +X-WR-CALNAME:pokus +X-WR-TIMEZONE:Pacific/Auckland +X-APPLE-CALENDAR-COLOR:#1BADF8 +BEGIN:VTIMEZONE +TZID:Pacific/Auckland +BEGIN:DAYLIGHT +TZOFFSETFROM:+1200 +RRULE:FREQ=YEARLY;BYMONTH=9;BYDAY=-1SU +DTSTART:20070930T020000 +TZNAME:NZDT +TZOFFSETTO:+1300 +END:DAYLIGHT +BEGIN:STANDARD +TZOFFSETFROM:+1300 +RRULE:FREQ=YEARLY;BYMONTH=4;BYDAY=1SU +DTSTART:20080406T030000 +TZNAME:NZST +TZOFFSETTO:+1200 +END:STANDARD +END:VTIMEZONE +BEGIN:VEVENT +CREATED:20141028T204253Z +UID:5571BBDC-AC05-4486-9E57-118EBDFFA385 +DTEND;TZID=Pacific/Auckland:20140917T023000 +TRANSP:OPAQUE +SUMMARY:Test event +DTSTART;TZID=Pacific/Auckland:20140917T020000 +DTSTAMP:20141028T204253Z +SEQUENCE:0 +DESCRIPTION:This is a note. +END:VEVENT +END:VCALENDAR -- 1.8.1.msysgit.1