2016-05-31 15:16:28 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-21 14:49:16 +00:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2017-11-06 19:15:27 +00:00
|
|
|
* @author Georg Ehrke <oc.list@georgehrke.com>
|
2017-11-06 14:56:42 +00:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2016-05-31 15:16:28 +00:00
|
|
|
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
|
|
|
*
|
|
|
|
* @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\Connector\Sabre\Principal;
|
2019-03-12 17:41:37 +00:00
|
|
|
use OCP\App\IAppManager;
|
2018-06-01 11:24:04 +00:00
|
|
|
use OCP\IConfig;
|
2017-10-05 10:32:46 +00:00
|
|
|
use OCP\IGroupManager;
|
2017-10-22 10:16:58 +00:00
|
|
|
use OCP\ILogger;
|
2016-10-13 09:04:22 +00:00
|
|
|
use OCP\IUserManager;
|
2018-04-23 12:29:46 +00:00
|
|
|
use OCP\IUserSession;
|
2016-09-03 08:52:05 +00:00
|
|
|
use OCP\Security\ISecureRandom;
|
2018-04-23 12:29:46 +00:00
|
|
|
use OCP\Share\IManager as ShareManager;
|
2016-05-31 15:16:28 +00:00
|
|
|
use Sabre\CalDAV\Xml\Property\SupportedCalendarComponentSet;
|
2018-06-28 11:07:33 +00:00
|
|
|
use Sabre\DAV\Xml\Property\Href;
|
2016-10-13 13:34:26 +00:00
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
2016-05-31 15:16:28 +00:00
|
|
|
use Test\TestCase;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class CalDavBackendTest
|
|
|
|
*
|
|
|
|
* @group DB
|
|
|
|
*
|
|
|
|
* @package OCA\DAV\Tests\unit\CalDAV
|
|
|
|
*/
|
2017-10-05 10:32:46 +00:00
|
|
|
abstract class AbstractCalDavBackend extends TestCase {
|
2016-05-31 15:16:28 +00:00
|
|
|
|
|
|
|
/** @var CalDavBackend */
|
|
|
|
protected $backend;
|
|
|
|
|
|
|
|
/** @var Principal | \PHPUnit_Framework_MockObject_MockObject */
|
|
|
|
protected $principal;
|
2016-10-13 09:04:22 +00:00
|
|
|
/** @var IUserManager|\PHPUnit_Framework_MockObject_MockObject */
|
2016-08-18 13:10:18 +00:00
|
|
|
protected $userManager;
|
2017-10-05 10:32:46 +00:00
|
|
|
/** @var IGroupManager|\PHPUnit_Framework_MockObject_MockObject */
|
|
|
|
protected $groupManager;
|
2016-11-03 11:28:12 +00:00
|
|
|
/** @var EventDispatcherInterface|\PHPUnit_Framework_MockObject_MockObject */
|
|
|
|
protected $dispatcher;
|
2016-08-18 13:10:18 +00:00
|
|
|
|
2016-09-03 08:52:05 +00:00
|
|
|
/** @var ISecureRandom */
|
|
|
|
private $random;
|
2017-10-22 10:16:58 +00:00
|
|
|
/** @var ILogger */
|
|
|
|
private $logger;
|
2016-09-03 08:52:05 +00:00
|
|
|
|
2016-05-31 15:16:28 +00:00
|
|
|
const UNIT_TEST_USER = 'principals/users/caldav-unit-test';
|
|
|
|
const UNIT_TEST_USER1 = 'principals/users/caldav-unit-test1';
|
|
|
|
const UNIT_TEST_GROUP = 'principals/groups/caldav-unit-test-group';
|
2017-03-02 11:27:59 +00:00
|
|
|
const UNIT_TEST_GROUP2 = 'principals/groups/caldav-unit-test-group2';
|
2016-05-31 15:16:28 +00:00
|
|
|
|
|
|
|
public function setUp() {
|
|
|
|
parent::setUp();
|
|
|
|
|
2016-10-13 09:04:22 +00:00
|
|
|
$this->userManager = $this->createMock(IUserManager::class);
|
2017-10-05 10:32:46 +00:00
|
|
|
$this->groupManager = $this->createMock(IGroupManager::class);
|
2016-11-03 11:28:12 +00:00
|
|
|
$this->dispatcher = $this->createMock(EventDispatcherInterface::class);
|
2017-10-24 13:26:53 +00:00
|
|
|
$this->principal = $this->getMockBuilder(Principal::class)
|
2018-04-23 12:29:46 +00:00
|
|
|
->setConstructorArgs([
|
|
|
|
$this->userManager,
|
|
|
|
$this->groupManager,
|
|
|
|
$this->createMock(ShareManager::class),
|
|
|
|
$this->createMock(IUserSession::class),
|
2018-06-01 11:24:04 +00:00
|
|
|
$this->createMock(IConfig::class),
|
2019-03-12 17:41:37 +00:00
|
|
|
$this->createMock(IAppManager::class),
|
2018-04-23 12:29:46 +00:00
|
|
|
])
|
2016-05-31 15:16:28 +00:00
|
|
|
->setMethods(['getPrincipalByPath', 'getGroupMembership'])
|
|
|
|
->getMock();
|
|
|
|
$this->principal->expects($this->any())->method('getPrincipalByPath')
|
|
|
|
->willReturn([
|
2017-04-19 14:18:44 +00:00
|
|
|
'uri' => 'principals/best-friend',
|
|
|
|
'{DAV:}displayname' => 'User\'s displayname',
|
2016-05-31 15:16:28 +00:00
|
|
|
]);
|
|
|
|
$this->principal->expects($this->any())->method('getGroupMembership')
|
|
|
|
->withAnyParameters()
|
2017-03-02 11:27:59 +00:00
|
|
|
->willReturn([self::UNIT_TEST_GROUP, self::UNIT_TEST_GROUP2]);
|
2016-05-31 15:16:28 +00:00
|
|
|
|
|
|
|
$db = \OC::$server->getDatabaseConnection();
|
2016-09-03 08:52:05 +00:00
|
|
|
$this->random = \OC::$server->getSecureRandom();
|
2017-10-22 10:16:58 +00:00
|
|
|
$this->logger = $this->createMock(ILogger::class);
|
|
|
|
$this->backend = new CalDavBackend($db, $this->principal, $this->userManager, $this->groupManager, $this->random, $this->logger, $this->dispatcher);
|
2016-10-13 09:04:22 +00:00
|
|
|
|
|
|
|
$this->cleanUpBackend();
|
2016-05-31 15:16:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function tearDown() {
|
2016-10-13 09:04:22 +00:00
|
|
|
$this->cleanUpBackend();
|
2016-05-31 15:16:28 +00:00
|
|
|
parent::tearDown();
|
2016-10-13 09:04:22 +00:00
|
|
|
}
|
2016-05-31 15:16:28 +00:00
|
|
|
|
2016-10-13 09:04:22 +00:00
|
|
|
public function cleanUpBackend() {
|
2016-05-31 15:16:28 +00:00
|
|
|
if (is_null($this->backend)) {
|
|
|
|
return;
|
|
|
|
}
|
2017-06-08 07:08:24 +00:00
|
|
|
$this->principal->expects($this->any())->method('getGroupMembership')
|
|
|
|
->withAnyParameters()
|
|
|
|
->willReturn([self::UNIT_TEST_GROUP, self::UNIT_TEST_GROUP2]);
|
2016-11-03 11:28:12 +00:00
|
|
|
$calendars = $this->backend->getCalendarsForUser(self::UNIT_TEST_USER);
|
|
|
|
foreach ($calendars as $calendar) {
|
|
|
|
$this->dispatcher->expects($this->at(0))
|
|
|
|
->method('dispatch')
|
|
|
|
->with('\OCA\DAV\CalDAV\CalDavBackend::deleteCalendar');
|
|
|
|
|
|
|
|
$this->backend->deleteCalendar($calendar['id']);
|
2016-05-31 15:16:28 +00:00
|
|
|
}
|
|
|
|
$subscriptions = $this->backend->getSubscriptionsForUser(self::UNIT_TEST_USER);
|
|
|
|
foreach ($subscriptions as $subscription) {
|
|
|
|
$this->backend->deleteSubscription($subscription['id']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function createTestCalendar() {
|
2016-11-03 11:28:12 +00:00
|
|
|
$this->dispatcher->expects($this->at(0))
|
|
|
|
->method('dispatch')
|
|
|
|
->with('\OCA\DAV\CalDAV\CalDavBackend::createCalendar');
|
|
|
|
|
2016-05-31 15:16:28 +00:00
|
|
|
$this->backend->createCalendar(self::UNIT_TEST_USER, 'Example', [
|
|
|
|
'{http://apple.com/ns/ical/}calendar-color' => '#1C4587FF'
|
|
|
|
]);
|
|
|
|
$calendars = $this->backend->getCalendarsForUser(self::UNIT_TEST_USER);
|
|
|
|
$this->assertEquals(1, count($calendars));
|
|
|
|
$this->assertEquals(self::UNIT_TEST_USER, $calendars[0]['principaluri']);
|
|
|
|
/** @var SupportedCalendarComponentSet $components */
|
|
|
|
$components = $calendars[0]['{urn:ietf:params:xml:ns:caldav}supported-calendar-component-set'];
|
|
|
|
$this->assertEquals(['VEVENT','VTODO'], $components->getValue());
|
|
|
|
$color = $calendars[0]['{http://apple.com/ns/ical/}calendar-color'];
|
|
|
|
$this->assertEquals('#1C4587FF', $color);
|
|
|
|
$this->assertEquals('Example', $calendars[0]['uri']);
|
|
|
|
$this->assertEquals('Example', $calendars[0]['{DAV:}displayname']);
|
|
|
|
$calendarId = $calendars[0]['id'];
|
|
|
|
|
|
|
|
return $calendarId;
|
|
|
|
}
|
|
|
|
|
2018-06-28 11:07:33 +00:00
|
|
|
protected function createTestSubscription() {
|
|
|
|
$this->backend->createSubscription(self::UNIT_TEST_USER, 'Example', [
|
|
|
|
'{http://apple.com/ns/ical/}calendar-color' => '#1C4587FF',
|
|
|
|
'{http://calendarserver.org/ns/}source' => new Href(['foo']),
|
|
|
|
]);
|
|
|
|
$calendars = $this->backend->getSubscriptionsForUser(self::UNIT_TEST_USER);
|
|
|
|
$this->assertEquals(1, count($calendars));
|
|
|
|
$this->assertEquals(self::UNIT_TEST_USER, $calendars[0]['principaluri']);
|
|
|
|
$this->assertEquals('Example', $calendars[0]['uri']);
|
|
|
|
$calendarId = $calendars[0]['id'];
|
|
|
|
|
|
|
|
return $calendarId;
|
|
|
|
}
|
|
|
|
|
2016-05-31 15:16:28 +00:00
|
|
|
protected function createEvent($calendarId, $start = '20130912T130000Z', $end = '20130912T140000Z') {
|
|
|
|
|
2017-11-01 21:00:53 +00:00
|
|
|
$randomPart = self::getUniqueID();
|
|
|
|
|
2016-05-31 15:16:28 +00:00
|
|
|
$calData = <<<EOD
|
|
|
|
BEGIN:VCALENDAR
|
|
|
|
VERSION:2.0
|
|
|
|
PRODID:ownCloud Calendar
|
|
|
|
BEGIN:VEVENT
|
|
|
|
CREATED;VALUE=DATE-TIME:20130910T125139Z
|
2017-11-01 21:00:53 +00:00
|
|
|
UID:47d15e3ec8-$randomPart
|
2016-05-31 15:16:28 +00:00
|
|
|
LAST-MODIFIED;VALUE=DATE-TIME:20130910T125139Z
|
|
|
|
DTSTAMP;VALUE=DATE-TIME:20130910T125139Z
|
|
|
|
SUMMARY:Test Event
|
|
|
|
DTSTART;VALUE=DATE-TIME:$start
|
|
|
|
DTEND;VALUE=DATE-TIME:$end
|
|
|
|
CLASS:PUBLIC
|
|
|
|
END:VEVENT
|
|
|
|
END:VCALENDAR
|
|
|
|
EOD;
|
|
|
|
$uri0 = $this->getUniqueID('event');
|
2016-11-03 11:28:12 +00:00
|
|
|
|
|
|
|
$this->dispatcher->expects($this->at(0))
|
|
|
|
->method('dispatch')
|
|
|
|
->with('\OCA\DAV\CalDAV\CalDavBackend::createCalendarObject');
|
|
|
|
|
2016-05-31 15:16:28 +00:00
|
|
|
$this->backend->createCalendarObject($calendarId, $uri0, $calData);
|
|
|
|
|
|
|
|
return $uri0;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function assertAcl($principal, $privilege, $acl) {
|
|
|
|
foreach($acl as $a) {
|
|
|
|
if ($a['principal'] === $principal && $a['privilege'] === $privilege) {
|
2018-01-25 10:23:12 +00:00
|
|
|
$this->addToAssertionCount(1);
|
2016-05-31 15:16:28 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$this->fail("ACL does not contain $principal / $privilege");
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function assertNotAcl($principal, $privilege, $acl) {
|
|
|
|
foreach($acl as $a) {
|
|
|
|
if ($a['principal'] === $principal && $a['privilege'] === $privilege) {
|
|
|
|
$this->fail("ACL contains $principal / $privilege");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2018-01-25 10:23:12 +00:00
|
|
|
$this->addToAssertionCount(1);
|
2016-05-31 15:16:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function assertAccess($shouldHaveAcl, $principal, $privilege, $acl) {
|
|
|
|
if ($shouldHaveAcl) {
|
|
|
|
$this->assertAcl($principal, $privilege, $acl);
|
|
|
|
} else {
|
|
|
|
$this->assertNotAcl($principal, $privilege, $acl);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|