123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335 |
- <?php
- /**
- * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
- * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
- * SPDX-License-Identifier: AGPL-3.0-only
- */
- namespace OCA\Files_Sharing\Tests;
- use OCA\Files_Trashbin\AppInfo\Application;
- use OCP\AppFramework\Bootstrap\IBootContext;
- use OCP\Share\IShare;
- /**
- * Class UpdaterTest
- *
- * @group DB
- */
- class UpdaterTest extends TestCase {
- public const TEST_FOLDER_NAME = '/folder_share_updater_test';
- public static function setUpBeforeClass(): void {
- parent::setUpBeforeClass();
- \OCA\Files_Sharing\Helper::registerHooks();
- }
- protected function setUp(): void {
- parent::setUp();
- $this->folder = self::TEST_FOLDER_NAME;
- $this->filename = '/share-updater-test.txt';
- // save file with content
- $this->view->file_put_contents($this->filename, $this->data);
- $this->view->mkdir($this->folder);
- $this->view->file_put_contents($this->folder . '/' . $this->filename, $this->data);
- }
- protected function tearDown(): void {
- if ($this->view) {
- $this->view->unlink($this->filename);
- $this->view->deleteAll($this->folder);
- }
- parent::tearDown();
- }
- /**
- * test deletion of a folder which contains share mount points. Share mount
- * points should be unshared before the folder gets deleted so
- * that the mount point doesn't end up at the trash bin
- */
- public function testDeleteParentFolder(): void {
- $status = \OC::$server->getAppManager()->isEnabledForUser('files_trashbin');
- (new \OC_App())->enable('files_trashbin');
- // register trashbin hooks
- $trashbinApp = new Application();
- $trashbinApp->boot($this->createMock(IBootContext::class));
- $fileinfo = \OC\Files\Filesystem::getFileInfo($this->folder);
- $this->assertTrue($fileinfo instanceof \OC\Files\FileInfo);
- $this->share(
- IShare::TYPE_USER,
- $this->folder,
- self::TEST_FILES_SHARING_API_USER1,
- self::TEST_FILES_SHARING_API_USER2,
- \OCP\Constants::PERMISSION_ALL
- );
- $this->loginHelper(self::TEST_FILES_SHARING_API_USER2);
- $view = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER2 . '/files');
- // check if user2 can see the shared folder
- $this->assertTrue($view->file_exists($this->folder));
- $foldersShared = $this->shareManager->getSharesBy(self::TEST_FILES_SHARING_API_USER1, IShare::TYPE_USER);
- $this->assertCount(1, $foldersShared);
- $view->mkdir('localFolder');
- $view->file_put_contents('localFolder/localFile.txt', 'local file');
- $view->rename($this->folder, 'localFolder/' . $this->folder);
- // share mount point should now be moved to the subfolder
- $this->assertFalse($view->file_exists($this->folder));
- $this->assertTrue($view->file_exists('localFolder/' . $this->folder));
- $view->unlink('localFolder');
- $this->loginHelper(self::TEST_FILES_SHARING_API_USER2);
- // shared folder should be unshared
- $foldersShared = $this->shareManager->getSharesBy(self::TEST_FILES_SHARING_API_USER1, IShare::TYPE_USER);
- $this->assertCount(0, $foldersShared);
- // trashbin should contain the local file but not the mount point
- $rootView = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER2);
- $trashContent = \OCA\Files_Trashbin\Helper::getTrashFiles('/', self::TEST_FILES_SHARING_API_USER2);
- $this->assertSame(1, count($trashContent));
- $firstElement = reset($trashContent);
- $timestamp = $firstElement['mtime'];
- $this->assertTrue($rootView->file_exists('files_trashbin/files/localFolder.d' . $timestamp . '/localFile.txt'));
- $this->assertFalse($rootView->file_exists('files_trashbin/files/localFolder.d' . $timestamp . '/' . $this->folder));
- //cleanup
- $rootView->deleteAll('files_trashin');
- if ($status === false) {
- \OC::$server->getAppManager()->disableApp('files_trashbin');
- }
- \OC\Files\Filesystem::getLoader()->removeStorageWrapper('oc_trashbin');
- }
- public function shareFolderProvider() {
- return [
- ['/'],
- ['/my_shares'],
- ];
- }
- /**
- * if a file gets shared the etag for the recipients root should change
- *
- * @dataProvider shareFolderProvider
- *
- * @param string $shareFolder share folder to use
- */
- public function testShareFile($shareFolder): void {
- $config = \OC::$server->getConfig();
- $oldShareFolder = $config->getSystemValue('share_folder');
- $config->setSystemValue('share_folder', $shareFolder);
- $this->loginHelper(self::TEST_FILES_SHARING_API_USER2);
- $beforeShareRoot = \OC\Files\Filesystem::getFileInfo('');
- $etagBeforeShareRoot = $beforeShareRoot->getEtag();
- \OC\Files\Filesystem::mkdir($shareFolder);
- $beforeShareDir = \OC\Files\Filesystem::getFileInfo($shareFolder);
- $etagBeforeShareDir = $beforeShareDir->getEtag();
- $this->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,
- \OCP\Constants::PERMISSION_ALL
- );
- $this->loginHelper(self::TEST_FILES_SHARING_API_USER2);
- $afterShareRoot = \OC\Files\Filesystem::getFileInfo('');
- $etagAfterShareRoot = $afterShareRoot->getEtag();
- $afterShareDir = \OC\Files\Filesystem::getFileInfo($shareFolder);
- $etagAfterShareDir = $afterShareDir->getEtag();
- $this->assertTrue(is_string($etagBeforeShareRoot));
- $this->assertTrue(is_string($etagBeforeShareDir));
- $this->assertTrue(is_string($etagAfterShareRoot));
- $this->assertTrue(is_string($etagAfterShareDir));
- $this->assertTrue($etagBeforeShareRoot !== $etagAfterShareRoot);
- $this->assertTrue($etagBeforeShareDir !== $etagAfterShareDir);
- // cleanup
- $this->loginHelper(self::TEST_FILES_SHARING_API_USER1);
- $this->shareManager->deleteShare($share);
- $config->setSystemValue('share_folder', $oldShareFolder);
- }
- /**
- * if a folder gets renamed all children mount points should be renamed too
- */
- public function testRename(): void {
- $fileinfo = \OC\Files\Filesystem::getFileInfo($this->folder);
- $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->loginHelper(self::TEST_FILES_SHARING_API_USER2);
- // make sure that the shared folder exists
- $this->assertTrue(\OC\Files\Filesystem::file_exists($this->folder));
- \OC\Files\Filesystem::mkdir('oldTarget');
- \OC\Files\Filesystem::mkdir('oldTarget/subfolder');
- \OC\Files\Filesystem::mkdir('newTarget');
- \OC\Files\Filesystem::rename($this->folder, 'oldTarget/subfolder/' . $this->folder);
- // re-login to make sure that the new mount points are initialized
- $this->loginHelper(self::TEST_FILES_SHARING_API_USER2);
- \OC\Files\Filesystem::rename('/oldTarget', '/newTarget/oldTarget');
- // re-login to make sure that the new mount points are initialized
- $this->loginHelper(self::TEST_FILES_SHARING_API_USER2);
- $this->assertTrue(\OC\Files\Filesystem::file_exists('/newTarget/oldTarget/subfolder/' . $this->folder));
- // cleanup
- $this->shareManager->deleteShare($share);
- }
- /**
- * If a folder gets moved into shared folder, children shares should have their uid_owner and permissions adjusted
- * user1
- * |-folder1 --> shared with user2
- * user2
- * |-folder2 --> shared with user3 and moved into folder1
- * |-subfolder1 --> shared with user3
- * |-file1.txt --> shared with user3
- * |-subfolder2
- * |-file2.txt --> shared with user3
- */
- public function testMovedIntoShareChangeOwner(): void {
- $this->markTestSkipped('Skipped because this is failing with S3 as primary as file id are change when moved.');
- // user1 creates folder1
- $viewUser1 = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER1 . '/files');
- $folder1 = 'folder1';
- $viewUser1->mkdir($folder1);
- // user1 shares folder1 to user2
- $folder1Share = $this->share(
- IShare::TYPE_USER,
- $folder1,
- self::TEST_FILES_SHARING_API_USER1,
- self::TEST_FILES_SHARING_API_USER2,
- \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_SHARE
- );
- $this->loginHelper(self::TEST_FILES_SHARING_API_USER2);
- $viewUser2 = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER2 . '/files');
- // Create user2 files
- $folder2 = 'folder2';
- $viewUser2->mkdir($folder2);
- $file1 = 'folder2/file1.txt';
- $viewUser2->touch($file1);
- $subfolder1 = 'folder2/subfolder1';
- $viewUser2->mkdir($subfolder1);
- $subfolder2 = 'folder2/subfolder2';
- $viewUser2->mkdir($subfolder2);
- $file2 = 'folder2/subfolder2/file2.txt';
- $viewUser2->touch($file2);
- // user2 shares folder2 to user3
- $folder2Share = $this->share(
- IShare::TYPE_USER,
- $folder2,
- self::TEST_FILES_SHARING_API_USER2,
- self::TEST_FILES_SHARING_API_USER3,
- \OCP\Constants::PERMISSION_ALL
- );
- // user2 shares folder2/file1 to user3
- $file1Share = $this->share(
- IShare::TYPE_USER,
- $file1,
- self::TEST_FILES_SHARING_API_USER2,
- self::TEST_FILES_SHARING_API_USER3,
- \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_SHARE
- );
- // user2 shares subfolder1 to user3
- $subfolder1Share = $this->share(
- IShare::TYPE_USER,
- $subfolder1,
- self::TEST_FILES_SHARING_API_USER2,
- self::TEST_FILES_SHARING_API_USER3,
- \OCP\Constants::PERMISSION_ALL
- );
- // user2 shares subfolder2/file2.txt to user3
- $file2Share = $this->share(
- IShare::TYPE_USER,
- $file2,
- self::TEST_FILES_SHARING_API_USER2,
- self::TEST_FILES_SHARING_API_USER3,
- \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_SHARE
- );
- // user2 moves folder2 into folder1
- $viewUser2->rename($folder2, $folder1 . '/' . $folder2);
- $folder2Share = $this->shareManager->getShareById($folder2Share->getFullId());
- $file1Share = $this->shareManager->getShareById($file1Share->getFullId());
- $subfolder1Share = $this->shareManager->getShareById($subfolder1Share->getFullId());
- $file2Share = $this->shareManager->getShareById($file2Share->getFullId());
- // Expect uid_owner of both shares to be user1
- $this->assertEquals(self::TEST_FILES_SHARING_API_USER1, $folder2Share->getShareOwner());
- $this->assertEquals(self::TEST_FILES_SHARING_API_USER1, $file1Share->getShareOwner());
- $this->assertEquals(self::TEST_FILES_SHARING_API_USER1, $subfolder1Share->getShareOwner());
- $this->assertEquals(self::TEST_FILES_SHARING_API_USER1, $file2Share->getShareOwner());
- // Expect permissions to be limited by the permissions of the destination share
- $this->assertEquals(\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_SHARE, $folder2Share->getPermissions());
- $this->assertEquals(\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_SHARE, $file1Share->getPermissions());
- $this->assertEquals(\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_SHARE, $subfolder1Share->getPermissions());
- $this->assertEquals(\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_SHARE, $file2Share->getPermissions());
- // user2 moves folder2 out of folder1
- $viewUser2->rename($folder1 . '/' . $folder2, $folder2);
- $folder2Share = $this->shareManager->getShareById($folder2Share->getFullId());
- $file1Share = $this->shareManager->getShareById($file1Share->getFullId());
- $subfolder1Share = $this->shareManager->getShareById($subfolder1Share->getFullId());
- $file2Share = $this->shareManager->getShareById($file2Share->getFullId());
- // Expect uid_owner of both shares to be user2
- $this->assertEquals(self::TEST_FILES_SHARING_API_USER2, $folder2Share->getShareOwner());
- $this->assertEquals(self::TEST_FILES_SHARING_API_USER2, $file1Share->getShareOwner());
- $this->assertEquals(self::TEST_FILES_SHARING_API_USER2, $subfolder1Share->getShareOwner());
- $this->assertEquals(self::TEST_FILES_SHARING_API_USER2, $file2Share->getShareOwner());
- // Expect permissions to not change
- $this->assertEquals(\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_SHARE, $folder2Share->getPermissions());
- $this->assertEquals(\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_SHARE, $file1Share->getPermissions());
- $this->assertEquals(\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_SHARE, $subfolder1Share->getPermissions());
- $this->assertEquals(\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_SHARE, $file2Share->getPermissions());
- // cleanup
- $this->shareManager->deleteShare($folder1Share);
- $this->shareManager->deleteShare($folder2Share);
- $this->shareManager->deleteShare($file1Share);
- $this->shareManager->deleteShare($subfolder1Share);
- $this->shareManager->deleteShare($file2Share);
- }
- }
|