Avoid duplicated code; add tests for invalid date parsing/catching
This commit is contained in:
parent
164fae8387
commit
d924ab06d8
3 changed files with 35 additions and 13 deletions
|
@ -213,20 +213,12 @@ class libvcalendar
|
||||||
|
|
||||||
// Catch possible exceptions when date is invalid (Bug #2144)
|
// Catch possible exceptions when date is invalid (Bug #2144)
|
||||||
// We can skip these fields, they aren't critical
|
// 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 {
|
try {
|
||||||
$event['created'] = $ve->CREATED->getDateTime();
|
if (!$event[$field] && $ve->{$attr}) {
|
||||||
} catch (Exception $e) {};
|
$event[$field] = $ve->{$attr}->getDateTime();
|
||||||
}
|
}
|
||||||
if ($ve->{'LAST-MODIFIED'}) {
|
} catch (Exception $e) {}
|
||||||
try {
|
|
||||||
$event['changed'] = $ve->{'LAST-MODIFIED'}->getDateTime();
|
|
||||||
} catch (Exception $e) {};
|
|
||||||
}
|
|
||||||
if (!$event['changed'] && $ve->DTSTAMP) {
|
|
||||||
try {
|
|
||||||
$event['changed'] = $ve->DTSTAMP->getDateTime();
|
|
||||||
} catch (Exception $e) {};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// map other attributes to internal fields
|
// map other attributes to internal fields
|
||||||
|
|
|
@ -41,6 +41,10 @@ class libvcalendar_test extends PHPUnit_Framework_TestCase
|
||||||
$this->assertEquals(1, count($events));
|
$this->assertEquals(1, count($events));
|
||||||
$event = $events[0];
|
$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['start'], "'start' property is DateTime object");
|
||||||
$this->assertInstanceOf('DateTime', $event['end'], "'end' 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");
|
$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);
|
$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
|
* Test some extended ical properties such as attendees, recurrence rules, alarms and attachments
|
||||||
*
|
*
|
||||||
|
|
15
plugins/libcalendaring/tests/resources/invalid-dates.ics
Normal file
15
plugins/libcalendaring/tests/resources/invalid-dates.ics
Normal file
|
@ -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
|
Loading…
Add table
Reference in a new issue