123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434 |
- <?php
- namespace OCA\Files_Sharing\Tests;
- use OC\Files\Filesystem;
- use OC\Files\View;
- use OC\Memcache\ArrayCache;
- use OCA\Files_Sharing\MountProvider;
- use OCA\Files_Sharing\SharedMount;
- use OCP\Constants;
- use OCP\ICacheFactory;
- use OCP\IGroupManager;
- use OCP\IUserManager;
- use OCP\Server;
- use OCP\Share\IShare;
- class SharedMountTest extends TestCase {
-
- private $groupManager;
-
- 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);
-
- $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();
- }
- protected function tearDown(): void {
- if ($this->view) {
- if ($this->view->file_exists($this->folder)) {
- $this->view->unlink($this->folder);
- }
- if ($this->view->file_exists($this->filename)) {
- $this->view->unlink($this->filename);
- }
- }
- parent::tearDown();
- }
-
- public function testShareMountLoseParentFolder(): void {
-
- $share = $this->share(
- IShare::TYPE_USER,
- $this->folder,
- self::TEST_FILES_SHARING_API_USER1,
- self::TEST_FILES_SHARING_API_USER2,
- Constants::PERMISSION_ALL);
- $this->shareManager->acceptShare($share, self::TEST_FILES_SHARING_API_USER2);
- $share->setTarget('/foo/bar' . $this->folder);
- $this->shareManager->moveShare($share, self::TEST_FILES_SHARING_API_USER2);
- $share = $this->shareManager->getShareById($share->getFullId());
- $this->assertSame('/foo/bar' . $this->folder, $share->getTarget());
- self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
-
- $share = $this->shareManager->getShareById($share->getFullId());
- $this->assertSame($this->folder, $share->getTarget());
-
- self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
- $this->shareManager->deleteShare($share);
- $this->view->unlink($this->folder);
- }
-
- public function testDeleteParentOfMountPoint(): void {
-
- $share = $this->share(
- IShare::TYPE_USER,
- $this->folder,
- self::TEST_FILES_SHARING_API_USER1,
- self::TEST_FILES_SHARING_API_USER2,
- Constants::PERMISSION_ALL
- );
- self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
- $user2View = new View('/' . self::TEST_FILES_SHARING_API_USER2 . '/files');
- $this->assertTrue($user2View->file_exists($this->folder));
-
- $result = $user2View->mkdir('localfolder');
- $this->assertTrue($result);
-
- $result = $user2View->rename($this->folder, '/localfolder/' . $this->folder);
- $this->assertTrue($result);
-
- $this->assertFalse($user2View->is_dir($this->folder));
-
- $result = $user2View->unlink('/localfolder');
- $this->assertTrue($result);
-
- self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
-
- $this->assertTrue($user2View->is_dir($this->folder));
-
- self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
- $this->view->unlink($this->folder);
- }
- public function testMoveSharedFile(): void {
- $share = $this->share(
- IShare::TYPE_USER,
- $this->filename,
- self::TEST_FILES_SHARING_API_USER1,
- self::TEST_FILES_SHARING_API_USER2,
- Constants::PERMISSION_READ | Constants::PERMISSION_UPDATE | Constants::PERMISSION_SHARE
- );
- self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
- Filesystem::rename($this->filename, $this->filename . '_renamed');
- $this->assertTrue(Filesystem::file_exists($this->filename . '_renamed'));
- $this->assertFalse(Filesystem::file_exists($this->filename));
- self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
- $this->assertTrue(Filesystem::file_exists($this->filename));
- $this->assertFalse(Filesystem::file_exists($this->filename . '_renamed'));
-
- self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
- Filesystem::rename($this->filename . '_renamed', $this->filename);
- $this->assertFalse(Filesystem::file_exists($this->filename . '_renamed'));
- $this->assertTrue(Filesystem::file_exists($this->filename));
-
- $this->shareManager->deleteShare($share);
- }
-
- public function testMoveGroupShare(): void {
- $testGroup = $this->groupManager->createGroup('testGroup');
- $user1 = $this->userManager->get(self::TEST_FILES_SHARING_API_USER1);
- $user2 = $this->userManager->get(self::TEST_FILES_SHARING_API_USER2);
- $user3 = $this->userManager->get(self::TEST_FILES_SHARING_API_USER3);
- $testGroup->addUser($user1);
- $testGroup->addUser($user2);
- $testGroup->addUser($user3);
- $fileinfo = $this->view->getFileInfo($this->filename);
- $share = $this->share(
- IShare::TYPE_GROUP,
- $this->filename,
- self::TEST_FILES_SHARING_API_USER1,
- 'testGroup',
- Constants::PERMISSION_READ | Constants::PERMISSION_UPDATE | Constants::PERMISSION_SHARE
- );
- $this->shareManager->acceptShare($share, $user1->getUID());
- $this->shareManager->acceptShare($share, $user2->getUID());
- $this->shareManager->acceptShare($share, $user3->getUID());
- self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
- $this->assertTrue(Filesystem::file_exists($this->filename));
- Filesystem::rename($this->filename, 'newFileName');
- $this->assertTrue(Filesystem::file_exists('newFileName'));
- $this->assertFalse(Filesystem::file_exists($this->filename));
- self::loginHelper(self::TEST_FILES_SHARING_API_USER3);
- $this->assertTrue(Filesystem::file_exists($this->filename));
- $this->assertFalse(Filesystem::file_exists('newFileName'));
- self::loginHelper(self::TEST_FILES_SHARING_API_USER3);
- $this->assertTrue(Filesystem::file_exists($this->filename));
- $this->assertFalse(Filesystem::file_exists('newFileName'));
-
- self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
- $this->shareManager->deleteShare($share);
- $testGroup->removeUser($user1);
- $testGroup->removeUser($user2);
- $testGroup->removeUser($user3);
- }
-
- public function testStripUserFilesPath($path, $expectedResult, $exception): void {
- $testClass = new DummyTestClassSharedMount(null, null);
- try {
- $result = $testClass->stripUserFilesPathDummy($path);
- $this->assertSame($expectedResult, $result);
- } catch (\Exception $e) {
- if ($exception) {
- $this->assertSame(10, $e->getCode());
- } else {
- $this->assertTrue(false, 'Exception caught, but expected: ' . $expectedResult);
- }
- }
- }
- public function dataProviderTestStripUserFilesPath() {
- return [
- ['/user/files/foo.txt', '/foo.txt', false],
- ['/user/files/folder/foo.txt', '/folder/foo.txt', false],
- ['/data/user/files/foo.txt', null, true],
- ['/data/user/files/', null, true],
- ['/files/foo.txt', null, true],
- ['/foo.txt', null, true],
- ];
- }
-
- public function testPermissionUpgradeOnUserDeletedGroupShare(): void {
- $testGroup = $this->groupManager->createGroup('testGroup');
- $user1 = $this->userManager->get(self::TEST_FILES_SHARING_API_USER1);
- $user2 = $this->userManager->get(self::TEST_FILES_SHARING_API_USER2);
- $user3 = $this->userManager->get(self::TEST_FILES_SHARING_API_USER3);
- $testGroup->addUser($user1);
- $testGroup->addUser($user2);
- $testGroup->addUser($user3);
- $connection = \OC::$server->getDatabaseConnection();
-
- $fileinfo = $this->view->getFileInfo($this->folder);
- $share = $this->share(
- IShare::TYPE_GROUP,
- $this->folder,
- self::TEST_FILES_SHARING_API_USER1,
- 'testGroup',
- Constants::PERMISSION_READ
- );
- $this->shareManager->acceptShare($share, $user1->getUID());
- $this->shareManager->acceptShare($share, $user2->getUID());
- $this->shareManager->acceptShare($share, $user3->getUID());
-
- self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
- $this->assertTrue(Filesystem::file_exists($this->folder));
- $result = $this->shareManager->getShareById($share->getFullId(), self::TEST_FILES_SHARING_API_USER2);
- $this->assertNotEmpty($result);
- $this->assertEquals(Constants::PERMISSION_READ, $result->getPermissions());
-
- $this->assertTrue(Filesystem::rmdir($this->folder));
- $this->assertFalse(Filesystem::file_exists($this->folder));
-
- $result = $this->shareManager->getShareById($share->getFullId(), self::TEST_FILES_SHARING_API_USER2);
- $this->assertEquals(0, $result->getPermissions());
-
- self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
- $share->setPermissions(Constants::PERMISSION_ALL);
- $share = $this->shareManager->updateShare($share);
-
- self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
- $this->assertFalse(Filesystem::file_exists($this->folder));
- $result = $this->shareManager->getShareById($share->getFullId(), self::TEST_FILES_SHARING_API_USER2);
- $this->assertEquals(0, $result->getPermissions());
- $this->shareManager->deleteShare($share);
-
- self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
- $testGroup->removeUser($user1);
- $testGroup->removeUser($user2);
- $testGroup->removeUser($user3);
- }
-
- public function testShareMountOverFolder(): void {
- self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
- $this->view2->mkdir('bar');
- self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
-
- $share = $this->share(
- IShare::TYPE_USER,
- $this->folder,
- self::TEST_FILES_SHARING_API_USER1,
- self::TEST_FILES_SHARING_API_USER2,
- 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 = $this->shareManager->getShareById($share->getFullId());
- $this->assertSame('/bar (2)', $share->getTarget());
-
- self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
- $this->shareManager->deleteShare($share);
- $this->view->unlink($this->folder);
- }
-
- public function testShareMountOverShare(): void {
-
- $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];
- });
-
- $mountProvider = Server::get(MountProvider::class);
- $reflectionClass = new \ReflectionClass($mountProvider);
- $reflectionCacheFactory = $reflectionClass->getProperty('cacheFactory');
- $reflectionCacheFactory->setAccessible(true);
- $reflectionCacheFactory->setValue($mountProvider, $cacheFactory);
-
- $share = $this->share(
- IShare::TYPE_USER,
- $this->folder,
- self::TEST_FILES_SHARING_API_USER1,
- self::TEST_FILES_SHARING_API_USER2,
- 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);
-
- $share2 = $this->share(
- IShare::TYPE_USER,
- $this->folder2,
- self::TEST_FILES_SHARING_API_USER1,
- self::TEST_FILES_SHARING_API_USER2,
- 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);
-
- $share = $this->shareManager->getShareById($share->getFullId());
- $share2 = $this->shareManager->getShareById($share2->getFullId());
-
- $this->assertNotEquals($share->getTarget(), $share2->getTarget());
- $this->assertSame('/foobar', min($share->getTarget(), $share2->getTarget()));
- $this->assertSame('/foobar (2)', max($share->getTarget(), $share2->getTarget()));
-
- self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
- $this->shareManager->deleteShare($share);
- $this->view->unlink($this->folder);
- }
- }
- class DummyTestClassSharedMount extends SharedMount {
- public function __construct($storage, $mountpoint, $arguments = null, $loader = null) {
-
- }
- public function stripUserFilesPathDummy($path) {
- return $this->stripUserFilesPath($path);
- }
- }
|