]> source.dussan.org Git - nextcloud-server.git/commitdiff
add test for shared mount conflict resolution 36052/head
authorRobin Appelman <robin@icewind.nl>
Mon, 9 Jan 2023 14:30:45 +0000 (15:30 +0100)
committerRobin Appelman <robin@icewind.nl>
Mon, 9 Jan 2023 14:30:45 +0000 (15:30 +0100)
Signed-off-by: Robin Appelman <robin@icewind.nl>
apps/files_sharing/tests/SharedMountTest.php
apps/files_sharing/tests/TestCase.php

index 8b19a856dda1d110af4b79b57faa0eea8bdf92fa..2e169f878ce9b44f3d60aa280687c010ad17fa68 100644 (file)
@@ -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;
@@ -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();
@@ -325,6 +333,112 @@ class SharedMountTest extends TestCase {
                $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);
+               $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 {
index d4077c8481625628dfbb8462f4b36c80b7264b75..2bd83d6c9c28f5fe3e90ac0a0dc3c580e68d52a5 100644 (file)
@@ -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;
 
@@ -124,6 +128,7 @@ abstract class TestCase extends \Test\TestCase {
 
                $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();