Skip to content

Commit b196f4c

Browse files
committed
Remove the loop of calendars when only one is needed
Signed-off-by: Anna Larch <anna@nextcloud.com>
1 parent 2576609 commit b196f4c

2 files changed

Lines changed: 40 additions & 19 deletions

File tree

apps/dav/lib/CalDAV/CalendarHome.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,16 @@ public function getChild($name) {
159159
return new TrashbinHome($this->caldavBackend, $this->principalInfo);
160160
}
161161

162-
// Calendars
162+
// Calendar - this covers all "regular" calendars, but not shared
163+
// only check if the method is available
164+
if($this->caldavBackend instanceof CalDavBackend) {
165+
$calendar = $this->caldavBackend->getCalendarByUri($this->principalInfo['uri'], $name);
166+
if(!empty($calendar)) {
167+
return new Calendar($this->caldavBackend, $calendar, $this->l10n, $this->config, $this->logger);
168+
}
169+
}
170+
171+
// Fallback to cover shared calendars
163172
foreach ($this->caldavBackend->getCalendarsForUser($this->principalInfo['uri']) as $calendar) {
164173
if ($calendar['uri'] === $name) {
165174
return new Calendar($this->caldavBackend, $calendar, $this->l10n, $this->config, $this->logger);

apps/dav/tests/unit/CalDAV/CalendarHomeTest.php

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public function testCreateCalendarValidName() {
8888
$mkCol->method('getRemainingValues')
8989
->willReturn(['... properties ...']);
9090

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

@@ -117,33 +117,33 @@ public function testCreateCalendarReservedNameAppGenerated() {
117117

118118
public function testGetChildren():void {
119119
$this->backend
120-
->expects($this->at(0))
120+
->expects(self::once())
121121
->method('getCalendarsForUser')
122122
->with('user-principal-123')
123123
->willReturn([]);
124124

125125
$this->backend
126-
->expects($this->at(1))
126+
->expects(self::once())
127127
->method('getSubscriptionsForUser')
128128
->with('user-principal-123')
129129
->willReturn([]);
130130

131131
$calendarPlugin1 = $this->createMock(ICalendarProvider::class);
132132
$calendarPlugin1
133-
->expects($this->once())
133+
->expects(self::once())
134134
->method('fetchAllForCalendarHome')
135135
->with('user-principal-123')
136136
->willReturn(['plugin1calendar1', 'plugin1calendar2']);
137137

138138
$calendarPlugin2 = $this->createMock(ICalendarProvider::class);
139139
$calendarPlugin2
140-
->expects($this->once())
140+
->expects(self::once())
141141
->method('fetchAllForCalendarHome')
142142
->with('user-principal-123')
143143
->willReturn(['plugin2calendar1', 'plugin2calendar2']);
144144

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

163163
public function testGetChildNonAppGenerated():void {
164164
$this->backend
165-
->expects($this->at(0))
165+
->expects(self::once())
166+
->method('getCalendarByUri')
167+
->with('user-principal-123')
168+
->willReturn([]);
169+
170+
$this->backend
171+
->expects(self::once())
166172
->method('getCalendarsForUser')
167173
->with('user-principal-123')
168174
->willReturn([]);
169175

170176
$this->backend
171-
->expects($this->at(1))
177+
->expects(self::once())
172178
->method('getSubscriptionsForUser')
173179
->with('user-principal-123')
174180
->willReturn([]);
175181

176182
$this->pluginManager
177-
->expects($this->never())
183+
->expects(self::never())
178184
->method('getCalendarPlugins');
179185

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

186192
public function testGetChildAppGenerated():void {
187193
$this->backend
188-
->expects($this->at(0))
194+
->expects(self::once())
195+
->method('getCalendarByUri')
196+
->with('user-principal-123')
197+
->willReturn([]);
198+
199+
$this->backend
200+
->expects(self::once())
189201
->method('getCalendarsForUser')
190202
->with('user-principal-123')
191203
->willReturn([]);
192204

193205
$this->backend
194-
->expects($this->at(1))
206+
->expects(self::once())
195207
->method('getSubscriptionsForUser')
196208
->with('user-principal-123')
197209
->willReturn([]);
198210

199211
$calendarPlugin1 = $this->createMock(ICalendarProvider::class);
200212
$calendarPlugin1
201-
->expects($this->once())
213+
->expects(self::once())
202214
->method('getAppId')
203215
->with()
204216
->willReturn('calendar_plugin_1');
205217
$calendarPlugin1
206-
->expects($this->never())
218+
->expects(self::never())
207219
->method('hasCalendarInCalendarHome');
208220
$calendarPlugin1
209-
->expects($this->never())
221+
->expects(self::never())
210222
->method('getCalendarInCalendarHome');
211223

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

214226
$calendarPlugin2 = $this->createMock(ICalendarProvider::class);
215227
$calendarPlugin2
216-
->expects($this->once())
228+
->expects(self::once())
217229
->method('getAppId')
218230
->with()
219231
->willReturn('calendar_plugin_2');
220232
$calendarPlugin2
221-
->expects($this->once())
233+
->expects(self::once())
222234
->method('hasCalendarInCalendarHome')
223235
->with('user-principal-123', 'calendar-uri-from-backend')
224236
->willReturn(true);
225237
$calendarPlugin2
226-
->expects($this->once())
238+
->expects(self::once())
227239
->method('getCalendarInCalendarHome')
228240
->with('user-principal-123', 'calendar-uri-from-backend')
229241
->willReturn($externalCalendarMock);
230242

231243
$this->pluginManager
232-
->expects($this->once())
244+
->expects(self::once())
233245
->method('getCalendarPlugins')
234246
->with()
235247
->willReturn([$calendarPlugin1, $calendarPlugin2]);

0 commit comments

Comments
 (0)