Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove the loop of calendars when only one is needed #33608

Merged
merged 1 commit into from
Oct 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion apps/dav/lib/CalDAV/CalendarHome.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,16 @@ public function getChild($name) {
return new TrashbinHome($this->caldavBackend, $this->principalInfo);
}

// Calendars
// Calendar - this covers all "regular" calendars, but not shared
// only check if the method is available
if($this->caldavBackend instanceof CalDavBackend) {
$calendar = $this->caldavBackend->getCalendarByUri($this->principalInfo['uri'], $name);
if(!empty($calendar)) {
return new Calendar($this->caldavBackend, $calendar, $this->l10n, $this->config, $this->logger);
}
}

// Fallback to cover shared calendars
foreach ($this->caldavBackend->getCalendarsForUser($this->principalInfo['uri']) as $calendar) {
if ($calendar['uri'] === $name) {
return new Calendar($this->caldavBackend, $calendar, $this->l10n, $this->config, $this->logger);
Expand Down
48 changes: 30 additions & 18 deletions apps/dav/tests/unit/CalDAV/CalendarHomeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public function testCreateCalendarValidName() {
$mkCol->method('getRemainingValues')
->willReturn(['... properties ...']);

$this->backend->expects($this->once())
$this->backend->expects(self::once())
->method('createCalendar')
->with('user-principal-123', 'name123', ['... properties ...']);

Expand Down Expand Up @@ -117,33 +117,33 @@ public function testCreateCalendarReservedNameAppGenerated() {

public function testGetChildren():void {
$this->backend
->expects($this->at(0))
->expects(self::once())
->method('getCalendarsForUser')
->with('user-principal-123')
->willReturn([]);

$this->backend
->expects($this->at(1))
->expects(self::once())
->method('getSubscriptionsForUser')
->with('user-principal-123')
->willReturn([]);

$calendarPlugin1 = $this->createMock(ICalendarProvider::class);
$calendarPlugin1
->expects($this->once())
->expects(self::once())
->method('fetchAllForCalendarHome')
->with('user-principal-123')
->willReturn(['plugin1calendar1', 'plugin1calendar2']);

$calendarPlugin2 = $this->createMock(ICalendarProvider::class);
$calendarPlugin2
->expects($this->once())
->expects(self::once())
->method('fetchAllForCalendarHome')
->with('user-principal-123')
->willReturn(['plugin2calendar1', 'plugin2calendar2']);

$this->pluginManager
->expects($this->once())
->expects(self::once())
->method('getCalendarPlugins')
->with()
->willReturn([$calendarPlugin1, $calendarPlugin2]);
Expand All @@ -162,19 +162,25 @@ public function testGetChildren():void {

public function testGetChildNonAppGenerated():void {
$this->backend
->expects($this->at(0))
->expects(self::once())
->method('getCalendarByUri')
->with('user-principal-123')
->willReturn([]);

$this->backend
->expects(self::once())
->method('getCalendarsForUser')
->with('user-principal-123')
->willReturn([]);

$this->backend
->expects($this->at(1))
->expects(self::once())
->method('getSubscriptionsForUser')
->with('user-principal-123')
->willReturn([]);

$this->pluginManager
->expects($this->never())
->expects(self::never())
->method('getCalendarPlugins');

$this->expectException(\Sabre\DAV\Exception\NotFound::class);
Expand All @@ -185,51 +191,57 @@ public function testGetChildNonAppGenerated():void {

public function testGetChildAppGenerated():void {
$this->backend
->expects($this->at(0))
->expects(self::once())
->method('getCalendarByUri')
->with('user-principal-123')
->willReturn([]);

$this->backend
->expects(self::once())
->method('getCalendarsForUser')
->with('user-principal-123')
->willReturn([]);

$this->backend
->expects($this->at(1))
->expects(self::once())
->method('getSubscriptionsForUser')
->with('user-principal-123')
->willReturn([]);

$calendarPlugin1 = $this->createMock(ICalendarProvider::class);
$calendarPlugin1
->expects($this->once())
->expects(self::once())
->method('getAppId')
->with()
->willReturn('calendar_plugin_1');
$calendarPlugin1
->expects($this->never())
->expects(self::never())
->method('hasCalendarInCalendarHome');
$calendarPlugin1
->expects($this->never())
->expects(self::never())
->method('getCalendarInCalendarHome');

$externalCalendarMock = $this->createMock(ExternalCalendar::class);

$calendarPlugin2 = $this->createMock(ICalendarProvider::class);
$calendarPlugin2
->expects($this->once())
->expects(self::once())
->method('getAppId')
->with()
->willReturn('calendar_plugin_2');
$calendarPlugin2
->expects($this->once())
->expects(self::once())
->method('hasCalendarInCalendarHome')
->with('user-principal-123', 'calendar-uri-from-backend')
->willReturn(true);
$calendarPlugin2
->expects($this->once())
->expects(self::once())
->method('getCalendarInCalendarHome')
->with('user-principal-123', 'calendar-uri-from-backend')
->willReturn($externalCalendarMock);

$this->pluginManager
->expects($this->once())
->expects(self::once())
->method('getCalendarPlugins')
->with()
->willReturn([$calendarPlugin1, $calendarPlugin2]);
Expand Down