diff --git a/lib/CalDAV/Backend/PDO.php b/lib/CalDAV/Backend/PDO.php index bb12d590da..acda1c9cbd 100644 --- a/lib/CalDAV/Backend/PDO.php +++ b/lib/CalDAV/Backend/PDO.php @@ -789,8 +789,10 @@ public function calendarQuery($calendarId, array $filters) // If start time OR the end time is not specified, we can do a // 100% accurate mysql query. - if (!$filters['prop-filters'] && !$filters['comp-filters'][0]['comp-filters'] && !$filters['comp-filters'][0]['prop-filters'] && (!$timeRange['start'] || !$timeRange['end'])) { - $requirePostFilter = false; + if (!$filters['prop-filters'] && !$filters['comp-filters'][0]['comp-filters'] && !$filters['comp-filters'][0]['prop-filters'] && $timeRange) { + if ((array_key_exists('start', $timeRange) && !$timeRange['start']) || (array_key_exists('end', $timeRange) && !$timeRange['end'])) { + $requirePostFilter = false; + } } } } @@ -810,11 +812,11 @@ public function calendarQuery($calendarId, array $filters) $values['componenttype'] = $componentType; } - if ($timeRange && $timeRange['start']) { + if ($timeRange && array_key_exists('start', $timeRange) && $timeRange['start']) { $query .= ' AND lastoccurence > :startdate'; $values['startdate'] = $timeRange['start']->getTimeStamp(); } - if ($timeRange && $timeRange['end']) { + if ($timeRange && array_key_exists('end', $timeRange) && $timeRange['end']) { $query .= ' AND firstoccurence < :enddate'; $values['enddate'] = $timeRange['end']->getTimeStamp(); } diff --git a/lib/CalDAV/CalendarQueryValidator.php b/lib/CalDAV/CalendarQueryValidator.php index ab6c602d2e..8217da0b69 100644 --- a/lib/CalDAV/CalendarQueryValidator.php +++ b/lib/CalDAV/CalendarQueryValidator.php @@ -69,7 +69,15 @@ protected function validateCompFilters(VObject\Component $parent, array $filters if ($filter['time-range']) { foreach ($parent->{$filter['name']} as $subComponent) { - if ($this->validateTimeRange($subComponent, $filter['time-range']['start'], $filter['time-range']['end'])) { + $start = null; + $end = null; + if (array_key_exists('start', $filter['time-range'])) { + $start = $filter['time-range']['start']; + } + if (array_key_exists('end', $filter['time-range'])) { + $end = $filter['time-range']['end']; + } + if ($this->validateTimeRange($subComponent, $start, $end)) { continue 2; } } @@ -130,7 +138,15 @@ protected function validatePropFilters(VObject\Component $parent, array $filters if ($filter['time-range']) { foreach ($parent->{$filter['name']} as $subComponent) { - if ($this->validateTimeRange($subComponent, $filter['time-range']['start'], $filter['time-range']['end'])) { + $start = null; + $end = null; + if (array_key_exists('start', $filter['time-range'])) { + $start = $filter['time-range']['start']; + } + if (array_key_exists('end', $filter['time-range'])) { + $end = $filter['time-range']['end']; + } + if ($this->validateTimeRange($subComponent, $start, $end)) { continue 2; } } diff --git a/tests/Sabre/CalDAV/Backend/AbstractPDOTest.php b/tests/Sabre/CalDAV/Backend/AbstractPDOTest.php index 9460b89228..77817c4fd0 100644 --- a/tests/Sabre/CalDAV/Backend/AbstractPDOTest.php +++ b/tests/Sabre/CalDAV/Backend/AbstractPDOTest.php @@ -780,7 +780,7 @@ public function testCalendarQueryTimeRange() ], $backend->calendarQuery([1, 1], $filters)); } - public function testCalendarQueryTimeRangeNoEnd() + public function testCalendarQueryTimeRangeEndNull() { $backend = new PDO($this->pdo); $backend->createCalendarObject([1, 1], 'todo', "BEGIN:VCALENDAR\r\nBEGIN:VTODO\r\nEND:VTODO\r\nEND:VCALENDAR\r\n"); @@ -811,6 +811,94 @@ public function testCalendarQueryTimeRangeNoEnd() ], $backend->calendarQuery([1, 1], $filters)); } + public function testCalendarQueryTimeRangeNoEnd() + { + $backend = new PDO($this->pdo); + $backend->createCalendarObject([1, 1], 'todo', "BEGIN:VCALENDAR\r\nBEGIN:VTODO\r\nEND:VTODO\r\nEND:VCALENDAR\r\n"); + $backend->createCalendarObject([1, 1], 'event', "BEGIN:VCALENDAR\r\nBEGIN:VEVENT\r\nDTSTART:20120101\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n"); + $backend->createCalendarObject([1, 1], 'event2', "BEGIN:VCALENDAR\r\nBEGIN:VEVENT\r\nDTSTART:20120103\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n"); + + $filters = [ + 'name' => 'VCALENDAR', + 'comp-filters' => [ + [ + 'name' => 'VEVENT', + 'comp-filters' => [], + 'prop-filters' => [], + 'is-not-defined' => false, + 'time-range' => [ + 'start' => new \DateTime('20120102'), + ], + ], + ], + 'prop-filters' => [], + 'is-not-defined' => false, + 'time-range' => null, + ]; + + $this->assertEquals([ + 'event2', + ], $backend->calendarQuery([1, 1], $filters)); + } + + public function testCalendarQueryTimeRangeNoStart() + { + $backend = new PDO($this->pdo); + $backend->createCalendarObject([1, 1], 'todo', "BEGIN:VCALENDAR\r\nBEGIN:VTODO\r\nEND:VTODO\r\nEND:VCALENDAR\r\n"); + $backend->createCalendarObject([1, 1], 'event', "BEGIN:VCALENDAR\r\nBEGIN:VEVENT\r\nDTSTART:20120101\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n"); + $backend->createCalendarObject([1, 1], 'event2', "BEGIN:VCALENDAR\r\nBEGIN:VEVENT\r\nDTSTART:20120103\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n"); + + $filters = [ + 'name' => 'VCALENDAR', + 'comp-filters' => [ + [ + 'name' => 'VEVENT', + 'comp-filters' => [], + 'prop-filters' => [], + 'is-not-defined' => false, + 'time-range' => [ + 'end' => new \DateTime('20120102'), + ], + ], + ], + 'prop-filters' => [], + 'is-not-defined' => false, + 'time-range' => null, + ]; + + $this->assertEquals([ + 'event', + ], $backend->calendarQuery([1, 1], $filters)); + } + + public function testCalendarQueryTimeRangeNotSpecified() + { + $backend = new PDO($this->pdo); + $backend->createCalendarObject([1, 1], 'todo', "BEGIN:VCALENDAR\r\nBEGIN:VTODO\r\nEND:VTODO\r\nEND:VCALENDAR\r\n"); + $backend->createCalendarObject([1, 1], 'event', "BEGIN:VCALENDAR\r\nBEGIN:VEVENT\r\nDTSTART:20120101\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n"); + $backend->createCalendarObject([1, 1], 'event2', "BEGIN:VCALENDAR\r\nBEGIN:VEVENT\r\nDTSTART:20120103\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n"); + + $filters = [ + 'name' => 'VCALENDAR', + 'comp-filters' => [ + [ + 'name' => 'VEVENT', + 'comp-filters' => [], + 'prop-filters' => [], + 'is-not-defined' => false, + 'time-range' => false, + ], + ], + 'prop-filters' => [], + 'is-not-defined' => false, + 'time-range' => null, + ]; + + $result = $backend->calendarQuery([1, 1], $filters); + $this->assertTrue(in_array('event', $result)); + $this->assertTrue(in_array('event2', $result)); + } + public function testGetChanges() { $backend = new PDO($this->pdo);