2015-02-12 06:29:01 -05:00
|
|
|
<?php
|
|
|
|
|
|
2025-05-27 17:36:08 -04:00
|
|
|
declare(strict_types=1);
|
2015-02-12 06:29:01 -05:00
|
|
|
/**
|
2024-05-28 06:34:11 -04:00
|
|
|
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
2015-02-12 06:29:01 -05:00
|
|
|
*/
|
2024-05-28 06:34:11 -04:00
|
|
|
namespace OCA\DAV\Tests\unit\Connector\Sabre;
|
2015-11-02 19:52:41 -05:00
|
|
|
|
2024-06-04 09:58:39 -04:00
|
|
|
use OCA\DAV\CalDAV\DefaultCalendarValidator;
|
2017-10-26 04:36:17 -04:00
|
|
|
use OCA\DAV\Connector\Sabre\Directory;
|
|
|
|
|
use OCA\DAV\Connector\Sabre\File;
|
2024-10-10 06:40:31 -04:00
|
|
|
use OCA\DAV\DAV\CustomPropertiesBackend;
|
2025-08-13 05:03:40 -04:00
|
|
|
use OCA\DAV\Db\PropertyMapper;
|
2025-02-03 09:34:01 -05:00
|
|
|
use OCP\IDBConnection;
|
2017-10-24 09:26:53 -04:00
|
|
|
use OCP\IUser;
|
2025-02-03 09:34:01 -05:00
|
|
|
use OCP\Server;
|
2024-06-04 09:58:39 -04:00
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
2017-10-24 09:26:53 -04:00
|
|
|
use Sabre\DAV\Tree;
|
|
|
|
|
|
2015-11-02 19:52:41 -05:00
|
|
|
/**
|
|
|
|
|
* Class CustomPropertiesBackend
|
|
|
|
|
*
|
|
|
|
|
*
|
2016-05-25 10:04:15 -04:00
|
|
|
* @package OCA\DAV\Tests\unit\Connector\Sabre
|
2015-11-02 19:52:41 -05:00
|
|
|
*/
|
2026-01-07 08:21:48 -05:00
|
|
|
#[\PHPUnit\Framework\Attributes\Group(name: 'DB')]
|
2016-05-25 10:04:15 -04:00
|
|
|
class CustomPropertiesBackendTest extends \Test\TestCase {
|
2025-05-27 17:36:08 -04:00
|
|
|
private \Sabre\DAV\Server $server;
|
|
|
|
|
private \Sabre\DAV\Tree&MockObject $tree;
|
|
|
|
|
private IUser&MockObject $user;
|
|
|
|
|
private DefaultCalendarValidator&MockObject $defaultCalendarValidator;
|
|
|
|
|
private CustomPropertiesBackend $plugin;
|
2024-06-04 09:58:39 -04:00
|
|
|
|
2019-11-27 09:27:18 -05:00
|
|
|
protected function setUp(): void {
|
2015-02-12 06:29:01 -05:00
|
|
|
parent::setUp();
|
2025-05-27 17:36:08 -04:00
|
|
|
|
2015-02-12 06:29:01 -05:00
|
|
|
$this->server = new \Sabre\DAV\Server();
|
2025-05-27 17:36:08 -04:00
|
|
|
$this->tree = $this->createMock(Tree::class);
|
2015-02-12 06:29:01 -05:00
|
|
|
|
2025-05-27 17:36:08 -04:00
|
|
|
$userId = self::getUniqueID('testcustompropertiesuser');
|
2015-02-12 06:29:01 -05:00
|
|
|
|
2025-05-27 17:36:08 -04:00
|
|
|
$this->user = $this->createMock(IUser::class);
|
2015-02-12 06:29:01 -05:00
|
|
|
$this->user->expects($this->any())
|
|
|
|
|
->method('getUID')
|
2020-03-25 17:21:27 -04:00
|
|
|
->willReturn($userId);
|
2015-02-12 06:29:01 -05:00
|
|
|
|
2024-06-04 09:58:39 -04:00
|
|
|
$this->defaultCalendarValidator = $this->createMock(DefaultCalendarValidator::class);
|
|
|
|
|
|
2024-10-10 06:40:31 -04:00
|
|
|
$this->plugin = new CustomPropertiesBackend(
|
2024-02-21 16:29:09 -05:00
|
|
|
$this->server,
|
2015-02-12 06:29:01 -05:00
|
|
|
$this->tree,
|
2025-02-03 09:34:01 -05:00
|
|
|
Server::get(IDBConnection::class),
|
2024-06-04 09:58:39 -04:00
|
|
|
$this->user,
|
2025-08-13 05:03:40 -04:00
|
|
|
Server::get(PropertyMapper::class),
|
2024-06-04 09:58:39 -04:00
|
|
|
$this->defaultCalendarValidator,
|
2015-02-12 06:29:01 -05:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-27 09:27:18 -05:00
|
|
|
protected function tearDown(): void {
|
2025-02-03 09:34:01 -05:00
|
|
|
$connection = Server::get(IDBConnection::class);
|
2015-02-12 06:29:01 -05:00
|
|
|
$deleteStatement = $connection->prepare(
|
|
|
|
|
'DELETE FROM `*PREFIX*properties`'
|
|
|
|
|
. ' WHERE `userid` = ?'
|
|
|
|
|
);
|
|
|
|
|
$deleteStatement->execute(
|
2020-03-26 04:30:18 -04:00
|
|
|
[
|
2015-02-12 06:29:01 -05:00
|
|
|
$this->user->getUID(),
|
2020-03-26 04:30:18 -04:00
|
|
|
]
|
2015-02-12 06:29:01 -05:00
|
|
|
);
|
|
|
|
|
$deleteStatement->closeCursor();
|
2025-05-27 17:36:08 -04:00
|
|
|
|
|
|
|
|
parent::tearDown();
|
2015-02-12 06:29:01 -05:00
|
|
|
}
|
|
|
|
|
|
2025-05-27 17:36:08 -04:00
|
|
|
private function createTestNode(string $class) {
|
|
|
|
|
$node = $this->createMock($class);
|
2015-02-12 06:29:01 -05:00
|
|
|
$node->expects($this->any())
|
|
|
|
|
->method('getId')
|
2020-03-25 17:21:27 -04:00
|
|
|
->willReturn(123);
|
2015-02-12 06:29:01 -05:00
|
|
|
|
|
|
|
|
$node->expects($this->any())
|
|
|
|
|
->method('getPath')
|
2020-03-25 17:21:27 -04:00
|
|
|
->willReturn('/dummypath');
|
2015-02-12 06:29:01 -05:00
|
|
|
|
|
|
|
|
return $node;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-20 02:38:43 -05:00
|
|
|
private function applyDefaultProps($path = '/dummypath'): void {
|
2015-02-12 06:29:01 -05:00
|
|
|
// properties to set
|
2020-03-26 04:30:18 -04:00
|
|
|
$propPatch = new \Sabre\DAV\PropPatch([
|
2015-02-12 06:29:01 -05:00
|
|
|
'customprop' => 'value1',
|
|
|
|
|
'customprop2' => 'value2',
|
2020-03-26 04:30:18 -04:00
|
|
|
]);
|
2015-02-12 06:29:01 -05:00
|
|
|
|
|
|
|
|
$this->plugin->propPatch(
|
|
|
|
|
$path,
|
|
|
|
|
$propPatch
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$propPatch->commit();
|
|
|
|
|
|
|
|
|
|
$this->assertEmpty($propPatch->getRemainingMutations());
|
|
|
|
|
|
|
|
|
|
$result = $propPatch->getResult();
|
|
|
|
|
$this->assertEquals(200, $result['customprop']);
|
|
|
|
|
$this->assertEquals(200, $result['customprop2']);
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-18 07:30:23 -04:00
|
|
|
/**
|
|
|
|
|
* Test that propFind on a missing file soft fails
|
|
|
|
|
*/
|
2023-01-20 02:38:43 -05:00
|
|
|
public function testPropFindMissingFileSoftFail(): void {
|
2015-03-18 07:30:23 -04:00
|
|
|
$propFind = new \Sabre\DAV\PropFind(
|
|
|
|
|
'/dummypath',
|
2020-03-26 04:30:18 -04:00
|
|
|
[
|
2015-03-18 07:30:23 -04:00
|
|
|
'customprop',
|
|
|
|
|
'customprop2',
|
|
|
|
|
'unsetprop',
|
2020-03-26 04:30:18 -04:00
|
|
|
],
|
2015-03-18 07:30:23 -04:00
|
|
|
0
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$this->plugin->propFind(
|
|
|
|
|
'/dummypath',
|
|
|
|
|
$propFind
|
|
|
|
|
);
|
|
|
|
|
|
2015-03-23 12:41:32 -04:00
|
|
|
$this->plugin->propFind(
|
|
|
|
|
'/dummypath',
|
|
|
|
|
$propFind
|
|
|
|
|
);
|
|
|
|
|
|
2020-02-03 07:41:55 -05:00
|
|
|
// assert that the above didn't throw exceptions
|
|
|
|
|
$this->assertTrue(true);
|
2015-03-18 07:30:23 -04:00
|
|
|
}
|
|
|
|
|
|
2015-02-12 06:29:01 -05:00
|
|
|
/**
|
|
|
|
|
* Test setting/getting properties
|
|
|
|
|
*/
|
2023-01-20 02:38:43 -05:00
|
|
|
public function testSetGetPropertiesForFile(): void {
|
2015-02-12 06:29:01 -05:00
|
|
|
$this->applyDefaultProps();
|
|
|
|
|
|
|
|
|
|
$propFind = new \Sabre\DAV\PropFind(
|
|
|
|
|
'/dummypath',
|
2020-03-26 04:30:18 -04:00
|
|
|
[
|
2015-02-12 06:29:01 -05:00
|
|
|
'customprop',
|
|
|
|
|
'customprop2',
|
|
|
|
|
'unsetprop',
|
2020-03-26 04:30:18 -04:00
|
|
|
],
|
2015-02-12 06:29:01 -05:00
|
|
|
0
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$this->plugin->propFind(
|
|
|
|
|
'/dummypath',
|
|
|
|
|
$propFind
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals('value1', $propFind->get('customprop'));
|
|
|
|
|
$this->assertEquals('value2', $propFind->get('customprop2'));
|
2020-03-26 04:30:18 -04:00
|
|
|
$this->assertEquals(['unsetprop'], $propFind->get404Properties());
|
2015-02-12 06:29:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Test getting properties from directory
|
|
|
|
|
*/
|
2023-01-20 02:38:43 -05:00
|
|
|
public function testGetPropertiesForDirectory(): void {
|
2015-02-12 06:29:01 -05:00
|
|
|
$this->applyDefaultProps('/dummypath');
|
|
|
|
|
$this->applyDefaultProps('/dummypath/test.txt');
|
|
|
|
|
|
2020-03-26 04:30:18 -04:00
|
|
|
$propNames = [
|
2015-02-12 06:29:01 -05:00
|
|
|
'customprop',
|
|
|
|
|
'customprop2',
|
|
|
|
|
'unsetprop',
|
2020-03-26 04:30:18 -04:00
|
|
|
];
|
2015-02-12 06:29:01 -05:00
|
|
|
|
|
|
|
|
$propFindRoot = new \Sabre\DAV\PropFind(
|
|
|
|
|
'/dummypath',
|
|
|
|
|
$propNames,
|
|
|
|
|
1
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$propFindSub = new \Sabre\DAV\PropFind(
|
|
|
|
|
'/dummypath/test.txt',
|
|
|
|
|
$propNames,
|
|
|
|
|
0
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$this->plugin->propFind(
|
|
|
|
|
'/dummypath',
|
|
|
|
|
$propFindRoot
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$this->plugin->propFind(
|
|
|
|
|
'/dummypath/test.txt',
|
|
|
|
|
$propFindSub
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// TODO: find a way to assert that no additional SQL queries were
|
|
|
|
|
// run while doing the second propFind
|
|
|
|
|
|
|
|
|
|
$this->assertEquals('value1', $propFindRoot->get('customprop'));
|
|
|
|
|
$this->assertEquals('value2', $propFindRoot->get('customprop2'));
|
2020-03-26 04:30:18 -04:00
|
|
|
$this->assertEquals(['unsetprop'], $propFindRoot->get404Properties());
|
2015-02-12 06:29:01 -05:00
|
|
|
|
|
|
|
|
$this->assertEquals('value1', $propFindSub->get('customprop'));
|
|
|
|
|
$this->assertEquals('value2', $propFindSub->get('customprop2'));
|
2020-03-26 04:30:18 -04:00
|
|
|
$this->assertEquals(['unsetprop'], $propFindSub->get404Properties());
|
2015-02-12 06:29:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Test delete property
|
|
|
|
|
*/
|
2023-01-20 02:38:43 -05:00
|
|
|
public function testDeleteProperty(): void {
|
2015-02-12 06:29:01 -05:00
|
|
|
$this->applyDefaultProps();
|
|
|
|
|
|
2020-03-26 04:30:18 -04:00
|
|
|
$propPatch = new \Sabre\DAV\PropPatch([
|
2015-02-12 06:29:01 -05:00
|
|
|
'customprop' => null,
|
2020-03-26 04:30:18 -04:00
|
|
|
]);
|
2015-02-12 06:29:01 -05:00
|
|
|
|
|
|
|
|
$this->plugin->propPatch(
|
|
|
|
|
'/dummypath',
|
|
|
|
|
$propPatch
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$propPatch->commit();
|
|
|
|
|
|
|
|
|
|
$this->assertEmpty($propPatch->getRemainingMutations());
|
|
|
|
|
|
|
|
|
|
$result = $propPatch->getResult();
|
|
|
|
|
$this->assertEquals(204, $result['customprop']);
|
|
|
|
|
}
|
|
|
|
|
}
|