Skip to content

Commit

Permalink
Fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rullzer committed Apr 13, 2016
1 parent 9b322c4 commit 60a523f
Show file tree
Hide file tree
Showing 21 changed files with 597 additions and 664 deletions.
1 change: 1 addition & 0 deletions apps/files_sharing/lib/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ public static function generateUniqueTarget($path, $excludeList, $view) {
$name = $pathinfo['filename'];
$dir = $pathinfo['dirname'];
$i = 2;
$ls = $view->getDirectoryContent($dir);
while ($view->file_exists($path) || in_array($path, $excludeList)) {
$path = Filesystem::normalizePath($dir . '/' . $name . ' ('.$i.')' . $ext);
$i++;
Expand Down
2 changes: 1 addition & 1 deletion apps/files_sharing/lib/sharedpropagator.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class SharedPropagator extends Propagator {
public function propagateChange($internalPath, $time, $sizeDifference = 0) {
$source = $this->storage->getSourcePath($internalPath);
/** @var \OC\Files\Storage\Storage $storage */
list($storage, $sourceInternalPath) = \OC\Files\Filesystem::resolvePath($source);
list($storage, $sourceInternalPath) = $this->storage->resolvePath($internalPath);
return $storage->getPropagator()->propagateChange($sourceInternalPath, $time, $sizeDifference);
}
}
78 changes: 53 additions & 25 deletions apps/files_sharing/lib/sharedstorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ class Shared extends \OC\Files\Storage\Wrapper\Jail implements ISharedStorage {
*/
private $sourceStorage;

/** @var string */
private $user;

/**
* @var \OCP\ILogger
*/
Expand All @@ -75,6 +78,7 @@ public function __construct($arguments) {
$this->ownerView = $arguments['ownerView'];
$this->logger = \OC::$server->getLogger();
$this->newShare = $arguments['newShare'];
$this->user = $arguments['user'];

Filesystem::initMountPoints($this->newShare->getShareOwner());
$sourcePath = $this->ownerView->getPath($this->newShare->getNodeId());
Expand Down Expand Up @@ -225,6 +229,31 @@ public function fopen($path, $mode) {
return false;
}

/**
* see http://php.net/manual/en/function.rename.php
*
* @param string $path1
* @param string $path2
* @return bool
*/
public function rename($path1, $path2) {
$isPartFile = pathinfo($path1, PATHINFO_EXTENSION) === 'part';
$targetExists = $this->file_exists($path2);
$sameFodler = dirname($path1) === dirname($path2);

if ($targetExists || ($sameFodler && !$isPartFile)) {
if (!$this->isUpdatable('')) {
return false;
}
} else {
if (!$this->isCreatable('')) {
return false;
}
}

return parent::rename($path1, $path2);
}

/**
* return mount point of share, relative to data/user/files
*
Expand Down Expand Up @@ -329,6 +358,8 @@ public function getOwner($path) {
* @return bool
*/
public function unshareStorage() {
\OC::$server->getShareManager()->deleteFromSelf($this->newShare, $this->user);
return true;
$result = true;
if (!empty($this->share['grouped'])) {
foreach ($this->share['grouped'] as $share) {
Expand All @@ -347,34 +378,10 @@ public function unshareStorage() {
* @return array
*/
public function resolvePath($path) {
$source = $this->getOwner($path) . '/' . $path;
$source = '/' . $this->newShare->getShareOwner() . '/' . $this->getSourcePath($path);
return \OC\Files\Filesystem::resolvePath($source);
}

/**
* @param \OCP\Files\Storage $sourceStorage
* @param string $sourceInternalPath
* @param string $targetInternalPath
* @return bool
*/
public function copyFromStorage(\OCP\Files\Storage $sourceStorage, $sourceInternalPath, $targetInternalPath) {
/** @var \OCP\Files\Storage $targetStorage */
list($targetStorage, $targetInternalPath) = $this->resolvePath($targetInternalPath);
return $targetStorage->copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
}

/**
* @param \OCP\Files\Storage $sourceStorage
* @param string $sourceInternalPath
* @param string $targetInternalPath
* @return bool
*/
public function moveFromStorage(\OCP\Files\Storage $sourceStorage, $sourceInternalPath, $targetInternalPath) {
/** @var \OCP\Files\Storage $targetStorage */
list($targetStorage, $targetInternalPath) = $this->resolvePath($targetInternalPath);
return $targetStorage->moveFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
}

/**
* @param string $path
* @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
Expand Down Expand Up @@ -440,4 +447,25 @@ public function setAvailability($available) {
public function getSourceStorage() {
return $this->sourceStorage;
}

/**
* @param \OCP\Files\Storage $sourceStorage
* @param string $sourceInternalPath
* @param string $targetInternalPath
* @return bool
*/
public function copyFromStorage(\OCP\Files\Storage $sourceStorage, $sourceInternalPath, $targetInternalPath) {
return parent::copyFromStorage($sourceStorage, $sourceInternalPath, $this->getSourcePath($targetInternalPath));
}

/**
* @param \OCP\Files\Storage $sourceStorage
* @param string $sourceInternalPath
* @param string $targetInternalPath
* @return bool
*/
public function moveFromStorage(\OCP\Files\Storage $sourceStorage, $sourceInternalPath, $targetInternalPath) {
return parent::moveFromStorage($sourceStorage, $sourceInternalPath, $this->getSourcePath($targetInternalPath));
}

}
46 changes: 26 additions & 20 deletions apps/files_sharing/tests/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,6 @@ class Test_Files_Sharing_Api extends TestCase {

private static $tempStorage;

/** @var \OCP\Share\IManager */
private $shareManager;

/** @var \OCP\Files\Folder */
private $userFolder;

Expand All @@ -66,7 +63,6 @@ protected function setUp() {
$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->shareManager = \OC::$server->getShareManager();
$this->userFolder = \OC::$server->getUserFolder(self::TEST_FILES_SHARING_API_USER1);
}

Expand Down Expand Up @@ -1195,10 +1191,13 @@ public function testShareFolderWithAMountPoint() {

$fileInfo = $this->view->getFileInfo($this->folder);

$result = \OCP\Share::shareItem('folder', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2, 31);

$this->assertTrue($result);
$share = $this->share(
\OCP\Share::SHARE_TYPE_USER,
$this->folder,
self::TEST_FILES_SHARING_API_USER1,
self::TEST_FILES_SHARING_API_USER2,
\OCP\Constants::PERMISSION_ALL
);

// user2 shares a file from the folder as link
\Test_Files_Sharing_Api::loginHelper(\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
Expand All @@ -1215,14 +1214,20 @@ public function testShareFolderWithAMountPoint() {

$this->assertTrue($fileInfo2 instanceof \OC\Files\FileInfo);

$pass = true;
try {
$result2 = \OCP\Share::shareItem('folder', $fileInfo2['fileid'], \OCP\Share::SHARE_TYPE_USER,
\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER3, 31);
$this->share(
\OCP\Share::SHARE_TYPE_USER,
'localDir',
self::TEST_FILES_SHARING_API_USER2,
self::TEST_FILES_SHARING_API_USER3,
\OCP\Constants::PERMISSION_ALL
);
} catch (\Exception $e) {
$result2 = false;
$pass = false;
}

$this->assertFalse($result2);
$this->assertFalse($pass);

//cleanup

Expand All @@ -1232,8 +1237,7 @@ public function testShareFolderWithAMountPoint() {

\Test_Files_Sharing_Api::loginHelper(\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER1);

\OCP\Share::unshare('folder', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
$this->shareManager->deleteShare($share);
}

/**
Expand Down Expand Up @@ -1264,10 +1268,13 @@ public function testShareStorageMountPoint() {
$fileInfo = $this->view->getFileInfo($this->folder);

// user 1 shares the mount point folder with user2
$result = \OCP\Share::shareItem('folder', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2, 31);

$this->assertTrue($result);
$share = $this->share(
\OCP\Share::SHARE_TYPE_USER,
$this->folder,
self::TEST_FILES_SHARING_API_USER1,
self::TEST_FILES_SHARING_API_USER2,
\OCP\Constants::PERMISSION_ALL
);

// user2: check that mount point name appears correctly
\Test_Files_Sharing_Api::loginHelper(\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
Expand All @@ -1279,8 +1286,7 @@ public function testShareStorageMountPoint() {

\Test_Files_Sharing_Api::loginHelper(\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER1);

\OCP\Share::unshare('folder', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
$this->shareManager->deleteShare($share);

\OC_Hook::clear('OC_Filesystem', 'post_initMountPoints', '\Test_Files_Sharing_Api', 'initTestMountPointsHook');
}
Expand Down
18 changes: 13 additions & 5 deletions apps/files_sharing/tests/etagpropagation.php
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,7 @@ public function testOwnerUnsharesFlatReshares() {

public function testRecipientUnsharesFromSelf() {
$this->loginAsUser(self::TEST_FILES_SHARING_API_USER2);
$ls = $this->rootView->getDirectoryContent('/' . self::TEST_FILES_SHARING_API_USER2 . '/files/sub1/sub2/');
$this->assertTrue(
$this->rootView->unlink('/' . self::TEST_FILES_SHARING_API_USER2 . '/files/sub1/sub2/folder')
);
Expand Down Expand Up @@ -438,14 +439,21 @@ public function testRecipientUploadInDirectReshare() {
}

public function testEtagChangeOnPermissionsChange() {
$this->loginAsUser(self::TEST_FILES_SHARING_API_USER1);
$userFolder = $this->rootFolder->getUserFolder(self::TEST_FILES_SHARING_API_USER1);
$node = $userFolder->get('/sub1/sub2/folder');

$view = new View('/' . self::TEST_FILES_SHARING_API_USER1 . '/files');
$folderInfo = $view->getFileInfo('/sub1/sub2/folder');
$shares = $this->shareManager->getSharesBy(self::TEST_FILES_SHARING_API_USER1, \OCP\Share::SHARE_TYPE_USER, $node);
/** @var \OCP\Share\IShare[] $shares */
$shares = array_filter($shares, function(\OCP\Share\IShare $share) {
return $share->getSharedWith() === self::TEST_FILES_SHARING_API_USER2;
});
$this->assertCount(1, $shares);

\OCP\Share::setPermissions('folder', $folderInfo->getId(), \OCP\Share::SHARE_TYPE_USER, self::TEST_FILES_SHARING_API_USER2, 17);
$share = $shares[0];
$share->setPermissions(\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_SHARE);
$this->shareManager->updateShare($share);

$this->assertEtagsForFoldersChanged([self::TEST_FILES_SHARING_API_USER2, self::TEST_FILES_SHARING_API_USER4]);
$this->assertEtagsForFoldersChanged([self::TEST_FILES_SHARING_API_USER2]);

$this->assertAllUnchanged();
}
Expand Down
8 changes: 7 additions & 1 deletion apps/files_sharing/tests/locking.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,13 @@ public function setUp() {
Filesystem::file_put_contents('/foo/bar.txt', 'asd');
$fileId = Filesystem::getFileInfo('/foo/bar.txt')->getId();

\OCP\Share::shareItem('file', $fileId, \OCP\Share::SHARE_TYPE_USER, $this->recipientUid, 31);
$this->share(
\OCP\Share::SHARE_TYPE_USER,
'/foo/bar.txt',
$this->ownerUid,
$this->recipientUid,
\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_SHARE
);

$this->loginAsUser($this->recipientUid);
$this->assertTrue(Filesystem::file_exists('bar.txt'));
Expand Down
4 changes: 0 additions & 4 deletions apps/files_sharing/tests/permissions.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,8 @@ class Test_Files_Sharing_Permissions extends OCA\Files_sharing\Tests\TestCase {
/** @var Cache */
private $ownerCache;

/** @var \OCP\Share\IManager */
private $shareManager;

protected function setUp() {
parent::setUp();
$this->shareManager = \OC::$server->getShareManager();

self::loginHelper(self::TEST_FILES_SHARING_API_USER1);

Expand Down
Loading

0 comments on commit 60a523f

Please sign in to comment.