2017-02-25 16:26:02 +03:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @copyright Copyright (c) 2017, Georg Ehrke <oc.list@georgehrke.com>
|
|
|
|
*
|
2020-03-31 11:49:10 +03:00
|
|
|
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
|
2017-11-06 22:15:27 +03:00
|
|
|
* @author Georg Ehrke <oc.list@georgehrke.com>
|
2020-08-24 15:54:25 +03:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2020-03-31 11:49:10 +03:00
|
|
|
* @author Robin Appelman <robin@icewind.nl>
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2017-02-25 16:26:02 +03:00
|
|
|
*
|
|
|
|
* @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
|
2019-12-03 21:57:53 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2017-02-25 16:26:02 +03:00
|
|
|
*
|
|
|
|
*/
|
2019-11-22 22:52:10 +03:00
|
|
|
|
2017-03-27 20:15:51 +03:00
|
|
|
namespace OCA\DAV\Tests\DAV;
|
2017-02-25 16:26:02 +03:00
|
|
|
|
2017-03-27 20:15:51 +03:00
|
|
|
use OCA\DAV\DAV\CustomPropertiesBackend;
|
2017-02-25 16:26:02 +03:00
|
|
|
use OCP\IDBConnection;
|
|
|
|
use OCP\IUser;
|
|
|
|
use Sabre\DAV\PropFind;
|
|
|
|
use Sabre\DAV\PropPatch;
|
|
|
|
use Sabre\DAV\Tree;
|
|
|
|
use Test\TestCase;
|
|
|
|
|
2020-01-31 17:47:13 +03:00
|
|
|
/**
|
|
|
|
* @group DB
|
|
|
|
*/
|
2017-02-25 16:26:02 +03:00
|
|
|
class CustomPropertiesBackendTest extends TestCase {
|
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var Tree | \PHPUnit\Framework\MockObject\MockObject */
|
2017-02-25 16:26:02 +03:00
|
|
|
private $tree;
|
|
|
|
|
2020-01-31 17:47:13 +03:00
|
|
|
/** @var IDBConnection */
|
2017-02-25 16:26:02 +03:00
|
|
|
private $dbConnection;
|
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var IUser | \PHPUnit\Framework\MockObject\MockObject */
|
2017-02-25 16:26:02 +03:00
|
|
|
private $user;
|
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var CustomPropertiesBackend | \PHPUnit\Framework\MockObject\MockObject */
|
2017-02-25 16:26:02 +03:00
|
|
|
private $backend;
|
|
|
|
|
2019-11-27 17:27:18 +03:00
|
|
|
protected function setUp(): void {
|
2017-02-25 16:26:02 +03:00
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$this->tree = $this->createMock(Tree::class);
|
|
|
|
$this->user = $this->createMock(IUser::class);
|
2020-01-31 17:06:26 +03:00
|
|
|
$this->user->method('getUID')
|
2017-02-25 16:26:02 +03:00
|
|
|
->with()
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn('dummy_user_42');
|
2020-01-31 17:47:13 +03:00
|
|
|
$this->dbConnection = \OC::$server->getDatabaseConnection();
|
2017-02-25 16:26:02 +03:00
|
|
|
|
2020-01-31 17:47:13 +03:00
|
|
|
$this->backend = new CustomPropertiesBackend(
|
|
|
|
$this->tree,
|
|
|
|
$this->dbConnection,
|
|
|
|
$this->user
|
|
|
|
);
|
2017-02-25 16:26:02 +03:00
|
|
|
}
|
|
|
|
|
2020-01-31 17:47:13 +03:00
|
|
|
protected function tearDown(): void {
|
|
|
|
$query = $this->dbConnection->getQueryBuilder();
|
|
|
|
$query->delete('properties');
|
|
|
|
$query->execute();
|
|
|
|
|
|
|
|
parent::tearDown();
|
|
|
|
}
|
|
|
|
|
2020-01-31 18:10:19 +03:00
|
|
|
private function formatPath(string $path): string {
|
|
|
|
if (strlen($path) > 250) {
|
|
|
|
return sha1($path);
|
|
|
|
} else {
|
|
|
|
return $path;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-31 17:47:13 +03:00
|
|
|
protected function insertProps(string $user, string $path, array $props) {
|
|
|
|
foreach ($props as $name => $value) {
|
|
|
|
$this->insertProp($user, $path, $name, $value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function insertProp(string $user, string $path, string $name, string $value) {
|
|
|
|
$query = $this->dbConnection->getQueryBuilder();
|
|
|
|
$query->insert('properties')
|
|
|
|
->values([
|
|
|
|
'userid' => $query->createNamedParameter($user),
|
2020-01-31 18:10:19 +03:00
|
|
|
'propertypath' => $query->createNamedParameter($this->formatPath($path)),
|
2020-01-31 17:47:13 +03:00
|
|
|
'propertyname' => $query->createNamedParameter($name),
|
|
|
|
'propertyvalue' => $query->createNamedParameter($value),
|
|
|
|
]);
|
|
|
|
$query->execute();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getProps(string $user, string $path) {
|
|
|
|
$query = $this->dbConnection->getQueryBuilder();
|
|
|
|
$query->select('propertyname', 'propertyvalue')
|
|
|
|
->from('properties')
|
|
|
|
->where($query->expr()->eq('userid', $query->createNamedParameter($user)))
|
2020-01-31 18:10:19 +03:00
|
|
|
->where($query->expr()->eq('propertypath', $query->createNamedParameter($this->formatPath($path))));
|
2020-11-05 12:50:53 +03:00
|
|
|
|
|
|
|
$result = $query->execute();
|
2020-11-06 16:13:11 +03:00
|
|
|
$data = [];
|
|
|
|
while ($row = $result->fetch()) {
|
|
|
|
$data[$row['propertyname']] = $row['propertyvalue'];
|
|
|
|
}
|
2020-11-05 12:50:53 +03:00
|
|
|
$result->closeCursor();
|
2020-11-06 16:13:11 +03:00
|
|
|
|
2020-11-05 12:50:53 +03:00
|
|
|
return $data;
|
2020-01-31 17:47:13 +03:00
|
|
|
}
|
|
|
|
|
2017-02-25 16:26:02 +03:00
|
|
|
public function testPropFindNoDbCalls() {
|
2020-01-31 17:47:13 +03:00
|
|
|
$db = $this->createMock(IDBConnection::class);
|
|
|
|
$backend = new CustomPropertiesBackend(
|
|
|
|
$this->tree,
|
|
|
|
$db,
|
|
|
|
$this->user
|
|
|
|
);
|
|
|
|
|
2017-02-25 16:26:02 +03:00
|
|
|
$propFind = $this->createMock(PropFind::class);
|
|
|
|
$propFind->expects($this->at(0))
|
|
|
|
->method('get404Properties')
|
|
|
|
->with()
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn([
|
2017-02-25 16:26:02 +03:00
|
|
|
'{http://owncloud.org/ns}permissions',
|
|
|
|
'{http://owncloud.org/ns}downloadURL',
|
|
|
|
'{http://owncloud.org/ns}dDC',
|
|
|
|
'{http://owncloud.org/ns}size',
|
2020-03-26 00:21:27 +03:00
|
|
|
]);
|
2017-02-25 16:26:02 +03:00
|
|
|
|
2020-01-31 17:47:13 +03:00
|
|
|
$db->expects($this->never())
|
2017-02-25 16:26:02 +03:00
|
|
|
->method($this->anything());
|
|
|
|
|
2020-01-31 17:47:13 +03:00
|
|
|
$backend->propFind('foo_bar_path_1337_0', $propFind);
|
2017-02-25 16:26:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testPropFindCalendarCall() {
|
|
|
|
$propFind = $this->createMock(PropFind::class);
|
2020-01-31 17:47:13 +03:00
|
|
|
$propFind->method('get404Properties')
|
2017-02-25 16:26:02 +03:00
|
|
|
->with()
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn([
|
2017-02-25 16:26:02 +03:00
|
|
|
'{DAV:}getcontentlength',
|
|
|
|
'{DAV:}getcontenttype',
|
|
|
|
'{DAV:}getetag',
|
2020-01-31 17:06:26 +03:00
|
|
|
'{abc}def',
|
2020-03-26 00:21:27 +03:00
|
|
|
]);
|
2017-02-25 16:26:02 +03:00
|
|
|
|
2020-01-31 17:47:13 +03:00
|
|
|
$propFind->method('getRequestedProperties')
|
2017-02-25 16:26:02 +03:00
|
|
|
->with()
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn([
|
2017-02-25 16:26:02 +03:00
|
|
|
'{DAV:}getcontentlength',
|
|
|
|
'{DAV:}getcontenttype',
|
|
|
|
'{DAV:}getetag',
|
|
|
|
'{DAV:}displayname',
|
|
|
|
'{urn:ietf:params:xml:ns:caldav}calendar-description',
|
|
|
|
'{urn:ietf:params:xml:ns:caldav}calendar-timezone',
|
2020-01-31 17:06:26 +03:00
|
|
|
'{abc}def',
|
2020-03-26 00:21:27 +03:00
|
|
|
]);
|
2017-02-25 16:26:02 +03:00
|
|
|
|
2020-01-31 17:47:13 +03:00
|
|
|
$props = [
|
|
|
|
'{abc}def' => 'a',
|
|
|
|
'{DAV:}displayname' => 'b',
|
|
|
|
'{urn:ietf:params:xml:ns:caldav}calendar-description' => 'c',
|
|
|
|
'{urn:ietf:params:xml:ns:caldav}calendar-timezone' => 'd',
|
|
|
|
];
|
|
|
|
|
|
|
|
$this->insertProps('dummy_user_42', 'calendars/foo/bar_path_1337_0', $props);
|
|
|
|
|
|
|
|
$setProps = [];
|
|
|
|
$propFind->method('set')
|
|
|
|
->willReturnCallback(function ($name, $value, $status) use (&$setProps) {
|
|
|
|
$setProps[$name] = $value;
|
|
|
|
});
|
2017-02-25 16:26:02 +03:00
|
|
|
|
|
|
|
$this->backend->propFind('calendars/foo/bar_path_1337_0', $propFind);
|
2020-01-31 17:47:13 +03:00
|
|
|
$this->assertEquals($props, $setProps);
|
2017-02-25 16:26:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider propPatchProvider
|
|
|
|
*/
|
2020-01-31 17:47:13 +03:00
|
|
|
public function testPropPatch(string $path, array $existing, array $props, array $result) {
|
|
|
|
$this->insertProps($this->user->getUID(), $path, $existing);
|
|
|
|
$propPatch = new PropPatch($props);
|
2017-02-25 16:26:02 +03:00
|
|
|
|
|
|
|
$this->backend->propPatch($path, $propPatch);
|
2020-01-31 17:47:13 +03:00
|
|
|
$propPatch->commit();
|
|
|
|
|
|
|
|
$storedProps = $this->getProps($this->user->getUID(), $path);
|
|
|
|
$this->assertEquals($result, $storedProps);
|
2017-02-25 16:26:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function propPatchProvider() {
|
2020-01-31 18:10:19 +03:00
|
|
|
$longPath = str_repeat('long_path', 100);
|
2017-02-25 16:26:02 +03:00
|
|
|
return [
|
2020-01-31 17:47:13 +03:00
|
|
|
['foo_bar_path_1337', [], ['{DAV:}displayname' => 'anything'], ['{DAV:}displayname' => 'anything']],
|
|
|
|
['foo_bar_path_1337', ['{DAV:}displayname' => 'foo'], ['{DAV:}displayname' => 'anything'], ['{DAV:}displayname' => 'anything']],
|
|
|
|
['foo_bar_path_1337', ['{DAV:}displayname' => 'foo'], ['{DAV:}displayname' => null], []],
|
2020-01-31 18:10:19 +03:00
|
|
|
[$longPath, [], ['{DAV:}displayname' => 'anything'], ['{DAV:}displayname' => 'anything']],
|
2017-02-25 16:26:02 +03:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2020-01-31 18:10:19 +03:00
|
|
|
/**
|
|
|
|
* @dataProvider deleteProvider
|
|
|
|
*/
|
|
|
|
public function testDelete(string $path) {
|
|
|
|
$this->insertProps('dummy_user_42', $path, ['foo' => 'bar']);
|
|
|
|
$this->backend->delete($path);
|
|
|
|
$this->assertEquals([], $this->getProps('dummy_user_42', $path));
|
2017-02-25 16:26:02 +03:00
|
|
|
}
|
|
|
|
|
2020-01-31 18:10:19 +03:00
|
|
|
public function deleteProvider() {
|
|
|
|
return [
|
|
|
|
['foo_bar_path_1337'],
|
|
|
|
[str_repeat('long_path', 100)]
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider moveProvider
|
|
|
|
*/
|
|
|
|
public function testMove(string $source, string $target) {
|
|
|
|
$this->insertProps('dummy_user_42', $source, ['foo' => 'bar']);
|
|
|
|
$this->backend->move($source, $target);
|
|
|
|
$this->assertEquals([], $this->getProps('dummy_user_42', $source));
|
|
|
|
$this->assertEquals(['foo' => 'bar'], $this->getProps('dummy_user_42', $target));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function moveProvider() {
|
|
|
|
return [
|
|
|
|
['foo_bar_path_1337', 'foo_bar_path_7333'],
|
|
|
|
[str_repeat('long_path1', 100), str_repeat('long_path2', 100)]
|
|
|
|
];
|
2017-02-25 16:26:02 +03:00
|
|
|
}
|
|
|
|
}
|