diff options
author | Côme Chilliet <91878298+come-nc@users.noreply.github.com> | 2022-12-06 10:51:41 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-12-06 10:51:41 +0100 |
commit | 67ff8fc738c13dc34a6eacf1effca4ac1e86745a (patch) | |
tree | 91b4f6aac11a0c456b3a64c85cb6937dbc544346 /apps | |
parent | 28358acdafa318d6b8482c5260525a31c279992d (diff) | |
parent | 8ec7e4301d58eaa8ff1f568e5ace7ac119ae2567 (diff) | |
download | nextcloud-server-67ff8fc738c13dc34a6eacf1effca4ac1e86745a.tar.gz nextcloud-server-67ff8fc738c13dc34a6eacf1effca4ac1e86745a.zip |
Merge pull request #34819 from nextcloud/tests/fix-phpunit-warnings
Fix some phpunit test warnings
Diffstat (limited to 'apps')
4 files changed, 128 insertions, 50 deletions
diff --git a/apps/dav/lib/CalDAV/CalendarImpl.php b/apps/dav/lib/CalDAV/CalendarImpl.php index 79a5626f322..3fb28a631a7 100644 --- a/apps/dav/lib/CalDAV/CalendarImpl.php +++ b/apps/dav/lib/CalDAV/CalendarImpl.php @@ -42,7 +42,6 @@ use Sabre\VObject\Reader; use function Sabre\Uri\split as uriSplit; class CalendarImpl implements ICreateFromString, IHandleImipMessage { - private CalDavBackend $backend; private Calendar $calendar; /** @var array<string, mixed> */ @@ -147,7 +146,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 +162,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 +181,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 +195,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 */ @@ -204,25 +203,25 @@ class CalendarImpl implements ICreateFromString, IHandleImipMessage { /** @var VEvent $vEvent */ $vEvent = $vObject->{'VEVENT'}; - if($vObject->{'METHOD'} === null) { + if ($vObject->{'METHOD'} === null) { throw new CalendarException('No Method provided for scheduling data. Could not process message'); } - if(!isset($vEvent->{'ORGANIZER'}) || !isset($vEvent->{'ATTENDEE'})) { + if (!isset($vEvent->{'ORGANIZER'}) || !isset($vEvent->{'ATTENDEE'})) { throw new CalendarException('Could not process scheduling data, neccessary data missing from ICAL'); } $organizer = $vEvent->{'ORGANIZER'}->getValue(); $attendee = $vEvent->{'ATTENDEE'}->getValue(); $iTipMessage->method = $vObject->{'METHOD'}->getValue(); - if($iTipMessage->method === 'REPLY') { + if ($iTipMessage->method === 'REPLY') { if ($server->isExternalAttendee($vEvent->{'ATTENDEE'}->getValue())) { $iTipMessage->recipient = $organizer; } else { $iTipMessage->recipient = $attendee; } $iTipMessage->sender = $attendee; - } else if($iTipMessage->method === 'CANCEL') { + } elseif ($iTipMessage->method === 'CANCEL') { $iTipMessage->recipient = $attendee; $iTipMessage->sender = $organizer; } @@ -232,4 +231,8 @@ class CalendarImpl implements ICreateFromString, IHandleImipMessage { $iTipMessage->message = $vObject; $schedulingPlugin->scheduleLocalDelivery($iTipMessage); } + + public function getInvitationResponseServer(): InvitationResponseServer { + return new InvitationResponseServer(false); + } } diff --git a/apps/dav/lib/CalDAV/InvitationResponse/InvitationResponseServer.php b/apps/dav/lib/CalDAV/InvitationResponse/InvitationResponseServer.php index a85892443cc..e64c815753b 100644 --- a/apps/dav/lib/CalDAV/InvitationResponse/InvitationResponseServer.php +++ b/apps/dav/lib/CalDAV/InvitationResponse/InvitationResponseServer.php @@ -39,7 +39,6 @@ use Psr\Log\LoggerInterface; use Sabre\VObject\ITip\Message; class InvitationResponseServer { - /** @var \OCA\DAV\Connector\Sabre\Server */ public $server; @@ -127,7 +126,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(): \OCA\DAV\Connector\Sabre\Server { + return $this->server; + } } diff --git a/apps/dav/tests/unit/CalDAV/CalendarImplTest.php b/apps/dav/tests/unit/CalDAV/CalendarImplTest.php index 6842bdadb53..cc0b963634c 100644 --- a/apps/dav/tests/unit/CalDAV/CalendarImplTest.php +++ b/apps/dav/tests/unit/CalDAV/CalendarImplTest.php @@ -31,10 +31,18 @@ use OCA\DAV\CalDAV\Calendar; use OCA\DAV\CalDAV\CalendarImpl; use OCA\DAV\CalDAV\InvitationResponse\InvitationResponseServer; use OCA\DAV\CalDAV\Schedule\Plugin; +use OCA\DAV\Connector\Sabre\Server; +use OCP\Calendar\Exceptions\CalendarException; use PHPUnit\Framework\MockObject\MockObject; +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 */ private $calendarImpl; @@ -69,7 +77,7 @@ class CalendarImplTest extends \Test\TestCase { } public function testGetDisplayname() { - $this->assertEquals($this->calendarImpl->getDisplayName(),'user readable name 123'); + $this->assertEquals($this->calendarImpl->getDisplayName(), 'user readable name 123'); } public function testGetDisplayColor() { @@ -132,52 +140,104 @@ 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) - ] - ]) - ]); - $message = <<<EOF BEGIN:VCALENDAR PRODID:-//Nextcloud/Nextcloud CalDAV Server//EN METHOD:REPLY VERSION:2.0 BEGIN:VEVENT -ATTENDEE;PARTSTAT=mailto:lewis@stardew-tent-living.com:ACCEPTED +ATTENDEE;PARTSTAT=ACCEPTED:mailto:lewis@stardew-tent-living.com ORGANIZER:mailto:pierre@generalstore.com UID:aUniqueUid SEQUENCE:2 REQUEST-STATUS:2.0;Success -%sEND:VEVENT +END:VEVENT END:VCALENDAR EOF; /** @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()); + /** @var \Sabre\DAVACL\Plugin|MockObject $aclPlugin*/ + $aclPlugin = $this->createMock(\Sabre\DAVACL\Plugin::class); + /** @var Plugin|MockObject $schedulingPlugin */ - $schedulingPlugin = $invitationResponseServer->server->getPlugin('caldav-schedule'); + $schedulingPlugin = $this->createMock(Plugin::class); + $iTipMessage = $this->getITipMessage($message); + $iTipMessage->recipient = "mailto:lewis@stardew-tent-living.com"; $schedulingPlugin->expects(self::once()) - ->method('setPathOfCalendarObjectChange') - ->with('fullcalendarname'); + ->method('scheduleLocalDelivery') + ->with($iTipMessage); + + $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', 'isExternalAttendee']); + $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); + + $calendarImpl->handleIMipMessage('filename.ics', $message); } 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 CustomPrincipalPlugin|MockObject $authPlugin */ + $authPlugin = $this->createMock(CustomPrincipalPlugin::class); + $authPlugin->expects(self::once()) + ->method('setCurrentPrincipal') + ->with($this->calendar->getPrincipalURI()); + unset($this->calendarInfo['uri']); + + /** @var Plugin|MockObject $schedulingPlugin */ + $schedulingPlugin = $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 @@ -185,23 +245,35 @@ PRODID:-//Nextcloud/Nextcloud CalDAV Server//EN METHOD:REPLY VERSION:2.0 BEGIN:VEVENT -ATTENDEE;PARTSTAT=mailto:lewis@stardew-tent-living.com:ACCEPTED +ATTENDEE;PARTSTAT=ACCEPTED:mailto:lewis@stardew-tent-living.com ORGANIZER:mailto:pierre@generalstore.com UID:aUniqueUid SEQUENCE:2 REQUEST-STATUS:2.0;Success -%sEND:VEVENT +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; } } diff --git a/apps/dav/tests/unit/Controller/InvitationResponseControllerTest.php b/apps/dav/tests/unit/Controller/InvitationResponseControllerTest.php index efbad5e3008..fdbb69e6c0d 100644 --- a/apps/dav/tests/unit/Controller/InvitationResponseControllerTest.php +++ b/apps/dav/tests/unit/Controller/InvitationResponseControllerTest.php @@ -480,7 +480,7 @@ EOF; $expr->expects($this->once()) ->method('eq') ->with('token', 'namedParameterToken') - ->willReturn($function); + ->willReturn((string)$function); $this->dbConnection->expects($this->once()) ->method('getQueryBuilder') |