diff options
author | Georg Ehrke <developer@georgehrke.com> | 2020-01-27 13:21:57 +0100 |
---|---|---|
committer | Georg Ehrke <developer@georgehrke.com> | 2020-02-18 14:35:14 +0100 |
commit | b46e5cb270b1730a6e6d4a98e52ca672eafebd39 (patch) | |
tree | a67f62762d13273c5b0037078443dc081f61744d /apps/dav/tests/unit | |
parent | a1fc233fcb30d9181415ad24a4141f0285b6899a (diff) | |
download | nextcloud-server-b46e5cb270b1730a6e6d4a98e52ca672eafebd39.tar.gz nextcloud-server-b46e5cb270b1730a6e6d4a98e52ca672eafebd39.zip |
Allow apps to provide Calendars in user's calendarHome
Signed-off-by: Georg Ehrke <developer@georgehrke.com>
Diffstat (limited to 'apps/dav/tests/unit')
-rw-r--r-- | apps/dav/tests/unit/AppInfo/PluginManagerTest.php | 23 | ||||
-rw-r--r-- | apps/dav/tests/unit/CalDAV/CalendarHomeTest.php | 149 | ||||
-rw-r--r-- | apps/dav/tests/unit/CalDAV/Integration/ExternalCalendarTest.php | 119 |
3 files changed, 290 insertions, 1 deletions
diff --git a/apps/dav/tests/unit/AppInfo/PluginManagerTest.php b/apps/dav/tests/unit/AppInfo/PluginManagerTest.php index e1b852b6a80..0d181f914a1 100644 --- a/apps/dav/tests/unit/AppInfo/PluginManagerTest.php +++ b/apps/dav/tests/unit/AppInfo/PluginManagerTest.php @@ -28,6 +28,7 @@ namespace OCA\DAV\Tests\unit\AppInfo; use OC\App\AppManager; use OC\ServerContainer; use OCA\DAV\AppInfo\PluginManager; +use OCA\DAV\CalDAV\Integration\ICalendarProvider; use Test\TestCase; /** @@ -53,6 +54,12 @@ class PluginManagerTest extends TestCase { '\OCA\DAV\ADavApp\PluginTwo', ], ], + 'calendar-plugins' => [ + 'plugin' => [ + '\OCA\DAV\ADavApp\CalendarPluginOne', + '\OCA\DAV\ADavApp\CalendarPluginTwo', + ], + ], 'collections' => [ 'collection' => [ '\OCA\DAV\ADavApp\CollectionOne', @@ -67,6 +74,9 @@ class PluginManagerTest extends TestCase { 'plugins' => [ 'plugin' => '\OCA\DAV\ADavApp2\PluginOne', ], + 'calendar-plugins' => [ + 'plugin' => '\OCA\DAV\ADavApp2\CalendarPluginOne', + ], 'collections' => [ 'collection' => '\OCA\DAV\ADavApp2\CollectionOne', ], @@ -81,13 +91,20 @@ class PluginManagerTest extends TestCase { $pluginManager = new PluginManager($server, $appManager); + $calendarPlugin1 = $this->createMock(ICalendarProvider::class); + $calendarPlugin2 = $this->createMock(ICalendarProvider::class); + $calendarPlugin3 = $this->createMock(ICalendarProvider::class); + $server->method('query') ->will($this->returnValueMap([ ['\OCA\DAV\ADavApp\PluginOne', true, 'dummyplugin1'], ['\OCA\DAV\ADavApp\PluginTwo', true, 'dummyplugin2'], + ['\OCA\DAV\ADavApp\CalendarPluginOne', true, $calendarPlugin1], + ['\OCA\DAV\ADavApp\CalendarPluginTwo', true, $calendarPlugin2], ['\OCA\DAV\ADavApp\CollectionOne', true, 'dummycollection1'], ['\OCA\DAV\ADavApp\CollectionTwo', true, 'dummycollection2'], ['\OCA\DAV\ADavApp2\PluginOne', true, 'dummy2plugin1'], + ['\OCA\DAV\ADavApp2\CalendarPluginOne', true, $calendarPlugin3], ['\OCA\DAV\ADavApp2\CollectionOne', true, 'dummy2collection1'], ])); @@ -96,6 +113,11 @@ class PluginManagerTest extends TestCase { 'dummyplugin2', 'dummy2plugin1', ]; + $expectedCalendarPlugins = [ + $calendarPlugin1, + $calendarPlugin2, + $calendarPlugin3, + ]; $expectedCollections = [ 'dummycollection1', 'dummycollection2', @@ -103,6 +125,7 @@ class PluginManagerTest extends TestCase { ]; $this->assertEquals($expectedPlugins, $pluginManager->getAppPlugins()); + $this->assertEquals($expectedCalendarPlugins, $pluginManager->getCalendarPlugins()); $this->assertEquals($expectedCollections, $pluginManager->getAppCollections()); } } diff --git a/apps/dav/tests/unit/CalDAV/CalendarHomeTest.php b/apps/dav/tests/unit/CalDAV/CalendarHomeTest.php index c811cb8b5a1..364286edd4b 100644 --- a/apps/dav/tests/unit/CalDAV/CalendarHomeTest.php +++ b/apps/dav/tests/unit/CalDAV/CalendarHomeTest.php @@ -24,8 +24,14 @@ namespace OCA\DAV\Tests\unit\CalDAV; +use OCA\DAV\AppInfo\PluginManager; 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 Sabre\CalDAV\Schedule\Inbox; +use Sabre\DAV\Exception\NotFound; use Sabre\DAV\MkCol; use Test\TestCase; @@ -37,6 +43,9 @@ class CalendarHomeTest extends TestCase { /** @var array */ private $principalInfo = []; + /** @var PluginManager */ + private $pluginManager; + /** @var CalendarHome */ private $calendarHome; @@ -47,9 +56,16 @@ class CalendarHomeTest extends TestCase { $this->principalInfo = [ 'uri' => 'user-principal-123', ]; + $this->pluginManager = $this->createMock(PluginManager::class); $this->calendarHome = new CalendarHome($this->backend, $this->principalInfo); + + // Replace PluginManager with our mock + $reflection = new \ReflectionClass($this->calendarHome); + $reflectionProperty = $reflection->getProperty('pluginManager'); + $reflectionProperty->setAccessible(true); + $reflectionProperty->setValue($this->calendarHome, $this->pluginManager); } public function testCreateCalendarValidName() { @@ -69,7 +85,6 @@ class CalendarHomeTest extends TestCase { $this->calendarHome->createExtendedCollection('name123', $mkCol); } - public function testCreateCalendarReservedName() { $this->expectException(\Sabre\DAV\Exception\MethodNotAllowed::class); $this->expectExceptionMessage('The resource you tried to create has a reserved name'); @@ -79,4 +94,136 @@ class CalendarHomeTest extends TestCase { $this->calendarHome->createExtendedCollection('contact_birthdays', $mkCol); } + + public function testCreateCalendarReservedNameAppGenerated() { + $this->expectException(\Sabre\DAV\Exception\MethodNotAllowed::class); + $this->expectExceptionMessage('The resource you tried to create has a reserved name'); + + /** @var MkCol | \PHPUnit_Framework_MockObject_MockObject $mkCol */ + $mkCol = $this->createMock(MkCol::class); + + $this->calendarHome->createExtendedCollection('app-generated--example--foo-1', $mkCol); + } + + public function testGetChildren():void { + $this->backend + ->expects($this->at(0)) + ->method('getCalendarsForUser') + ->with('user-principal-123') + ->willReturn([]); + + $this->backend + ->expects($this->at(1)) + ->method('getSubscriptionsForUser') + ->with('user-principal-123') + ->willReturn([]); + + $calendarPlugin1 = $this->createMock(ICalendarProvider::class); + $calendarPlugin1 + ->expects($this->once()) + ->method('fetchAllForCalendarHome') + ->with('user-principal-123') + ->willReturn(['plugin1calendar1', 'plugin1calendar2']); + + $calendarPlugin2 = $this->createMock(ICalendarProvider::class); + $calendarPlugin2 + ->expects($this->once()) + ->method('fetchAllForCalendarHome') + ->with('user-principal-123') + ->willReturn(['plugin2calendar1', 'plugin2calendar2']); + + $this->pluginManager + ->expects($this->once()) + ->method('getCalendarPlugins') + ->with() + ->willReturn([$calendarPlugin1, $calendarPlugin2]); + + $actual = $this->calendarHome->getChildren(); + + $this->assertCount(6, $actual); + $this->assertInstanceOf(Inbox::class, $actual[0]); + $this->assertInstanceOf(Outbox::class, $actual[1]); + $this->assertEquals('plugin1calendar1', $actual[2]); + $this->assertEquals('plugin1calendar2', $actual[3]); + $this->assertEquals('plugin2calendar1', $actual[4]); + $this->assertEquals('plugin2calendar2', $actual[5]); + } + + public function testGetChildNonAppGenerated():void { + $this->backend + ->expects($this->at(0)) + ->method('getCalendarsForUser') + ->with('user-principal-123') + ->willReturn([]); + + $this->backend + ->expects($this->at(1)) + ->method('getSubscriptionsForUser') + ->with('user-principal-123') + ->willReturn([]); + + $this->pluginManager + ->expects($this->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($this->at(0)) + ->method('getCalendarsForUser') + ->with('user-principal-123') + ->willReturn([]); + + $this->backend + ->expects($this->at(1)) + ->method('getSubscriptionsForUser') + ->with('user-principal-123') + ->willReturn([]); + + $calendarPlugin1 = $this->createMock(ICalendarProvider::class); + $calendarPlugin1 + ->expects($this->once()) + ->method('getAppId') + ->with() + ->willReturn('calendar_plugin_1'); + $calendarPlugin1 + ->expects($this->never()) + ->method('hasCalendarInCalendarHome'); + $calendarPlugin1 + ->expects($this->never()) + ->method('getCalendarInCalendarHome'); + + $externalCalendarMock = $this->createMock(ExternalCalendar::class); + + $calendarPlugin2 = $this->createMock(ICalendarProvider::class); + $calendarPlugin2 + ->expects($this->once()) + ->method('getAppId') + ->with() + ->willReturn('calendar_plugin_2'); + $calendarPlugin2 + ->expects($this->once()) + ->method('hasCalendarInCalendarHome') + ->with('user-principal-123', 'calendar-uri-from-backend') + ->willReturn(true); + $calendarPlugin2 + ->expects($this->once()) + ->method('getCalendarInCalendarHome') + ->with('user-principal-123', 'calendar-uri-from-backend') + ->willReturn($externalCalendarMock); + + $this->pluginManager + ->expects($this->once()) + ->method('getCalendarPlugins') + ->with() + ->willReturn([$calendarPlugin1, $calendarPlugin2]); + + $actual = $this->calendarHome->getChild('app-generated--calendar_plugin_2--calendar-uri-from-backend'); + $this->assertEquals($externalCalendarMock, $actual); + } } diff --git a/apps/dav/tests/unit/CalDAV/Integration/ExternalCalendarTest.php b/apps/dav/tests/unit/CalDAV/Integration/ExternalCalendarTest.php new file mode 100644 index 00000000000..a6698087155 --- /dev/null +++ b/apps/dav/tests/unit/CalDAV/Integration/ExternalCalendarTest.php @@ -0,0 +1,119 @@ +<?php +/** + * @copyright 2020, Georg Ehrke <oc.list@georgehrke.com> + * + * @author Georg Ehrke <oc.list@georgehrke.com> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +namespace OCA\DAV\Tests\unit\CalDAV\Integration; + +use OCA\DAV\CalDAV\Integration\ExternalCalendar; +use Test\TestCase; + +class ExternalCalendarTest extends TestCase { + + private $abstractExternalCalendar; + + protected function setUp(): void { + parent::setUp(); + + $this->abstractExternalCalendar = + $this->getMockForAbstractClass(ExternalCalendar::class, ['example-app-id', 'calendar-uri-in-backend']); + } + + public function testGetName():void { + // Check that the correct name is returned + $this->assertEquals('app-generated--example-app-id--calendar-uri-in-backend', + $this->abstractExternalCalendar->getName()); + + // Check that the method is final and can't be overriden by other classes + $reflectionMethod = new \ReflectionMethod(ExternalCalendar::class, 'getName'); + $this->assertTrue($reflectionMethod->isFinal()); + } + + public function testSetName():void { + // Check that the method is final and can't be overriden by other classes + $reflectionMethod = new \ReflectionMethod(ExternalCalendar::class, 'setName'); + $this->assertTrue($reflectionMethod->isFinal()); + + $this->expectException(\Sabre\DAV\Exception\MethodNotAllowed::class); + $this->expectExceptionMessage('Renaming calendars is not yet supported'); + + $this->abstractExternalCalendar->setName('other-name'); + } + + public function createDirectory():void { + // Check that the method is final and can't be overriden by other classes + $reflectionMethod = new \ReflectionMethod(ExternalCalendar::class, 'createDirectory'); + $this->assertTrue($reflectionMethod->isFinal()); + + $this->expectException(\Sabre\DAV\Exception\MethodNotAllowed::class); + $this->expectExceptionMessage('Creating collections in calendar objects is not allowed'); + + $this->abstractExternalCalendar->createDirectory('other-name'); + } + + public function testIsAppGeneratedCalendar():void { + $this->assertFalse(ExternalCalendar::isAppGeneratedCalendar('personal')); + $this->assertFalse(ExternalCalendar::isAppGeneratedCalendar('work')); + $this->assertFalse(ExternalCalendar::isAppGeneratedCalendar('contact_birthdays')); + $this->assertFalse(ExternalCalendar::isAppGeneratedCalendar('company')); + $this->assertFalse(ExternalCalendar::isAppGeneratedCalendar('app-generated')); + $this->assertFalse(ExternalCalendar::isAppGeneratedCalendar('app-generated--example')); + + $this->assertTrue(ExternalCalendar::isAppGeneratedCalendar('app-generated--deck--board-1')); + $this->assertTrue(ExternalCalendar::isAppGeneratedCalendar('app-generated--example--foo-2')); + $this->assertTrue(ExternalCalendar::isAppGeneratedCalendar('app-generated--example--foo--2')); + } + + /** + * @dataProvider splitAppGeneratedCalendarUriDataProvider + */ + public function testSplitAppGeneratedCalendarUriInvalid(string $name):void { + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessage('Provided calendar uri was not app-generated'); + + ExternalCalendar::splitAppGeneratedCalendarUri($name); + } + + public function splitAppGeneratedCalendarUriDataProvider():array { + return [ + ['personal'], + ['foo_shared_by_admin'], + ['contact_birthdays'], + ]; + } + + public function testSplitAppGeneratedCalendarUri():void { + $this->assertEquals(['deck', 'board-1'], ExternalCalendar::splitAppGeneratedCalendarUri('app-generated--deck--board-1')); + $this->assertEquals(['example', 'foo-2'], ExternalCalendar::splitAppGeneratedCalendarUri('app-generated--example--foo-2')); + $this->assertEquals(['example', 'foo--2'], ExternalCalendar::splitAppGeneratedCalendarUri('app-generated--example--foo--2')); + } + + public function testDoesViolateReservedName():void { + $this->assertFalse(ExternalCalendar::doesViolateReservedName('personal')); + $this->assertFalse(ExternalCalendar::doesViolateReservedName('work')); + $this->assertFalse(ExternalCalendar::doesViolateReservedName('contact_birthdays')); + $this->assertFalse(ExternalCalendar::doesViolateReservedName('company')); + + $this->assertTrue(ExternalCalendar::doesViolateReservedName('app-generated')); + $this->assertTrue(ExternalCalendar::doesViolateReservedName('app-generated-calendar')); + $this->assertTrue(ExternalCalendar::doesViolateReservedName('app-generated--deck-123')); + } +} |