|
|
|
@ -134,170 +134,6 @@ class ShareTest extends \Test\TestCase {
|
|
|
|
|
parent::tearDown();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testShareInvalidShareType() {
|
|
|
|
|
$message = 'Share type foobar is not valid for test.txt';
|
|
|
|
|
try {
|
|
|
|
|
\OC\Share\Share::shareItem('test', 'test.txt', 'foobar', $this->user2, \OCP\Constants::PERMISSION_READ);
|
|
|
|
|
} catch (\Exception $exception) {
|
|
|
|
|
$this->assertEquals($message, $exception->getMessage());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testGetShareFromOutsideFilesFolder() {
|
|
|
|
|
\OC_User::setUserId($this->user1->getUID());
|
|
|
|
|
$view = new \OC\Files\View('/' . $this->user1->getUID() . '/');
|
|
|
|
|
$view->mkdir('files/test');
|
|
|
|
|
$view->mkdir('files/test/sub');
|
|
|
|
|
|
|
|
|
|
$view->mkdir('files_trashbin');
|
|
|
|
|
$view->mkdir('files_trashbin/files');
|
|
|
|
|
|
|
|
|
|
$fileInfo = $view->getFileInfo('files/test/sub');
|
|
|
|
|
$this->assertInstanceOf('\OC\Files\FileInfo', $fileInfo);
|
|
|
|
|
$fileId = $fileInfo->getId();
|
|
|
|
|
|
|
|
|
|
$this->assertTrue(
|
|
|
|
|
\OC\Share\Share::shareItem('folder', $fileId, \OCP\Share::SHARE_TYPE_USER, $this->user2->getUID(), \OCP\Constants::PERMISSION_READ),
|
|
|
|
|
'Failed asserting that user 1 successfully shared "test/sub" with user 2.'
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$result = \OCP\Share::getItemShared('folder', $fileId, Backend::FORMAT_SOURCE);
|
|
|
|
|
$this->assertNotEmpty($result);
|
|
|
|
|
|
|
|
|
|
$result = \OC\Share\Share::getItemSharedWithUser('folder', $fileId, $this->user2->getUID());
|
|
|
|
|
$this->assertNotEmpty($result);
|
|
|
|
|
|
|
|
|
|
$result = \OC\Share\Share::getItemsSharedWithUser('folder', $this->user2->getUID());
|
|
|
|
|
$this->assertNotEmpty($result);
|
|
|
|
|
|
|
|
|
|
// move to trash (keeps file id)
|
|
|
|
|
$view->rename('files/test', 'files_trashbin/files/test');
|
|
|
|
|
|
|
|
|
|
$result = \OCP\Share::getItemShared('folder', $fileId, Backend::FORMAT_SOURCE);
|
|
|
|
|
$this->assertEmpty($result, 'Share must not be returned for files outside of "files"');
|
|
|
|
|
|
|
|
|
|
$result = \OC\Share\Share::getItemSharedWithUser('folder', $fileId, $this->user2->getUID());
|
|
|
|
|
$this->assertEmpty($result, 'Share must not be returned for files outside of "files"');
|
|
|
|
|
|
|
|
|
|
$result = \OC\Share\Share::getItemsSharedWithUser('folder', $this->user2->getUID());
|
|
|
|
|
$this->assertEmpty($result, 'Share must not be returned for files outside of "files"');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testSharingAFolderThatIsSharedWithAGroupOfTheOwner() {
|
|
|
|
|
\OC_User::setUserId($this->user1->getUID());
|
|
|
|
|
$view = new \OC\Files\View('/' . $this->user1->getUID() . '/');
|
|
|
|
|
$view->mkdir('files/test');
|
|
|
|
|
$view->mkdir('files/test/sub1');
|
|
|
|
|
$view->mkdir('files/test/sub1/sub2');
|
|
|
|
|
|
|
|
|
|
$fileInfo = $view->getFileInfo('files/test/sub1');
|
|
|
|
|
$this->assertInstanceOf('\OC\Files\FileInfo', $fileInfo);
|
|
|
|
|
$fileId = $fileInfo->getId();
|
|
|
|
|
|
|
|
|
|
$this->assertTrue(
|
|
|
|
|
\OC\Share\Share::shareItem('folder', $fileId, \OCP\Share::SHARE_TYPE_GROUP, $this->group1->getGID(), \OCP\Constants::PERMISSION_READ + \OCP\Constants::PERMISSION_CREATE),
|
|
|
|
|
'Failed asserting that user 1 successfully shared "test/sub1" with group 1.'
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$result = \OCP\Share::getItemShared('folder', $fileId, Backend::FORMAT_SOURCE);
|
|
|
|
|
$this->assertNotEmpty($result);
|
|
|
|
|
$this->assertEquals(\OCP\Constants::PERMISSION_READ + \OCP\Constants::PERMISSION_CREATE, $result['permissions']);
|
|
|
|
|
|
|
|
|
|
$fileInfo = $view->getFileInfo('files/test/sub1/sub2');
|
|
|
|
|
$this->assertInstanceOf('\OC\Files\FileInfo', $fileInfo);
|
|
|
|
|
$fileId = $fileInfo->getId();
|
|
|
|
|
|
|
|
|
|
$this->assertTrue(
|
|
|
|
|
\OC\Share\Share::shareItem('folder', $fileId, \OCP\Share::SHARE_TYPE_USER, $this->user4->getUID(), \OCP\Constants::PERMISSION_READ),
|
|
|
|
|
'Failed asserting that user 1 successfully shared "test/sub1/sub2" with user 4.'
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$result = \OCP\Share::getItemShared('folder', $fileId, Backend::FORMAT_SOURCE);
|
|
|
|
|
$this->assertNotEmpty($result);
|
|
|
|
|
$this->assertEquals(\OCP\Constants::PERMISSION_READ, $result['permissions']);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testSharingAFileInsideAFolderThatIsAlreadyShared() {
|
|
|
|
|
\OC_User::setUserId($this->user1->getUID());
|
|
|
|
|
$view = new \OC\Files\View('/' . $this->user1->getUID() . '/');
|
|
|
|
|
$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');
|
|
|
|
|
$this->assertInstanceOf('\OC\Files\FileInfo', $folderInfo);
|
|
|
|
|
$folderId = $folderInfo->getId();
|
|
|
|
|
|
|
|
|
|
$fileInfo = $view->getFileInfo('files/test/sub1/file.txt');
|
|
|
|
|
$this->assertInstanceOf('\OC\Files\FileInfo', $fileInfo);
|
|
|
|
|
$fileId = $fileInfo->getId();
|
|
|
|
|
|
|
|
|
|
$this->assertTrue(
|
|
|
|
|
\OC\Share\Share::shareItem('folder', $folderId, \OCP\Share::SHARE_TYPE_GROUP, $this->group2->getGID(), \OCP\Constants::PERMISSION_READ + \OCP\Constants::PERMISSION_UPDATE),
|
|
|
|
|
'Failed asserting that user 1 successfully shared "test/sub1" with group 2.'
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$this->assertTrue(
|
|
|
|
|
\OC\Share\Share::shareItem('file', $fileId, \OCP\Share::SHARE_TYPE_USER, $this->user2->getUID(), \OCP\Constants::PERMISSION_READ),
|
|
|
|
|
'Failed asserting that user 1 successfully shared "test/sub1/file.txt" with user 2.'
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$result = \OC\Share\Share::getItemsSharedWithUser('file', $this->user2->getUID());
|
|
|
|
|
$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');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Test that unsharing from group will also delete all
|
|
|
|
|
* child entries
|
|
|
|
|
*/
|
|
|
|
|
public function testShareWithGroupThenUnshare() {
|
|
|
|
|
\OC_User::setUserId($this->user5->getUID());
|
|
|
|
|
\OC\Share\Share::shareItem(
|
|
|
|
|
'test',
|
|
|
|
|
'test.txt',
|
|
|
|
|
\OCP\Share::SHARE_TYPE_GROUP,
|
|
|
|
|
$this->group1->getGID(),
|
|
|
|
|
\OCP\Constants::PERMISSION_ALL
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$targetUsers = array($this->user1->getUID(), $this->user2->getUID(), $this->user3->getUID());
|
|
|
|
|
|
|
|
|
|
foreach($targetUsers as $targetUser) {
|
|
|
|
|
\OC_User::setUserId($targetUser);
|
|
|
|
|
$items = \OC\Share\Share::getItemsSharedWithUser(
|
|
|
|
|
'test',
|
|
|
|
|
$targetUser,
|
|
|
|
|
Backend::FORMAT_TARGET
|
|
|
|
|
);
|
|
|
|
|
$this->assertEquals(1, count($items));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
\OC_User::setUserId($this->user5->getUID());
|
|
|
|
|
\OC\Share\Share::unshare(
|
|
|
|
|
'test',
|
|
|
|
|
'test.txt',
|
|
|
|
|
\OCP\Share::SHARE_TYPE_GROUP,
|
|
|
|
|
$this->group1->getGID()
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// verify that all were deleted
|
|
|
|
|
foreach($targetUsers as $targetUser) {
|
|
|
|
|
\OC_User::setUserId($targetUser);
|
|
|
|
|
$items = \OC\Share\Share::getItemsSharedWithUser(
|
|
|
|
|
'test',
|
|
|
|
|
$targetUser,
|
|
|
|
|
Backend::FORMAT_TARGET
|
|
|
|
|
);
|
|
|
|
|
$this->assertEquals(0, count($items));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param boolean|string $token
|
|
|
|
|
* @return array
|
|
|
|
@ -400,100 +236,6 @@ class ShareTest extends \Test\TestCase {
|
|
|
|
|
$this->assertEmpty($expected, 'did not found all expected values');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testGetShareSubItemsWhenUserNotInGroup() {
|
|
|
|
|
\OC\Share\Share::shareItem('test', 'test.txt', \OCP\Share::SHARE_TYPE_GROUP, $this->group1->getGID(), \OCP\Constants::PERMISSION_READ);
|
|
|
|
|
|
|
|
|
|
$result = \OC\Share\Share::getItemsSharedWithUser('test', $this->user2->getUID());
|
|
|
|
|
$this->assertCount(1, $result);
|
|
|
|
|
|
|
|
|
|
$groupShareId = array_keys($result)[0];
|
|
|
|
|
|
|
|
|
|
// remove user from group
|
|
|
|
|
$userObject = \OC::$server->getUserManager()->get($this->user2->getUID());
|
|
|
|
|
\OC::$server->getGroupManager()->get($this->group1->getGID())->removeUser($userObject);
|
|
|
|
|
|
|
|
|
|
$result = \OC\Share\Share::getItemsSharedWithUser('test', $this->user2->getUID());
|
|
|
|
|
$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
|
|
|
|
|
'share_with' => $qb->expr()->literal($this->user2->getUID()),
|
|
|
|
|
'parent' => $qb->expr()->literal($groupShareId),
|
|
|
|
|
'uid_owner' => $qb->expr()->literal($this->user1->getUID()),
|
|
|
|
|
'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();
|
|
|
|
|
|
|
|
|
|
$result = \OC\Share\Share::getItemsSharedWithUser('test', $this->user2->getUID());
|
|
|
|
|
$this->assertCount(0, $result);
|
|
|
|
|
|
|
|
|
|
$qb->delete('share')->execute();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testShareItemWithLinkAndDefaultExpireDate() {
|
|
|
|
|
\OC_User::setUserId($this->user1->getUID());
|
|
|
|
|
|
|
|
|
|
$config = \OC::$server->getConfig();
|
|
|
|
|
|
|
|
|
|
$config->setAppValue('core', 'shareapi_default_expire_date', 'yes');
|
|
|
|
|
$config->setAppValue('core', 'shareapi_expire_after_n_days', '2');
|
|
|
|
|
|
|
|
|
|
$token = \OC\Share\Share::shareItem('test', 'test.txt', \OCP\Share::SHARE_TYPE_LINK, null, \OCP\Constants::PERMISSION_READ);
|
|
|
|
|
$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.'
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$config->deleteAppValue('core', 'shareapi_default_expire_date');
|
|
|
|
|
$config->deleteAppValue('core', 'shareapi_expire_after_n_days');
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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) {
|
|
|
|
|
\OC_User::setUserId($this->user1->getUID());
|
|
|
|
|
\OC\Share\Share::shareItem('test', 'test.txt', \OCP\Share::SHARE_TYPE_REMOTE, $remoteId, \OCP\Constants::PERMISSION_ALL);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider checkPasswordProtectedShareDataProvider
|
|
|
|
|
* @param $expected
|
|
|
|
@ -618,103 +360,6 @@ class ShareTest extends \Test\TestCase {
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Test case for #17560
|
|
|
|
|
*/
|
|
|
|
|
public function testAccesToSharedSubFolder() {
|
|
|
|
|
\OC_User::setUserId($this->user1->getUID());
|
|
|
|
|
$view = new \OC\Files\View('/' . $this->user1->getUID() . '/');
|
|
|
|
|
$view->mkdir('files/folder1');
|
|
|
|
|
|
|
|
|
|
$fileInfo = $view->getFileInfo('files/folder1');
|
|
|
|
|
$this->assertInstanceOf('\OC\Files\FileInfo', $fileInfo);
|
|
|
|
|
$fileId = $fileInfo->getId();
|
|
|
|
|
|
|
|
|
|
$this->assertTrue(
|
|
|
|
|
\OC\Share\Share::shareItem('folder', $fileId, \OCP\Share::SHARE_TYPE_USER, $this->user2->getUID(), \OCP\Constants::PERMISSION_ALL),
|
|
|
|
|
'Failed asserting that user 1 successfully shared "test" with user 2.'
|
|
|
|
|
);
|
|
|
|
|
$this->assertTrue(
|
|
|
|
|
\OC\Share\Share::shareItem('folder', $fileId, \OCP\Share::SHARE_TYPE_USER, $this->user3->getUID(), \OCP\Constants::PERMISSION_ALL),
|
|
|
|
|
'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(
|
|
|
|
|
\OC\Share\Share::shareItem('folder', $fileId, \OCP\Share::SHARE_TYPE_USER, $this->user4->getUID(), \OCP\Constants::PERMISSION_ALL),
|
|
|
|
|
'Failed asserting that user 1 successfully shared "test" with user 4.'
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$res = \OCP\Share::getItemShared(
|
|
|
|
|
'folder',
|
|
|
|
|
$fileId,
|
|
|
|
|
\OCP\Share::FORMAT_NONE,
|
|
|
|
|
null,
|
|
|
|
|
true
|
|
|
|
|
);
|
|
|
|
|
$this->assertCount(3, $res);
|
|
|
|
|
|
|
|
|
|
$this->assertTrue(
|
|
|
|
|
\OC\Share\Share::shareItem('folder', $fileId, \OCP\Share::SHARE_TYPE_USER, $this->user5->getUID(), \OCP\Constants::PERMISSION_ALL),
|
|
|
|
|
'Failed asserting that user 1 successfully shared "test" with user 5.'
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$res = \OCP\Share::getItemShared(
|
|
|
|
|
'folder',
|
|
|
|
|
$fileId,
|
|
|
|
|
\OCP\Share::FORMAT_NONE,
|
|
|
|
|
null,
|
|
|
|
|
true
|
|
|
|
|
);
|
|
|
|
|
$this->assertCount(4, $res);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testShareWithSelfError() {
|
|
|
|
|
\OC_User::setUserId($this->user1->getUID());
|
|
|
|
|
$view = new \OC\Files\View('/' . $this->user1->getUID() . '/');
|
|
|
|
|
$view->mkdir('files/folder1');
|
|
|
|
|
|
|
|
|
|
$fileInfo = $view->getFileInfo('files/folder1');
|
|
|
|
|
$this->assertInstanceOf('\OC\Files\FileInfo', $fileInfo);
|
|
|
|
|
$fileId = $fileInfo->getId();
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
\OC\Share\Share::shareItem('folder', $fileId, \OCP\Share::SHARE_TYPE_USER, $this->user1->getUID(), \OCP\Constants::PERMISSION_ALL);
|
|
|
|
|
$this->fail();
|
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
|
$this->assertEquals('Sharing /folder1 failed, because you can not share with yourself', $e->getMessage());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function testShareWithOwnerError() {
|
|
|
|
|
\OC_User::setUserId($this->user1->getUID());
|
|
|
|
|
$view = new \OC\Files\View('/' . $this->user1->getUID() . '/');
|
|
|
|
|
$view->mkdir('files/folder1');
|
|
|
|
|
|
|
|
|
|
$fileInfo = $view->getFileInfo('files/folder1');
|
|
|
|
|
$this->assertInstanceOf('\OC\Files\FileInfo', $fileInfo);
|
|
|
|
|
$fileId = $fileInfo->getId();
|
|
|
|
|
|
|
|
|
|
$this->assertTrue(
|
|
|
|
|
\OC\Share\Share::shareItem('folder', $fileId, \OCP\Share::SHARE_TYPE_USER, $this->user2->getUID(), \OCP\Constants::PERMISSION_ALL),
|
|
|
|
|
'Failed asserting that user 1 successfully shared "test" with user 2.'
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
\OC_User::setUserId($this->user2->getUID());
|
|
|
|
|
try {
|
|
|
|
|
\OC\Share\Share::shareItem('folder', $fileId, \OCP\Share::SHARE_TYPE_USER, $this->user1->getUID(), \OCP\Constants::PERMISSION_ALL);
|
|
|
|
|
$this->fail();
|
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
|
$this->assertEquals('Sharing failed, because the user ' . $this->user1->getUID() . ' is the original sharer', $e->getMessage());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class DummyShareClass extends \OC\Share\Share {
|
|
|
|
|