Skip to content

Commit

Permalink
Attempt to fix CalendarImpl tests
Browse files Browse the repository at this point in the history
Signed-off-by: Julius Härtl <[email protected]>
  • Loading branch information
juliusknorr committed Oct 28, 2022
1 parent bc740ad commit 187a707
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 41 deletions.
16 changes: 10 additions & 6 deletions apps/dav/lib/CalDAV/CalendarImpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,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 @@ -166,14 +166,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 @@ -185,10 +185,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 @@ -199,7 +199,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 @@ -235,4 +235,8 @@ public function handleIMipMessage(string $name, string $calendarData): void {
$iTipMessage->message = $vObject;
$schedulingPlugin->scheduleLocalDelivery($iTipMessage);
}

public function getInvitationResponseServer() {
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() {
return $this->server;
}
}
119 changes: 85 additions & 34 deletions apps/dav/tests/unit/CalDAV/CalendarImplTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,13 @@
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;

/**
* @group DB
*/
class CalendarImplTest extends \Test\TestCase {

/** @var CalendarImpl */
Expand Down Expand Up @@ -131,15 +136,50 @@ public function testGetPermissionAll() {
$this->assertEquals(31, $this->calendarImpl->getPermissions());
}

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)
]
])
]);
public function testHandleImipMssage(): void {
/** @var CustomPrincipalPlugin|MockObject $authPlugin */
$authPlugin = $this->createMock(CustomPrincipalPlugin::class);
$authPlugin->expects(self::once())
->method('setCurrentPrincipal')
->with($this->calendar->getPrincipalURI());

/** @var Plugin|MockObject $schedulingPlugin */
$schedulingPlugin = $this->createMock(Plugin::class);
// FIXME: THis was expected to be called once but isn't
$schedulingPlugin->expects(self::once())
->method('setPathOfCalendarObjectChange')
->with('fullcalendarname');

/** @var \Sabre\DAVACL\Plugin|MockObject $schedulingPlugin */
$aclPlugin = $this->createMock(\Sabre\DAVACL\Plugin::class);
// FIXME: The :ACCEPTED looks odd in the call
$aclPlugin->expects(self::once())
->method('getPrincipalByUri')
->with('[email protected]:ACCEPTED');

$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
Expand All @@ -156,28 +196,46 @@ public function testHandleImipMessage(): void {
END:VCALENDAR
EOF;

$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
Expand All @@ -194,14 +252,7 @@ public function testHandleImipMessageNoCalendarUri(): void {
END:VCALENDAR
EOF;

/** @var CustomPrincipalPlugin|MockObject $authPlugin */
$authPlugin = $invitationResponseServer->server->getPlugin('auth');
$authPlugin->expects(self::once())
->method('setPrincipalUri')
->with($this->calendar->getPrincipalURI());

unset($this->calendarInfo['uri']);
$this->expectException('CalendarException');
$this->calendarImpl->handleIMipMessage('filename.ics', $message);
$this->expectException(CalendarException::class);
$calendarImpl->handleIMipMessage('filename.ics', $message);
}
}
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

0 comments on commit 187a707

Please sign in to comment.