2012-07-25 21:08:18 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* ownCloud
|
|
|
|
*
|
|
|
|
* @author Michael Gapczynski
|
|
|
|
* @copyright 2012 Michael Gapczynski mtgap@owncloud.com
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 3 of the License, or any later version.
|
|
|
|
*
|
|
|
|
* This library 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 along with this library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2016-05-19 07:15:10 +00:00
|
|
|
namespace Test\Share;
|
2016-11-02 22:26:49 +00:00
|
|
|
use OC\Share\Share;
|
2016-11-04 12:07:16 +00:00
|
|
|
use OCP\IGroup;
|
|
|
|
use OCP\IGroupManager;
|
2016-11-02 22:26:49 +00:00
|
|
|
use OCP\ILogger;
|
2016-11-04 12:07:16 +00:00
|
|
|
use OCP\IUser;
|
2016-11-02 22:26:49 +00:00
|
|
|
use OCP\IUserManager;
|
2016-05-19 07:15:10 +00:00
|
|
|
|
2015-11-03 00:52:41 +00:00
|
|
|
/**
|
|
|
|
* Class Test_Share
|
|
|
|
*
|
|
|
|
* @group DB
|
|
|
|
*/
|
2016-05-20 13:38:20 +00:00
|
|
|
class ShareTest extends \Test\TestCase {
|
2012-07-25 21:08:18 +00:00
|
|
|
|
|
|
|
protected $itemType;
|
2016-11-04 12:07:16 +00:00
|
|
|
|
|
|
|
/** @var IUser */
|
2012-07-25 21:08:18 +00:00
|
|
|
protected $user1;
|
2016-11-04 12:07:16 +00:00
|
|
|
/** @var IUser */
|
2012-07-25 21:08:18 +00:00
|
|
|
protected $user2;
|
2016-11-04 12:07:16 +00:00
|
|
|
/** @var IUser */
|
2014-01-21 11:07:08 +00:00
|
|
|
protected $user3;
|
2016-11-04 12:07:16 +00:00
|
|
|
/** @var IUser */
|
2014-01-21 11:07:08 +00:00
|
|
|
protected $user4;
|
2016-11-04 12:07:16 +00:00
|
|
|
/** @var IUser */
|
2015-01-19 13:39:00 +00:00
|
|
|
protected $user5;
|
2016-11-04 12:07:16 +00:00
|
|
|
/** @var IUser */
|
2015-01-19 13:39:00 +00:00
|
|
|
protected $user6;
|
2016-11-04 12:07:16 +00:00
|
|
|
/** @var IUser */
|
|
|
|
protected $groupAndUser_user;
|
|
|
|
|
|
|
|
/** @var IGroup */
|
2012-07-25 21:08:18 +00:00
|
|
|
protected $group1;
|
2016-11-04 12:07:16 +00:00
|
|
|
/** @var IGroup */
|
2012-07-25 21:08:18 +00:00
|
|
|
protected $group2;
|
2016-11-04 12:07:16 +00:00
|
|
|
/** @var IGroup */
|
|
|
|
protected $groupAndUser_group;
|
|
|
|
|
2013-02-26 18:31:15 +00:00
|
|
|
protected $resharing;
|
2013-09-12 21:37:43 +00:00
|
|
|
protected $dateInFuture;
|
|
|
|
protected $dateInPast;
|
2013-09-05 00:27:29 +00:00
|
|
|
|
2016-11-04 12:07:16 +00:00
|
|
|
/** @var IGroupManager */
|
|
|
|
protected $groupManager;
|
|
|
|
/** @var IUserManager */
|
|
|
|
protected $userManager;
|
|
|
|
|
2014-11-18 10:08:01 +00:00
|
|
|
protected function setUp() {
|
|
|
|
parent::setUp();
|
2014-11-10 22:30:38 +00:00
|
|
|
|
2016-11-04 12:07:16 +00:00
|
|
|
$this->groupManager = \OC::$server->getGroupManager();
|
|
|
|
$this->userManager = \OC::$server->getUserManager();
|
|
|
|
|
|
|
|
$this->userManager->clearBackends();
|
|
|
|
$this->userManager->registerBackend(new \Test\Util\User\Dummy());
|
|
|
|
|
|
|
|
$this->user1 = $this->userManager->createUser($this->getUniqueID('user1_'), 'pass');
|
|
|
|
$this->user2 = $this->userManager->createUser($this->getUniqueID('user2_'), 'pass');
|
|
|
|
$this->user3 = $this->userManager->createUser($this->getUniqueID('user3_'), 'pass');
|
|
|
|
$this->user4 = $this->userManager->createUser($this->getUniqueID('user4_'), 'pass');
|
|
|
|
$this->user5 = $this->userManager->createUser($this->getUniqueID('user5_'), 'pass');
|
|
|
|
$this->user6 = $this->userManager->createUser($this->getUniqueID('user6_'), 'pass');
|
|
|
|
$groupAndUserId = $this->getUniqueID('groupAndUser_');
|
|
|
|
$this->groupAndUser_user = $this->userManager->createUser($groupAndUserId, 'pass');
|
|
|
|
\OC_User::setUserId($this->user1->getUID());
|
|
|
|
|
|
|
|
$this->groupManager->clearBackends();
|
|
|
|
$this->groupManager->addBackend(new \Test\Util\Group\Dummy());
|
|
|
|
$this->group1 = $this->groupManager->createGroup($this->getUniqueID('group1_'));
|
|
|
|
$this->group2 = $this->groupManager->createGroup($this->getUniqueID('group2_'));
|
|
|
|
$this->groupAndUser_group = $this->groupManager->createGroup($groupAndUserId);
|
|
|
|
|
|
|
|
$this->group1->addUser($this->user1);
|
|
|
|
$this->group1->addUser($this->user2);
|
|
|
|
$this->group1->addUser($this->user3);
|
|
|
|
$this->group2->addUser($this->user2);
|
|
|
|
$this->group2->addUser($this->user4);
|
|
|
|
$this->groupAndUser_group->addUser($this->user2);
|
|
|
|
$this->groupAndUser_group->addUser($this->user3);
|
|
|
|
|
2017-08-15 08:34:27 +00:00
|
|
|
\OC\Share\Share::registerBackend('test', 'Test\Share\Backend');
|
2016-05-19 07:15:10 +00:00
|
|
|
\OC_Hook::clear('OCP\\Share');
|
|
|
|
\OC::registerShareHooks();
|
2018-01-17 20:10:40 +00:00
|
|
|
$this->resharing = \OC::$server->getConfig()->getAppValue('core', 'shareapi_allow_resharing', 'yes');
|
2015-07-03 12:16:29 +00:00
|
|
|
\OC::$server->getAppConfig()->setValue('core', 'shareapi_allow_resharing', 'yes');
|
2013-09-12 21:37:43 +00:00
|
|
|
|
|
|
|
// 20 Minutes in the past, 20 minutes in the future.
|
|
|
|
$now = time();
|
|
|
|
$dateFormat = 'Y-m-d H:i:s';
|
|
|
|
$this->dateInPast = date($dateFormat, $now - 20 * 60);
|
|
|
|
$this->dateInFuture = date($dateFormat, $now + 20 * 60);
|
2012-07-25 21:08:18 +00:00
|
|
|
}
|
|
|
|
|
2014-11-18 10:08:01 +00:00
|
|
|
protected function tearDown() {
|
2016-05-19 08:11:42 +00:00
|
|
|
$query = \OC_DB::prepare('DELETE FROM `*PREFIX*share` WHERE `item_type` = ?');
|
2012-08-15 15:55:54 +00:00
|
|
|
$query->execute(array('test'));
|
2015-07-03 12:16:29 +00:00
|
|
|
\OC::$server->getAppConfig()->setValue('core', 'shareapi_allow_resharing', $this->resharing);
|
2014-11-10 22:30:38 +00:00
|
|
|
|
2016-11-04 12:07:16 +00:00
|
|
|
$this->user1->delete();
|
|
|
|
$this->user2->delete();
|
|
|
|
$this->user3->delete();
|
|
|
|
$this->user4->delete();
|
|
|
|
$this->user5->delete();
|
|
|
|
$this->user6->delete();
|
|
|
|
$this->groupAndUser_user->delete();
|
2015-01-19 13:39:00 +00:00
|
|
|
|
2016-11-04 12:07:16 +00:00
|
|
|
$this->group1->delete();
|
|
|
|
$this->group2->delete();
|
|
|
|
$this->groupAndUser_group->delete();
|
2015-01-19 13:39:00 +00:00
|
|
|
|
2015-03-27 14:54:29 +00:00
|
|
|
$this->logout();
|
2014-11-18 10:08:01 +00:00
|
|
|
parent::tearDown();
|
2012-07-25 21:08:18 +00:00
|
|
|
}
|
|
|
|
|
2012-08-15 15:55:54 +00:00
|
|
|
public function testShareInvalidShareType() {
|
2012-10-12 17:28:24 +00:00
|
|
|
$message = 'Share type foobar is not valid for test.txt';
|
|
|
|
try {
|
2017-08-15 09:57:18 +00:00
|
|
|
\OC\Share\Share::shareItem('test', 'test.txt', 'foobar', $this->user2, \OCP\Constants::PERMISSION_READ);
|
2016-05-19 07:15:10 +00:00
|
|
|
} catch (\Exception $exception) {
|
2012-10-12 18:05:45 +00:00
|
|
|
$this->assertEquals($message, $exception->getMessage());
|
2012-10-12 17:28:24 +00:00
|
|
|
}
|
2012-07-25 21:08:18 +00:00
|
|
|
}
|
|
|
|
|
2015-03-24 10:08:19 +00:00
|
|
|
public function testGetShareFromOutsideFilesFolder() {
|
2016-11-04 12:07:16 +00:00
|
|
|
\OC_User::setUserId($this->user1->getUID());
|
|
|
|
$view = new \OC\Files\View('/' . $this->user1->getUID() . '/');
|
2015-03-24 10:08:19 +00:00
|
|
|
$view->mkdir('files/test');
|
|
|
|
$view->mkdir('files/test/sub');
|
|
|
|
|
|
|
|
$view->mkdir('files_trashbin');
|
|
|
|
$view->mkdir('files_trashbin/files');
|
|
|
|
|
|
|
|
$fileInfo = $view->getFileInfo('files/test/sub');
|
2015-09-28 14:38:01 +00:00
|
|
|
$this->assertInstanceOf('\OC\Files\FileInfo', $fileInfo);
|
2015-03-24 10:08:19 +00:00
|
|
|
$fileId = $fileInfo->getId();
|
|
|
|
|
|
|
|
$this->assertTrue(
|
2017-08-15 09:57:18 +00:00
|
|
|
\OC\Share\Share::shareItem('folder', $fileId, \OCP\Share::SHARE_TYPE_USER, $this->user2->getUID(), \OCP\Constants::PERMISSION_READ),
|
2015-03-24 10:08:19 +00:00
|
|
|
'Failed asserting that user 1 successfully shared "test/sub" with user 2.'
|
|
|
|
);
|
|
|
|
|
2016-05-19 07:15:10 +00:00
|
|
|
$result = \OCP\Share::getItemShared('folder', $fileId, Backend::FORMAT_SOURCE);
|
2015-03-24 10:08:19 +00:00
|
|
|
$this->assertNotEmpty($result);
|
|
|
|
|
2017-08-15 12:29:26 +00:00
|
|
|
$result = \OC\Share\Share::getItemSharedWithUser('folder', $fileId, $this->user2->getUID());
|
2015-03-24 10:08:19 +00:00
|
|
|
$this->assertNotEmpty($result);
|
|
|
|
|
2017-08-15 12:29:26 +00:00
|
|
|
$result = \OC\Share\Share::getItemsSharedWithUser('folder', $this->user2->getUID());
|
2015-03-24 10:08:19 +00:00
|
|
|
$this->assertNotEmpty($result);
|
|
|
|
|
|
|
|
// move to trash (keeps file id)
|
|
|
|
$view->rename('files/test', 'files_trashbin/files/test');
|
|
|
|
|
2016-05-19 07:15:10 +00:00
|
|
|
$result = \OCP\Share::getItemShared('folder', $fileId, Backend::FORMAT_SOURCE);
|
2015-03-24 10:08:19 +00:00
|
|
|
$this->assertEmpty($result, 'Share must not be returned for files outside of "files"');
|
|
|
|
|
2017-08-15 12:29:26 +00:00
|
|
|
$result = \OC\Share\Share::getItemSharedWithUser('folder', $fileId, $this->user2->getUID());
|
2015-03-24 10:08:19 +00:00
|
|
|
$this->assertEmpty($result, 'Share must not be returned for files outside of "files"');
|
|
|
|
|
2017-08-15 12:29:26 +00:00
|
|
|
$result = \OC\Share\Share::getItemsSharedWithUser('folder', $this->user2->getUID());
|
2015-03-24 10:08:19 +00:00
|
|
|
$this->assertEmpty($result, 'Share must not be returned for files outside of "files"');
|
|
|
|
}
|
|
|
|
|
2015-04-29 11:33:20 +00:00
|
|
|
public function testSharingAFolderThatIsSharedWithAGroupOfTheOwner() {
|
2016-11-04 12:07:16 +00:00
|
|
|
\OC_User::setUserId($this->user1->getUID());
|
|
|
|
$view = new \OC\Files\View('/' . $this->user1->getUID() . '/');
|
2015-04-29 11:33:20 +00:00
|
|
|
$view->mkdir('files/test');
|
|
|
|
$view->mkdir('files/test/sub1');
|
|
|
|
$view->mkdir('files/test/sub1/sub2');
|
|
|
|
|
|
|
|
$fileInfo = $view->getFileInfo('files/test/sub1');
|
2015-09-28 14:38:01 +00:00
|
|
|
$this->assertInstanceOf('\OC\Files\FileInfo', $fileInfo);
|
2015-04-29 11:33:20 +00:00
|
|
|
$fileId = $fileInfo->getId();
|
|
|
|
|
|
|
|
$this->assertTrue(
|
2017-08-15 09:57:18 +00:00
|
|
|
\OC\Share\Share::shareItem('folder', $fileId, \OCP\Share::SHARE_TYPE_GROUP, $this->group1->getGID(), \OCP\Constants::PERMISSION_READ + \OCP\Constants::PERMISSION_CREATE),
|
2015-04-29 11:33:20 +00:00
|
|
|
'Failed asserting that user 1 successfully shared "test/sub1" with group 1.'
|
|
|
|
);
|
|
|
|
|
2016-05-19 07:15:10 +00:00
|
|
|
$result = \OCP\Share::getItemShared('folder', $fileId, Backend::FORMAT_SOURCE);
|
2015-04-29 11:33:20 +00:00
|
|
|
$this->assertNotEmpty($result);
|
|
|
|
$this->assertEquals(\OCP\Constants::PERMISSION_READ + \OCP\Constants::PERMISSION_CREATE, $result['permissions']);
|
|
|
|
|
|
|
|
$fileInfo = $view->getFileInfo('files/test/sub1/sub2');
|
2015-09-28 14:38:01 +00:00
|
|
|
$this->assertInstanceOf('\OC\Files\FileInfo', $fileInfo);
|
2015-04-29 11:33:20 +00:00
|
|
|
$fileId = $fileInfo->getId();
|
|
|
|
|
|
|
|
$this->assertTrue(
|
2017-08-15 09:57:18 +00:00
|
|
|
\OC\Share\Share::shareItem('folder', $fileId, \OCP\Share::SHARE_TYPE_USER, $this->user4->getUID(), \OCP\Constants::PERMISSION_READ),
|
2015-04-29 11:33:20 +00:00
|
|
|
'Failed asserting that user 1 successfully shared "test/sub1/sub2" with user 4.'
|
|
|
|
);
|
|
|
|
|
2016-05-19 07:15:10 +00:00
|
|
|
$result = \OCP\Share::getItemShared('folder', $fileId, Backend::FORMAT_SOURCE);
|
2015-04-29 11:33:20 +00:00
|
|
|
$this->assertNotEmpty($result);
|
|
|
|
$this->assertEquals(\OCP\Constants::PERMISSION_READ, $result['permissions']);
|
|
|
|
}
|
|
|
|
|
2015-10-05 10:36:16 +00:00
|
|
|
public function testSharingAFileInsideAFolderThatIsAlreadyShared() {
|
2016-11-04 12:07:16 +00:00
|
|
|
\OC_User::setUserId($this->user1->getUID());
|
|
|
|
$view = new \OC\Files\View('/' . $this->user1->getUID() . '/');
|
2015-10-05 10:36:16 +00:00
|
|
|
$view->mkdir('files/test');
|
|
|
|
$view->mkdir('files/test/sub1');
|
|
|
|
$view->file_put_contents('files/test/sub1/file.txt', 'abc');
|
|
|
|
|
|
|
|
$folderInfo = $view->getFileInfo('files/test/sub1');
|
2015-09-28 14:38:01 +00:00
|
|
|
$this->assertInstanceOf('\OC\Files\FileInfo', $folderInfo);
|
2015-10-05 10:36:16 +00:00
|
|
|
$folderId = $folderInfo->getId();
|
|
|
|
|
|
|
|
$fileInfo = $view->getFileInfo('files/test/sub1/file.txt');
|
2015-09-28 14:38:01 +00:00
|
|
|
$this->assertInstanceOf('\OC\Files\FileInfo', $fileInfo);
|
2015-10-05 10:36:16 +00:00
|
|
|
$fileId = $fileInfo->getId();
|
|
|
|
|
|
|
|
$this->assertTrue(
|
2017-08-15 09:57:18 +00:00
|
|
|
\OC\Share\Share::shareItem('folder', $folderId, \OCP\Share::SHARE_TYPE_GROUP, $this->group2->getGID(), \OCP\Constants::PERMISSION_READ + \OCP\Constants::PERMISSION_UPDATE),
|
2015-10-05 10:36:16 +00:00
|
|
|
'Failed asserting that user 1 successfully shared "test/sub1" with group 2.'
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertTrue(
|
2017-08-15 09:57:18 +00:00
|
|
|
\OC\Share\Share::shareItem('file', $fileId, \OCP\Share::SHARE_TYPE_USER, $this->user2->getUID(), \OCP\Constants::PERMISSION_READ),
|
2015-10-05 10:36:16 +00:00
|
|
|
'Failed asserting that user 1 successfully shared "test/sub1/file.txt" with user 2.'
|
|
|
|
);
|
|
|
|
|
2017-08-15 12:29:26 +00:00
|
|
|
$result = \OC\Share\Share::getItemsSharedWithUser('file', $this->user2->getUID());
|
2015-10-05 10:36:16 +00:00
|
|
|
$this->assertCount(2, $result);
|
|
|
|
|
|
|
|
foreach ($result as $share) {
|
|
|
|
$itemName = substr($share['path'], strrpos($share['path'], '/'));
|
|
|
|
$this->assertSame($itemName, $share['file_target'], 'Asserting that the file_target is the last segment of the path');
|
|
|
|
$this->assertSame($share['item_target'], '/' . $share['item_source'], 'Asserting that the item is the item that was shared');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-20 12:09:39 +00:00
|
|
|
/**
|
|
|
|
* Test that unsharing from group will also delete all
|
|
|
|
* child entries
|
|
|
|
*/
|
|
|
|
public function testShareWithGroupThenUnshare() {
|
2016-11-04 12:07:16 +00:00
|
|
|
\OC_User::setUserId($this->user5->getUID());
|
2017-08-15 09:57:18 +00:00
|
|
|
\OC\Share\Share::shareItem(
|
2015-01-20 12:09:39 +00:00
|
|
|
'test',
|
|
|
|
'test.txt',
|
2016-05-19 07:15:10 +00:00
|
|
|
\OCP\Share::SHARE_TYPE_GROUP,
|
2016-11-04 12:07:16 +00:00
|
|
|
$this->group1->getGID(),
|
2015-01-20 12:09:39 +00:00
|
|
|
\OCP\Constants::PERMISSION_ALL
|
|
|
|
);
|
|
|
|
|
2016-11-04 12:07:16 +00:00
|
|
|
$targetUsers = array($this->user1->getUID(), $this->user2->getUID(), $this->user3->getUID());
|
2015-01-20 12:09:39 +00:00
|
|
|
|
|
|
|
foreach($targetUsers as $targetUser) {
|
2016-05-19 07:15:10 +00:00
|
|
|
\OC_User::setUserId($targetUser);
|
2017-08-15 12:29:26 +00:00
|
|
|
$items = \OC\Share\Share::getItemsSharedWithUser(
|
2015-01-20 12:09:39 +00:00
|
|
|
'test',
|
|
|
|
$targetUser,
|
2016-05-19 07:15:10 +00:00
|
|
|
Backend::FORMAT_TARGET
|
2015-01-20 12:09:39 +00:00
|
|
|
);
|
|
|
|
$this->assertEquals(1, count($items));
|
|
|
|
}
|
|
|
|
|
2016-11-04 12:07:16 +00:00
|
|
|
\OC_User::setUserId($this->user5->getUID());
|
2017-08-15 12:29:26 +00:00
|
|
|
\OC\Share\Share::unshare(
|
2015-01-20 12:09:39 +00:00
|
|
|
'test',
|
|
|
|
'test.txt',
|
2016-05-19 07:15:10 +00:00
|
|
|
\OCP\Share::SHARE_TYPE_GROUP,
|
2016-11-04 12:07:16 +00:00
|
|
|
$this->group1->getGID()
|
2015-01-20 12:09:39 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// verify that all were deleted
|
|
|
|
foreach($targetUsers as $targetUser) {
|
2016-05-19 07:15:10 +00:00
|
|
|
\OC_User::setUserId($targetUser);
|
2017-08-15 12:29:26 +00:00
|
|
|
$items = \OC\Share\Share::getItemsSharedWithUser(
|
2015-01-20 12:09:39 +00:00
|
|
|
'test',
|
|
|
|
$targetUser,
|
2016-05-19 07:15:10 +00:00
|
|
|
Backend::FORMAT_TARGET
|
2015-01-20 12:09:39 +00:00
|
|
|
);
|
|
|
|
$this->assertEquals(0, count($items));
|
|
|
|
}
|
|
|
|
}
|
2014-11-17 12:09:13 +00:00
|
|
|
|
2014-02-19 08:31:54 +00:00
|
|
|
/**
|
|
|
|
* @param boolean|string $token
|
2015-04-29 11:33:20 +00:00
|
|
|
* @return array
|
2014-02-19 08:31:54 +00:00
|
|
|
*/
|
2013-09-14 15:56:55 +00:00
|
|
|
protected function getShareByValidToken($token) {
|
2016-05-19 07:15:10 +00:00
|
|
|
$row = \OCP\Share::getShareByToken($token);
|
2013-09-14 15:56:55 +00:00
|
|
|
$this->assertInternalType(
|
|
|
|
'array',
|
|
|
|
$row,
|
|
|
|
"Failed asserting that a share for token $token exists."
|
|
|
|
);
|
|
|
|
return $row;
|
|
|
|
}
|
|
|
|
|
2014-11-10 12:08:45 +00:00
|
|
|
public function testGetItemSharedWithUser() {
|
2016-11-04 12:07:16 +00:00
|
|
|
\OC_User::setUserId($this->user1->getUID());
|
2014-11-10 12:08:45 +00:00
|
|
|
|
|
|
|
//add dummy values to the share table
|
|
|
|
$query = \OC_DB::prepare('INSERT INTO `*PREFIX*share` ('
|
|
|
|
.' `item_type`, `item_source`, `item_target`, `share_type`,'
|
|
|
|
.' `share_with`, `uid_owner`) VALUES (?,?,?,?,?,?)');
|
2016-11-04 12:07:16 +00:00
|
|
|
$args = array('test', 99, 'target1', \OCP\Share::SHARE_TYPE_USER, $this->user2->getUID(), $this->user1->getUID());
|
2014-11-10 12:08:45 +00:00
|
|
|
$query->execute($args);
|
2016-11-04 12:07:16 +00:00
|
|
|
$args = array('test', 99, 'target2', \OCP\Share::SHARE_TYPE_USER, $this->user4->getUID(), $this->user1->getUID());
|
2014-11-10 12:08:45 +00:00
|
|
|
$query->execute($args);
|
2016-11-04 12:07:16 +00:00
|
|
|
$args = array('test', 99, 'target3', \OCP\Share::SHARE_TYPE_USER, $this->user3->getUID(), $this->user2->getUID());
|
2014-11-10 12:08:45 +00:00
|
|
|
$query->execute($args);
|
2016-11-04 12:07:16 +00:00
|
|
|
$args = array('test', 99, 'target4', \OCP\Share::SHARE_TYPE_USER, $this->user3->getUID(), $this->user4->getUID());
|
2014-11-10 12:08:45 +00:00
|
|
|
$query->execute($args);
|
2016-11-04 12:07:16 +00:00
|
|
|
$args = array('test', 99, 'target4', \OCP\Share::SHARE_TYPE_USER, $this->user6->getUID(), $this->user4->getUID());
|
2015-01-19 13:39:00 +00:00
|
|
|
$query->execute($args);
|
2014-11-10 12:08:45 +00:00
|
|
|
|
|
|
|
|
2016-11-04 12:07:16 +00:00
|
|
|
$result1 = \OCP\Share::getItemSharedWithUser('test', 99, $this->user2->getUID(), $this->user1->getUID());
|
2014-11-10 12:08:45 +00:00
|
|
|
$this->assertSame(1, count($result1));
|
|
|
|
$this->verifyResult($result1, array('target1'));
|
|
|
|
|
2016-11-04 12:07:16 +00:00
|
|
|
$result2 = \OCP\Share::getItemSharedWithUser('test', 99, null, $this->user1->getUID());
|
2014-11-10 12:08:45 +00:00
|
|
|
$this->assertSame(2, count($result2));
|
|
|
|
$this->verifyResult($result2, array('target1', 'target2'));
|
|
|
|
|
2016-11-04 12:07:16 +00:00
|
|
|
$result3 = \OCP\Share::getItemSharedWithUser('test', 99, $this->user3->getUID());
|
2014-11-10 12:08:45 +00:00
|
|
|
$this->assertSame(2, count($result3));
|
|
|
|
$this->verifyResult($result3, array('target3', 'target4'));
|
|
|
|
|
|
|
|
$result4 = \OCP\Share::getItemSharedWithUser('test', 99, null, null);
|
2015-01-19 13:39:00 +00:00
|
|
|
$this->assertSame(5, count($result4)); // 5 because target4 appears twice
|
2014-11-10 12:08:45 +00:00
|
|
|
$this->verifyResult($result4, array('target1', 'target2', 'target3', 'target4'));
|
2015-01-19 13:39:00 +00:00
|
|
|
|
2016-11-04 12:07:16 +00:00
|
|
|
$result6 = \OCP\Share::getItemSharedWithUser('test', 99, $this->user6->getUID(), null);
|
2015-01-19 13:39:00 +00:00
|
|
|
$this->assertSame(1, count($result6));
|
|
|
|
$this->verifyResult($result6, array('target4'));
|
2014-11-10 12:08:45 +00:00
|
|
|
}
|
|
|
|
|
2015-01-16 17:11:13 +00:00
|
|
|
public function testGetItemSharedWithUserFromGroupShare() {
|
2016-11-04 12:07:16 +00:00
|
|
|
\OC_User::setUserId($this->user1->getUID());
|
2015-01-16 17:11:13 +00:00
|
|
|
|
|
|
|
//add dummy values to the share table
|
|
|
|
$query = \OC_DB::prepare('INSERT INTO `*PREFIX*share` ('
|
|
|
|
.' `item_type`, `item_source`, `item_target`, `share_type`,'
|
|
|
|
.' `share_with`, `uid_owner`) VALUES (?,?,?,?,?,?)');
|
2016-11-04 12:07:16 +00:00
|
|
|
$args = array('test', 99, 'target1', \OCP\Share::SHARE_TYPE_GROUP, $this->group1->getGID(), $this->user1->getUID());
|
2015-01-16 17:11:13 +00:00
|
|
|
$query->execute($args);
|
2016-11-04 12:07:16 +00:00
|
|
|
$args = array('test', 99, 'target2', \OCP\Share::SHARE_TYPE_GROUP, $this->group2->getGID(), $this->user1->getUID());
|
2015-01-16 17:11:13 +00:00
|
|
|
$query->execute($args);
|
2016-11-04 12:07:16 +00:00
|
|
|
$args = array('test', 99, 'target3', \OCP\Share::SHARE_TYPE_GROUP, $this->group1->getGID(), $this->user2->getUID());
|
2015-01-16 17:11:13 +00:00
|
|
|
$query->execute($args);
|
2016-11-04 12:07:16 +00:00
|
|
|
$args = array('test', 99, 'target4', \OCP\Share::SHARE_TYPE_GROUP, $this->group1->getGID(), $this->user4->getUID());
|
2015-01-16 17:11:13 +00:00
|
|
|
$query->execute($args);
|
|
|
|
|
|
|
|
// user2 is in group1 and group2
|
2016-11-04 12:07:16 +00:00
|
|
|
$result1 = \OCP\Share::getItemSharedWithUser('test', 99, $this->user2->getUID(), $this->user1->getUID());
|
2015-01-16 17:11:13 +00:00
|
|
|
$this->assertSame(2, count($result1));
|
|
|
|
$this->verifyResult($result1, array('target1', 'target2'));
|
|
|
|
|
2016-11-04 12:07:16 +00:00
|
|
|
$result2 = \OCP\Share::getItemSharedWithUser('test', 99, null, $this->user1->getUID());
|
2015-01-16 17:11:13 +00:00
|
|
|
$this->assertSame(2, count($result2));
|
|
|
|
$this->verifyResult($result2, array('target1', 'target2'));
|
|
|
|
|
|
|
|
// user3 is in group1 and group2
|
2016-11-04 12:07:16 +00:00
|
|
|
$result3 = \OCP\Share::getItemSharedWithUser('test', 99, $this->user3->getUID());
|
2015-01-16 17:11:13 +00:00
|
|
|
$this->assertSame(3, count($result3));
|
|
|
|
$this->verifyResult($result3, array('target1', 'target3', 'target4'));
|
|
|
|
|
|
|
|
$result4 = \OCP\Share::getItemSharedWithUser('test', 99, null, null);
|
|
|
|
$this->assertSame(4, count($result4));
|
|
|
|
$this->verifyResult($result4, array('target1', 'target2', 'target3', 'target4'));
|
2015-01-19 13:39:00 +00:00
|
|
|
|
2016-11-04 12:07:16 +00:00
|
|
|
$result6 = \OCP\Share::getItemSharedWithUser('test', 99, $this->user6->getUID(), null);
|
2015-01-19 13:39:00 +00:00
|
|
|
$this->assertSame(0, count($result6));
|
2015-01-16 17:11:13 +00:00
|
|
|
}
|
|
|
|
|
2014-11-10 12:08:45 +00:00
|
|
|
public function verifyResult($result, $expected) {
|
|
|
|
foreach ($result as $r) {
|
|
|
|
if (in_array($r['item_target'], $expected)) {
|
|
|
|
$key = array_search($r['item_target'], $expected);
|
|
|
|
unset($expected[$key]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$this->assertEmpty($expected, 'did not found all expected values');
|
|
|
|
}
|
|
|
|
|
2015-10-05 09:27:47 +00:00
|
|
|
public function testGetShareSubItemsWhenUserNotInGroup() {
|
2017-08-15 09:57:18 +00:00
|
|
|
\OC\Share\Share::shareItem('test', 'test.txt', \OCP\Share::SHARE_TYPE_GROUP, $this->group1->getGID(), \OCP\Constants::PERMISSION_READ);
|
2015-10-05 09:27:47 +00:00
|
|
|
|
2017-08-15 12:29:26 +00:00
|
|
|
$result = \OC\Share\Share::getItemsSharedWithUser('test', $this->user2->getUID());
|
2015-10-05 09:27:47 +00:00
|
|
|
$this->assertCount(1, $result);
|
|
|
|
|
|
|
|
$groupShareId = array_keys($result)[0];
|
|
|
|
|
|
|
|
// remove user from group
|
2016-11-04 12:07:16 +00:00
|
|
|
$userObject = \OC::$server->getUserManager()->get($this->user2->getUID());
|
|
|
|
\OC::$server->getGroupManager()->get($this->group1->getGID())->removeUser($userObject);
|
2015-10-05 09:27:47 +00:00
|
|
|
|
2017-08-15 12:29:26 +00:00
|
|
|
$result = \OC\Share\Share::getItemsSharedWithUser('test', $this->user2->getUID());
|
2015-10-05 09:27:47 +00:00
|
|
|
$this->assertCount(0, $result);
|
|
|
|
|
|
|
|
// test with buggy data
|
|
|
|
$qb = \OC::$server->getDatabaseConnection()->getQueryBuilder();
|
|
|
|
$qb->insert('share')
|
|
|
|
->values([
|
|
|
|
'share_type' => $qb->expr()->literal(2), // group sub-share
|
2016-11-04 12:07:16 +00:00
|
|
|
'share_with' => $qb->expr()->literal($this->user2->getUID()),
|
2015-10-05 09:27:47 +00:00
|
|
|
'parent' => $qb->expr()->literal($groupShareId),
|
2016-11-04 12:07:16 +00:00
|
|
|
'uid_owner' => $qb->expr()->literal($this->user1->getUID()),
|
2015-10-05 09:27:47 +00:00
|
|
|
'item_type' => $qb->expr()->literal('test'),
|
|
|
|
'item_source' => $qb->expr()->literal('test.txt'),
|
|
|
|
'item_target' => $qb->expr()->literal('test.txt'),
|
|
|
|
'file_target' => $qb->expr()->literal('test2.txt'),
|
|
|
|
'permissions' => $qb->expr()->literal(1),
|
|
|
|
'stime' => $qb->expr()->literal(time()),
|
|
|
|
])->execute();
|
|
|
|
|
2017-08-15 12:29:26 +00:00
|
|
|
$result = \OC\Share\Share::getItemsSharedWithUser('test', $this->user2->getUID());
|
2015-10-05 09:27:47 +00:00
|
|
|
$this->assertCount(0, $result);
|
|
|
|
|
|
|
|
$qb->delete('share')->execute();
|
|
|
|
}
|
|
|
|
|
2014-07-28 15:19:19 +00:00
|
|
|
public function testShareItemWithLinkAndDefaultExpireDate() {
|
2016-11-04 12:07:16 +00:00
|
|
|
\OC_User::setUserId($this->user1->getUID());
|
2014-07-28 15:19:19 +00:00
|
|
|
|
2015-03-17 16:26:03 +00:00
|
|
|
$config = \OC::$server->getConfig();
|
|
|
|
|
|
|
|
$config->setAppValue('core', 'shareapi_default_expire_date', 'yes');
|
|
|
|
$config->setAppValue('core', 'shareapi_expire_after_n_days', '2');
|
2014-07-28 15:19:19 +00:00
|
|
|
|
2017-08-15 09:57:18 +00:00
|
|
|
$token = \OC\Share\Share::shareItem('test', 'test.txt', \OCP\Share::SHARE_TYPE_LINK, null, \OCP\Constants::PERMISSION_READ);
|
2014-07-28 15:19:19 +00:00
|
|
|
$this->assertInternalType(
|
|
|
|
'string',
|
|
|
|
$token,
|
|
|
|
'Failed asserting that user 1 successfully shared text.txt as link with token.'
|
|
|
|
);
|
|
|
|
|
|
|
|
// share should have default expire date
|
|
|
|
|
|
|
|
$row = $this->getShareByValidToken($token);
|
|
|
|
$this->assertNotEmpty(
|
|
|
|
$row['expiration'],
|
|
|
|
'Failed asserting that the returned row has an default expiration date.'
|
|
|
|
);
|
|
|
|
|
2015-03-17 16:26:03 +00:00
|
|
|
$config->deleteAppValue('core', 'shareapi_default_expire_date');
|
|
|
|
$config->deleteAppValue('core', 'shareapi_expire_after_n_days');
|
2014-07-28 15:19:19 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-06-18 10:46:52 +00:00
|
|
|
public function dataShareWithRemoteUserAndRemoteIsInvalid() {
|
|
|
|
return [
|
|
|
|
// Invalid path
|
|
|
|
array('user@'),
|
|
|
|
|
|
|
|
// Invalid user
|
|
|
|
array('@server'),
|
|
|
|
array('us/er@server'),
|
|
|
|
array('us:er@server'),
|
|
|
|
|
|
|
|
// Invalid splitting
|
|
|
|
array('user'),
|
|
|
|
array(''),
|
|
|
|
array('us/erserver'),
|
|
|
|
array('us:erserver'),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider dataShareWithRemoteUserAndRemoteIsInvalid
|
|
|
|
*
|
|
|
|
* @param string $remoteId
|
|
|
|
* @expectedException \OC\HintException
|
|
|
|
*/
|
|
|
|
public function testShareWithRemoteUserAndRemoteIsInvalid($remoteId) {
|
2016-11-04 12:07:16 +00:00
|
|
|
\OC_User::setUserId($this->user1->getUID());
|
2017-08-15 09:57:18 +00:00
|
|
|
\OC\Share\Share::shareItem('test', 'test.txt', \OCP\Share::SHARE_TYPE_REMOTE, $remoteId, \OCP\Constants::PERMISSION_ALL);
|
2015-06-18 10:46:52 +00:00
|
|
|
}
|
|
|
|
|
2014-01-21 11:07:08 +00:00
|
|
|
/**
|
|
|
|
* @dataProvider checkPasswordProtectedShareDataProvider
|
|
|
|
* @param $expected
|
|
|
|
* @param $item
|
|
|
|
*/
|
|
|
|
public function testCheckPasswordProtectedShare($expected, $item) {
|
2016-05-31 04:53:28 +00:00
|
|
|
\OC::$server->getSession()->set('public_link_authenticated', '100');
|
2017-08-15 08:20:40 +00:00
|
|
|
$result = \OC\Share\Share::checkPasswordProtectedShare($item);
|
2014-01-21 11:07:08 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
function checkPasswordProtectedShareDataProvider() {
|
|
|
|
return array(
|
|
|
|
array(true, array()),
|
|
|
|
array(true, array('share_with' => null)),
|
|
|
|
array(true, array('share_with' => '')),
|
|
|
|
array(true, array('share_with' => '1234567890', 'share_type' => '1')),
|
|
|
|
array(true, array('share_with' => '1234567890', 'share_type' => 1)),
|
2016-05-31 04:53:28 +00:00
|
|
|
array(true, array('share_with' => '1234567890', 'share_type' => '3', 'id' => '100')),
|
|
|
|
array(true, array('share_with' => '1234567890', 'share_type' => 3, 'id' => '100')),
|
2014-01-21 11:07:08 +00:00
|
|
|
array(true, array('share_with' => '1234567890', 'share_type' => '3', 'id' => 100)),
|
|
|
|
array(true, array('share_with' => '1234567890', 'share_type' => 3, 'id' => 100)),
|
2016-05-31 04:53:28 +00:00
|
|
|
array(false, array('share_with' => '1234567890', 'share_type' => '3', 'id' => '101')),
|
|
|
|
array(false, array('share_with' => '1234567890', 'share_type' => 3, 'id' => '101')),
|
2014-01-21 11:07:08 +00:00
|
|
|
array(false, array('share_with' => '1234567890', 'share_type' => '3', 'id' => 101)),
|
|
|
|
array(false, array('share_with' => '1234567890', 'share_type' => 3, 'id' => 101)),
|
|
|
|
);
|
2014-08-01 14:24:19 +00:00
|
|
|
}
|
2014-01-21 11:07:08 +00:00
|
|
|
|
2014-12-04 18:51:04 +00:00
|
|
|
/**
|
|
|
|
* @dataProvider urls
|
2016-11-04 12:07:16 +00:00
|
|
|
* @param string $url
|
|
|
|
* @param string $expectedResult
|
2014-12-04 18:51:04 +00:00
|
|
|
*/
|
|
|
|
function testRemoveProtocolFromUrl($url, $expectedResult) {
|
|
|
|
$share = new \OC\Share\Share();
|
2015-06-03 10:03:02 +00:00
|
|
|
$result = self::invokePrivate($share, 'removeProtocolFromUrl', array($url));
|
2014-12-04 18:51:04 +00:00
|
|
|
$this->assertSame($expectedResult, $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
function urls() {
|
|
|
|
return array(
|
|
|
|
array('http://owncloud.org', 'owncloud.org'),
|
|
|
|
array('https://owncloud.org', 'owncloud.org'),
|
|
|
|
array('owncloud.org', 'owncloud.org'),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-04-27 20:07:35 +00:00
|
|
|
public function dataRemoteShareUrlCalls() {
|
|
|
|
return [
|
|
|
|
['admin@localhost', 'localhost'],
|
|
|
|
['admin@https://localhost', 'localhost'],
|
|
|
|
['admin@http://localhost', 'localhost'],
|
|
|
|
['admin@localhost/subFolder', 'localhost/subFolder'],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider dataRemoteShareUrlCalls
|
|
|
|
*
|
|
|
|
* @param string $shareWith
|
|
|
|
* @param string $urlHost
|
|
|
|
*/
|
|
|
|
public function testRemoteShareUrlCalls($shareWith, $urlHost) {
|
2016-10-20 13:11:01 +00:00
|
|
|
$httpHelperMock = $this->getMockBuilder('OC\HTTPHelper')
|
2015-04-27 20:07:35 +00:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2016-10-20 13:11:01 +00:00
|
|
|
$this->overwriteService('HTTPHelper', $httpHelperMock);
|
2015-04-27 20:07:35 +00:00
|
|
|
|
|
|
|
$httpHelperMock->expects($this->at(0))
|
|
|
|
->method('post')
|
2017-03-10 14:37:21 +00:00
|
|
|
->with($this->stringStartsWith('https://' . $urlHost . '/ocs/v2.php/cloud/shares'), $this->anything())
|
2015-04-27 20:07:35 +00:00
|
|
|
->willReturn(['success' => false, 'result' => 'Exception']);
|
|
|
|
$httpHelperMock->expects($this->at(1))
|
|
|
|
->method('post')
|
2017-03-10 14:37:21 +00:00
|
|
|
->with($this->stringStartsWith('http://' . $urlHost . '/ocs/v2.php/cloud/shares'), $this->anything())
|
2015-04-27 20:07:35 +00:00
|
|
|
->willReturn(['success' => true, 'result' => json_encode(['ocs' => ['meta' => ['statuscode' => 100]]])]);
|
|
|
|
|
2017-08-15 09:57:18 +00:00
|
|
|
\OC\Share\Share::shareItem('test', 'test.txt', \OCP\Share::SHARE_TYPE_REMOTE, $shareWith, \OCP\Constants::PERMISSION_READ);
|
2015-04-28 12:56:13 +00:00
|
|
|
$shares = \OCP\Share::getItemShared('test', 'test.txt');
|
|
|
|
$share = array_shift($shares);
|
|
|
|
|
|
|
|
$httpHelperMock->expects($this->at(0))
|
2015-04-27 20:07:35 +00:00
|
|
|
->method('post')
|
2017-03-10 14:37:21 +00:00
|
|
|
->with($this->stringStartsWith('https://' . $urlHost . '/ocs/v2.php/cloud/shares/' . $share['id'] . '/unshare'), $this->anything())
|
2015-04-27 20:07:35 +00:00
|
|
|
->willReturn(['success' => false, 'result' => 'Exception']);
|
2015-04-28 12:56:13 +00:00
|
|
|
$httpHelperMock->expects($this->at(1))
|
2015-04-27 20:07:35 +00:00
|
|
|
->method('post')
|
2017-03-10 14:37:21 +00:00
|
|
|
->with($this->stringStartsWith('http://' . $urlHost . '/ocs/v2.php/cloud/shares/' . $share['id'] . '/unshare'), $this->anything())
|
2015-04-27 20:07:35 +00:00
|
|
|
->willReturn(['success' => true, 'result' => json_encode(['ocs' => ['meta' => ['statuscode' => 100]]])]);
|
|
|
|
|
2017-08-15 12:29:26 +00:00
|
|
|
\OC\Share\Share::unshare('test', 'test.txt', \OCP\Share::SHARE_TYPE_REMOTE, $shareWith);
|
2016-10-20 13:11:01 +00:00
|
|
|
$this->restoreService('HTTPHelper');
|
2015-04-27 20:07:35 +00:00
|
|
|
}
|
|
|
|
|
2014-08-01 14:24:19 +00:00
|
|
|
/**
|
|
|
|
* @dataProvider dataProviderTestGroupItems
|
2015-04-27 20:07:35 +00:00
|
|
|
* @param array $ungrouped
|
|
|
|
* @param array $grouped
|
2014-08-01 14:24:19 +00:00
|
|
|
*/
|
|
|
|
function testGroupItems($ungrouped, $grouped) {
|
2014-01-21 11:07:08 +00:00
|
|
|
|
2014-08-01 14:24:19 +00:00
|
|
|
$result = DummyShareClass::groupItemsTest($ungrouped);
|
2014-01-21 11:07:08 +00:00
|
|
|
|
2014-08-01 14:24:19 +00:00
|
|
|
$this->compareArrays($grouped, $result);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function compareArrays($result, $expectedResult) {
|
|
|
|
foreach ($expectedResult as $key => $value) {
|
|
|
|
if (is_array($value)) {
|
|
|
|
$this->compareArrays($result[$key], $value);
|
|
|
|
} else {
|
|
|
|
$this->assertSame($value, $result[$key]);
|
|
|
|
}
|
2014-01-21 11:07:08 +00:00
|
|
|
}
|
2014-08-01 14:24:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function dataProviderTestGroupItems() {
|
|
|
|
return array(
|
|
|
|
// one array with one share
|
|
|
|
array(
|
|
|
|
array( // input
|
2014-11-25 15:28:41 +00:00
|
|
|
array('item_source' => 1, 'permissions' => \OCP\Constants::PERMISSION_ALL, 'item_target' => 't1')),
|
2014-08-01 14:24:19 +00:00
|
|
|
array( // expected result
|
2014-11-25 15:28:41 +00:00
|
|
|
array('item_source' => 1, 'permissions' => \OCP\Constants::PERMISSION_ALL, 'item_target' => 't1'))),
|
2014-08-01 14:24:19 +00:00
|
|
|
// two shares both point to the same source
|
|
|
|
array(
|
|
|
|
array( // input
|
2014-11-25 15:28:41 +00:00
|
|
|
array('item_source' => 1, 'permissions' => \OCP\Constants::PERMISSION_READ, 'item_target' => 't1'),
|
|
|
|
array('item_source' => 1, 'permissions' => \OCP\Constants::PERMISSION_UPDATE, 'item_target' => 't1'),
|
2014-08-01 14:24:19 +00:00
|
|
|
),
|
|
|
|
array( // expected result
|
2014-11-25 15:28:41 +00:00
|
|
|
array('item_source' => 1, 'permissions' => \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_UPDATE, 'item_target' => 't1',
|
2014-08-01 14:24:19 +00:00
|
|
|
'grouped' => array(
|
2014-11-25 15:28:41 +00:00
|
|
|
array('item_source' => 1, 'permissions' => \OCP\Constants::PERMISSION_READ, 'item_target' => 't1'),
|
|
|
|
array('item_source' => 1, 'permissions' => \OCP\Constants::PERMISSION_UPDATE, 'item_target' => 't1'),
|
2014-08-01 14:24:19 +00:00
|
|
|
)
|
|
|
|
),
|
|
|
|
)
|
|
|
|
),
|
|
|
|
// two shares both point to the same source but with different targets
|
|
|
|
array(
|
|
|
|
array( // input
|
2014-11-25 15:28:41 +00:00
|
|
|
array('item_source' => 1, 'permissions' => \OCP\Constants::PERMISSION_READ, 'item_target' => 't1'),
|
|
|
|
array('item_source' => 1, 'permissions' => \OCP\Constants::PERMISSION_UPDATE, 'item_target' => 't2'),
|
2014-08-01 14:24:19 +00:00
|
|
|
),
|
|
|
|
array( // expected result
|
2014-11-25 15:28:41 +00:00
|
|
|
array('item_source' => 1, 'permissions' => \OCP\Constants::PERMISSION_READ, 'item_target' => 't1'),
|
|
|
|
array('item_source' => 1, 'permissions' => \OCP\Constants::PERMISSION_UPDATE, 'item_target' => 't2'),
|
2014-08-01 14:24:19 +00:00
|
|
|
)
|
|
|
|
),
|
|
|
|
// three shares two point to the same source
|
|
|
|
array(
|
|
|
|
array( // input
|
2014-11-25 15:28:41 +00:00
|
|
|
array('item_source' => 1, 'permissions' => \OCP\Constants::PERMISSION_READ, 'item_target' => 't1'),
|
|
|
|
array('item_source' => 2, 'permissions' => \OCP\Constants::PERMISSION_CREATE, 'item_target' => 't2'),
|
|
|
|
array('item_source' => 1, 'permissions' => \OCP\Constants::PERMISSION_UPDATE, 'item_target' => 't1'),
|
2014-08-01 14:24:19 +00:00
|
|
|
),
|
|
|
|
array( // expected result
|
2014-11-25 15:28:41 +00:00
|
|
|
array('item_source' => 1, 'permissions' => \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_UPDATE, 'item_target' => 't1',
|
2014-08-01 14:24:19 +00:00
|
|
|
'grouped' => array(
|
2014-11-25 15:28:41 +00:00
|
|
|
array('item_source' => 1, 'permissions' => \OCP\Constants::PERMISSION_READ, 'item_target' => 't1'),
|
|
|
|
array('item_source' => 1, 'permissions' => \OCP\Constants::PERMISSION_UPDATE, 'item_target' => 't1'),
|
2014-08-01 14:24:19 +00:00
|
|
|
)
|
|
|
|
),
|
2014-11-25 15:28:41 +00:00
|
|
|
array('item_source' => 2, 'permissions' => \OCP\Constants::PERMISSION_CREATE, 'item_target' => 't2'),
|
2014-08-01 14:24:19 +00:00
|
|
|
)
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
2015-03-19 09:47:09 +00:00
|
|
|
|
2015-07-15 10:49:26 +00:00
|
|
|
/**
|
|
|
|
* Make sure that a user cannot have multiple identical shares to remote users
|
|
|
|
*/
|
|
|
|
public function testOnlyOneRemoteShare() {
|
2016-10-20 13:11:01 +00:00
|
|
|
$httpHelperMock = $this->getMockBuilder('OC\HTTPHelper')
|
2015-07-15 10:49:26 +00:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2016-10-20 13:11:01 +00:00
|
|
|
$this->overwriteService('HTTPHelper', $httpHelperMock);
|
2015-07-15 10:49:26 +00:00
|
|
|
|
|
|
|
$httpHelperMock->expects($this->at(0))
|
|
|
|
->method('post')
|
2017-03-10 14:37:21 +00:00
|
|
|
->with($this->stringStartsWith('https://localhost/ocs/v2.php/cloud/shares'), $this->anything())
|
2015-07-15 10:49:26 +00:00
|
|
|
->willReturn(['success' => true, 'result' => json_encode(['ocs' => ['meta' => ['statuscode' => 100]]])]);
|
|
|
|
|
2017-08-15 09:57:18 +00:00
|
|
|
\OC\Share\Share::shareItem('test', 'test.txt', \OCP\Share::SHARE_TYPE_REMOTE, 'foo@localhost', \OCP\Constants::PERMISSION_READ);
|
2015-07-15 10:49:26 +00:00
|
|
|
$shares = \OCP\Share::getItemShared('test', 'test.txt');
|
|
|
|
$share = array_shift($shares);
|
|
|
|
|
|
|
|
//Try share again
|
|
|
|
try {
|
2017-08-15 09:57:18 +00:00
|
|
|
\OC\Share\Share::shareItem('test', 'test.txt', \OCP\Share::SHARE_TYPE_REMOTE, 'foo@localhost', \OCP\Constants::PERMISSION_READ);
|
2015-07-15 10:49:26 +00:00
|
|
|
$this->fail('Identical remote shares are not allowed');
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
$this->assertEquals('Sharing test.txt failed, because this item is already shared with foo@localhost', $e->getMessage());
|
|
|
|
}
|
|
|
|
|
|
|
|
$httpHelperMock->expects($this->at(0))
|
|
|
|
->method('post')
|
2017-03-10 14:37:21 +00:00
|
|
|
->with($this->stringStartsWith('https://localhost/ocs/v2.php/cloud/shares/' . $share['id'] . '/unshare'), $this->anything())
|
2015-07-15 10:49:26 +00:00
|
|
|
->willReturn(['success' => true, 'result' => json_encode(['ocs' => ['meta' => ['statuscode' => 100]]])]);
|
|
|
|
|
2017-08-15 12:29:26 +00:00
|
|
|
\OC\Share\Share::unshare('test', 'test.txt', \OCP\Share::SHARE_TYPE_REMOTE, 'foo@localhost');
|
2016-10-20 13:11:01 +00:00
|
|
|
$this->restoreService('HTTPHelper');
|
2015-07-15 10:49:26 +00:00
|
|
|
}
|
|
|
|
|
2015-10-13 08:05:49 +00:00
|
|
|
/**
|
|
|
|
* Test case for #17560
|
|
|
|
*/
|
|
|
|
public function testAccesToSharedSubFolder() {
|
2016-11-04 12:07:16 +00:00
|
|
|
\OC_User::setUserId($this->user1->getUID());
|
|
|
|
$view = new \OC\Files\View('/' . $this->user1->getUID() . '/');
|
2015-10-13 08:05:49 +00:00
|
|
|
$view->mkdir('files/folder1');
|
|
|
|
|
|
|
|
$fileInfo = $view->getFileInfo('files/folder1');
|
|
|
|
$this->assertInstanceOf('\OC\Files\FileInfo', $fileInfo);
|
|
|
|
$fileId = $fileInfo->getId();
|
|
|
|
|
|
|
|
$this->assertTrue(
|
2017-08-15 09:57:18 +00:00
|
|
|
\OC\Share\Share::shareItem('folder', $fileId, \OCP\Share::SHARE_TYPE_USER, $this->user2->getUID(), \OCP\Constants::PERMISSION_ALL),
|
2015-10-13 08:05:49 +00:00
|
|
|
'Failed asserting that user 1 successfully shared "test" with user 2.'
|
|
|
|
);
|
|
|
|
$this->assertTrue(
|
2017-08-15 09:57:18 +00:00
|
|
|
\OC\Share\Share::shareItem('folder', $fileId, \OCP\Share::SHARE_TYPE_USER, $this->user3->getUID(), \OCP\Constants::PERMISSION_ALL),
|
2015-10-13 08:05:49 +00:00
|
|
|
'Failed asserting that user 1 successfully shared "test" with user 3.'
|
|
|
|
);
|
|
|
|
|
|
|
|
$view->mkdir('files/folder1/folder2');
|
|
|
|
|
|
|
|
$fileInfo = $view->getFileInfo('files/folder1/folder2');
|
|
|
|
$this->assertInstanceOf('\OC\Files\FileInfo', $fileInfo);
|
|
|
|
$fileId = $fileInfo->getId();
|
|
|
|
|
|
|
|
$this->assertTrue(
|
2017-08-15 09:57:18 +00:00
|
|
|
\OC\Share\Share::shareItem('folder', $fileId, \OCP\Share::SHARE_TYPE_USER, $this->user4->getUID(), \OCP\Constants::PERMISSION_ALL),
|
2015-10-13 08:05:49 +00:00
|
|
|
'Failed asserting that user 1 successfully shared "test" with user 4.'
|
|
|
|
);
|
|
|
|
|
2016-05-19 07:15:10 +00:00
|
|
|
$res = \OCP\Share::getItemShared(
|
2015-10-13 08:05:49 +00:00
|
|
|
'folder',
|
|
|
|
$fileId,
|
2016-05-19 07:15:10 +00:00
|
|
|
\OCP\Share::FORMAT_NONE,
|
2015-10-13 08:05:49 +00:00
|
|
|
null,
|
|
|
|
true
|
|
|
|
);
|
|
|
|
$this->assertCount(3, $res);
|
|
|
|
|
|
|
|
$this->assertTrue(
|
2017-08-15 09:57:18 +00:00
|
|
|
\OC\Share\Share::shareItem('folder', $fileId, \OCP\Share::SHARE_TYPE_USER, $this->user5->getUID(), \OCP\Constants::PERMISSION_ALL),
|
2015-10-13 08:05:49 +00:00
|
|
|
'Failed asserting that user 1 successfully shared "test" with user 5.'
|
|
|
|
);
|
|
|
|
|
2016-05-19 07:15:10 +00:00
|
|
|
$res = \OCP\Share::getItemShared(
|
2015-10-13 08:05:49 +00:00
|
|
|
'folder',
|
|
|
|
$fileId,
|
2016-05-19 07:15:10 +00:00
|
|
|
\OCP\Share::FORMAT_NONE,
|
2015-10-13 08:05:49 +00:00
|
|
|
null,
|
|
|
|
true
|
|
|
|
);
|
|
|
|
$this->assertCount(4, $res);
|
|
|
|
}
|
2015-10-16 07:27:02 +00:00
|
|
|
|
2015-10-16 08:00:33 +00:00
|
|
|
public function testShareWithSelfError() {
|
2016-11-04 12:07:16 +00:00
|
|
|
\OC_User::setUserId($this->user1->getUID());
|
|
|
|
$view = new \OC\Files\View('/' . $this->user1->getUID() . '/');
|
2015-10-16 08:00:33 +00:00
|
|
|
$view->mkdir('files/folder1');
|
|
|
|
|
|
|
|
$fileInfo = $view->getFileInfo('files/folder1');
|
|
|
|
$this->assertInstanceOf('\OC\Files\FileInfo', $fileInfo);
|
|
|
|
$fileId = $fileInfo->getId();
|
|
|
|
|
|
|
|
try {
|
2017-08-15 09:57:18 +00:00
|
|
|
\OC\Share\Share::shareItem('folder', $fileId, \OCP\Share::SHARE_TYPE_USER, $this->user1->getUID(), \OCP\Constants::PERMISSION_ALL);
|
2015-10-16 08:00:33 +00:00
|
|
|
$this->fail();
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
$this->assertEquals('Sharing /folder1 failed, because you can not share with yourself', $e->getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-10-16 07:27:02 +00:00
|
|
|
public function testShareWithOwnerError() {
|
2016-11-04 12:07:16 +00:00
|
|
|
\OC_User::setUserId($this->user1->getUID());
|
|
|
|
$view = new \OC\Files\View('/' . $this->user1->getUID() . '/');
|
2015-10-16 07:27:02 +00:00
|
|
|
$view->mkdir('files/folder1');
|
|
|
|
|
|
|
|
$fileInfo = $view->getFileInfo('files/folder1');
|
|
|
|
$this->assertInstanceOf('\OC\Files\FileInfo', $fileInfo);
|
|
|
|
$fileId = $fileInfo->getId();
|
|
|
|
|
|
|
|
$this->assertTrue(
|
2017-08-15 09:57:18 +00:00
|
|
|
\OC\Share\Share::shareItem('folder', $fileId, \OCP\Share::SHARE_TYPE_USER, $this->user2->getUID(), \OCP\Constants::PERMISSION_ALL),
|
2015-10-16 07:27:02 +00:00
|
|
|
'Failed asserting that user 1 successfully shared "test" with user 2.'
|
|
|
|
);
|
|
|
|
|
2016-11-04 12:07:16 +00:00
|
|
|
\OC_User::setUserId($this->user2->getUID());
|
2015-10-16 07:27:02 +00:00
|
|
|
try {
|
2017-08-15 09:57:18 +00:00
|
|
|
\OC\Share\Share::shareItem('folder', $fileId, \OCP\Share::SHARE_TYPE_USER, $this->user1->getUID(), \OCP\Constants::PERMISSION_ALL);
|
2015-10-16 07:27:02 +00:00
|
|
|
$this->fail();
|
|
|
|
} catch (\Exception $e) {
|
2016-11-04 12:07:16 +00:00
|
|
|
$this->assertEquals('Sharing failed, because the user ' . $this->user1->getUID() . ' is the original sharer', $e->getMessage());
|
2015-10-16 07:27:02 +00:00
|
|
|
}
|
|
|
|
}
|
2014-08-01 14:24:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class DummyShareClass extends \OC\Share\Share {
|
|
|
|
public static function groupItemsTest($items) {
|
|
|
|
return parent::groupItems($items, 'test');
|
2014-01-21 11:07:08 +00:00
|
|
|
}
|
2012-07-25 21:08:18 +00:00
|
|
|
}
|
2015-02-09 12:12:34 +00:00
|
|
|
|
|
|
|
class DummyHookListener {
|
|
|
|
static $shareType = null;
|
|
|
|
|
|
|
|
public static function listen($params) {
|
|
|
|
self::$shareType = $params['shareType'];
|
|
|
|
}
|
|
|
|
}
|