diff options
author | Daniel Kesselberg <mail@danielkesselberg.de> | 2024-05-07 15:55:47 +0200 |
---|---|---|
committer | Daniel <mail@danielkesselberg.de> | 2024-05-28 19:56:36 +0200 |
commit | 4718a7e4e0c2b738e1076979dbdc08661bd5f305 (patch) | |
tree | 6b48cdc4ad78a3f19babcc6e4faff873a0efc546 /apps/dav/tests/unit/CalDAV | |
parent | ddb840c36babd02abedce7da41b0c04849009edb (diff) | |
download | nextcloud-server-4718a7e4e0c2b738e1076979dbdc08661bd5f305.tar.gz nextcloud-server-4718a7e4e0c2b738e1076979dbdc08661bd5f305.zip |
fix(caldav): event search with limit and timerange
Event recurrences are evaluated at runtime because the database only knows the first and last occurrence.
Given, a user created 8 events with a yearly reoccurrence and two for events tomorrow.
The upcoming event widget asks the CalDAV backend for 7 events within the next 14 days.
If limit 7 is applied to the SQL query, we find the 7 events with a yearly reoccurrence and discard the events after evaluating the reoccurrence rules because they are not due within the next 14 days and end up with an empty result even if there are two events to show.
The workaround for search requests with a limit and time range is asking for more row than requested and retrying if we have not reached the limit.
Signed-off-by: Daniel Kesselberg <mail@danielkesselberg.de>
Diffstat (limited to 'apps/dav/tests/unit/CalDAV')
-rw-r--r-- | apps/dav/tests/unit/CalDAV/CalDavBackendTest.php | 164 |
1 files changed, 164 insertions, 0 deletions
diff --git a/apps/dav/tests/unit/CalDAV/CalDavBackendTest.php b/apps/dav/tests/unit/CalDAV/CalDavBackendTest.php index f5c85190050..f0712ecdc93 100644 --- a/apps/dav/tests/unit/CalDAV/CalDavBackendTest.php +++ b/apps/dav/tests/unit/CalDAV/CalDavBackendTest.php @@ -1573,4 +1573,168 @@ EOD; self::assertEqualsCanonicalizing([$uri1, $uri3], $changesAfter['modified']); self::assertEquals([$uri2], $changesAfter['deleted']); } + + public function testSearchWithLimitAndTimeRange() { + $calendarId = $this->createTestCalendar(); + $calendarInfo = [ + 'id' => $calendarId, + 'principaluri' => 'user1', + '{http://owncloud.org/ns}owner-principal' => 'user1', + ]; + + $testFiles = [ + __DIR__ . '/../../misc/caldav-search-limit-timerange-1.ics', + __DIR__ . '/../../misc/caldav-search-limit-timerange-2.ics', + __DIR__ . '/../../misc/caldav-search-limit-timerange-3.ics', + __DIR__ . '/../../misc/caldav-search-limit-timerange-4.ics', + __DIR__ . '/../../misc/caldav-search-limit-timerange-5.ics', + __DIR__ . '/../../misc/caldav-search-limit-timerange-6.ics', + ]; + + foreach ($testFiles as $testFile) { + $objectUri = static::getUniqueID('search-limit-timerange-'); + $calendarData = \file_get_contents($testFile); + $this->backend->createCalendarObject($calendarId, $objectUri, $calendarData); + } + + $start = new DateTimeImmutable('2024-05-06T00:00:00Z'); + $end = $start->add(new DateInterval('P14D')); + + $results = $this->backend->search( + $calendarInfo, + '', + [], + [ + 'timerange' => [ + 'start' => $start, + 'end' => $end, + ] + ], + 4, + null, + ); + + $this->assertCount(2, $results); + + $this->assertEquals('Cake Tasting', $results[0]['objects'][0]['SUMMARY'][0]); + $this->assertGreaterThanOrEqual( + $start->getTimestamp(), + $results[0]['objects'][0]['DTSTART'][0]->getTimestamp(), + 'Recurrence starting before requested start', + ); + + $this->assertEquals('Pasta Day', $results[1]['objects'][0]['SUMMARY'][0]); + $this->assertGreaterThanOrEqual( + $start->getTimestamp(), + $results[1]['objects'][0]['DTSTART'][0]->getTimestamp(), + 'Recurrence starting before requested start', + ); + } + + public function testSearchWithLimitAndTimeRangeShouldNotReturnMoreObjectsThenLimit() { + $calendarId = $this->createTestCalendar(); + $calendarInfo = [ + 'id' => $calendarId, + 'principaluri' => 'user1', + '{http://owncloud.org/ns}owner-principal' => 'user1', + ]; + + $testFiles = [ + __DIR__ . '/../../misc/caldav-search-limit-timerange-1.ics', + __DIR__ . '/../../misc/caldav-search-limit-timerange-2.ics', + __DIR__ . '/../../misc/caldav-search-limit-timerange-3.ics', + __DIR__ . '/../../misc/caldav-search-limit-timerange-4.ics', + __DIR__ . '/../../misc/caldav-search-limit-timerange-5.ics', + __DIR__ . '/../../misc/caldav-search-limit-timerange-6.ics', + ]; + + foreach ($testFiles as $testFile) { + $objectUri = static::getUniqueID('search-limit-timerange-'); + $calendarData = \file_get_contents($testFile); + $this->backend->createCalendarObject($calendarId, $objectUri, $calendarData); + } + + $start = new DateTimeImmutable('2024-05-06T00:00:00Z'); + $end = $start->add(new DateInterval('P14D')); + + $results = $this->backend->search( + $calendarInfo, + '', + [], + [ + 'timerange' => [ + 'start' => $start, + 'end' => $end, + ] + ], + 1, + null, + ); + + $this->assertCount(1, $results); + + $this->assertEquals('Cake Tasting', $results[0]['objects'][0]['SUMMARY'][0]); + $this->assertGreaterThanOrEqual( + $start->getTimestamp(), + $results[0]['objects'][0]['DTSTART'][0]->getTimestamp(), + 'Recurrence starting before requested start', + ); + } + + public function testSearchWithLimitAndTimeRangeShouldReturnObjectsInTheSameOrder() { + $calendarId = $this->createTestCalendar(); + $calendarInfo = [ + 'id' => $calendarId, + 'principaluri' => 'user1', + '{http://owncloud.org/ns}owner-principal' => 'user1', + ]; + + $testFiles = [ + __DIR__ . '/../../misc/caldav-search-limit-timerange-1.ics', + __DIR__ . '/../../misc/caldav-search-limit-timerange-2.ics', + __DIR__ . '/../../misc/caldav-search-limit-timerange-3.ics', + __DIR__ . '/../../misc/caldav-search-limit-timerange-4.ics', + __DIR__ . '/../../misc/caldav-search-limit-timerange-6.ics', // <-- intentional! + __DIR__ . '/../../misc/caldav-search-limit-timerange-5.ics', + ]; + + foreach ($testFiles as $testFile) { + $objectUri = static::getUniqueID('search-limit-timerange-'); + $calendarData = \file_get_contents($testFile); + $this->backend->createCalendarObject($calendarId, $objectUri, $calendarData); + } + + $start = new DateTimeImmutable('2024-05-06T00:00:00Z'); + $end = $start->add(new DateInterval('P14D')); + + $results = $this->backend->search( + $calendarInfo, + '', + [], + [ + 'timerange' => [ + 'start' => $start, + 'end' => $end, + ] + ], + 2, + null, + ); + + $this->assertCount(2, $results); + + $this->assertEquals('Cake Tasting', $results[0]['objects'][0]['SUMMARY'][0]); + $this->assertGreaterThanOrEqual( + $start->getTimestamp(), + $results[0]['objects'][0]['DTSTART'][0]->getTimestamp(), + 'Recurrence starting before requested start', + ); + + $this->assertEquals('Pasta Day', $results[1]['objects'][0]['SUMMARY'][0]); + $this->assertGreaterThanOrEqual( + $start->getTimestamp(), + $results[1]['objects'][0]['DTSTART'][0]->getTimestamp(), + 'Recurrence starting before requested start', + ); + } } |