2016-03-02 20:25:31 +03:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-21 17:49:16 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Daniel Calviño Sánchez <danxuliu@gmail.com>
|
2016-07-21 17:49:16 +03:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Maxence Lange <maxence@nextcloud.com>
|
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
|
|
|
* @author Robin Appelman <robin@icewind.nl>
|
2016-07-21 17:49:16 +03:00
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2016-03-02 20:25:31 +03:00
|
|
|
* @author Vincent Petry <pvince81@owncloud.com>
|
|
|
|
*
|
|
|
|
* @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/>
|
2016-03-02 20:25:31 +03:00
|
|
|
*
|
|
|
|
*/
|
2019-11-22 22:52:10 +03:00
|
|
|
|
2016-05-25 17:04:15 +03:00
|
|
|
namespace OCA\DAV\Tests\unit\Connector\Sabre;
|
2016-03-02 20:25:31 +03:00
|
|
|
|
2017-10-24 16:26:53 +03:00
|
|
|
use OCA\DAV\Connector\Sabre\Directory;
|
|
|
|
use OCA\DAV\Connector\Sabre\File;
|
2017-10-25 01:03:28 +03:00
|
|
|
use OCA\DAV\Connector\Sabre\Node;
|
2017-10-24 16:26:53 +03:00
|
|
|
use OCP\Files\Folder;
|
|
|
|
use OCP\IUser;
|
|
|
|
use OCP\IUserSession;
|
|
|
|
use OCP\Share\IManager;
|
2017-10-25 01:03:28 +03:00
|
|
|
use OCP\Share\IShare;
|
2017-10-24 16:26:53 +03:00
|
|
|
use Sabre\DAV\Tree;
|
|
|
|
|
2016-05-25 17:04:15 +03:00
|
|
|
class SharesPluginTest extends \Test\TestCase {
|
2016-03-02 20:25:31 +03:00
|
|
|
|
|
|
|
const SHARETYPES_PROPERTYNAME = \OCA\DAV\Connector\Sabre\SharesPlugin::SHARETYPES_PROPERTYNAME;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Sabre\DAV\Server
|
|
|
|
*/
|
|
|
|
private $server;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Sabre\DAV\Tree
|
|
|
|
*/
|
|
|
|
private $tree;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \OCP\Share\IManager
|
|
|
|
*/
|
|
|
|
private $shareManager;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \OCP\Files\Folder
|
|
|
|
*/
|
|
|
|
private $userFolder;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \OCA\DAV\Connector\Sabre\SharesPlugin
|
|
|
|
*/
|
|
|
|
private $plugin;
|
|
|
|
|
2019-11-27 17:27:18 +03:00
|
|
|
protected function setUp(): void {
|
2016-03-02 20:25:31 +03:00
|
|
|
parent::setUp();
|
|
|
|
$this->server = new \Sabre\DAV\Server();
|
2019-08-01 11:02:25 +03:00
|
|
|
$this->tree = $this->createMock(Tree::class);
|
|
|
|
$this->shareManager = $this->createMock(IManager::class);
|
|
|
|
$user = $this->createMock(IUser::class);
|
2016-03-02 20:25:31 +03:00
|
|
|
$user->expects($this->once())
|
|
|
|
->method('getUID')
|
|
|
|
->will($this->returnValue('user1'));
|
2019-08-01 11:02:25 +03:00
|
|
|
$userSession = $this->createMock(IUserSession::class);
|
2016-03-02 20:25:31 +03:00
|
|
|
$userSession->expects($this->once())
|
|
|
|
->method('getUser')
|
|
|
|
->will($this->returnValue($user));
|
2019-08-01 11:02:25 +03:00
|
|
|
$this->userFolder = $this->createMock(Folder::class);
|
2016-03-02 20:25:31 +03:00
|
|
|
|
|
|
|
$this->plugin = new \OCA\DAV\Connector\Sabre\SharesPlugin(
|
|
|
|
$this->tree,
|
|
|
|
$userSession,
|
|
|
|
$this->userFolder,
|
|
|
|
$this->shareManager
|
|
|
|
);
|
|
|
|
$this->plugin->initialize($this->server);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider sharesGetPropertiesDataProvider
|
|
|
|
*/
|
|
|
|
public function testGetProperties($shareTypes) {
|
2017-10-25 01:03:28 +03:00
|
|
|
$sabreNode = $this->getMockBuilder(Node::class)
|
2016-03-02 20:25:31 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$sabreNode->expects($this->any())
|
|
|
|
->method('getId')
|
|
|
|
->will($this->returnValue(123));
|
2016-11-10 17:06:24 +03:00
|
|
|
$sabreNode->expects($this->any())
|
2016-03-02 20:25:31 +03:00
|
|
|
->method('getPath')
|
|
|
|
->will($this->returnValue('/subdir'));
|
|
|
|
|
|
|
|
// node API nodes
|
2017-10-24 16:26:53 +03:00
|
|
|
$node = $this->getMockBuilder(Folder::class)
|
2016-07-15 10:52:46 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2016-03-02 20:25:31 +03:00
|
|
|
|
|
|
|
$this->userFolder->expects($this->once())
|
|
|
|
->method('get')
|
|
|
|
->with('/subdir')
|
|
|
|
->will($this->returnValue($node));
|
|
|
|
|
|
|
|
$this->shareManager->expects($this->any())
|
|
|
|
->method('getSharesBy')
|
|
|
|
->with(
|
|
|
|
$this->equalTo('user1'),
|
|
|
|
$this->anything(),
|
|
|
|
$this->anything(),
|
|
|
|
$this->equalTo(false),
|
2019-08-11 22:34:28 +03:00
|
|
|
$this->equalTo(-1)
|
2016-03-02 20:25:31 +03:00
|
|
|
)
|
|
|
|
->will($this->returnCallback(function($userId, $requestedShareType, $node, $flag, $limit) use ($shareTypes){
|
|
|
|
if (in_array($requestedShareType, $shareTypes)) {
|
2019-08-01 11:02:25 +03:00
|
|
|
$share = $this->createMock(IShare::class);
|
|
|
|
$share->method('getShareType')
|
|
|
|
->willReturn($requestedShareType);
|
|
|
|
return [$share];
|
2016-03-02 20:25:31 +03:00
|
|
|
}
|
|
|
|
return [];
|
|
|
|
}));
|
|
|
|
|
|
|
|
$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());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider sharesGetPropertiesDataProvider
|
|
|
|
*/
|
|
|
|
public function testPreloadThenGetProperties($shareTypes) {
|
2019-08-01 11:02:25 +03: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 20:25:31 +03:00
|
|
|
// never, because we use getDirectoryListing from the Node API instead
|
|
|
|
$sabreNode->expects($this->never())
|
|
|
|
->method('getChildren');
|
|
|
|
$sabreNode->expects($this->any())
|
|
|
|
->method('getPath')
|
|
|
|
->will($this->returnValue('/subdir'));
|
|
|
|
|
|
|
|
// node API nodes
|
2019-08-01 11:02:25 +03:00
|
|
|
$node = $this->createMock(Folder::class);
|
|
|
|
$node->method('getId')
|
|
|
|
->willReturn(123);
|
|
|
|
$node1 = $this->createMock(File::class);
|
|
|
|
$node1->method('getId')
|
|
|
|
->willReturn(111);
|
|
|
|
$node2 = $this->createMock(File::class);
|
|
|
|
$node2->method('getId')
|
|
|
|
->willReturn(222);
|
|
|
|
|
|
|
|
$this->userFolder->method('get')
|
2016-03-02 20:25:31 +03:00
|
|
|
->with('/subdir')
|
2019-08-01 11:02:25 +03:00
|
|
|
->willReturn($node);
|
2016-06-18 23:04:56 +03:00
|
|
|
|
|
|
|
$dummyShares = array_map(function($type) {
|
2017-10-25 01:03:28 +03:00
|
|
|
$share = $this->getMockBuilder(IShare::class)->getMock();
|
2016-06-18 23:04:56 +03:00
|
|
|
$share->expects($this->any())
|
|
|
|
->method('getShareType')
|
|
|
|
->will($this->returnValue($type));
|
|
|
|
return $share;
|
|
|
|
}, $shareTypes);
|
2016-03-02 20:25:31 +03:00
|
|
|
|
|
|
|
$this->shareManager->expects($this->any())
|
|
|
|
->method('getSharesBy')
|
|
|
|
->with(
|
|
|
|
$this->equalTo('user1'),
|
|
|
|
$this->anything(),
|
|
|
|
$this->anything(),
|
|
|
|
$this->equalTo(false),
|
2019-08-11 22:34:28 +03:00
|
|
|
$this->equalTo(-1)
|
2016-03-02 20:25:31 +03:00
|
|
|
)
|
2019-08-01 11:02:25 +03:00
|
|
|
->will($this->returnCallback(function($userId, $requestedShareType, $node, $flag, $limit) use ($shareTypes, $dummyShares){
|
2016-03-02 20:25:31 +03:00
|
|
|
if ($node->getId() === 111 && in_array($requestedShareType, $shareTypes)) {
|
2019-08-01 11:02:25 +03:00
|
|
|
foreach ($dummyShares as $dummyShare) {
|
|
|
|
if ($dummyShare->getShareType() === $requestedShareType) {
|
|
|
|
return [$dummyShare];
|
|
|
|
}
|
|
|
|
}
|
2016-03-02 20:25:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return [];
|
|
|
|
}));
|
|
|
|
|
2016-06-18 23:04:56 +03:00
|
|
|
$this->shareManager->expects($this->any())
|
|
|
|
->method('getSharesInFolder')
|
|
|
|
->with(
|
|
|
|
$this->equalTo('user1'),
|
|
|
|
$this->anything(),
|
2017-03-14 18:28:45 +03:00
|
|
|
$this->equalTo(true)
|
2016-06-18 23:04:56 +03:00
|
|
|
)
|
|
|
|
->will($this->returnCallback(function ($userId, $node, $flag) use ($shareTypes, $dummyShares) {
|
|
|
|
return [111 => $dummyShares];
|
|
|
|
}));
|
|
|
|
|
2016-03-02 20:25:31 +03: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
|
|
|
|
);
|
|
|
|
|
|
|
|
$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());
|
|
|
|
}
|
|
|
|
|
|
|
|
function sharesGetPropertiesDataProvider() {
|
|
|
|
return [
|
|
|
|
[[]],
|
|
|
|
[[\OCP\Share::SHARE_TYPE_USER]],
|
|
|
|
[[\OCP\Share::SHARE_TYPE_GROUP]],
|
|
|
|
[[\OCP\Share::SHARE_TYPE_LINK]],
|
2016-03-24 14:16:57 +03:00
|
|
|
[[\OCP\Share::SHARE_TYPE_REMOTE]],
|
2018-06-30 14:45:23 +03:00
|
|
|
[[\OCP\Share::SHARE_TYPE_ROOM]],
|
2016-03-02 20:25:31 +03:00
|
|
|
[[\OCP\Share::SHARE_TYPE_USER, \OCP\Share::SHARE_TYPE_GROUP]],
|
|
|
|
[[\OCP\Share::SHARE_TYPE_USER, \OCP\Share::SHARE_TYPE_GROUP, \OCP\Share::SHARE_TYPE_LINK]],
|
|
|
|
[[\OCP\Share::SHARE_TYPE_USER, \OCP\Share::SHARE_TYPE_LINK]],
|
|
|
|
[[\OCP\Share::SHARE_TYPE_GROUP, \OCP\Share::SHARE_TYPE_LINK]],
|
2016-03-24 14:16:57 +03:00
|
|
|
[[\OCP\Share::SHARE_TYPE_USER, \OCP\Share::SHARE_TYPE_REMOTE]],
|
2016-03-02 20:25:31 +03:00
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|