123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355 |
- <?php
- /**
- * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
- * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
- * SPDX-License-Identifier: AGPL-3.0-only
- */
- 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\CalendarHome;
- use OCA\DAV\CalDAV\Integration\ExternalCalendar;
- 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\DAV\MkCol;
- use Test\TestCase;
- class CalendarHomeTest extends TestCase {
- /** @var CalDavBackend | MockObject */
- private $backend;
- /** @var array */
- private $principalInfo = [];
- /** @var PluginManager */
- private $pluginManager;
- /** @var CalendarHome */
- private $calendarHome;
- /** @var MockObject|LoggerInterface */
- private $logger;
- protected function setUp(): void {
- parent::setUp();
- $this->backend = $this->createMock(CalDavBackend::class);
- $this->principalInfo = [
- 'uri' => 'user-principal-123',
- ];
- $this->pluginManager = $this->createMock(PluginManager::class);
- $this->logger = $this->createMock(LoggerInterface::class);
- $this->calendarHome = new CalendarHome(
- $this->backend,
- $this->principalInfo,
- $this->logger,
- false
- );
- // Replace PluginManager with our mock
- $reflection = new \ReflectionClass($this->calendarHome);
- $reflectionProperty = $reflection->getProperty('pluginManager');
- $reflectionProperty->setValue($this->calendarHome, $this->pluginManager);
- }
- public function testCreateCalendarValidName(): void {
- /** @var MkCol | MockObject $mkCol */
- $mkCol = $this->createMock(MkCol::class);
- $mkCol->method('getResourceType')
- ->willReturn(['{DAV:}collection',
- '{urn:ietf:params:xml:ns:caldav}calendar']);
- $mkCol->method('getRemainingValues')
- ->willReturn(['... properties ...']);
- $this->backend->expects(self::once())
- ->method('createCalendar')
- ->with('user-principal-123', 'name123', ['... properties ...']);
- $this->calendarHome->createExtendedCollection('name123', $mkCol);
- }
- public function testCreateCalendarReservedName(): void {
- $this->expectException(\Sabre\DAV\Exception\MethodNotAllowed::class);
- $this->expectExceptionMessage('The resource you tried to create has a reserved name');
- /** @var MkCol | MockObject $mkCol */
- $mkCol = $this->createMock(MkCol::class);
- $this->calendarHome->createExtendedCollection('contact_birthdays', $mkCol);
- }
- public function testCreateCalendarReservedNameAppGenerated(): void {
- $this->expectException(\Sabre\DAV\Exception\MethodNotAllowed::class);
- $this->expectExceptionMessage('The resource you tried to create has a reserved name');
- /** @var MkCol | MockObject $mkCol */
- $mkCol = $this->createMock(MkCol::class);
- $this->calendarHome->createExtendedCollection('app-generated--example--foo-1', $mkCol);
- }
- public function testGetChildren():void {
- $this->backend
- ->expects(self::once())
- ->method('getCalendarsForUser')
- ->with('user-principal-123')
- ->willReturn([]);
- $this->backend
- ->expects(self::once())
- ->method('getSubscriptionsForUser')
- ->with('user-principal-123')
- ->willReturn([]);
- $calendarPlugin1 = $this->createMock(ICalendarProvider::class);
- $calendarPlugin1
- ->expects(self::once())
- ->method('fetchAllForCalendarHome')
- ->with('user-principal-123')
- ->willReturn(['plugin1calendar1', 'plugin1calendar2']);
- $calendarPlugin2 = $this->createMock(ICalendarProvider::class);
- $calendarPlugin2
- ->expects(self::once())
- ->method('fetchAllForCalendarHome')
- ->with('user-principal-123')
- ->willReturn(['plugin2calendar1', 'plugin2calendar2']);
- $this->pluginManager
- ->expects(self::once())
- ->method('getCalendarPlugins')
- ->with()
- ->willReturn([$calendarPlugin1, $calendarPlugin2]);
- $actual = $this->calendarHome->getChildren();
- $this->assertCount(7, $actual);
- $this->assertInstanceOf(Inbox::class, $actual[0]);
- $this->assertInstanceOf(Outbox::class, $actual[1]);
- $this->assertInstanceOf(TrashbinHome::class, $actual[2]);
- $this->assertEquals('plugin1calendar1', $actual[3]);
- $this->assertEquals('plugin1calendar2', $actual[4]);
- $this->assertEquals('plugin2calendar1', $actual[5]);
- $this->assertEquals('plugin2calendar2', $actual[6]);
- }
- public function testGetChildNonAppGenerated():void {
- $this->backend
- ->expects(self::once())
- ->method('getCalendarByUri')
- ->with('user-principal-123')
- ->willReturn([]);
- $this->backend
- ->expects(self::once())
- ->method('getCalendarsForUser')
- ->with('user-principal-123')
- ->willReturn([]);
- $this->backend
- ->expects(self::once())
- ->method('getSubscriptionsForUser')
- ->with('user-principal-123')
- ->willReturn([]);
- $this->pluginManager
- ->expects(self::never())
- ->method('getCalendarPlugins');
- $this->expectException(\Sabre\DAV\Exception\NotFound::class);
- $this->expectExceptionMessage('Node with name \'personal\' could not be found');
- $this->calendarHome->getChild('personal');
- }
- public function testGetChildAppGenerated():void {
- $this->backend
- ->expects(self::once())
- ->method('getCalendarByUri')
- ->with('user-principal-123')
- ->willReturn([]);
- $this->backend
- ->expects(self::once())
- ->method('getCalendarsForUser')
- ->with('user-principal-123')
- ->willReturn([]);
- $this->backend
- ->expects(self::once())
- ->method('getSubscriptionsForUser')
- ->with('user-principal-123')
- ->willReturn([]);
- $calendarPlugin1 = $this->createMock(ICalendarProvider::class);
- $calendarPlugin1
- ->expects(self::once())
- ->method('getAppId')
- ->with()
- ->willReturn('calendar_plugin_1');
- $calendarPlugin1
- ->expects(self::never())
- ->method('hasCalendarInCalendarHome');
- $calendarPlugin1
- ->expects(self::never())
- ->method('getCalendarInCalendarHome');
- $externalCalendarMock = $this->createMock(ExternalCalendar::class);
- $calendarPlugin2 = $this->createMock(ICalendarProvider::class);
- $calendarPlugin2
- ->expects(self::once())
- ->method('getAppId')
- ->with()
- ->willReturn('calendar_plugin_2');
- $calendarPlugin2
- ->expects(self::once())
- ->method('hasCalendarInCalendarHome')
- ->with('user-principal-123', 'calendar-uri-from-backend')
- ->willReturn(true);
- $calendarPlugin2
- ->expects(self::once())
- ->method('getCalendarInCalendarHome')
- ->with('user-principal-123', 'calendar-uri-from-backend')
- ->willReturn($externalCalendarMock);
- $this->pluginManager
- ->expects(self::once())
- ->method('getCalendarPlugins')
- ->with()
- ->willReturn([$calendarPlugin1, $calendarPlugin2]);
- $actual = $this->calendarHome->getChild('app-generated--calendar_plugin_2--calendar-uri-from-backend');
- $this->assertEquals($externalCalendarMock, $actual);
- }
- public function testGetChildrenSubscriptions(): void {
- $this->backend
- ->expects(self::once())
- ->method('getCalendarsForUser')
- ->with('user-principal-123')
- ->willReturn([]);
- $this->backend
- ->expects(self::once())
- ->method('getSubscriptionsForUser')
- ->with('user-principal-123')
- ->willReturn([
- [
- 'id' => 'subscription-1',
- 'uri' => 'subscription-1',
- 'principaluri' => 'user-principal-123',
- 'source' => 'https://localhost/subscription-1',
- // A subscription array has actually more properties.
- ],
- [
- 'id' => 'subscription-2',
- 'uri' => 'subscription-2',
- 'principaluri' => 'user-principal-123',
- 'source' => 'https://localhost/subscription-2',
- // A subscription array has actually more properties.
- ]
- ]);
- /*
- * @FIXME: PluginManager should be injected via constructor.
- */
- $pluginManager = $this->createMock(PluginManager::class);
- $pluginManager
- ->expects(self::once())
- ->method('getCalendarPlugins')
- ->with()
- ->willReturn([]);
- $calendarHome = new CalendarHome(
- $this->backend,
- $this->principalInfo,
- $this->logger,
- false
- );
- $reflection = new \ReflectionClass($calendarHome);
- $reflectionProperty = $reflection->getProperty('pluginManager');
- $reflectionProperty->setValue($calendarHome, $pluginManager);
- $actual = $calendarHome->getChildren();
- $this->assertCount(5, $actual);
- $this->assertInstanceOf(Inbox::class, $actual[0]);
- $this->assertInstanceOf(Outbox::class, $actual[1]);
- $this->assertInstanceOf(TrashbinHome::class, $actual[2]);
- $this->assertInstanceOf(Subscription::class, $actual[3]);
- $this->assertInstanceOf(Subscription::class, $actual[4]);
- }
- public function testGetChildrenCachedSubscriptions(): void {
- $this->backend
- ->expects(self::once())
- ->method('getCalendarsForUser')
- ->with('user-principal-123')
- ->willReturn([]);
- $this->backend
- ->expects(self::once())
- ->method('getSubscriptionsForUser')
- ->with('user-principal-123')
- ->willReturn([
- [
- 'id' => 'subscription-1',
- 'uri' => 'subscription-1',
- 'principaluris' => 'user-principal-123',
- 'source' => 'https://localhost/subscription-1',
- // A subscription array has actually more properties.
- ],
- [
- 'id' => 'subscription-2',
- 'uri' => 'subscription-2',
- 'principaluri' => 'user-principal-123',
- 'source' => 'https://localhost/subscription-2',
- // A subscription array has actually more properties.
- ]
- ]);
- /*
- * @FIXME: PluginManager should be injected via constructor.
- */
- $pluginManager = $this->createMock(PluginManager::class);
- $pluginManager
- ->expects(self::once())
- ->method('getCalendarPlugins')
- ->with()
- ->willReturn([]);
- $calendarHome = new CalendarHome(
- $this->backend,
- $this->principalInfo,
- $this->logger,
- true
- );
- $reflection = new \ReflectionClass($calendarHome);
- $reflectionProperty = $reflection->getProperty('pluginManager');
- $reflectionProperty->setValue($calendarHome, $pluginManager);
- $actual = $calendarHome->getChildren();
- $this->assertCount(5, $actual);
- $this->assertInstanceOf(Inbox::class, $actual[0]);
- $this->assertInstanceOf(Outbox::class, $actual[1]);
- $this->assertInstanceOf(TrashbinHome::class, $actual[2]);
- $this->assertInstanceOf(CachedSubscription::class, $actual[3]);
- $this->assertInstanceOf(CachedSubscription::class, $actual[4]);
- }
- }
|