diff --git a/plugins/libcalendaring/libvcalendar.php b/plugins/libcalendaring/libvcalendar.php index 5776af57..6de8feb7 100644 --- a/plugins/libcalendaring/libvcalendar.php +++ b/plugins/libcalendaring/libvcalendar.php @@ -213,20 +213,12 @@ class libvcalendar // Catch possible exceptions when date is invalid (Bug #2144) // We can skip these fields, they aren't critical - if ($ve->CREATED) { + foreach (array('CREATED' => 'created', 'LAST-MODIFIED' => 'changed', 'DTSTAMP' => 'changed') as $attr => $field) { try { - $event['created'] = $ve->CREATED->getDateTime(); - } catch (Exception $e) {}; - } - if ($ve->{'LAST-MODIFIED'}) { - try { - $event['changed'] = $ve->{'LAST-MODIFIED'}->getDateTime(); - } catch (Exception $e) {}; - } - if (!$event['changed'] && $ve->DTSTAMP) { - try { - $event['changed'] = $ve->DTSTAMP->getDateTime(); - } catch (Exception $e) {}; + if (!$event[$field] && $ve->{$attr}) { + $event[$field] = $ve->{$attr}->getDateTime(); + } + } catch (Exception $e) {} } // map other attributes to internal fields diff --git a/plugins/libcalendaring/tests/libvcalendar.php b/plugins/libcalendaring/tests/libvcalendar.php index a2043384..9567759e 100644 --- a/plugins/libcalendaring/tests/libvcalendar.php +++ b/plugins/libcalendaring/tests/libvcalendar.php @@ -41,6 +41,10 @@ class libvcalendar_test extends PHPUnit_Framework_TestCase $this->assertEquals(1, count($events)); $event = $events[0]; + $this->assertInstanceOf('DateTime', $event['created'], "'created' property is DateTime object"); + $this->assertInstanceOf('DateTime', $event['changed'], "'changed' property is DateTime object"); + $this->assertEquals('UTC', $event['created']->getTimezone()->getName(), "'created' date is in UTC"); + $this->assertInstanceOf('DateTime', $event['start'], "'start' property is DateTime object"); $this->assertInstanceOf('DateTime', $event['end'], "'end' property is DateTime object"); $this->assertEquals('08-01', $event['start']->format('m-d'), "Start date is August 1st"); @@ -70,6 +74,17 @@ class libvcalendar_test extends PHPUnit_Framework_TestCase $this->assertEmpty($events); } + function test_invalid_dates() + { + $ical = new libvcalendar(); + $events = $ical->import_from_file(__DIR__ . '/resources/invalid-dates.ics', 'UTF-8'); + $event = $events[0]; + + $this->assertEquals(1, count($events), "Import event data"); + $this->assertFalse(array_key_exists('created', $event), "No created date field"); + $this->assertFalse(array_key_exists('changed', $event), "No changed date field"); + } + /** * Test some extended ical properties such as attendees, recurrence rules, alarms and attachments * diff --git a/plugins/libcalendaring/tests/resources/invalid-dates.ics b/plugins/libcalendaring/tests/resources/invalid-dates.ics new file mode 100644 index 00000000..e376bc5e --- /dev/null +++ b/plugins/libcalendaring/tests/resources/invalid-dates.ics @@ -0,0 +1,15 @@ +BEGIN:VCALENDAR +VERSION:2.0 +PRODID:-//Apple Inc.//iCal 5.0.3//EN +CALSCALE:GREGORIAN +BEGIN:VEVENT +CREATED:00001231T000000Z +LAST-MODIFIED:20130755 +UID:C968B885-08FB-40E5-B89E-6DA05F26AAFF +TRANSP:TRANSPARENT +SUMMARY:Swiss National Day +DTSTART;VALUE=DATE:20130801 +DTEND;VALUE=DATE:20130802 +SEQUENCE:2 +END:VEVENT +END:VCALENDAR