diff options
author | Julius Härtl <jus@bitgrid.net> | 2022-10-28 11:19:13 +0200 |
---|---|---|
committer | Julius Härtl <jus@bitgrid.net> | 2022-11-09 16:37:34 +0100 |
commit | 908b7aa70af4417a0011e1b09ba44dc9b50792f2 (patch) | |
tree | 54e09dbbaa05d8355c5642aa54ba2644e15baddd /apps | |
parent | 2a684f674164640d917a4fd1b3631b6ed3b36eb3 (diff) | |
download | nextcloud-server-908b7aa70af4417a0011e1b09ba44dc9b50792f2.tar.gz nextcloud-server-908b7aa70af4417a0011e1b09ba44dc9b50792f2.zip |
Attempt to fix CalendarImpl tests
Signed-off-by: Julius Härtl <jus@bitgrid.net>
Diffstat (limited to 'apps')
-rw-r--r-- | apps/dav/lib/CalDAV/CalendarImpl.php | 16 | ||||
-rw-r--r-- | apps/dav/lib/CalDAV/InvitationResponse/InvitationResponseServer.php | 6 | ||||
-rw-r--r-- | apps/dav/tests/unit/CalDAV/CalendarImplTest.php | 138 |
3 files changed, 121 insertions, 39 deletions
diff --git a/apps/dav/lib/CalDAV/CalendarImpl.php b/apps/dav/lib/CalDAV/CalendarImpl.php index 79a5626f322..f9e9c584808 100644 --- a/apps/dav/lib/CalDAV/CalendarImpl.php +++ b/apps/dav/lib/CalDAV/CalendarImpl.php @@ -147,7 +147,7 @@ class CalendarImpl implements ICreateFromString, IHandleImipMessage { $server = new InvitationResponseServer(false); /** @var CustomPrincipalPlugin $plugin */ - $plugin = $server->server->getPlugin('auth'); + $plugin = $server->getServer()->getPlugin('auth'); // we're working around the previous implementation // that only allowed the public system principal to be used // so set the custom principal here @@ -163,14 +163,14 @@ class CalendarImpl implements ICreateFromString, IHandleImipMessage { // Force calendar change URI /** @var Schedule\Plugin $schedulingPlugin */ - $schedulingPlugin = $server->server->getPlugin('caldav-schedule'); + $schedulingPlugin = $server->getServer()->getPlugin('caldav-schedule'); $schedulingPlugin->setPathOfCalendarObjectChange($fullCalendarFilename); $stream = fopen('php://memory', 'rb+'); fwrite($stream, $calendarData); rewind($stream); try { - $server->server->createFile($fullCalendarFilename, $stream); + $server->getServer()->createFile($fullCalendarFilename, $stream); } catch (Conflict $e) { throw new CalendarException('Could not create new calendar event: ' . $e->getMessage(), 0, $e); } finally { @@ -182,10 +182,10 @@ class CalendarImpl implements ICreateFromString, IHandleImipMessage { * @throws CalendarException */ public function handleIMipMessage(string $name, string $calendarData): void { - $server = new InvitationResponseServer(false); + $server = $this->getInvitationResponseServer(); /** @var CustomPrincipalPlugin $plugin */ - $plugin = $server->server->getPlugin('auth'); + $plugin = $server->getServer()->getPlugin('auth'); // we're working around the previous implementation // that only allowed the public system principal to be used // so set the custom principal here @@ -196,7 +196,7 @@ class CalendarImpl implements ICreateFromString, IHandleImipMessage { } // Force calendar change URI /** @var Schedule\Plugin $schedulingPlugin */ - $schedulingPlugin = $server->server->getPlugin('caldav-schedule'); + $schedulingPlugin = $server->getServer()->getPlugin('caldav-schedule'); // Let sabre handle the rest $iTipMessage = new Message(); /** @var VCalendar $vObject */ @@ -232,4 +232,8 @@ class CalendarImpl implements ICreateFromString, IHandleImipMessage { $iTipMessage->message = $vObject; $schedulingPlugin->scheduleLocalDelivery($iTipMessage); } + + public function getInvitationResponseServer() { + return new InvitationResponseServer(false); + } } diff --git a/apps/dav/lib/CalDAV/InvitationResponse/InvitationResponseServer.php b/apps/dav/lib/CalDAV/InvitationResponse/InvitationResponseServer.php index a85892443cc..c2e31f7bdc4 100644 --- a/apps/dav/lib/CalDAV/InvitationResponse/InvitationResponseServer.php +++ b/apps/dav/lib/CalDAV/InvitationResponse/InvitationResponseServer.php @@ -127,7 +127,11 @@ class InvitationResponseServer { public function isExternalAttendee(string $principalUri): bool { /** @var \Sabre\DAVACL\Plugin $aclPlugin */ - $aclPlugin = $this->server->getPlugin('acl'); + $aclPlugin = $this->getServer()->getPlugin('acl'); return $aclPlugin->getPrincipalByUri($principalUri) === null; } + + public function getServer() { + return $this->server; + } } diff --git a/apps/dav/tests/unit/CalDAV/CalendarImplTest.php b/apps/dav/tests/unit/CalDAV/CalendarImplTest.php index 56aef20ca74..b504e8b1249 100644 --- a/apps/dav/tests/unit/CalDAV/CalendarImplTest.php +++ b/apps/dav/tests/unit/CalDAV/CalendarImplTest.php @@ -31,8 +31,17 @@ use OCA\DAV\CalDAV\Calendar; use OCA\DAV\CalDAV\CalendarImpl; use OCA\DAV\CalDAV\InvitationResponse\InvitationResponseServer; use OCA\DAV\CalDAV\Schedule\Plugin; +use OCP\Calendar\Exceptions\CalendarException; use PHPUnit\Framework\MockObject\MockObject; +use Sabre\DAV\Server; +use Sabre\VObject\Component\VCalendar; +use Sabre\VObject\Component\VEvent; +use Sabre\VObject\ITip\Message; +use Sabre\VObject\Reader; +/** + * @group DB + */ class CalendarImplTest extends \Test\TestCase { /** @var CalendarImpl */ @@ -132,14 +141,43 @@ class CalendarImplTest extends \Test\TestCase { } public function testHandleImipMessage(): void { - $invitationResponseServer = $this->createConfiguredMock(InvitationResponseServer::class, [ - 'server' => $this->createConfiguredMock(CalDavBackend::class, [ - 'getPlugin' => [ - 'auth' => $this->createMock(CustomPrincipalPlugin::class), - 'schedule' => $this->createMock(Plugin::class) - ] - ]) - ]); + /** @var CustomPrincipalPlugin|MockObject $authPlugin */ + $authPlugin = $this->createMock(CustomPrincipalPlugin::class); + $authPlugin->expects(self::once()) + ->method('setCurrentPrincipal') + ->with($this->calendar->getPrincipalURI()); + + /** @var \Sabre\DAVACL\Plugin|MockObject $schedulingPlugin */ + $aclPlugin = $this->createMock(\Sabre\DAVACL\Plugin::class); + $aclPlugin->expects(self::once()) + ->method('getPrincipalByUri') + ->with('mailto:lewis@stardew-tent-living.com'); + + $server = $this->createMock(Server::class); + $server->expects($this->any()) + ->method('getPlugin') + ->willReturnMap([ + ['auth', $authPlugin], + ['acl', $aclPlugin], + ['caldav-schedule', $schedulingPlugin] + ]); + + $invitationResponseServer = $this->createPartialMock(InvitationResponseServer::class, ['getServer']); + $invitationResponseServer->server = $server; + $invitationResponseServer->expects($this->any()) + ->method('getServer') + ->willReturn($server); + $invitationResponseServer->expects(self::once()) + ->method('isExternalAttendee') + ->willReturn(false); + + $calendarImpl = $this->getMockBuilder(CalendarImpl::class) + ->setConstructorArgs([$this->calendar, $this->calendarInfo, $this->backend]) + ->onlyMethods(['getInvitationResponseServer']) + ->getMock(); + $calendarImpl->expects($this->once()) + ->method('getInvitationResponseServer') + ->willReturn($invitationResponseServer); $message = <<<EOF BEGIN:VCALENDAR @@ -155,29 +193,53 @@ REQUEST-STATUS:2.0;Success END:VEVENT END:VCALENDAR EOF; + /** @var Plugin|MockObject $schedulingPlugin */ + $schedulingPlugin = $this->createMock(Plugin::class); + $iTipMessage = $this->getITipMessage($message); + $schedulingPlugin->expects(self::once()) + ->method('scheduleLocalDelivery') + ->with($iTipMessage); + $calendarImpl->handleIMipMessage('filename.ics', $message); + } + + public function testHandleImipMessageNoCalendarUri(): void { /** @var CustomPrincipalPlugin|MockObject $authPlugin */ - $authPlugin = $invitationResponseServer->server->getPlugin('auth'); + $authPlugin = $this->createMock(CustomPrincipalPlugin::class); $authPlugin->expects(self::once()) - ->method('setPrincipalUri') + ->method('setCurrentPrincipal') ->with($this->calendar->getPrincipalURI()); + unset($this->calendarInfo['uri']); /** @var Plugin|MockObject $schedulingPlugin */ - $schedulingPlugin = $invitationResponseServer->server->getPlugin('caldav-schedule'); - $schedulingPlugin->expects(self::once()) - ->method('setPathOfCalendarObjectChange') - ->with('fullcalendarname'); - } + $schedulingPlugin = $this->createMock(Plugin::class); - public function testHandleImipMessageNoCalendarUri(): void { - $invitationResponseServer = $this->createConfiguredMock(InvitationResponseServer::class, [ - 'server' => $this->createConfiguredMock(CalDavBackend::class, [ - 'getPlugin' => [ - 'auth' => $this->createMock(CustomPrincipalPlugin::class), - 'schedule' => $this->createMock(Plugin::class) - ] - ]) - ]); + /** @var \Sabre\DAVACL\Plugin|MockObject $schedulingPlugin */ + $aclPlugin = $this->createMock(\Sabre\DAVACL\Plugin::class); + + $server = + $this->createMock(Server::class); + $server->expects($this->any()) + ->method('getPlugin') + ->willReturnMap([ + ['auth', $authPlugin], + ['acl', $aclPlugin], + ['caldav-schedule', $schedulingPlugin] + ]); + + $invitationResponseServer = $this->createPartialMock(InvitationResponseServer::class, ['getServer']); + $invitationResponseServer->server = $server; + $invitationResponseServer->expects($this->any()) + ->method('getServer') + ->willReturn($server); + + $calendarImpl = $this->getMockBuilder(CalendarImpl::class) + ->setConstructorArgs([$this->calendar, $this->calendarInfo, $this->backend]) + ->onlyMethods(['getInvitationResponseServer']) + ->getMock(); + $calendarImpl->expects($this->once()) + ->method('getInvitationResponseServer') + ->willReturn($invitationResponseServer); $message = <<<EOF BEGIN:VCALENDAR @@ -194,14 +256,26 @@ END:VEVENT END:VCALENDAR EOF; - /** @var CustomPrincipalPlugin|MockObject $authPlugin */ - $authPlugin = $invitationResponseServer->server->getPlugin('auth'); - $authPlugin->expects(self::once()) - ->method('setPrincipalUri') - ->with($this->calendar->getPrincipalURI()); + $this->expectException(CalendarException::class); + $calendarImpl->handleIMipMessage('filename.ics', $message); + } - unset($this->calendarInfo['uri']); - $this->expectException('CalendarException'); - $this->calendarImpl->handleIMipMessage('filename.ics', $message); + private function getITipMessage($calendarData): Message { + $iTipMessage = new Message(); + /** @var VCalendar $vObject */ + $vObject = Reader::read($calendarData); + /** @var VEvent $vEvent */ + $vEvent = $vObject->{'VEVENT'}; + $orgaizer = $vEvent->{'ORGANIZER'}->getValue(); + $attendee = $vEvent->{'ATTENDEE'}->getValue(); + + $iTipMessage->method = $vObject->{'METHOD'}->getValue(); + $iTipMessage->recipient = $orgaizer; + $iTipMessage->sender = $attendee; + $iTipMessage->uid = isset($vEvent->{'UID'}) ? $vEvent->{'UID'}->getValue() : ''; + $iTipMessage->component = 'VEVENT'; + $iTipMessage->sequence = isset($vEvent->{'SEQUENCE'}) ? (int)$vEvent->{'SEQUENCE'}->getValue() : 0; + $iTipMessage->message = $vObject; + return $iTipMessage; } } |