refactor: implement reusable calendar factory

Signed-off-by: Richard Steinmetz <richard@steinmetz.cloud>
This commit is contained in:
Richard Steinmetz 2025-08-28 14:59:34 +02:00
parent d785bcdc6e
commit ca3b35e756
No known key found for this signature in database
GPG key ID: 27137D9E7D273FB2
19 changed files with 156 additions and 171 deletions

View file

@ -8,6 +8,7 @@
// Backends
use OC\KnownUser\KnownUserService;
use OCA\DAV\CalDAV\CalDavBackend;
use OCA\DAV\CalDAV\CalendarFactory;
use OCA\DAV\CalDAV\CalendarRoot;
use OCA\DAV\CalDAV\DefaultCalendarValidator;
use OCA\DAV\CalDAV\Federation\FederatedCalendarFactory;
@ -67,6 +68,7 @@ $config = Server::get(IConfig::class);
$l10nFactory = Server::get(IL10NFactory::class);
$davL10n = $l10nFactory->get('dav');
$federatedCalendarFactory = Server::get(FederatedCalendarFactory::class);
$calendarFactory = Server::get(CalendarFactory::class);
$calDavBackend = new CalDavBackend(
$db,
@ -88,7 +90,7 @@ $sendInvitations = Server::get(IConfig::class)->getAppValue('dav', 'sendInvitati
$principalCollection = new \Sabre\CalDAV\Principal\Collection($principalBackend);
$principalCollection->disableListing = !$debugging; // Disable listing
$addressBookRoot = new CalendarRoot($principalBackend, $calDavBackend, 'principals', $logger, $davL10n, $config, $federatedCalendarFactory);
$addressBookRoot = new CalendarRoot($principalBackend, $calDavBackend, 'principals', $federatedCalendarFactory, $calendarFactory);
$addressBookRoot->disableListing = !$debugging; // Disable listing
$nodes = [

View file

@ -55,6 +55,7 @@ return array(
'OCA\\DAV\\CalDAV\\CachedSubscriptionProvider' => $baseDir . '/../lib/CalDAV/CachedSubscriptionProvider.php',
'OCA\\DAV\\CalDAV\\CalDavBackend' => $baseDir . '/../lib/CalDAV/CalDavBackend.php',
'OCA\\DAV\\CalDAV\\Calendar' => $baseDir . '/../lib/CalDAV/Calendar.php',
'OCA\\DAV\\CalDAV\\CalendarFactory' => $baseDir . '/../lib/CalDAV/CalendarFactory.php',
'OCA\\DAV\\CalDAV\\CalendarHome' => $baseDir . '/../lib/CalDAV/CalendarHome.php',
'OCA\\DAV\\CalDAV\\CalendarImpl' => $baseDir . '/../lib/CalDAV/CalendarImpl.php',
'OCA\\DAV\\CalDAV\\CalendarManager' => $baseDir . '/../lib/CalDAV/CalendarManager.php',

View file

@ -70,6 +70,7 @@ class ComposerStaticInitDAV
'OCA\\DAV\\CalDAV\\CachedSubscriptionProvider' => __DIR__ . '/..' . '/../lib/CalDAV/CachedSubscriptionProvider.php',
'OCA\\DAV\\CalDAV\\CalDavBackend' => __DIR__ . '/..' . '/../lib/CalDAV/CalDavBackend.php',
'OCA\\DAV\\CalDAV\\Calendar' => __DIR__ . '/..' . '/../lib/CalDAV/Calendar.php',
'OCA\\DAV\\CalDAV\\CalendarFactory' => __DIR__ . '/..' . '/../lib/CalDAV/CalendarFactory.php',
'OCA\\DAV\\CalDAV\\CalendarHome' => __DIR__ . '/..' . '/../lib/CalDAV/CalendarHome.php',
'OCA\\DAV\\CalDAV\\CalendarImpl' => __DIR__ . '/..' . '/../lib/CalDAV/CalendarImpl.php',
'OCA\\DAV\\CalDAV\\CalendarManager' => __DIR__ . '/..' . '/../lib/CalDAV/CalendarManager.php',

View file

@ -0,0 +1,49 @@
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\DAV\CalDAV;
use OCA\DAV\AppInfo\Application;
use OCP\IConfig;
use OCP\IL10N;
use OCP\L10N\IFactory as IL10NFactory;
use Psr\Log\LoggerInterface;
class CalendarFactory {
private readonly IL10N $l10n;
public function __construct(
private readonly CalDavBackend $calDavBackend,
private readonly IConfig $config,
private readonly LoggerInterface $logger,
IL10NFactory $l10nFactory,
) {
$this->l10n = $l10nFactory->get(Application::APP_ID);
}
public function createCalendar(array $calendarInfo): Calendar {
return new Calendar(
$this->calDavBackend,
$calendarInfo,
$this->l10n,
$this->config,
$this->logger,
);
}
public function createPublicCalendar(array $calendarInfo): PublicCalendar {
return new PublicCalendar(
$this->calDavBackend,
$calendarInfo,
$this->l10n,
$this->config,
$this->logger,
);
}
}

View file

@ -14,9 +14,7 @@ use OCA\DAV\CalDAV\Integration\ICalendarProvider;
use OCA\DAV\CalDAV\Trashbin\TrashbinHome;
use OCP\App\IAppManager;
use OCP\IConfig;
use OCP\IL10N;
use OCP\Server;
use Psr\Log\LoggerInterface;
use Sabre\CalDAV\Backend\BackendInterface;
use Sabre\CalDAV\Backend\NotificationSupport;
use Sabre\CalDAV\Backend\SchedulingSupport;
@ -30,9 +28,6 @@ use Sabre\DAV\MkCol;
class CalendarHome extends \Sabre\CalDAV\CalendarHome {
/** @var IL10N */
private $l10n;
/** @var IConfig */
private $config;
@ -44,12 +39,11 @@ class CalendarHome extends \Sabre\CalDAV\CalendarHome {
public function __construct(
BackendInterface $caldavBackend,
array $principalInfo,
private LoggerInterface $logger,
private FederatedCalendarFactory $federatedCalendarFactory,
private readonly CalendarFactory $calendarFactory,
private bool $returnCachedSubscriptions,
) {
parent::__construct($caldavBackend, $principalInfo);
$this->l10n = \OC::$server->getL10N('dav');
$this->config = Server::get(IConfig::class);
$this->pluginManager = new PluginManager(
\OC::$server,
@ -90,7 +84,7 @@ class CalendarHome extends \Sabre\CalDAV\CalendarHome {
$calendars = $this->caldavBackend->getCalendarsForUser($this->principalInfo['uri']);
$objects = [];
foreach ($calendars as $calendar) {
$objects[] = new Calendar($this->caldavBackend, $calendar, $this->l10n, $this->config, $this->logger);
$objects[] = $this->calendarFactory->createCalendar($calendar);
}
if ($this->caldavBackend instanceof SchedulingSupport) {
@ -164,7 +158,7 @@ class CalendarHome extends \Sabre\CalDAV\CalendarHome {
// Calendar - this covers all "regular" calendars, but not shared
$calendar = $this->caldavBackend->getCalendarByUri($this->principalInfo['uri'], $name);
if (!empty($calendar)) {
return new Calendar($this->caldavBackend, $calendar, $this->l10n, $this->config, $this->logger);
return $this->calendarFactory->createCalendar($calendar);
}
// Federated calendar
@ -180,7 +174,7 @@ class CalendarHome extends \Sabre\CalDAV\CalendarHome {
// Fallback to cover shared calendars
foreach ($this->caldavBackend->getCalendarsForUser($this->principalInfo['uri']) as $calendar) {
if ($calendar['uri'] === $name) {
return new Calendar($this->caldavBackend, $calendar, $this->l10n, $this->config, $this->logger);
return $this->calendarFactory->createCalendar($calendar);
}
}

View file

@ -7,9 +7,6 @@
namespace OCA\DAV\CalDAV;
use OCP\Calendar\IManager;
use OCP\IConfig;
use OCP\IL10N;
use Psr\Log\LoggerInterface;
class CalendarManager {
@ -17,14 +14,10 @@ class CalendarManager {
* CalendarManager constructor.
*
* @param CalDavBackend $backend
* @param IL10N $l10n
* @param IConfig $config
*/
public function __construct(
private CalDavBackend $backend,
private IL10N $l10n,
private IConfig $config,
private LoggerInterface $logger,
private readonly CalendarFactory $calendarFactory,
) {
}
@ -43,7 +36,7 @@ class CalendarManager {
*/
private function register(IManager $cm, array $calendars) {
foreach ($calendars as $calendarInfo) {
$calendar = new Calendar($this->backend, $calendarInfo, $this->l10n, $this->config, $this->logger);
$calendar = $this->calendarFactory->createCalendar($calendarInfo);
$cm->registerCalendar(new CalendarImpl(
$calendar,
$calendarInfo,

View file

@ -12,18 +12,13 @@ use OCA\DAV\CalDAV\Federation\FederatedCalendarImpl;
use OCA\DAV\Db\Property;
use OCA\DAV\Db\PropertyMapper;
use OCP\Calendar\ICalendarProvider;
use OCP\IConfig;
use OCP\IL10N;
use Psr\Log\LoggerInterface;
class CalendarProvider implements ICalendarProvider {
public function __construct(
private CalDavBackend $calDavBackend,
private IL10N $l10n,
private IConfig $config,
private LoggerInterface $logger,
private PropertyMapper $propertyMapper,
private readonly CalendarFactory $calendarFactory,
) {
}
@ -52,7 +47,7 @@ class CalendarProvider implements ICalendarProvider {
$calendarInfo = array_merge($calendarInfo, $additionalProperties[$path] ?? []);
$calendar = new Calendar($this->calDavBackend, $calendarInfo, $this->l10n, $this->config, $this->logger);
$calendar = $this->calendarFactory->createCalendar($calendarInfo);
$iCalendars[] = new CalendarImpl(
$calendar,
$calendarInfo,

View file

@ -10,9 +10,6 @@ namespace OCA\DAV\CalDAV;
use OCA\DAV\CalDAV\Federation\FederatedCalendarFactory;
use OCA\DAV\CalDAV\Federation\RemoteUserCalendarHome;
use OCA\DAV\DAV\RemoteUserPrincipalBackend;
use OCP\IConfig;
use OCP\IL10N;
use Psr\Log\LoggerInterface;
use Sabre\CalDAV\Backend;
use Sabre\DAVACL\PrincipalBackend;
@ -23,10 +20,8 @@ class CalendarRoot extends \Sabre\CalDAV\CalendarRoot {
PrincipalBackend\BackendInterface $principalBackend,
Backend\BackendInterface $caldavBackend,
$principalPrefix,
private LoggerInterface $logger,
private IL10N $l10n,
private IConfig $config,
private FederatedCalendarFactory $federatedCalendarFactory,
private readonly CalendarFactory $calendarFactory,
) {
parent::__construct($principalBackend, $caldavBackend, $principalPrefix);
}
@ -37,17 +32,15 @@ class CalendarRoot extends \Sabre\CalDAV\CalendarRoot {
return new RemoteUserCalendarHome(
$this->caldavBackend,
$principal,
$this->l10n,
$this->config,
$this->logger,
$this->calendarFactory,
);
}
return new CalendarHome(
$this->caldavBackend,
$principal,
$this->logger,
$this->federatedCalendarFactory,
$this->calendarFactory,
array_key_exists($principal['uri'], $this->returnCachedSubscriptions)
);
}

View file

@ -9,10 +9,7 @@ declare(strict_types=1);
namespace OCA\DAV\CalDAV\Federation;
use OCA\DAV\CalDAV\Calendar;
use OCP\IConfig;
use OCP\IL10N;
use Psr\Log\LoggerInterface;
use OCA\DAV\CalDAV\CalendarFactory;
use Sabre\CalDAV\Backend;
use Sabre\CalDAV\CalendarHome;
use Sabre\DAV\Exception\NotFound;
@ -21,9 +18,7 @@ class RemoteUserCalendarHome extends CalendarHome {
public function __construct(
Backend\BackendInterface $caldavBackend,
$principalInfo,
private readonly IL10N $l10n,
private readonly IConfig $config,
private readonly LoggerInterface $logger,
private readonly CalendarFactory $calendarFactory,
) {
parent::__construct($caldavBackend, $principalInfo);
}
@ -33,13 +28,7 @@ class RemoteUserCalendarHome extends CalendarHome {
// calendar home
foreach ($this->caldavBackend->getCalendarsForUser($this->principalInfo['uri']) as $calendar) {
if ($calendar['uri'] === $name) {
return new Calendar(
$this->caldavBackend,
$calendar,
$this->l10n,
$this->config,
$this->logger,
);
return $this->calendarFactory->createCalendar($calendar);
}
}
@ -47,21 +36,9 @@ class RemoteUserCalendarHome extends CalendarHome {
}
public function getChildren(): array {
$objects = [];
// Remote users can only have incoming shared calendars so we can skip the rest of a regular
// calendar home
$calendars = $this->caldavBackend->getCalendarsForUser($this->principalInfo['uri']);
foreach ($calendars as $calendar) {
$objects[] = new Calendar(
$this->caldavBackend,
$calendar,
$this->l10n,
$this->config,
$this->logger,
);
}
return $objects;
return array_map($this->calendarFactory->createCalendar(...), $calendars);
}
}

View file

@ -7,9 +7,6 @@
*/
namespace OCA\DAV\CalDAV;
use OCP\IConfig;
use OCP\IL10N;
use Psr\Log\LoggerInterface;
use Sabre\DAV\Collection;
class PublicCalendarRoot extends Collection {
@ -18,14 +15,10 @@ class PublicCalendarRoot extends Collection {
* PublicCalendarRoot constructor.
*
* @param CalDavBackend $caldavBackend
* @param IL10N $l10n
* @param IConfig $config
*/
public function __construct(
protected CalDavBackend $caldavBackend,
protected IL10N $l10n,
protected IConfig $config,
private LoggerInterface $logger,
private readonly CalendarFactory $calendarFactory,
) {
}
@ -41,7 +34,7 @@ class PublicCalendarRoot extends Collection {
*/
public function getChild($name) {
$calendar = $this->caldavBackend->getPublicCalendar($name);
return new PublicCalendar($this->caldavBackend, $calendar, $this->l10n, $this->config, $this->logger);
return $this->calendarFactory->createPublicCalendar($calendar);
}
/**

View file

@ -10,11 +10,8 @@ namespace OCA\DAV\Command;
use OCA\DAV\CalDAV\BirthdayService;
use OCA\DAV\CalDAV\CalDavBackend;
use OCA\DAV\CalDAV\Calendar;
use OCP\IConfig;
use OCP\IL10N;
use OCA\DAV\CalDAV\CalendarFactory;
use OCP\IUserManager;
use Psr\Log\LoggerInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
@ -24,10 +21,8 @@ use Symfony\Component\Console\Output\OutputInterface;
class DeleteCalendar extends Command {
public function __construct(
private CalDavBackend $calDav,
private IConfig $config,
private IL10N $l10n,
private IUserManager $userManager,
private LoggerInterface $logger,
private readonly CalendarFactory $calendarFactory,
) {
parent::__construct();
}
@ -83,13 +78,7 @@ class DeleteCalendar extends Command {
'User <' . $user . '> has no calendar named <' . $name . '>. You can run occ dav:list-calendars to list calendars URIs for this user.');
}
$calendar = new Calendar(
$this->calDav,
$calendarInfo,
$this->l10n,
$this->config,
$this->logger
);
$calendar = $this->calendarFactory->createCalendar($calendarInfo);
$force = $input->getOption('force');
if ($force) {

View file

@ -7,13 +7,10 @@
namespace OCA\DAV\Command;
use OCA\DAV\CalDAV\CalDavBackend;
use OCA\DAV\CalDAV\Calendar;
use OCP\IConfig;
use OCA\DAV\CalDAV\CalendarFactory;
use OCP\IGroupManager;
use OCP\IL10N;
use OCP\IUserManager;
use OCP\Share\IManager as IShareManager;
use Psr\Log\LoggerInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
@ -30,10 +27,8 @@ class MoveCalendar extends Command {
private IUserManager $userManager,
private IGroupManager $groupManager,
private IShareManager $shareManager,
private IConfig $config,
private IL10N $l10n,
private CalDavBackend $calDav,
private LoggerInterface $logger,
private readonly CalendarFactory $calendarFactory,
) {
parent::__construct();
}
@ -154,7 +149,7 @@ class MoveCalendar extends Command {
*/
if ($this->shareManager->shareWithGroupMembersOnly() === true && $prefix === 'groups' && !$this->groupManager->isInGroup($userDestination, $userOrGroup)) {
if ($force) {
$this->calDav->updateShares(new Calendar($this->calDav, $calendar, $this->l10n, $this->config, $this->logger), [], ['principal:principals/groups/' . $userOrGroup]);
$this->calDav->updateShares($this->calendarFactory->createCalendar($calendar), [], ['principal:principals/groups/' . $userOrGroup]);
} else {
throw new \InvalidArgumentException("User <$userDestination> is not part of the group <$userOrGroup> with whom the calendar <" . $calendar['uri'] . '> was shared. You may use -f to move the calendar while deleting this share.');
}
@ -165,7 +160,7 @@ class MoveCalendar extends Command {
*/
if ($userOrGroup === $userDestination) {
if ($force) {
$this->calDav->updateShares(new Calendar($this->calDav, $calendar, $this->l10n, $this->config, $this->logger), [], ['principal:principals/users/' . $userOrGroup]);
$this->calDav->updateShares($this->calendarFactory->createCalendar($calendar), [], ['principal:principals/users/' . $userOrGroup]);
} else {
throw new \InvalidArgumentException('The calendar <' . $calendar['uri'] . "> is already shared to user <$userDestination>.You may use -f to move the calendar while deleting this share.");
}

View file

@ -10,6 +10,7 @@ namespace OCA\DAV;
use OC\KnownUser\KnownUserService;
use OCA\DAV\AppInfo\PluginManager;
use OCA\DAV\CalDAV\CalDavBackend;
use OCA\DAV\CalDAV\CalendarFactory;
use OCA\DAV\CalDAV\CalendarRoot;
use OCA\DAV\CalDAV\Federation\FederatedCalendarFactory;
use OCA\DAV\CalDAV\Federation\FederatedCalendarMapper;
@ -63,6 +64,7 @@ class RootCollection extends SimpleCollection {
$proxyMapper = Server::get(ProxyMapper::class);
$rootFolder = Server::get(IRootFolder::class);
$federatedCalendarFactory = Server::get(FederatedCalendarFactory::class);
$calendarFactory = Server::get(CalendarFactory::class);
$userPrincipalBackend = new Principal(
$userManager,
@ -110,18 +112,18 @@ class RootCollection extends SimpleCollection {
Server::get(FederatedCalendarMapper::class),
false,
);
$userCalendarRoot = new CalendarRoot($userPrincipalBackend, $caldavBackend, 'principals/users', $logger, $l10n, $config, $federatedCalendarFactory);
$userCalendarRoot = new CalendarRoot($userPrincipalBackend, $caldavBackend, 'principals/users', $federatedCalendarFactory, $calendarFactory);
$userCalendarRoot->disableListing = $disableListing;
$remoteUserCalendarRoot = new CalendarRoot($remoteUserPrincipalBackend, $caldavBackend, RemoteUserPrincipalBackend::PRINCIPAL_PREFIX, $logger, $l10n, $config, $federatedCalendarFactory);
$remoteUserCalendarRoot = new CalendarRoot($remoteUserPrincipalBackend, $caldavBackend, RemoteUserPrincipalBackend::PRINCIPAL_PREFIX, $federatedCalendarFactory, $calendarFactory);
$remoteUserCalendarRoot->disableListing = $disableListing;
$resourceCalendarRoot = new CalendarRoot($calendarResourcePrincipalBackend, $caldavBackend, 'principals/calendar-resources', $logger, $l10n, $config, $federatedCalendarFactory);
$resourceCalendarRoot = new CalendarRoot($calendarResourcePrincipalBackend, $caldavBackend, 'principals/calendar-resources', $federatedCalendarFactory, $calendarFactory);
$resourceCalendarRoot->disableListing = $disableListing;
$roomCalendarRoot = new CalendarRoot($calendarRoomPrincipalBackend, $caldavBackend, 'principals/calendar-rooms', $logger, $l10n, $config, $federatedCalendarFactory);
$roomCalendarRoot = new CalendarRoot($calendarRoomPrincipalBackend, $caldavBackend, 'principals/calendar-rooms', $federatedCalendarFactory, $calendarFactory);
$roomCalendarRoot->disableListing = $disableListing;
$publicCalendarRoot = new PublicCalendarRoot($caldavBackend, $l10n, $config, $logger);
$publicCalendarRoot = new PublicCalendarRoot($caldavBackend, $calendarFactory);
$systemTagCollection = Server::get(SystemTagsByIdCollection::class);
$systemTagRelationsCollection = new SystemTagsRelationsCollection(

View file

@ -11,6 +11,7 @@ namespace OCA\DAV\Tests\unit\CalDAV;
use OCA\DAV\AppInfo\PluginManager;
use OCA\DAV\CalDAV\CachedSubscription;
use OCA\DAV\CalDAV\CalDavBackend;
use OCA\DAV\CalDAV\CalendarFactory;
use OCA\DAV\CalDAV\CalendarHome;
use OCA\DAV\CalDAV\Federation\FederatedCalendar;
use OCA\DAV\CalDAV\Federation\FederatedCalendarFactory;
@ -19,7 +20,6 @@ use OCA\DAV\CalDAV\Integration\ICalendarProvider;
use OCA\DAV\CalDAV\Outbox;
use OCA\DAV\CalDAV\Trashbin\TrashbinHome;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\LoggerInterface;
use Sabre\CalDAV\Schedule\Inbox;
use Sabre\CalDAV\Subscriptions\Subscription;
use Sabre\CalDAV\Xml\Property\SupportedCalendarComponentSet;
@ -30,8 +30,8 @@ class CalendarHomeTest extends TestCase {
private CalDavBackend&MockObject $backend;
private array $principalInfo = [];
private PluginManager&MockObject $pluginManager;
private LoggerInterface&MockObject $logger;
private FederatedCalendarFactory&MockObject $federatedCalendarFactory;
private CalendarFactory&MockObject $calendarFactory;
private CalendarHome $calendarHome;
protected function setUp(): void {
@ -42,14 +42,14 @@ class CalendarHomeTest extends TestCase {
'uri' => 'user-principal-123',
];
$this->pluginManager = $this->createMock(PluginManager::class);
$this->logger = $this->createMock(LoggerInterface::class);
$this->federatedCalendarFactory = $this->createMock(FederatedCalendarFactory::class);
$this->calendarFactory = $this->createMock(CalendarFactory::class);
$this->calendarHome = new CalendarHome(
$this->backend,
$this->principalInfo,
$this->logger,
$this->federatedCalendarFactory,
$this->calendarFactory,
false
);
@ -293,8 +293,8 @@ class CalendarHomeTest extends TestCase {
$calendarHome = new CalendarHome(
$this->backend,
$this->principalInfo,
$this->logger,
$this->federatedCalendarFactory,
$this->calendarFactory,
false
);
@ -360,8 +360,8 @@ class CalendarHomeTest extends TestCase {
$calendarHome = new CalendarHome(
$this->backend,
$this->principalInfo,
$this->logger,
$this->federatedCalendarFactory,
$this->calendarFactory,
true
);

View file

@ -9,32 +9,24 @@ namespace OCA\DAV\Tests\unit\CalDAV;
use OC\Calendar\Manager;
use OCA\DAV\CalDAV\CalDavBackend;
use OCA\DAV\CalDAV\CalendarFactory;
use OCA\DAV\CalDAV\CalendarImpl;
use OCA\DAV\CalDAV\CalendarManager;
use OCP\Calendar\IManager;
use OCP\IConfig;
use OCP\IL10N;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\LoggerInterface;
class CalendarManagerTest extends \Test\TestCase {
private CalDavBackend&MockObject $backend;
private IL10N&MockObject $l10n;
private IConfig&MockObject $config;
private LoggerInterface&MockObject $logger;
private CalendarFactory&MockObject $calendarFactory;
private CalendarManager $manager;
protected function setUp(): void {
parent::setUp();
$this->backend = $this->createMock(CalDavBackend::class);
$this->l10n = $this->createMock(IL10N::class);
$this->config = $this->createMock(IConfig::class);
$this->logger = $this->createMock(LoggerInterface::class);
$this->calendarFactory = $this->createMock(CalendarFactory::class);
$this->manager = new CalendarManager(
$this->backend,
$this->l10n,
$this->config,
$this->logger
$this->calendarFactory,
);
}

View file

@ -10,11 +10,9 @@ declare(strict_types=1);
namespace OCA\DAV\Tests\unit\CalDAV\Federation;
use OCA\DAV\CalDAV\Calendar;
use OCA\DAV\CalDAV\CalendarFactory;
use OCA\DAV\CalDAV\Federation\RemoteUserCalendarHome;
use OCP\IConfig;
use OCP\IL10N;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\LoggerInterface;
use Sabre\CalDAV\Backend\BackendInterface;
use Sabre\DAV\Exception\NotFound;
use Test\TestCase;
@ -23,26 +21,20 @@ class RemoteUserCalendarHomeTest extends TestCase {
private RemoteUserCalendarHome $remoteUserCalendarHome;
private BackendInterface&MockObject $calDavBackend;
private IL10N&MockObject $l10n;
private IConfig&MockObject $config;
private LoggerInterface&MockObject $logger;
private CalendarFactory&MockObject $calendarFactory;
protected function setUp(): void {
parent::setUp();
$this->calDavBackend = $this->createMock(BackendInterface::class);
$this->l10n = $this->createMock(IL10N::class);
$this->config = $this->createMock(IConfig::class);
$this->logger = $this->createMock(LoggerInterface::class);
$this->calendarFactory = $this->createMock(CalendarFactory::class);
$this->remoteUserCalendarHome = new RemoteUserCalendarHome(
$this->calDavBackend,
[
'uri' => 'principals/remote-users/abcdef123',
],
$this->l10n,
$this->config,
$this->logger,
$this->calendarFactory,
);
}

View file

@ -9,6 +9,7 @@ namespace OCA\DAV\Tests\unit\CalDAV;
use OCA\DAV\CalDAV\CalDavBackend;
use OCA\DAV\CalDAV\Calendar;
use OCA\DAV\CalDAV\CalendarFactory;
use OCA\DAV\CalDAV\Federation\FederatedCalendarMapper;
use OCA\DAV\CalDAV\PublicCalendar;
use OCA\DAV\CalDAV\PublicCalendarRoot;
@ -43,6 +44,7 @@ class PublicCalendarRootTest extends TestCase {
protected IConfig&MockObject $config;
private ISecureRandom $random;
private LoggerInterface&MockObject $logger;
private CalendarFactory&MockObject $calendarFactory;
protected FederatedCalendarMapper&MockObject $federatedCalendarMapper;
@ -59,6 +61,7 @@ class PublicCalendarRootTest extends TestCase {
$dispatcher = $this->createMock(IEventDispatcher::class);
$config = $this->createMock(IConfig::class);
$sharingBackend = $this->createMock(\OCA\DAV\CalDAV\Sharing\Backend::class);
$this->calendarFactory = $this->createMock(CalendarFactory::class);
$this->principal->expects($this->any())->method('getGroupMembership')
->withAnyParameters()
@ -83,8 +86,10 @@ class PublicCalendarRootTest extends TestCase {
$this->l10n = $this->createMock(IL10N::class);
$this->config = $this->createMock(IConfig::class);
$this->publicCalendarRoot = new PublicCalendarRoot($this->backend,
$this->l10n, $this->config, $this->logger);
$this->publicCalendarRoot = new PublicCalendarRoot(
$this->backend,
$this->calendarFactory,
);
}
protected function tearDown(): void {
@ -113,11 +118,16 @@ class PublicCalendarRootTest extends TestCase {
}
public function testGetChild(): void {
$calendar = $this->createPublicCalendar();
[$calendar, $calendarInfo] = $this->createPublicCalendar();
$this->calendarFactory->expects(self::once())
->method('createPublicCalendar')
->with($calendarInfo)
->willReturn($calendar);
$publicCalendars = $this->backend->getPublicCalendars();
$this->assertEquals(1, count($publicCalendars));
$this->assertEquals(true, $publicCalendars[0]['{http://owncloud.org/ns}public']);
$this->assertCount(1, $publicCalendars);
$this->assertTrue($publicCalendars[0]['{http://owncloud.org/ns}public']);
$publicCalendarURI = $publicCalendars[0]['uri'];
@ -127,11 +137,15 @@ class PublicCalendarRootTest extends TestCase {
public function testGetChildren(): void {
$this->createPublicCalendar();
$this->calendarFactory->expects(self::never())
->method('createPublicCalendar');
$calendarResults = $this->publicCalendarRoot->getChildren();
$this->assertSame([], $calendarResults);
}
protected function createPublicCalendar(): Calendar {
protected function createPublicCalendar(): array {
$this->backend->createCalendar(self::UNIT_TEST_USER, 'Example', []);
$calendarInfo = $this->backend->getCalendarsForUser(self::UNIT_TEST_USER)[0];
@ -141,6 +155,6 @@ class PublicCalendarRootTest extends TestCase {
$calendarInfo = $this->backend->getPublicCalendar($publicUri);
$calendar = new PublicCalendar($this->backend, $calendarInfo, $this->l10n, $this->config, $this->logger);
return $calendar;
return [$calendar, $calendarInfo];
}
}

View file

@ -10,12 +10,11 @@ namespace OCA\DAV\Tests\unit\Command;
use OCA\DAV\CalDAV\BirthdayService;
use OCA\DAV\CalDAV\CalDavBackend;
use OCA\DAV\CalDAV\Calendar;
use OCA\DAV\CalDAV\CalendarFactory;
use OCA\DAV\Command\DeleteCalendar;
use OCP\IConfig;
use OCP\IL10N;
use OCP\IUserManager;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\LoggerInterface;
use Symfony\Component\Console\Tester\CommandTester;
use Test\TestCase;
@ -29,27 +28,21 @@ class DeleteCalendarTest extends TestCase {
public const NAME = 'calendar';
private CalDavBackend&MockObject $calDav;
private IConfig&MockObject $config;
private IL10N&MockObject $l10n;
private IUserManager&MockObject $userManager;
private LoggerInterface&MockObject $logger;
private CalendarFactory&MockObject $calendarFactory;
private DeleteCalendar $command;
protected function setUp(): void {
parent::setUp();
$this->calDav = $this->createMock(CalDavBackend::class);
$this->config = $this->createMock(IConfig::class);
$this->l10n = $this->createMock(IL10N::class);
$this->userManager = $this->createMock(IUserManager::class);
$this->logger = $this->createMock(LoggerInterface::class);
$this->calendarFactory = $this->createMock(CalendarFactory::class);
$this->command = new DeleteCalendar(
$this->calDav,
$this->config,
$this->l10n,
$this->userManager,
$this->logger
$this->calendarFactory,
);
}
@ -129,9 +122,15 @@ class DeleteCalendarTest extends TestCase {
self::NAME
)
->willReturn($calendar);
$this->calDav->expects($this->once())
->method('deleteCalendar')
->with($id, false);
$calendarObj = $this->createMock(Calendar::class);
$this->calendarFactory->expects(self::once())
->method('createCalendar')
->with($calendar)
->willReturn($calendarObj);
$calendarObj->expects(self::never())
->method('disableTrashbin');
$calendarObj->expects(self::once())
->method('delete');
$commandTester = new CommandTester($this->command);
$commandTester->execute([
@ -159,9 +158,15 @@ class DeleteCalendarTest extends TestCase {
self::NAME
)
->willReturn($calendar);
$this->calDav->expects($this->once())
->method('deleteCalendar')
->with($id, true);
$calendarObj = $this->createMock(Calendar::class);
$this->calendarFactory->expects(self::once())
->method('createCalendar')
->with($calendar)
->willReturn($calendarObj);
$calendarObj->expects(self::once())
->method('disableTrashbin');
$calendarObj->expects(self::once())
->method('delete');
$commandTester = new CommandTester($this->command);
$commandTester->execute([
@ -191,9 +196,15 @@ class DeleteCalendarTest extends TestCase {
BirthdayService::BIRTHDAY_CALENDAR_URI
)
->willReturn($calendar);
$this->calDav->expects($this->once())
->method('deleteCalendar')
->with($id);
$calendarObj = $this->createMock(Calendar::class);
$this->calendarFactory->expects(self::once())
->method('createCalendar')
->with($calendar)
->willReturn($calendarObj);
$calendarObj->expects(self::never())
->method('disableTrashbin');
$calendarObj->expects(self::once())
->method('delete');
$commandTester = new CommandTester($this->command);
$commandTester->execute([

View file

@ -9,14 +9,12 @@ namespace OCA\DAV\Tests\unit\Command;
use InvalidArgumentException;
use OCA\DAV\CalDAV\CalDavBackend;
use OCA\DAV\CalDAV\CalendarFactory;
use OCA\DAV\Command\MoveCalendar;
use OCP\IConfig;
use OCP\IGroupManager;
use OCP\IL10N;
use OCP\IUserManager;
use OCP\Share\IManager;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\LoggerInterface;
use Symfony\Component\Console\Tester\CommandTester;
use Test\TestCase;
@ -29,10 +27,8 @@ class MoveCalendarTest extends TestCase {
private IUserManager&MockObject $userManager;
private IGroupManager&MockObject $groupManager;
private \OCP\Share\IManager&MockObject $shareManager;
private IConfig&MockObject $config;
private IL10N&MockObject $l10n;
private CalDavBackend&MockObject $calDav;
private LoggerInterface&MockObject $logger;
private CalendarFactory&MockObject $calendarFactory;
private MoveCalendar $command;
protected function setUp(): void {
@ -41,19 +37,15 @@ class MoveCalendarTest extends TestCase {
$this->userManager = $this->createMock(IUserManager::class);
$this->groupManager = $this->createMock(IGroupManager::class);
$this->shareManager = $this->createMock(IManager::class);
$this->config = $this->createMock(IConfig::class);
$this->l10n = $this->createMock(IL10N::class);
$this->calDav = $this->createMock(CalDavBackend::class);
$this->logger = $this->createMock(LoggerInterface::class);
$this->calendarFactory = $this->createMock(CalendarFactory::class);
$this->command = new MoveCalendar(
$this->userManager,
$this->groupManager,
$this->shareManager,
$this->config,
$this->l10n,
$this->calDav,
$this->logger
$this->calendarFactory,
);
}