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

Fix some phpunit test warnings #34819

Merged
merged 7 commits into from
Dec 6, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
16 changes: 10 additions & 6 deletions apps/dav/lib/CalDAV/CalendarImpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public function createFromString(string $name, string $calendarData): void {
$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
Expand All @@ -163,14 +163,14 @@ public function createFromString(string $name, string $calendarData): void {

// 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 {
Expand All @@ -182,10 +182,10 @@ public function createFromString(string $name, string $calendarData): void {
* @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
Expand All @@ -196,7 +196,7 @@ public function handleIMipMessage(string $name, string $calendarData): void {
}
// 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 */
Expand Down Expand Up @@ -232,4 +232,8 @@ public function handleIMipMessage(string $name, string $calendarData): void {
$iTipMessage->message = $vObject;
$schedulingPlugin->scheduleLocalDelivery($iTipMessage);
}

public function getInvitationResponseServer() {
Fixed Show fixed Hide fixed
come-nc marked this conversation as resolved.
Show resolved Hide resolved
return new InvitationResponseServer(false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,11 @@ public function handleITipMessage(Message $iTipMessage) {

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() {
Fixed Show fixed Hide fixed
come-nc marked this conversation as resolved.
Show resolved Hide resolved
return $this->server;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public function testRun() {
$expr->expects($this->once())
->method('lt')
->with('expiration', 'namedParameter1337')
->willReturn($function);
->willReturn((string)$function);
come-nc marked this conversation as resolved.
Show resolved Hide resolved

$this->dbConnection->expects($this->once())
->method('getQueryBuilder')
Expand Down
143 changes: 108 additions & 35 deletions apps/dav/tests/unit/CalDAV/CalendarImplTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,17 @@
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 */
Expand Down Expand Up @@ -69,7 +78,7 @@ public function testGetKey() {
}

public function testGetDisplayname() {
$this->assertEquals($this->calendarImpl->getDisplayName(),'user readable name 123');
$this->assertEquals($this->calendarImpl->getDisplayName(), 'user readable name 123');
}

public function testGetDisplayColor() {
Expand Down Expand Up @@ -132,76 +141,140 @@ public function testGetPermissionAll() {
}

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:[email protected]:ACCEPTED
ATTENDEE;PARTSTAT=ACCEPTED:mailto:[email protected]
ORGANIZER:mailto:[email protected]
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:[email protected]";
$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
PRODID:-//Nextcloud/Nextcloud CalDAV Server//EN
METHOD:REPLY
VERSION:2.0
BEGIN:VEVENT
ATTENDEE;PARTSTAT=mailto:[email protected]:ACCEPTED
ATTENDEE;PARTSTAT=ACCEPTED:mailto:[email protected]
ORGANIZER:mailto:[email protected]
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ private function buildQueryExpects($token, $return, $time) {
$expr->expects($this->once())
->method('eq')
->with('token', 'namedParameterToken')
->willReturn($function);
->willReturn((string)$function);

$this->dbConnection->expects($this->once())
->method('getQueryBuilder')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\IConfig;
use OCP\IDBConnection;
use OCP\Security\ICrypto;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\LoggerInterface;
Expand Down Expand Up @@ -73,9 +74,6 @@ protected function setUp(): void {
['openssl', [], []],
]);
$this->db = $this->createMock(IDBConnection::class);
$this->db->method('atomic')->willReturnCallback(function ($cb) {
return $cb();
});
$this->logger = $this->createMock(LoggerInterface::class);
$this->timeFactory = $this->createMock(ITimeFactory::class);
$this->time = 1313131;
Expand Down
2 changes: 2 additions & 0 deletions tests/lib/Encryption/UtilTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
namespace Test\Encryption;

use OC\Encryption\Util;
use OC\Files\Filesystem;
use OC\Files\View;
use OC\User\Manager;
use OCA\Files_External\Lib\StorageConfig;
use OCA\Files_External\Service\GlobalStoragesService;
use OCP\Encryption\IEncryptionModule;
Expand Down