2017-10-21 12:19:01 +03:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
* @copyright Copyright (c) 2017, Georg Ehrke
|
|
|
|
*
|
2020-03-31 11:49:10 +03:00
|
|
|
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
|
2017-10-21 12:19:01 +03:00
|
|
|
* @author Georg Ehrke <oc.list@georgehrke.com>
|
2020-08-24 15:54:25 +03:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2017-10-21 12:19:01 +03:00
|
|
|
*
|
|
|
|
* @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,
|
2019-12-03 21:57:53 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
2017-10-21 12:19:01 +03:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OCA\DAV\Tests\unit\CalDAV;
|
|
|
|
|
2020-01-27 15:21:57 +03:00
|
|
|
use OCA\DAV\AppInfo\PluginManager;
|
2017-10-21 12:19:01 +03:00
|
|
|
use OCA\DAV\CalDAV\CalDavBackend;
|
|
|
|
use OCA\DAV\CalDAV\CalendarHome;
|
2020-01-27 15:21:57 +03:00
|
|
|
use OCA\DAV\CalDAV\Integration\ExternalCalendar;
|
|
|
|
use OCA\DAV\CalDAV\Integration\ICalendarProvider;
|
|
|
|
use OCA\DAV\CalDAV\Outbox;
|
|
|
|
use Sabre\CalDAV\Schedule\Inbox;
|
2017-10-21 12:19:01 +03:00
|
|
|
use Sabre\DAV\MkCol;
|
|
|
|
use Test\TestCase;
|
|
|
|
|
|
|
|
class CalendarHomeTest extends TestCase {
|
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var CalDavBackend | \PHPUnit\Framework\MockObject\MockObject */
|
2017-10-21 12:19:01 +03:00
|
|
|
private $backend;
|
|
|
|
|
|
|
|
/** @var array */
|
|
|
|
private $principalInfo = [];
|
|
|
|
|
2020-01-27 15:21:57 +03:00
|
|
|
/** @var PluginManager */
|
|
|
|
private $pluginManager;
|
|
|
|
|
2017-10-21 12:19:01 +03:00
|
|
|
/** @var CalendarHome */
|
|
|
|
private $calendarHome;
|
|
|
|
|
2019-11-21 18:40:38 +03:00
|
|
|
protected function setUp(): void {
|
2017-10-21 12:19:01 +03:00
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$this->backend = $this->createMock(CalDavBackend::class);
|
|
|
|
$this->principalInfo = [
|
|
|
|
'uri' => 'user-principal-123',
|
|
|
|
];
|
2020-01-27 15:21:57 +03:00
|
|
|
$this->pluginManager = $this->createMock(PluginManager::class);
|
2017-10-21 12:19:01 +03:00
|
|
|
|
|
|
|
$this->calendarHome = new CalendarHome($this->backend,
|
|
|
|
$this->principalInfo);
|
2020-01-27 15:21:57 +03:00
|
|
|
|
|
|
|
// Replace PluginManager with our mock
|
|
|
|
$reflection = new \ReflectionClass($this->calendarHome);
|
|
|
|
$reflectionProperty = $reflection->getProperty('pluginManager');
|
|
|
|
$reflectionProperty->setAccessible(true);
|
|
|
|
$reflectionProperty->setValue($this->calendarHome, $this->pluginManager);
|
2017-10-21 12:19:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testCreateCalendarValidName() {
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var MkCol | \PHPUnit\Framework\MockObject\MockObject $mkCol */
|
2017-10-21 12:19:01 +03:00
|
|
|
$mkCol = $this->createMock(MkCol::class);
|
|
|
|
|
|
|
|
$mkCol->method('getResourceType')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn(['{DAV:}collection',
|
|
|
|
'{urn:ietf:params:xml:ns:caldav}calendar']);
|
2017-10-21 12:19:01 +03:00
|
|
|
$mkCol->method('getRemainingValues')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn(['... properties ...']);
|
2017-10-21 12:19:01 +03:00
|
|
|
|
|
|
|
$this->backend->expects($this->once())
|
|
|
|
->method('createCalendar')
|
|
|
|
->with('user-principal-123', 'name123', ['... properties ...']);
|
|
|
|
|
|
|
|
$this->calendarHome->createExtendedCollection('name123', $mkCol);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCreateCalendarReservedName() {
|
2019-11-27 17:27:18 +03:00
|
|
|
$this->expectException(\Sabre\DAV\Exception\MethodNotAllowed::class);
|
|
|
|
$this->expectExceptionMessage('The resource you tried to create has a reserved name');
|
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var MkCol | \PHPUnit\Framework\MockObject\MockObject $mkCol */
|
2017-10-21 12:19:01 +03:00
|
|
|
$mkCol = $this->createMock(MkCol::class);
|
|
|
|
|
|
|
|
$this->calendarHome->createExtendedCollection('contact_birthdays', $mkCol);
|
|
|
|
}
|
2020-01-27 15:21:57 +03:00
|
|
|
|
|
|
|
public function testCreateCalendarReservedNameAppGenerated() {
|
|
|
|
$this->expectException(\Sabre\DAV\Exception\MethodNotAllowed::class);
|
|
|
|
$this->expectExceptionMessage('The resource you tried to create has a reserved name');
|
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var MkCol | \PHPUnit\Framework\MockObject\MockObject $mkCol */
|
2020-01-27 15:21:57 +03:00
|
|
|
$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);
|
|
|
|
}
|
2017-10-21 12:19:01 +03:00
|
|
|
}
|