Merge pull request #8854 from nextcloud/drop-old-sharing-code

Get rid of shareItem
This commit is contained in:
Morris Jobke 2018-06-29 10:40:32 +02:00 committed by GitHub
commit 326f07e756
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 2 additions and 490 deletions

View File

@ -1213,28 +1213,6 @@ class ApiTest extends TestCase {
\OC_Hook::clear('OC_Filesystem', 'post_initMountPoints');
\OC_Hook::clear('\OCA\Files_Sharing\Tests\ApiTest', 'initTestMountPointsHook');
}
/**
* @expectedException \Exception
*/
public function XtestShareNonExisting() {
self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
$id = PHP_INT_MAX - 1;
\OC\Share\Share::shareItem('file', $id, \OCP\Share::SHARE_TYPE_LINK, self::TEST_FILES_SHARING_API_USER2, 31);
}
/**
* @expectedException \Exception
*/
public function testShareNotOwner() {
self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
\OC\Files\Filesystem::file_put_contents('foo.txt', 'bar');
$info = \OC\Files\Filesystem::getFileInfo('foo.txt');
self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
\OC\Share\Share::shareItem('file', $info->getId(), \OCP\Share::SHARE_TYPE_LINK, self::TEST_FILES_SHARING_API_USER2, 31);
}
public function datesProvider() {
$date = new \DateTime();

View File

@ -1,113 +0,0 @@
<?php
/**
* @copyright Copyright (c) 2016, ownCloud, Inc.
*
* @author Björn Schießle <bjoern@schiessle.org>
* @author Joas Schilling <coding@schilljs.com>
* @author Morris Jobke <hey@morrisjobke.de>
* @author Robin Appelman <robin@icewind.nl>
* @author Roeland Jago Douma <roeland@famdouma.nl>
* @author Thomas Müller <thomas.mueller@tmit.eu>
*
* @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,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
namespace OCA\Files_Sharing\Tests;
/**
* Class BackendTest
*
* @group DB
*/
class BackendTest extends TestCase {
const TEST_FOLDER_NAME = '/folder_share_api_test';
public $folder;
public $subfolder;
public $subsubfolder;
protected function setUp() {
parent::setUp();
$this->folder = self::TEST_FOLDER_NAME;
$this->subfolder = '/subfolder_share_backend_test';
$this->subsubfolder = '/subsubfolder_share_backend_test';
$this->filename = '/share-backend-test.txt';
// save file with content
$this->view->file_put_contents($this->filename, $this->data);
$this->view->mkdir($this->folder);
$this->view->mkdir($this->folder . $this->subfolder);
$this->view->mkdir($this->folder . $this->subfolder . $this->subsubfolder);
$this->view->file_put_contents($this->folder.$this->filename, $this->data);
$this->view->file_put_contents($this->folder . $this->subfolder . $this->filename, $this->data);
$this->view->file_put_contents($this->folder . $this->subfolder . $this->subsubfolder . $this->filename, $this->data);
}
protected function tearDown() {
if ($this->view) {
$this->view->unlink($this->filename);
$this->view->deleteAll($this->folder);
}
parent::tearDown();
}
public function testGetParents() {
$fileinfo1 = $this->view->getFileInfo($this->folder);
$fileinfo2 = $this->view->getFileInfo($this->folder . $this->subfolder . $this->subsubfolder);
$fileinfo3 = $this->view->getFileInfo($this->folder . $this->subfolder . $this->subsubfolder . $this->filename);
$this->assertTrue(\OC\Share\Share::shareItem('folder', $fileinfo1['fileid'], \OCP\Share::SHARE_TYPE_USER,
self::TEST_FILES_SHARING_API_USER2, 31));
$this->assertTrue(\OC\Share\Share::shareItem('folder', $fileinfo2['fileid'], \OCP\Share::SHARE_TYPE_USER,
self::TEST_FILES_SHARING_API_USER3, 31));
$backend = new \OCA\Files_Sharing\ShareBackend\Folder();
$result = $backend->getParents($fileinfo3['fileid']);
$this->assertSame(2, count($result));
$count1 = 0;
$count2 = 0;
foreach($result as $r) {
if ($r['path'] === 'files' . $this->folder) {
$this->assertSame(ltrim($this->folder, '/'), $r['collection']['path']);
$count1++;
} elseif ($r['path'] === 'files' . $this->folder . $this->subfolder . $this->subsubfolder) {
$this->assertSame(ltrim($this->subsubfolder, '/'), $r['collection']['path']);
$count2++;
} else {
$this->assertTrue(false, 'unexpected result');
}
}
$this->assertSame(1, $count1);
$this->assertSame(1, $count2);
$result1 = $backend->getParents($fileinfo3['fileid'], self::TEST_FILES_SHARING_API_USER3);
$this->assertSame(1, count($result1));
$elemet = reset($result1);
$this->assertSame('files' . $this->folder . $this->subfolder . $this->subsubfolder ,$elemet['path']);
$this->assertSame(ltrim($this->subsubfolder, '/') ,$elemet['collection']['path']);
}
}

View File

@ -327,6 +327,7 @@ class Share extends Constants {
* @throws \OC\HintException when the share type is remote and the shareWith is invalid
* @throws \Exception
* @since 5.0.0 - parameter $itemSourceName was added in 6.0.0, parameter $expirationDate was added in 7.0.0, parameter $passwordChanged added in 9.0.0
* @deprecated 14.0.0 TESTS ONLY - this methods is as of 2018-06 only used by tests
*/
public static function shareItem($itemType, $itemSource, $shareType, $shareWith, $permissions, $itemSourceName = null, \DateTime $expirationDate = null, $passwordChanged = null) {

View File

@ -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 {

View File

@ -306,6 +306,7 @@ class TagsTest extends \Test\TestCase {
$this->assertFalse($otherTagger->hasTag($testTag));
\OC_User::setUserId($this->user->getUID());
// TODO new sharing
\OC\Share\Share::shareItem('test', 1, \OCP\Share::SHARE_TYPE_USER, $otherUserId, \OCP\Constants::PERMISSION_READ);
\OC_User::setUserId($otherUserId);