2016-03-02 12:25:31 -05:00
|
|
|
<?php
|
2024-05-28 06:34:11 -04:00
|
|
|
|
2025-05-27 17:36:08 -04:00
|
|
|
declare(strict_types=1);
|
2016-03-02 12:25:31 -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
|
2016-03-02 12:25:31 -05:00
|
|
|
*/
|
2016-05-25 10:04:15 -04:00
|
|
|
namespace OCA\DAV\Tests\unit\Connector\Sabre;
|
2016-03-02 12:25:31 -05:00
|
|
|
|
2017-10-24 09:26:53 -04:00
|
|
|
use OCA\DAV\Connector\Sabre\Directory;
|
|
|
|
|
use OCA\DAV\Connector\Sabre\File;
|
2017-10-24 18:03:28 -04:00
|
|
|
use OCA\DAV\Connector\Sabre\Node;
|
2024-10-10 06:40:31 -04:00
|
|
|
use OCA\DAV\Connector\Sabre\SharesPlugin;
|
2020-04-21 08:00:52 -04:00
|
|
|
use OCA\DAV\Upload\UploadFile;
|
2017-10-24 09:26:53 -04:00
|
|
|
use OCP\Files\Folder;
|
|
|
|
|
use OCP\IUser;
|
|
|
|
|
use OCP\IUserSession;
|
|
|
|
|
use OCP\Share\IManager;
|
2017-10-24 18:03:28 -04:00
|
|
|
use OCP\Share\IShare;
|
2025-05-27 17:36:08 -04:00
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
2017-10-24 09:26:53 -04:00
|
|
|
use Sabre\DAV\Tree;
|
|
|
|
|
|
2016-05-25 10:04:15 -04:00
|
|
|
class SharesPluginTest extends \Test\TestCase {
|
2024-10-10 06:40:31 -04:00
|
|
|
public const SHARETYPES_PROPERTYNAME = SharesPlugin::SHARETYPES_PROPERTYNAME;
|
2016-03-02 12:25:31 -05:00
|
|
|
|
2025-05-27 17:36:08 -04:00
|
|
|
private \Sabre\DAV\Server $server;
|
|
|
|
|
private \Sabre\DAV\Tree&MockObject $tree;
|
|
|
|
|
private \OCP\Share\IManager&MockObject $shareManager;
|
|
|
|
|
private SharesPlugin $plugin;
|
2016-03-02 12:25:31 -05:00
|
|
|
|
2019-11-27 09:27:18 -05:00
|
|
|
protected function setUp(): void {
|
2016-03-02 12:25:31 -05:00
|
|
|
parent::setUp();
|
|
|
|
|
$this->server = new \Sabre\DAV\Server();
|
2019-08-01 04:02:25 -04:00
|
|
|
$this->tree = $this->createMock(Tree::class);
|
|
|
|
|
$this->shareManager = $this->createMock(IManager::class);
|
|
|
|
|
$user = $this->createMock(IUser::class);
|
2016-03-02 12:25:31 -05:00
|
|
|
$user->expects($this->once())
|
|
|
|
|
->method('getUID')
|
2020-03-25 17:21:27 -04:00
|
|
|
->willReturn('user1');
|
2019-08-01 04:02:25 -04:00
|
|
|
$userSession = $this->createMock(IUserSession::class);
|
2016-03-02 12:25:31 -05:00
|
|
|
$userSession->expects($this->once())
|
|
|
|
|
->method('getUser')
|
2020-03-25 17:21:27 -04:00
|
|
|
->willReturn($user);
|
2016-03-02 12:25:31 -05:00
|
|
|
|
2024-10-10 06:40:31 -04:00
|
|
|
$this->plugin = new SharesPlugin(
|
2016-03-02 12:25:31 -05:00
|
|
|
$this->tree,
|
|
|
|
|
$userSession,
|
|
|
|
|
$this->shareManager
|
|
|
|
|
);
|
|
|
|
|
$this->plugin->initialize($this->server);
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-07 08:21:48 -05:00
|
|
|
#[\PHPUnit\Framework\Attributes\DataProvider(methodName: 'sharesGetPropertiesDataProvider')]
|
2025-05-27 17:36:08 -04:00
|
|
|
public function testGetProperties(array $shareTypes): void {
|
|
|
|
|
$sabreNode = $this->createMock(Node::class);
|
2016-03-02 12:25:31 -05:00
|
|
|
$sabreNode->expects($this->any())
|
|
|
|
|
->method('getId')
|
2020-03-25 17:21:27 -04:00
|
|
|
->willReturn(123);
|
2016-11-10 09:06:24 -05:00
|
|
|
$sabreNode->expects($this->any())
|
2016-03-02 12:25:31 -05:00
|
|
|
->method('getPath')
|
2020-03-25 17:21:27 -04:00
|
|
|
->willReturn('/subdir');
|
2016-03-02 12:25:31 -05:00
|
|
|
|
|
|
|
|
// node API nodes
|
2025-05-27 17:36:08 -04:00
|
|
|
$node = $this->createMock(Folder::class);
|
2016-03-02 12:25:31 -05:00
|
|
|
|
2022-04-05 09:30:10 -04:00
|
|
|
$sabreNode->method('getNode')
|
2020-03-25 17:21:27 -04:00
|
|
|
->willReturn($node);
|
2016-03-02 12:25:31 -05:00
|
|
|
|
|
|
|
|
$this->shareManager->expects($this->any())
|
|
|
|
|
->method('getSharesBy')
|
|
|
|
|
->with(
|
|
|
|
|
$this->equalTo('user1'),
|
|
|
|
|
$this->anything(),
|
2024-06-06 12:55:33 -04:00
|
|
|
$this->equalTo($node),
|
2016-03-02 12:25:31 -05:00
|
|
|
$this->equalTo(false),
|
2019-08-11 15:34:28 -04:00
|
|
|
$this->equalTo(-1)
|
2016-03-02 12:25:31 -05:00
|
|
|
)
|
2020-04-09 07:53:40 -04:00
|
|
|
->willReturnCallback(function ($userId, $requestedShareType, $node, $flag, $limit) use ($shareTypes) {
|
2016-03-02 12:25:31 -05:00
|
|
|
if (in_array($requestedShareType, $shareTypes)) {
|
2019-08-01 04:02:25 -04:00
|
|
|
$share = $this->createMock(IShare::class);
|
|
|
|
|
$share->method('getShareType')
|
|
|
|
|
->willReturn($requestedShareType);
|
|
|
|
|
return [$share];
|
2016-03-02 12:25:31 -05:00
|
|
|
}
|
|
|
|
|
return [];
|
2020-03-25 17:21:27 -04:00
|
|
|
});
|
2016-03-02 12:25:31 -05:00
|
|
|
|
2024-06-06 12:55:33 -04:00
|
|
|
$this->shareManager->expects($this->any())
|
|
|
|
|
->method('getSharedWith')
|
|
|
|
|
->with(
|
|
|
|
|
$this->equalTo('user1'),
|
|
|
|
|
$this->anything(),
|
|
|
|
|
$this->equalTo($node),
|
|
|
|
|
$this->equalTo(-1)
|
|
|
|
|
)
|
|
|
|
|
->willReturn([]);
|
|
|
|
|
|
2016-03-02 12:25:31 -05:00
|
|
|
$propFind = new \Sabre\DAV\PropFind(
|
|
|
|
|
'/dummyPath',
|
|
|
|
|
[self::SHARETYPES_PROPERTYNAME],
|
|
|
|
|
0
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$this->plugin->handleGetProperties(
|
|
|
|
|
$propFind,
|
|
|
|
|
$sabreNode
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$result = $propFind->getResultForMultiStatus();
|
|
|
|
|
|
|
|
|
|
$this->assertEmpty($result[404]);
|
|
|
|
|
unset($result[404]);
|
|
|
|
|
$this->assertEquals($shareTypes, $result[200][self::SHARETYPES_PROPERTYNAME]->getShareTypes());
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-07 08:21:48 -05:00
|
|
|
#[\PHPUnit\Framework\Attributes\DataProvider(methodName: 'sharesGetPropertiesDataProvider')]
|
2025-05-27 17:36:08 -04:00
|
|
|
public function testPreloadThenGetProperties(array $shareTypes): void {
|
2019-08-01 04:02:25 -04:00
|
|
|
$sabreNode1 = $this->createMock(File::class);
|
|
|
|
|
$sabreNode1->method('getId')
|
|
|
|
|
->willReturn(111);
|
|
|
|
|
$sabreNode2 = $this->createMock(File::class);
|
|
|
|
|
$sabreNode2->method('getId')
|
|
|
|
|
->willReturn(222);
|
|
|
|
|
$sabreNode2->method('getPath')
|
|
|
|
|
->willReturn('/subdir/foo');
|
|
|
|
|
|
|
|
|
|
$sabreNode = $this->createMock(Directory::class);
|
|
|
|
|
$sabreNode->method('getId')
|
|
|
|
|
->willReturn(123);
|
2016-03-02 12:25:31 -05:00
|
|
|
// never, because we use getDirectoryListing from the Node API instead
|
|
|
|
|
$sabreNode->expects($this->never())
|
|
|
|
|
->method('getChildren');
|
|
|
|
|
$sabreNode->expects($this->any())
|
|
|
|
|
->method('getPath')
|
2020-03-25 17:21:27 -04:00
|
|
|
->willReturn('/subdir');
|
2016-03-02 12:25:31 -05:00
|
|
|
|
|
|
|
|
// node API nodes
|
2019-08-01 04:02:25 -04:00
|
|
|
$node = $this->createMock(Folder::class);
|
|
|
|
|
$node->method('getId')
|
|
|
|
|
->willReturn(123);
|
2022-04-05 09:30:10 -04:00
|
|
|
$node1 = $this->createMock(\OC\Files\Node\File::class);
|
2019-08-01 04:02:25 -04:00
|
|
|
$node1->method('getId')
|
|
|
|
|
->willReturn(111);
|
2022-04-05 09:30:10 -04:00
|
|
|
$node2 = $this->createMock(\OC\Files\Node\File::class);
|
2019-08-01 04:02:25 -04:00
|
|
|
$node2->method('getId')
|
|
|
|
|
->willReturn(222);
|
|
|
|
|
|
2022-04-05 09:30:10 -04:00
|
|
|
$sabreNode->method('getNode')
|
2019-08-01 04:02:25 -04:00
|
|
|
->willReturn($node);
|
2022-04-05 09:30:10 -04:00
|
|
|
$sabreNode1->method('getNode')
|
|
|
|
|
->willReturn($node1);
|
|
|
|
|
$sabreNode2->method('getNode')
|
|
|
|
|
->willReturn($node2);
|
2020-04-21 08:00:52 -04:00
|
|
|
|
2020-04-09 07:53:40 -04:00
|
|
|
$dummyShares = array_map(function ($type) {
|
2025-05-27 17:36:08 -04:00
|
|
|
$share = $this->createMock(IShare::class);
|
2016-06-18 16:04:56 -04:00
|
|
|
$share->expects($this->any())
|
|
|
|
|
->method('getShareType')
|
2020-03-25 17:21:27 -04:00
|
|
|
->willReturn($type);
|
2016-06-18 16:04:56 -04:00
|
|
|
return $share;
|
|
|
|
|
}, $shareTypes);
|
2016-03-02 12:25:31 -05:00
|
|
|
|
|
|
|
|
$this->shareManager->expects($this->any())
|
|
|
|
|
->method('getSharesBy')
|
|
|
|
|
->with(
|
|
|
|
|
$this->equalTo('user1'),
|
|
|
|
|
$this->anything(),
|
|
|
|
|
$this->anything(),
|
|
|
|
|
$this->equalTo(false),
|
2019-08-11 15:34:28 -04:00
|
|
|
$this->equalTo(-1)
|
2016-03-02 12:25:31 -05:00
|
|
|
)
|
2020-04-09 07:53:40 -04:00
|
|
|
->willReturnCallback(function ($userId, $requestedShareType, $node, $flag, $limit) use ($shareTypes, $dummyShares) {
|
2016-03-02 12:25:31 -05:00
|
|
|
if ($node->getId() === 111 && in_array($requestedShareType, $shareTypes)) {
|
2019-08-01 04:02:25 -04:00
|
|
|
foreach ($dummyShares as $dummyShare) {
|
|
|
|
|
if ($dummyShare->getShareType() === $requestedShareType) {
|
|
|
|
|
return [$dummyShare];
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-03-02 12:25:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return [];
|
2020-03-25 17:21:27 -04:00
|
|
|
});
|
2016-03-02 12:25:31 -05:00
|
|
|
|
2016-06-18 16:04:56 -04:00
|
|
|
$this->shareManager->expects($this->any())
|
2024-06-06 12:55:33 -04:00
|
|
|
->method('getSharedWith')
|
|
|
|
|
->with(
|
|
|
|
|
$this->equalTo('user1'),
|
|
|
|
|
$this->anything(),
|
|
|
|
|
$this->equalTo($node),
|
|
|
|
|
$this->equalTo(-1)
|
|
|
|
|
)
|
|
|
|
|
->willReturn([]);
|
|
|
|
|
|
|
|
|
|
$this->shareManager->expects($this->any())
|
2016-06-18 16:04:56 -04:00
|
|
|
->method('getSharesInFolder')
|
|
|
|
|
->with(
|
|
|
|
|
$this->equalTo('user1'),
|
|
|
|
|
$this->anything(),
|
2017-03-14 11:28:45 -04:00
|
|
|
$this->equalTo(true)
|
2016-06-18 16:04:56 -04:00
|
|
|
)
|
2020-03-25 17:21:27 -04:00
|
|
|
->willReturnCallback(function ($userId, $node, $flag) use ($shareTypes, $dummyShares) {
|
2016-06-18 16:04:56 -04:00
|
|
|
return [111 => $dummyShares];
|
2020-03-25 17:21:27 -04:00
|
|
|
});
|
2016-06-18 16:04:56 -04:00
|
|
|
|
2016-03-02 12:25:31 -05:00
|
|
|
// simulate sabre recursive PROPFIND traversal
|
|
|
|
|
$propFindRoot = new \Sabre\DAV\PropFind(
|
|
|
|
|
'/subdir',
|
|
|
|
|
[self::SHARETYPES_PROPERTYNAME],
|
|
|
|
|
1
|
|
|
|
|
);
|
|
|
|
|
$propFind1 = new \Sabre\DAV\PropFind(
|
|
|
|
|
'/subdir/test.txt',
|
|
|
|
|
[self::SHARETYPES_PROPERTYNAME],
|
|
|
|
|
0
|
|
|
|
|
);
|
|
|
|
|
$propFind2 = new \Sabre\DAV\PropFind(
|
|
|
|
|
'/subdir/test2.txt',
|
|
|
|
|
[self::SHARETYPES_PROPERTYNAME],
|
|
|
|
|
0
|
|
|
|
|
);
|
|
|
|
|
|
2025-08-05 13:38:43 -04:00
|
|
|
$this->server->emit('preloadCollection', [$propFindRoot, $sabreNode]);
|
2016-03-02 12:25:31 -05:00
|
|
|
$this->plugin->handleGetProperties(
|
|
|
|
|
$propFindRoot,
|
|
|
|
|
$sabreNode
|
|
|
|
|
);
|
|
|
|
|
$this->plugin->handleGetProperties(
|
|
|
|
|
$propFind1,
|
|
|
|
|
$sabreNode1
|
|
|
|
|
);
|
|
|
|
|
$this->plugin->handleGetProperties(
|
|
|
|
|
$propFind2,
|
|
|
|
|
$sabreNode2
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$result = $propFind1->getResultForMultiStatus();
|
|
|
|
|
|
|
|
|
|
$this->assertEmpty($result[404]);
|
|
|
|
|
unset($result[404]);
|
|
|
|
|
$this->assertEquals($shareTypes, $result[200][self::SHARETYPES_PROPERTYNAME]->getShareTypes());
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-27 17:36:08 -04:00
|
|
|
public static function sharesGetPropertiesDataProvider(): array {
|
2016-03-02 12:25:31 -05:00
|
|
|
return [
|
|
|
|
|
[[]],
|
2020-06-24 10:49:16 -04:00
|
|
|
[[IShare::TYPE_USER]],
|
|
|
|
|
[[IShare::TYPE_GROUP]],
|
|
|
|
|
[[IShare::TYPE_LINK]],
|
|
|
|
|
[[IShare::TYPE_REMOTE]],
|
|
|
|
|
[[IShare::TYPE_ROOM]],
|
2020-12-08 06:56:54 -05:00
|
|
|
[[IShare::TYPE_DECK]],
|
2020-06-24 10:49:16 -04:00
|
|
|
[[IShare::TYPE_USER, IShare::TYPE_GROUP]],
|
|
|
|
|
[[IShare::TYPE_USER, IShare::TYPE_GROUP, IShare::TYPE_LINK]],
|
|
|
|
|
[[IShare::TYPE_USER, IShare::TYPE_LINK]],
|
|
|
|
|
[[IShare::TYPE_GROUP, IShare::TYPE_LINK]],
|
|
|
|
|
[[IShare::TYPE_USER, IShare::TYPE_REMOTE]],
|
2016-03-02 12:25:31 -05:00
|
|
|
];
|
|
|
|
|
}
|
2020-04-21 08:00:52 -04:00
|
|
|
|
|
|
|
|
public function testGetPropertiesSkipChunks(): void {
|
2025-05-27 17:36:08 -04:00
|
|
|
$sabreNode = $this->createMock(UploadFile::class);
|
2020-04-21 08:00:52 -04:00
|
|
|
|
|
|
|
|
$propFind = new \Sabre\DAV\PropFind(
|
|
|
|
|
'/dummyPath',
|
|
|
|
|
[self::SHARETYPES_PROPERTYNAME],
|
|
|
|
|
0
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$this->plugin->handleGetProperties(
|
|
|
|
|
$propFind,
|
|
|
|
|
$sabreNode
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$result = $propFind->getResultForMultiStatus();
|
|
|
|
|
$this->assertCount(1, $result[404]);
|
|
|
|
|
}
|
2016-03-02 12:25:31 -05:00
|
|
|
}
|