Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add test for shared mount conflict resolution #36052

Merged
merged 1 commit into from
Jan 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 114 additions & 0 deletions apps/files_sharing/tests/SharedMountTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
*/
namespace OCA\Files_Sharing\Tests;

use OC\Memcache\ArrayCache;
use OCA\Files_Sharing\MountProvider;
use OCP\ICacheFactory;
use OCP\IGroupManager;
use OCP\IUserManager;
use OCP\Share\IShare;
Expand All @@ -46,19 +49,24 @@ class SharedMountTest extends TestCase {
/** @var IUserManager */
private $userManager;

private $folder2;

protected function setUp(): void {
parent::setUp();

$this->folder = '/folder_share_storage_test';
$this->folder2 = '/folder_share_storage_test2';

$this->filename = '/share-api-storage.txt';


$this->view->mkdir($this->folder);
$this->view->mkdir($this->folder2);

// save file with content
$this->view->file_put_contents($this->filename, 'root file');
$this->view->file_put_contents($this->folder . $this->filename, 'file in subfolder');
$this->view->file_put_contents($this->folder2 . $this->filename, 'file in subfolder2');

$this->groupManager = \OC::$server->getGroupManager();
$this->userManager = \OC::$server->getUserManager();
Expand Down Expand Up @@ -325,6 +333,112 @@ public function testPermissionUpgradeOnUserDeletedGroupShare() {
$testGroup->removeUser($user2);
$testGroup->removeUser($user3);
}

/**
* test if the mount point gets renamed if a folder exists at the target
*/
public function testShareMountOverFolder() {
self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
$this->view2->mkdir('bar');

self::loginHelper(self::TEST_FILES_SHARING_API_USER1);

// share to user
$share = $this->share(
IShare::TYPE_USER,
$this->folder,
self::TEST_FILES_SHARING_API_USER1,
self::TEST_FILES_SHARING_API_USER2,
\OCP\Constants::PERMISSION_ALL);
$this->shareManager->acceptShare($share, self::TEST_FILES_SHARING_API_USER2);

$share->setTarget('/bar');
$this->shareManager->moveShare($share, self::TEST_FILES_SHARING_API_USER2);

$share = $this->shareManager->getShareById($share->getFullId());

self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
// share should have been moved

$share = $this->shareManager->getShareById($share->getFullId());
$this->assertSame('/bar (2)', $share->getTarget());

//cleanup
self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
$this->shareManager->deleteShare($share);
$this->view->unlink($this->folder);
}

/**
* test if the mount point gets renamed if another share exists at the target
*/
public function testShareMountOverShare() {
// create a shared cache
$caches = [];
$cacheFactory = $this->createMock(ICacheFactory::class);
$cacheFactory->method('createLocal')
->willReturnCallback(function(string $prefix) use (&$caches) {
if (!isset($caches[$prefix])) {
$caches[$prefix] = new ArrayCache($prefix);
}
return $caches[$prefix];
});
$cacheFactory->method('createDistributed')
->willReturnCallback(function(string $prefix) use (&$caches) {
if (!isset($caches[$prefix])) {
$caches[$prefix] = new ArrayCache($prefix);
}
return $caches[$prefix];
});

// hack to overwrite the cache factory, we can't use the proper "overwriteService" since the mount provider is created before this test is called
$mountProvider = \OCP\Server::get(MountProvider::class);
$reflectionClass = new \ReflectionClass($mountProvider);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you might be able to use self::invokePrivate() also to set values

$reflectionCacheFactory = $reflectionClass->getProperty("cacheFactory");
$reflectionCacheFactory->setAccessible(true);
$reflectionCacheFactory->setValue($mountProvider, $cacheFactory);

// share to user
$share = $this->share(
IShare::TYPE_USER,
$this->folder,
self::TEST_FILES_SHARING_API_USER1,
self::TEST_FILES_SHARING_API_USER2,
\OCP\Constants::PERMISSION_ALL);
$this->shareManager->acceptShare($share, self::TEST_FILES_SHARING_API_USER2);

$share->setTarget('/foobar');
$this->shareManager->moveShare($share, self::TEST_FILES_SHARING_API_USER2);


// share to user
$share2 = $this->share(
IShare::TYPE_USER,
$this->folder2,
self::TEST_FILES_SHARING_API_USER1,
self::TEST_FILES_SHARING_API_USER2,
\OCP\Constants::PERMISSION_ALL);
$this->shareManager->acceptShare($share2, self::TEST_FILES_SHARING_API_USER2);

$share2->setTarget('/foobar');
$this->shareManager->moveShare($share2, self::TEST_FILES_SHARING_API_USER2);

self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
// one of the shares should have been moved

$share = $this->shareManager->getShareById($share->getFullId());
$share2 = $this->shareManager->getShareById($share2->getFullId());

// we don't know or care which share got the "(2)" just that one of them did
$this->assertNotEquals($share->getTarget(), $share2->getTarget());
$this->assertSame('/foobar', min($share->getTarget(), $share2->getTarget()));
$this->assertSame('/foobar (2)', max($share->getTarget(), $share2->getTarget()));

//cleanup
self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
$this->shareManager->deleteShare($share);
$this->view->unlink($this->folder);
}
}

class DummyTestClassSharedMount extends \OCA\Files_Sharing\SharedMount {
Expand Down
5 changes: 5 additions & 0 deletions apps/files_sharing/tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ abstract class TestCase extends \Test\TestCase {
* @var \OC\Files\View
*/
public $view;
/**
* @var \OC\Files\View
*/
public $view2;
public $folder;
public $subfolder;

Expand Down Expand Up @@ -124,6 +128,7 @@ protected function setUp(): void {

$this->data = 'foobar';
$this->view = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER1 . '/files');
$this->view2 = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER2 . '/files');

$this->shareManager = \OC::$server->getShareManager();
$this->rootFolder = \OC::$server->getRootFolder();
Expand Down