Skip to content

Commit 67ff8fc

Browse files
authored
Merge pull request #34819 from nextcloud/tests/fix-phpunit-warnings
Fix some phpunit test warnings
2 parents 28358ac + 8ec7e43 commit 67ff8fc

File tree

5 files changed

+128
-51
lines changed

5 files changed

+128
-51
lines changed

apps/dav/lib/CalDAV/CalendarImpl.php

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
use function Sabre\Uri\split as uriSplit;
4343

4444
class CalendarImpl implements ICreateFromString, IHandleImipMessage {
45-
4645
private CalDavBackend $backend;
4746
private Calendar $calendar;
4847
/** @var array<string, mixed> */
@@ -147,7 +146,7 @@ public function createFromString(string $name, string $calendarData): void {
147146
$server = new InvitationResponseServer(false);
148147

149148
/** @var CustomPrincipalPlugin $plugin */
150-
$plugin = $server->server->getPlugin('auth');
149+
$plugin = $server->getServer()->getPlugin('auth');
151150
// we're working around the previous implementation
152151
// that only allowed the public system principal to be used
153152
// so set the custom principal here
@@ -163,14 +162,14 @@ public function createFromString(string $name, string $calendarData): void {
163162

164163
// Force calendar change URI
165164
/** @var Schedule\Plugin $schedulingPlugin */
166-
$schedulingPlugin = $server->server->getPlugin('caldav-schedule');
165+
$schedulingPlugin = $server->getServer()->getPlugin('caldav-schedule');
167166
$schedulingPlugin->setPathOfCalendarObjectChange($fullCalendarFilename);
168167

169168
$stream = fopen('php://memory', 'rb+');
170169
fwrite($stream, $calendarData);
171170
rewind($stream);
172171
try {
173-
$server->server->createFile($fullCalendarFilename, $stream);
172+
$server->getServer()->createFile($fullCalendarFilename, $stream);
174173
} catch (Conflict $e) {
175174
throw new CalendarException('Could not create new calendar event: ' . $e->getMessage(), 0, $e);
176175
} finally {
@@ -182,10 +181,10 @@ public function createFromString(string $name, string $calendarData): void {
182181
* @throws CalendarException
183182
*/
184183
public function handleIMipMessage(string $name, string $calendarData): void {
185-
$server = new InvitationResponseServer(false);
184+
$server = $this->getInvitationResponseServer();
186185

187186
/** @var CustomPrincipalPlugin $plugin */
188-
$plugin = $server->server->getPlugin('auth');
187+
$plugin = $server->getServer()->getPlugin('auth');
189188
// we're working around the previous implementation
190189
// that only allowed the public system principal to be used
191190
// so set the custom principal here
@@ -196,33 +195,33 @@ public function handleIMipMessage(string $name, string $calendarData): void {
196195
}
197196
// Force calendar change URI
198197
/** @var Schedule\Plugin $schedulingPlugin */
199-
$schedulingPlugin = $server->server->getPlugin('caldav-schedule');
198+
$schedulingPlugin = $server->getServer()->getPlugin('caldav-schedule');
200199
// Let sabre handle the rest
201200
$iTipMessage = new Message();
202201
/** @var VCalendar $vObject */
203202
$vObject = Reader::read($calendarData);
204203
/** @var VEvent $vEvent */
205204
$vEvent = $vObject->{'VEVENT'};
206205

207-
if($vObject->{'METHOD'} === null) {
206+
if ($vObject->{'METHOD'} === null) {
208207
throw new CalendarException('No Method provided for scheduling data. Could not process message');
209208
}
210209

211-
if(!isset($vEvent->{'ORGANIZER'}) || !isset($vEvent->{'ATTENDEE'})) {
210+
if (!isset($vEvent->{'ORGANIZER'}) || !isset($vEvent->{'ATTENDEE'})) {
212211
throw new CalendarException('Could not process scheduling data, neccessary data missing from ICAL');
213212
}
214213
$organizer = $vEvent->{'ORGANIZER'}->getValue();
215214
$attendee = $vEvent->{'ATTENDEE'}->getValue();
216215

217216
$iTipMessage->method = $vObject->{'METHOD'}->getValue();
218-
if($iTipMessage->method === 'REPLY') {
217+
if ($iTipMessage->method === 'REPLY') {
219218
if ($server->isExternalAttendee($vEvent->{'ATTENDEE'}->getValue())) {
220219
$iTipMessage->recipient = $organizer;
221220
} else {
222221
$iTipMessage->recipient = $attendee;
223222
}
224223
$iTipMessage->sender = $attendee;
225-
} else if($iTipMessage->method === 'CANCEL') {
224+
} elseif ($iTipMessage->method === 'CANCEL') {
226225
$iTipMessage->recipient = $attendee;
227226
$iTipMessage->sender = $organizer;
228227
}
@@ -232,4 +231,8 @@ public function handleIMipMessage(string $name, string $calendarData): void {
232231
$iTipMessage->message = $vObject;
233232
$schedulingPlugin->scheduleLocalDelivery($iTipMessage);
234233
}
234+
235+
public function getInvitationResponseServer(): InvitationResponseServer {
236+
return new InvitationResponseServer(false);
237+
}
235238
}

apps/dav/lib/CalDAV/InvitationResponse/InvitationResponseServer.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
use Sabre\VObject\ITip\Message;
4040

4141
class InvitationResponseServer {
42-
4342
/** @var \OCA\DAV\Connector\Sabre\Server */
4443
public $server;
4544

@@ -127,7 +126,11 @@ public function handleITipMessage(Message $iTipMessage) {
127126

128127
public function isExternalAttendee(string $principalUri): bool {
129128
/** @var \Sabre\DAVACL\Plugin $aclPlugin */
130-
$aclPlugin = $this->server->getPlugin('acl');
129+
$aclPlugin = $this->getServer()->getPlugin('acl');
131130
return $aclPlugin->getPrincipalByUri($principalUri) === null;
132131
}
132+
133+
public function getServer(): \OCA\DAV\Connector\Sabre\Server {
134+
return $this->server;
135+
}
133136
}

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

Lines changed: 108 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,18 @@
3131
use OCA\DAV\CalDAV\CalendarImpl;
3232
use OCA\DAV\CalDAV\InvitationResponse\InvitationResponseServer;
3333
use OCA\DAV\CalDAV\Schedule\Plugin;
34+
use OCA\DAV\Connector\Sabre\Server;
35+
use OCP\Calendar\Exceptions\CalendarException;
3436
use PHPUnit\Framework\MockObject\MockObject;
37+
use Sabre\VObject\Component\VCalendar;
38+
use Sabre\VObject\Component\VEvent;
39+
use Sabre\VObject\ITip\Message;
40+
use Sabre\VObject\Reader;
3541

42+
/**
43+
* @group DB
44+
*/
3645
class CalendarImplTest extends \Test\TestCase {
37-
3846
/** @var CalendarImpl */
3947
private $calendarImpl;
4048

@@ -69,7 +77,7 @@ public function testGetKey() {
6977
}
7078

7179
public function testGetDisplayname() {
72-
$this->assertEquals($this->calendarImpl->getDisplayName(),'user readable name 123');
80+
$this->assertEquals($this->calendarImpl->getDisplayName(), 'user readable name 123');
7381
}
7482

7583
public function testGetDisplayColor() {
@@ -132,76 +140,140 @@ public function testGetPermissionAll() {
132140
}
133141

134142
public function testHandleImipMessage(): void {
135-
$invitationResponseServer = $this->createConfiguredMock(InvitationResponseServer::class, [
136-
'server' => $this->createConfiguredMock(CalDavBackend::class, [
137-
'getPlugin' => [
138-
'auth' => $this->createMock(CustomPrincipalPlugin::class),
139-
'schedule' => $this->createMock(Plugin::class)
140-
]
141-
])
142-
]);
143-
144143
$message = <<<EOF
145144
BEGIN:VCALENDAR
146145
PRODID:-//Nextcloud/Nextcloud CalDAV Server//EN
147146
METHOD:REPLY
148147
VERSION:2.0
149148
BEGIN:VEVENT
150-
ATTENDEE;PARTSTAT=mailto:[email protected]:ACCEPTED
149+
ATTENDEE;PARTSTAT=ACCEPTED:mailto:[email protected]
151150
ORGANIZER:mailto:[email protected]
152151
UID:aUniqueUid
153152
SEQUENCE:2
154153
REQUEST-STATUS:2.0;Success
155-
%sEND:VEVENT
154+
END:VEVENT
156155
END:VCALENDAR
157156
EOF;
158157

159158
/** @var CustomPrincipalPlugin|MockObject $authPlugin */
160-
$authPlugin = $invitationResponseServer->server->getPlugin('auth');
159+
$authPlugin = $this->createMock(CustomPrincipalPlugin::class);
161160
$authPlugin->expects(self::once())
162-
->method('setPrincipalUri')
161+
->method('setCurrentPrincipal')
163162
->with($this->calendar->getPrincipalURI());
164163

164+
/** @var \Sabre\DAVACL\Plugin|MockObject $aclPlugin*/
165+
$aclPlugin = $this->createMock(\Sabre\DAVACL\Plugin::class);
166+
165167
/** @var Plugin|MockObject $schedulingPlugin */
166-
$schedulingPlugin = $invitationResponseServer->server->getPlugin('caldav-schedule');
168+
$schedulingPlugin = $this->createMock(Plugin::class);
169+
$iTipMessage = $this->getITipMessage($message);
170+
$iTipMessage->recipient = "mailto:[email protected]";
167171
$schedulingPlugin->expects(self::once())
168-
->method('setPathOfCalendarObjectChange')
169-
->with('fullcalendarname');
172+
->method('scheduleLocalDelivery')
173+
->with($iTipMessage);
174+
175+
$server = $this->createMock(Server::class);
176+
$server->expects($this->any())
177+
->method('getPlugin')
178+
->willReturnMap([
179+
['auth', $authPlugin],
180+
['acl', $aclPlugin],
181+
['caldav-schedule', $schedulingPlugin]
182+
]);
183+
184+
$invitationResponseServer = $this->createPartialMock(InvitationResponseServer::class, ['getServer', 'isExternalAttendee']);
185+
$invitationResponseServer->server = $server;
186+
$invitationResponseServer->expects($this->any())
187+
->method('getServer')
188+
->willReturn($server);
189+
$invitationResponseServer->expects(self::once())
190+
->method('isExternalAttendee')
191+
->willReturn(false);
192+
193+
$calendarImpl = $this->getMockBuilder(CalendarImpl::class)
194+
->setConstructorArgs([$this->calendar, $this->calendarInfo, $this->backend])
195+
->onlyMethods(['getInvitationResponseServer'])
196+
->getMock();
197+
$calendarImpl->expects($this->once())
198+
->method('getInvitationResponseServer')
199+
->willReturn($invitationResponseServer);
200+
201+
$calendarImpl->handleIMipMessage('filename.ics', $message);
170202
}
171203

172204
public function testHandleImipMessageNoCalendarUri(): void {
173-
$invitationResponseServer = $this->createConfiguredMock(InvitationResponseServer::class, [
174-
'server' => $this->createConfiguredMock(CalDavBackend::class, [
175-
'getPlugin' => [
176-
'auth' => $this->createMock(CustomPrincipalPlugin::class),
177-
'schedule' => $this->createMock(Plugin::class)
178-
]
179-
])
180-
]);
205+
/** @var CustomPrincipalPlugin|MockObject $authPlugin */
206+
$authPlugin = $this->createMock(CustomPrincipalPlugin::class);
207+
$authPlugin->expects(self::once())
208+
->method('setCurrentPrincipal')
209+
->with($this->calendar->getPrincipalURI());
210+
unset($this->calendarInfo['uri']);
211+
212+
/** @var Plugin|MockObject $schedulingPlugin */
213+
$schedulingPlugin = $this->createMock(Plugin::class);
214+
215+
/** @var \Sabre\DAVACL\Plugin|MockObject $schedulingPlugin */
216+
$aclPlugin = $this->createMock(\Sabre\DAVACL\Plugin::class);
217+
218+
$server =
219+
$this->createMock(Server::class);
220+
$server->expects($this->any())
221+
->method('getPlugin')
222+
->willReturnMap([
223+
['auth', $authPlugin],
224+
['acl', $aclPlugin],
225+
['caldav-schedule', $schedulingPlugin]
226+
]);
227+
228+
$invitationResponseServer = $this->createPartialMock(InvitationResponseServer::class, ['getServer']);
229+
$invitationResponseServer->server = $server;
230+
$invitationResponseServer->expects($this->any())
231+
->method('getServer')
232+
->willReturn($server);
233+
234+
$calendarImpl = $this->getMockBuilder(CalendarImpl::class)
235+
->setConstructorArgs([$this->calendar, $this->calendarInfo, $this->backend])
236+
->onlyMethods(['getInvitationResponseServer'])
237+
->getMock();
238+
$calendarImpl->expects($this->once())
239+
->method('getInvitationResponseServer')
240+
->willReturn($invitationResponseServer);
181241

182242
$message = <<<EOF
183243
BEGIN:VCALENDAR
184244
PRODID:-//Nextcloud/Nextcloud CalDAV Server//EN
185245
METHOD:REPLY
186246
VERSION:2.0
187247
BEGIN:VEVENT
188-
ATTENDEE;PARTSTAT=mailto:[email protected]:ACCEPTED
248+
ATTENDEE;PARTSTAT=ACCEPTED:mailto:[email protected]
189249
ORGANIZER:mailto:[email protected]
190250
UID:aUniqueUid
191251
SEQUENCE:2
192252
REQUEST-STATUS:2.0;Success
193-
%sEND:VEVENT
253+
END:VEVENT
194254
END:VCALENDAR
195255
EOF;
196256

197-
/** @var CustomPrincipalPlugin|MockObject $authPlugin */
198-
$authPlugin = $invitationResponseServer->server->getPlugin('auth');
199-
$authPlugin->expects(self::once())
200-
->method('setPrincipalUri')
201-
->with($this->calendar->getPrincipalURI());
257+
$this->expectException(CalendarException::class);
258+
$calendarImpl->handleIMipMessage('filename.ics', $message);
259+
}
202260

203-
unset($this->calendarInfo['uri']);
204-
$this->expectException('CalendarException');
205-
$this->calendarImpl->handleIMipMessage('filename.ics', $message);
261+
private function getITipMessage($calendarData): Message {
262+
$iTipMessage = new Message();
263+
/** @var VCalendar $vObject */
264+
$vObject = Reader::read($calendarData);
265+
/** @var VEvent $vEvent */
266+
$vEvent = $vObject->{'VEVENT'};
267+
$orgaizer = $vEvent->{'ORGANIZER'}->getValue();
268+
$attendee = $vEvent->{'ATTENDEE'}->getValue();
269+
270+
$iTipMessage->method = $vObject->{'METHOD'}->getValue();
271+
$iTipMessage->recipient = $orgaizer;
272+
$iTipMessage->sender = $attendee;
273+
$iTipMessage->uid = isset($vEvent->{'UID'}) ? $vEvent->{'UID'}->getValue() : '';
274+
$iTipMessage->component = 'VEVENT';
275+
$iTipMessage->sequence = isset($vEvent->{'SEQUENCE'}) ? (int)$vEvent->{'SEQUENCE'}->getValue() : 0;
276+
$iTipMessage->message = $vObject;
277+
return $iTipMessage;
206278
}
207279
}

apps/dav/tests/unit/Controller/InvitationResponseControllerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ private function buildQueryExpects($token, $return, $time) {
480480
$expr->expects($this->once())
481481
->method('eq')
482482
->with('token', 'namedParameterToken')
483-
->willReturn($function);
483+
->willReturn((string)$function);
484484

485485
$this->dbConnection->expects($this->once())
486486
->method('getQueryBuilder')

tests/lib/Encryption/UtilTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
use Test\TestCase;
1414

1515
class UtilTest extends TestCase {
16-
1716
/**
1817
* block size will always be 8192 for a PHP stream
1918
* @see https://bugs.php.net/bug.php?id=21641

0 commit comments

Comments
 (0)