diff options
Diffstat (limited to 'apps/dav/tests/unit/CalDAV')
-rw-r--r-- | apps/dav/tests/unit/CalDAV/BirthdayCalendar/EnablePluginTest.php | 186 | ||||
-rw-r--r-- | apps/dav/tests/unit/CalDAV/CalDavBackendTest.php | 10 | ||||
-rw-r--r-- | apps/dav/tests/unit/CalDAV/CalendarHomeTest.php | 81 | ||||
-rw-r--r-- | apps/dav/tests/unit/CalDAV/CalendarTest.php | 48 | ||||
-rw-r--r-- | apps/dav/tests/unit/CalDAV/PublicCalendarRootTest.php | 14 | ||||
-rw-r--r-- | apps/dav/tests/unit/CalDAV/PublicCalendarTest.php | 9 |
6 files changed, 328 insertions, 20 deletions
diff --git a/apps/dav/tests/unit/CalDAV/BirthdayCalendar/EnablePluginTest.php b/apps/dav/tests/unit/CalDAV/BirthdayCalendar/EnablePluginTest.php new file mode 100644 index 00000000000..44bf9237b2f --- /dev/null +++ b/apps/dav/tests/unit/CalDAV/BirthdayCalendar/EnablePluginTest.php @@ -0,0 +1,186 @@ +<?php +/** + * @author Georg Ehrke <oc.list@georgehrke.com> + * + * @copyright Copyright (c) 2017 Georg Ehrke <oc.list@georgehrke.com> + * @license GNU AGPL version 3 or any later version + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * 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, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ + +namespace OCA\DAV\Tests\unit\CalDAV\BirthdayCalendar; + +use OCA\DAV\CalDAV\BirthdayCalendar\EnablePlugin; +use OCA\DAV\CalDAV\BirthdayService; +use OCA\DAV\CalDAV\Calendar; +use OCA\DAV\CalDAV\CalendarHome; +use OCP\IConfig; +use Test\TestCase; + +class EnablePluginTest extends TestCase { + + /** @var \Sabre\DAV\Server|\PHPUnit_Framework_MockObject_MockObject */ + protected $server; + + /** @var \OCP\IConfig|\PHPUnit_Framework_MockObject_MockObject */ + protected $config; + + /** @var BirthdayService |\PHPUnit_Framework_MockObject_MockObject */ + protected $birthdayService; + + /** @var \OCA\DAV\CalDAV\BirthdayCalendar\EnablePlugin $plugin */ + protected $plugin; + + protected $request; + + protected $response; + + public function setUp() { + parent::setUp(); + + $this->server = $this->createMock(\Sabre\DAV\Server::class); + $this->server->tree = $this->createMock(\Sabre\DAV\Tree::class); + $this->server->httpResponse = $this->createMock(\Sabre\HTTP\Response::class); + $this->server->xml = $this->createMock(\Sabre\DAV\Xml\Service::class); + + $this->config = $this->createMock(IConfig::class); + $this->birthdayService = $this->createMock(BirthdayService::class); + + $this->plugin = new EnablePlugin($this->config, $this->birthdayService); + $this->plugin->initialize($this->server); + + $this->request = $this->createMock(\Sabre\HTTP\RequestInterface::class); + $this->response = $this->createMock(\Sabre\HTTP\ResponseInterface::class); + } + + public function testGetFeatures() { + $this->assertEquals(['nc-enable-birthday-calendar'], $this->plugin->getFeatures()); + } + + public function testGetName() { + $this->assertEquals('nc-enable-birthday-calendar', $this->plugin->getPluginName()); + } + + public function testInitialize() { + $server = $this->createMock(\Sabre\DAV\Server::class); + + $plugin = new EnablePlugin($this->config, $this->birthdayService); + + $server->expects($this->at(0)) + ->method('on') + ->with('method:POST', [$plugin, 'httpPost']); + + $plugin->initialize($server); + } + + public function testHttpPostNoCalendarHome() { + $calendar = $this->createMock(Calendar::class); + + $this->server->expects($this->once()) + ->method('getRequestUri') + ->will($this->returnValue('/bar/foo')); + $this->server->tree->expects($this->once()) + ->method('getNodeForPath') + ->with('/bar/foo') + ->will($this->returnValue($calendar)); + + $this->config->expects($this->never()) + ->method('setUserValue'); + + $this->birthdayService->expects($this->never()) + ->method('syncUser'); + + $this->plugin->httpPost($this->request, $this->response); + } + + public function testHttpPostWrongRequest() { + $calendarHome = $this->createMock(CalendarHome::class); + + $this->server->expects($this->once()) + ->method('getRequestUri') + ->will($this->returnValue('/bar/foo')); + $this->server->tree->expects($this->once()) + ->method('getNodeForPath') + ->with('/bar/foo') + ->will($this->returnValue($calendarHome)); + + $this->request->expects($this->at(0)) + ->method('getBodyAsString') + ->will($this->returnValue('<nc:disable-birthday-calendar xmlns:nc="http://nextcloud.com/ns"/>')); + + $this->request->expects($this->at(1)) + ->method('getUrl') + ->will($this->returnValue('url_abc')); + + $this->server->xml->expects($this->once()) + ->method('parse') + ->will($this->returnCallback(function($requestBody, $url, &$documentType) { + $documentType = '{http://nextcloud.com/ns}disable-birthday-calendar'; + })); + + $this->config->expects($this->never()) + ->method('setUserValue'); + + $this->birthdayService->expects($this->never()) + ->method('syncUser'); + + $this->plugin->httpPost($this->request, $this->response); + } + + public function testHttpPost() { + $calendarHome = $this->createMock(CalendarHome::class); + + $this->server->expects($this->once()) + ->method('getRequestUri') + ->will($this->returnValue('/bar/foo')); + $this->server->tree->expects($this->once()) + ->method('getNodeForPath') + ->with('/bar/foo') + ->will($this->returnValue($calendarHome)); + + $calendarHome->expects($this->once()) + ->method('getOwner') + ->will($this->returnValue('principals/users/BlaBlub')); + + $this->request->expects($this->at(0)) + ->method('getBodyAsString') + ->will($this->returnValue('<nc:enable-birthday-calendar xmlns:nc="http://nextcloud.com/ns"/>')); + + $this->request->expects($this->at(1)) + ->method('getUrl') + ->will($this->returnValue('url_abc')); + + $this->server->xml->expects($this->once()) + ->method('parse') + ->will($this->returnCallback(function($requestBody, $url, &$documentType) { + $documentType = '{http://nextcloud.com/ns}enable-birthday-calendar'; + })); + + $this->config->expects($this->once()) + ->method('setUserValue') + ->with('BlaBlub', 'dav', 'generateBirthdayCalendar', 'yes'); + + $this->birthdayService->expects($this->once()) + ->method('syncUser') + ->with('BlaBlub'); + + $this->server->httpResponse->expects($this->once()) + ->method('setStatus') + ->with(204); + + $result = $this->plugin->httpPost($this->request, $this->response); + + $this->assertEquals(false, $result); + } +} diff --git a/apps/dav/tests/unit/CalDAV/CalDavBackendTest.php b/apps/dav/tests/unit/CalDAV/CalDavBackendTest.php index fc34a7af952..0b8978a0409 100644 --- a/apps/dav/tests/unit/CalDAV/CalDavBackendTest.php +++ b/apps/dav/tests/unit/CalDAV/CalDavBackendTest.php @@ -31,6 +31,7 @@ use DateTime; use DateTimeZone; use OCA\DAV\CalDAV\CalDavBackend; use OCA\DAV\CalDAV\Calendar; +use OCP\IConfig; use OCP\IL10N; use Sabre\DAV\PropPatch; use Sabre\DAV\Xml\Property\Href; @@ -131,6 +132,8 @@ class CalDavBackendTest extends AbstractCalDavBackend { return vsprintf($text, $parameters); })); + $config = $this->createMock(IConfig::class); + $this->userManager->expects($this->any()) ->method('userExists') ->willReturn(true); @@ -142,14 +145,14 @@ class CalDavBackendTest extends AbstractCalDavBackend { $calendarId = $this->createTestCalendar(); $calendars = $this->backend->getCalendarsForUser(self::UNIT_TEST_USER); $this->assertCount(1, $calendars); - $calendar = new Calendar($this->backend, $calendars[0], $l10n); + $calendar = new Calendar($this->backend, $calendars[0], $l10n, $config); $this->dispatcher->expects($this->at(0)) ->method('dispatch') ->with('\OCA\DAV\CalDAV\CalDavBackend::updateShares'); $this->backend->updateShares($calendar, $add, []); $calendars = $this->backend->getCalendarsForUser(self::UNIT_TEST_USER1); $this->assertCount(1, $calendars); - $calendar = new Calendar($this->backend, $calendars[0], $l10n); + $calendar = new Calendar($this->backend, $calendars[0], $l10n, $config); $acl = $calendar->getACL(); $this->assertAcl(self::UNIT_TEST_USER, '{DAV:}read', $acl); $this->assertAcl(self::UNIT_TEST_USER, '{DAV:}write', $acl); @@ -505,8 +508,9 @@ EOD; /** @var IL10N|\PHPUnit_Framework_MockObject_MockObject $l10n */ $l10n = $this->createMock(IL10N::class); + $config = $this->createMock(IConfig::class); - $calendar = new Calendar($this->backend, $calendarInfo, $l10n); + $calendar = new Calendar($this->backend, $calendarInfo, $l10n, $config); $calendar->setPublishStatus(true); $this->assertNotEquals(false, $calendar->getPublishStatus()); diff --git a/apps/dav/tests/unit/CalDAV/CalendarHomeTest.php b/apps/dav/tests/unit/CalDAV/CalendarHomeTest.php new file mode 100644 index 00000000000..a7981cfa159 --- /dev/null +++ b/apps/dav/tests/unit/CalDAV/CalendarHomeTest.php @@ -0,0 +1,81 @@ +<?php +/** + * @copyright Copyright (c) 2016, ownCloud, Inc. + * @copyright Copyright (c) 2017, Georg Ehrke + * + * @author Georg Ehrke <oc.list@georgehrke.com> + * + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * 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, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ + +namespace OCA\DAV\Tests\unit\CalDAV; + +use OCA\DAV\CalDAV\CalDavBackend; +use OCA\DAV\CalDAV\CalendarHome; +use Sabre\DAV\MkCol; +use Test\TestCase; + +class CalendarHomeTest extends TestCase { + + /** @var CalDavBackend | \PHPUnit_Framework_MockObject_MockObject */ + private $backend; + + /** @var array */ + private $principalInfo = []; + + /** @var CalendarHome */ + private $calendarHome; + + protected function setUp() { + parent::setUp(); + + $this->backend = $this->createMock(CalDavBackend::class); + $this->principalInfo = [ + 'uri' => 'user-principal-123', + ]; + + $this->calendarHome = new CalendarHome($this->backend, + $this->principalInfo); + } + + public function testCreateCalendarValidName() { + /** @var MkCol | \PHPUnit_Framework_MockObject_MockObject $mkCol */ + $mkCol = $this->createMock(MkCol::class); + + $mkCol->method('getResourceType') + ->will($this->returnValue(['{DAV:}collection', + '{urn:ietf:params:xml:ns:caldav}calendar'])); + $mkCol->method('getRemainingValues') + ->will($this->returnValue(['... properties ...'])); + + $this->backend->expects($this->once()) + ->method('createCalendar') + ->with('user-principal-123', 'name123', ['... properties ...']); + + $this->calendarHome->createExtendedCollection('name123', $mkCol); + } + + /** + * @expectedException \Sabre\DAV\Exception\MethodNotAllowed + * @expectedExceptionMessage The resource you tried to create has a reserved name + */ + public function testCreateCalendarReservedName() { + /** @var MkCol | \PHPUnit_Framework_MockObject_MockObject $mkCol */ + $mkCol = $this->createMock(MkCol::class); + + $this->calendarHome->createExtendedCollection('contact_birthdays', $mkCol); + } +} diff --git a/apps/dav/tests/unit/CalDAV/CalendarTest.php b/apps/dav/tests/unit/CalDAV/CalendarTest.php index 99ad640c447..dbdbf0dbafd 100644 --- a/apps/dav/tests/unit/CalDAV/CalendarTest.php +++ b/apps/dav/tests/unit/CalDAV/CalendarTest.php @@ -29,6 +29,7 @@ namespace OCA\DAV\Tests\unit\CalDAV; use OCA\DAV\CalDAV\BirthdayService; use OCA\DAV\CalDAV\CalDavBackend; use OCA\DAV\CalDAV\Calendar; +use OCP\IConfig; use OCP\IL10N; use Sabre\DAV\PropPatch; use Sabre\VObject\Reader; @@ -39,10 +40,14 @@ class CalendarTest extends TestCase { /** @var IL10N */ protected $l10n; + /** @var IConfig */ + protected $config; + public function setUp() { parent::setUp(); $this->l10n = $this->getMockBuilder(IL10N::class) ->disableOriginalConstructor()->getMock(); + $this->config = $this->createMock(IConfig::class); $this->l10n ->expects($this->any()) ->method('t') @@ -64,7 +69,7 @@ class CalendarTest extends TestCase { 'id' => 666, 'uri' => 'cal', ]; - $c = new Calendar($backend, $calendarInfo, $this->l10n); + $c = new Calendar($backend, $calendarInfo, $this->l10n, $this->config); $c->delete(); } @@ -84,7 +89,7 @@ class CalendarTest extends TestCase { 'id' => 666, 'uri' => 'cal', ]; - $c = new Calendar($backend, $calendarInfo, $this->l10n); + $c = new Calendar($backend, $calendarInfo, $this->l10n, $this->config); $c->delete(); } @@ -94,6 +99,8 @@ class CalendarTest extends TestCase { $backend->expects($this->never())->method('updateShares'); $backend->expects($this->never())->method('getShares'); + $this->config->expects($this->never())->method('setUserValue'); + $backend->expects($this->once())->method('deleteCalendar') ->with(666); @@ -103,7 +110,28 @@ class CalendarTest extends TestCase { 'id' => 666, 'uri' => 'cal', ]; - $c = new Calendar($backend, $calendarInfo, $this->l10n); + $c = new Calendar($backend, $calendarInfo, $this->l10n, $this->config); + $c->delete(); + } + + public function testDeleteBirthdayCalendar() { + /** @var \PHPUnit_Framework_MockObject_MockObject | CalDavBackend $backend */ + $backend = $this->createMock(CalDavBackend::class); + $backend->expects($this->once())->method('deleteCalendar') + ->with(666); + + $this->config->expects($this->once()) + ->method('setUserValue') + ->with('user1', 'dav', 'generateBirthdayCalendar', 'no'); + + $calendarInfo = [ + '{http://owncloud.org/ns}owner-principal' => 'principals/users/user1', + 'principaluri' => 'principals/users/user1', + 'id' => 666, + 'uri' => 'contact_birthdays', + ]; + + $c = new Calendar($backend, $calendarInfo, $this->l10n, $this->config); $c->delete(); } @@ -146,7 +174,7 @@ class CalendarTest extends TestCase { 'id' => 666, 'uri' => 'default' ]; - $c = new Calendar($backend, $calendarInfo, $this->l10n); + $c = new Calendar($backend, $calendarInfo, $this->l10n, $this->config); $propPatch = new PropPatch($mutations); if (!$shared) { @@ -176,7 +204,7 @@ class CalendarTest extends TestCase { if ($hasOwnerSet) { $calendarInfo['{http://owncloud.org/ns}owner-principal'] = 'user1'; } - $c = new Calendar($backend, $calendarInfo, $this->l10n); + $c = new Calendar($backend, $calendarInfo, $this->l10n, $this->config); $acl = $c->getACL(); $childAcl = $c->getChildACL(); @@ -271,7 +299,7 @@ class CalendarTest extends TestCase { $calendarInfo['{http://owncloud.org/ns}owner-principal'] = 'user1'; } - $c = new Calendar($backend, $calendarInfo, $this->l10n); + $c = new Calendar($backend, $calendarInfo, $this->l10n, $this->config); $children = $c->getChildren(); $this->assertEquals($expectedChildren, count($children)); $children = $c->getMultipleChildren(['event-0', 'event-1', 'event-2']); @@ -355,7 +383,7 @@ EOD; 'id' => 666, 'uri' => 'cal', ]; - $c = new Calendar($backend, $calendarInfo, $this->l10n); + $c = new Calendar($backend, $calendarInfo, $this->l10n, $this->config); $this->assertEquals(count($c->getChildren()), $expectedChildren); @@ -531,9 +559,9 @@ EOD; 'uri' => 'cal', ]; - $ownerCalendar = new Calendar($backend, $calendarInfoOwner, $this->l10n); - $rwCalendar = new Calendar($backend, $calendarInfoSharedRW, $this->l10n); - $roCalendar = new Calendar($backend, $calendarInfoSharedRO, $this->l10n); + $ownerCalendar = new Calendar($backend, $calendarInfoOwner, $this->l10n, $this->config); + $rwCalendar = new Calendar($backend, $calendarInfoSharedRW, $this->l10n, $this->config); + $roCalendar = new Calendar($backend, $calendarInfoSharedRO, $this->l10n, $this->config); $this->assertEquals(count($ownerCalendar->getChildren()), 2); $this->assertEquals(count($rwCalendar->getChildren()), 2); diff --git a/apps/dav/tests/unit/CalDAV/PublicCalendarRootTest.php b/apps/dav/tests/unit/CalDAV/PublicCalendarRootTest.php index 57707c6c0a4..c10b333e28d 100644 --- a/apps/dav/tests/unit/CalDAV/PublicCalendarRootTest.php +++ b/apps/dav/tests/unit/CalDAV/PublicCalendarRootTest.php @@ -30,6 +30,7 @@ namespace OCA\DAV\Tests\unit\CalDAV; use OCA\DAV\CalDAV\Calendar; use OCA\DAV\CalDAV\PublicCalendar; use OCA\DAV\Connector\Sabre\Principal; +use OCP\IConfig; use OCP\IGroupManager; use OCP\IL10N; use OCA\DAV\CalDAV\CalDavBackend; @@ -62,6 +63,8 @@ class PublicCalendarRootTest extends TestCase { protected $userManager; /** @var IGroupManager|\PHPUnit_Framework_MockObject_MockObject */ protected $groupManager; + /** @var IConfig */ + protected $config; /** @var ISecureRandom */ private $random; @@ -92,11 +95,12 @@ class PublicCalendarRootTest extends TestCase { $this->logger, $dispatcher ); - - $this->publicCalendarRoot = new PublicCalendarRoot($this->backend); - $this->l10n = $this->getMockBuilder(IL10N::class) ->disableOriginalConstructor()->getMock(); + $this->config = $this->createMock(IConfig::class); + + $this->publicCalendarRoot = new PublicCalendarRoot($this->backend, + $this->l10n, $this->config); } public function tearDown() { @@ -146,11 +150,11 @@ class PublicCalendarRootTest extends TestCase { $this->backend->createCalendar(self::UNIT_TEST_USER, 'Example', []); $calendarInfo = $this->backend->getCalendarsForUser(self::UNIT_TEST_USER)[0]; - $calendar = new PublicCalendar($this->backend, $calendarInfo, $this->l10n); + $calendar = new PublicCalendar($this->backend, $calendarInfo, $this->l10n, $this->config); $publicUri = $calendar->setPublishStatus(true); $calendarInfo = $this->backend->getPublicCalendar($publicUri); - $calendar = new PublicCalendar($this->backend, $calendarInfo, $this->l10n); + $calendar = new PublicCalendar($this->backend, $calendarInfo, $this->l10n, $this->config); return $calendar; } diff --git a/apps/dav/tests/unit/CalDAV/PublicCalendarTest.php b/apps/dav/tests/unit/CalDAV/PublicCalendarTest.php index 9783d1a6267..98dd330f427 100644 --- a/apps/dav/tests/unit/CalDAV/PublicCalendarTest.php +++ b/apps/dav/tests/unit/CalDAV/PublicCalendarTest.php @@ -26,6 +26,7 @@ namespace OCA\DAV\Tests\unit\CalDAV; use OCA\DAV\CalDAV\PublicCalendar; use OCA\DAV\CalDAV\CalDavBackend; +use OCP\IConfig; use Sabre\VObject\Reader; class PublicCalendarTest extends CalendarTest { @@ -61,8 +62,10 @@ class PublicCalendarTest extends CalendarTest { 'id' => 666, 'uri' => 'cal', ]; + /** @var \PHPUnit_Framework_MockObject_MockObject | IConfig $config */ + $config = $this->createMock(IConfig::class); - $c = new PublicCalendar($backend, $calendarInfo, $this->l10n); + $c = new PublicCalendar($backend, $calendarInfo, $this->l10n, $config); $children = $c->getChildren(); $this->assertEquals(2, count($children)); $children = $c->getMultipleChildren(['event-0', 'event-1', 'event-2']); @@ -146,7 +149,9 @@ EOD; 'id' => 666, 'uri' => 'cal', ]; - $c = new PublicCalendar($backend, $calendarInfo, $this->l10n); + /** @var \PHPUnit_Framework_MockObject_MockObject | IConfig $config */ + $config = $this->createMock(IConfig::class); + $c = new PublicCalendar($backend, $calendarInfo, $this->l10n, $config); $this->assertEquals(count($c->getChildren()), 2); |