UpdaterTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\Files_Sharing\Tests;
  8. use OCA\Files_Trashbin\AppInfo\Application;
  9. use OCP\AppFramework\Bootstrap\IBootContext;
  10. use OCP\Share\IShare;
  11. /**
  12. * Class UpdaterTest
  13. *
  14. * @group DB
  15. */
  16. class UpdaterTest extends TestCase {
  17. public const TEST_FOLDER_NAME = '/folder_share_updater_test';
  18. public static function setUpBeforeClass(): void {
  19. parent::setUpBeforeClass();
  20. \OCA\Files_Sharing\Helper::registerHooks();
  21. }
  22. protected function setUp(): void {
  23. parent::setUp();
  24. $this->folder = self::TEST_FOLDER_NAME;
  25. $this->filename = '/share-updater-test.txt';
  26. // save file with content
  27. $this->view->file_put_contents($this->filename, $this->data);
  28. $this->view->mkdir($this->folder);
  29. $this->view->file_put_contents($this->folder . '/' . $this->filename, $this->data);
  30. }
  31. protected function tearDown(): void {
  32. if ($this->view) {
  33. $this->view->unlink($this->filename);
  34. $this->view->deleteAll($this->folder);
  35. }
  36. parent::tearDown();
  37. }
  38. /**
  39. * test deletion of a folder which contains share mount points. Share mount
  40. * points should be unshared before the folder gets deleted so
  41. * that the mount point doesn't end up at the trash bin
  42. */
  43. public function testDeleteParentFolder() {
  44. $status = \OC::$server->getAppManager()->isEnabledForUser('files_trashbin');
  45. (new \OC_App())->enable('files_trashbin');
  46. // register trashbin hooks
  47. $trashbinApp = new Application();
  48. $trashbinApp->boot($this->createMock(IBootContext::class));
  49. $fileinfo = \OC\Files\Filesystem::getFileInfo($this->folder);
  50. $this->assertTrue($fileinfo instanceof \OC\Files\FileInfo);
  51. $this->share(
  52. IShare::TYPE_USER,
  53. $this->folder,
  54. self::TEST_FILES_SHARING_API_USER1,
  55. self::TEST_FILES_SHARING_API_USER2,
  56. \OCP\Constants::PERMISSION_ALL
  57. );
  58. $this->loginHelper(self::TEST_FILES_SHARING_API_USER2);
  59. $view = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER2 . '/files');
  60. // check if user2 can see the shared folder
  61. $this->assertTrue($view->file_exists($this->folder));
  62. $foldersShared = $this->shareManager->getSharesBy(self::TEST_FILES_SHARING_API_USER1, IShare::TYPE_USER);
  63. $this->assertCount(1, $foldersShared);
  64. $view->mkdir('localFolder');
  65. $view->file_put_contents('localFolder/localFile.txt', 'local file');
  66. $view->rename($this->folder, 'localFolder/' . $this->folder);
  67. // share mount point should now be moved to the subfolder
  68. $this->assertFalse($view->file_exists($this->folder));
  69. $this->assertTrue($view->file_exists('localFolder/' .$this->folder));
  70. $view->unlink('localFolder');
  71. $this->loginHelper(self::TEST_FILES_SHARING_API_USER2);
  72. // shared folder should be unshared
  73. $foldersShared = $this->shareManager->getSharesBy(self::TEST_FILES_SHARING_API_USER1, IShare::TYPE_USER);
  74. $this->assertCount(0, $foldersShared);
  75. // trashbin should contain the local file but not the mount point
  76. $rootView = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER2);
  77. $trashContent = \OCA\Files_Trashbin\Helper::getTrashFiles('/', self::TEST_FILES_SHARING_API_USER2);
  78. $this->assertSame(1, count($trashContent));
  79. $firstElement = reset($trashContent);
  80. $timestamp = $firstElement['mtime'];
  81. $this->assertTrue($rootView->file_exists('files_trashbin/files/localFolder.d' . $timestamp . '/localFile.txt'));
  82. $this->assertFalse($rootView->file_exists('files_trashbin/files/localFolder.d' . $timestamp . '/' . $this->folder));
  83. //cleanup
  84. $rootView->deleteAll('files_trashin');
  85. if ($status === false) {
  86. \OC::$server->getAppManager()->disableApp('files_trashbin');
  87. }
  88. \OC\Files\Filesystem::getLoader()->removeStorageWrapper('oc_trashbin');
  89. }
  90. public function shareFolderProvider() {
  91. return [
  92. ['/'],
  93. ['/my_shares'],
  94. ];
  95. }
  96. /**
  97. * if a file gets shared the etag for the recipients root should change
  98. *
  99. * @dataProvider shareFolderProvider
  100. *
  101. * @param string $shareFolder share folder to use
  102. */
  103. public function testShareFile($shareFolder) {
  104. $config = \OC::$server->getConfig();
  105. $oldShareFolder = $config->getSystemValue('share_folder');
  106. $config->setSystemValue('share_folder', $shareFolder);
  107. $this->loginHelper(self::TEST_FILES_SHARING_API_USER2);
  108. $beforeShareRoot = \OC\Files\Filesystem::getFileInfo('');
  109. $etagBeforeShareRoot = $beforeShareRoot->getEtag();
  110. \OC\Files\Filesystem::mkdir($shareFolder);
  111. $beforeShareDir = \OC\Files\Filesystem::getFileInfo($shareFolder);
  112. $etagBeforeShareDir = $beforeShareDir->getEtag();
  113. $this->loginHelper(self::TEST_FILES_SHARING_API_USER1);
  114. $share = $this->share(
  115. IShare::TYPE_USER,
  116. $this->folder,
  117. self::TEST_FILES_SHARING_API_USER1,
  118. self::TEST_FILES_SHARING_API_USER2,
  119. \OCP\Constants::PERMISSION_ALL
  120. );
  121. $this->loginHelper(self::TEST_FILES_SHARING_API_USER2);
  122. $afterShareRoot = \OC\Files\Filesystem::getFileInfo('');
  123. $etagAfterShareRoot = $afterShareRoot->getEtag();
  124. $afterShareDir = \OC\Files\Filesystem::getFileInfo($shareFolder);
  125. $etagAfterShareDir = $afterShareDir->getEtag();
  126. $this->assertTrue(is_string($etagBeforeShareRoot));
  127. $this->assertTrue(is_string($etagBeforeShareDir));
  128. $this->assertTrue(is_string($etagAfterShareRoot));
  129. $this->assertTrue(is_string($etagAfterShareDir));
  130. $this->assertTrue($etagBeforeShareRoot !== $etagAfterShareRoot);
  131. $this->assertTrue($etagBeforeShareDir !== $etagAfterShareDir);
  132. // cleanup
  133. $this->loginHelper(self::TEST_FILES_SHARING_API_USER1);
  134. $this->shareManager->deleteShare($share);
  135. $config->setSystemValue('share_folder', $oldShareFolder);
  136. }
  137. /**
  138. * if a folder gets renamed all children mount points should be renamed too
  139. */
  140. public function testRename() {
  141. $fileinfo = \OC\Files\Filesystem::getFileInfo($this->folder);
  142. $share = $this->share(
  143. IShare::TYPE_USER,
  144. $this->folder,
  145. self::TEST_FILES_SHARING_API_USER1,
  146. self::TEST_FILES_SHARING_API_USER2,
  147. \OCP\Constants::PERMISSION_ALL
  148. );
  149. $this->loginHelper(self::TEST_FILES_SHARING_API_USER2);
  150. // make sure that the shared folder exists
  151. $this->assertTrue(\OC\Files\Filesystem::file_exists($this->folder));
  152. \OC\Files\Filesystem::mkdir('oldTarget');
  153. \OC\Files\Filesystem::mkdir('oldTarget/subfolder');
  154. \OC\Files\Filesystem::mkdir('newTarget');
  155. \OC\Files\Filesystem::rename($this->folder, 'oldTarget/subfolder/' . $this->folder);
  156. // re-login to make sure that the new mount points are initialized
  157. $this->loginHelper(self::TEST_FILES_SHARING_API_USER2);
  158. \OC\Files\Filesystem::rename('/oldTarget', '/newTarget/oldTarget');
  159. // re-login to make sure that the new mount points are initialized
  160. $this->loginHelper(self::TEST_FILES_SHARING_API_USER2);
  161. $this->assertTrue(\OC\Files\Filesystem::file_exists('/newTarget/oldTarget/subfolder/' . $this->folder));
  162. // cleanup
  163. $this->shareManager->deleteShare($share);
  164. }
  165. /**
  166. * If a folder gets moved into shared folder, children shares should have their uid_owner and permissions adjusted
  167. * user1
  168. * |-folder1 --> shared with user2
  169. * user2
  170. * |-folder2 --> shared with user3 and moved into folder1
  171. * |-subfolder1 --> shared with user3
  172. * |-file1.txt --> shared with user3
  173. * |-subfolder2
  174. * |-file2.txt --> shared with user3
  175. */
  176. public function testMovedIntoShareChangeOwner() {
  177. $this->markTestSkipped('Skipped because this is failing with S3 as primary as file id are change when moved.');
  178. // user1 creates folder1
  179. $viewUser1 = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER1 . '/files');
  180. $folder1 = 'folder1';
  181. $viewUser1->mkdir($folder1);
  182. // user1 shares folder1 to user2
  183. $folder1Share = $this->share(
  184. IShare::TYPE_USER,
  185. $folder1,
  186. self::TEST_FILES_SHARING_API_USER1,
  187. self::TEST_FILES_SHARING_API_USER2,
  188. \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_SHARE
  189. );
  190. $this->loginHelper(self::TEST_FILES_SHARING_API_USER2);
  191. $viewUser2 = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER2 . '/files');
  192. // Create user2 files
  193. $folder2 = 'folder2';
  194. $viewUser2->mkdir($folder2);
  195. $file1 = 'folder2/file1.txt';
  196. $viewUser2->touch($file1);
  197. $subfolder1 = 'folder2/subfolder1';
  198. $viewUser2->mkdir($subfolder1);
  199. $subfolder2 = 'folder2/subfolder2';
  200. $viewUser2->mkdir($subfolder2);
  201. $file2 = 'folder2/subfolder2/file2.txt';
  202. $viewUser2->touch($file2);
  203. // user2 shares folder2 to user3
  204. $folder2Share = $this->share(
  205. IShare::TYPE_USER,
  206. $folder2,
  207. self::TEST_FILES_SHARING_API_USER2,
  208. self::TEST_FILES_SHARING_API_USER3,
  209. \OCP\Constants::PERMISSION_ALL
  210. );
  211. // user2 shares folder2/file1 to user3
  212. $file1Share = $this->share(
  213. IShare::TYPE_USER,
  214. $file1,
  215. self::TEST_FILES_SHARING_API_USER2,
  216. self::TEST_FILES_SHARING_API_USER3,
  217. \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_SHARE
  218. );
  219. // user2 shares subfolder1 to user3
  220. $subfolder1Share = $this->share(
  221. IShare::TYPE_USER,
  222. $subfolder1,
  223. self::TEST_FILES_SHARING_API_USER2,
  224. self::TEST_FILES_SHARING_API_USER3,
  225. \OCP\Constants::PERMISSION_ALL
  226. );
  227. // user2 shares subfolder2/file2.txt to user3
  228. $file2Share = $this->share(
  229. IShare::TYPE_USER,
  230. $file2,
  231. self::TEST_FILES_SHARING_API_USER2,
  232. self::TEST_FILES_SHARING_API_USER3,
  233. \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_SHARE
  234. );
  235. // user2 moves folder2 into folder1
  236. $viewUser2->rename($folder2, $folder1.'/'.$folder2);
  237. $folder2Share = $this->shareManager->getShareById($folder2Share->getFullId());
  238. $file1Share = $this->shareManager->getShareById($file1Share->getFullId());
  239. $subfolder1Share = $this->shareManager->getShareById($subfolder1Share->getFullId());
  240. $file2Share = $this->shareManager->getShareById($file2Share->getFullId());
  241. // Expect uid_owner of both shares to be user1
  242. $this->assertEquals(self::TEST_FILES_SHARING_API_USER1, $folder2Share->getShareOwner());
  243. $this->assertEquals(self::TEST_FILES_SHARING_API_USER1, $file1Share->getShareOwner());
  244. $this->assertEquals(self::TEST_FILES_SHARING_API_USER1, $subfolder1Share->getShareOwner());
  245. $this->assertEquals(self::TEST_FILES_SHARING_API_USER1, $file2Share->getShareOwner());
  246. // Expect permissions to be limited by the permissions of the destination share
  247. $this->assertEquals(\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_SHARE, $folder2Share->getPermissions());
  248. $this->assertEquals(\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_SHARE, $file1Share->getPermissions());
  249. $this->assertEquals(\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_SHARE, $subfolder1Share->getPermissions());
  250. $this->assertEquals(\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_SHARE, $file2Share->getPermissions());
  251. // user2 moves folder2 out of folder1
  252. $viewUser2->rename($folder1.'/'.$folder2, $folder2);
  253. $folder2Share = $this->shareManager->getShareById($folder2Share->getFullId());
  254. $file1Share = $this->shareManager->getShareById($file1Share->getFullId());
  255. $subfolder1Share = $this->shareManager->getShareById($subfolder1Share->getFullId());
  256. $file2Share = $this->shareManager->getShareById($file2Share->getFullId());
  257. // Expect uid_owner of both shares to be user2
  258. $this->assertEquals(self::TEST_FILES_SHARING_API_USER2, $folder2Share->getShareOwner());
  259. $this->assertEquals(self::TEST_FILES_SHARING_API_USER2, $file1Share->getShareOwner());
  260. $this->assertEquals(self::TEST_FILES_SHARING_API_USER2, $subfolder1Share->getShareOwner());
  261. $this->assertEquals(self::TEST_FILES_SHARING_API_USER2, $file2Share->getShareOwner());
  262. // Expect permissions to not change
  263. $this->assertEquals(\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_SHARE, $folder2Share->getPermissions());
  264. $this->assertEquals(\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_SHARE, $file1Share->getPermissions());
  265. $this->assertEquals(\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_SHARE, $subfolder1Share->getPermissions());
  266. $this->assertEquals(\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_SHARE, $file2Share->getPermissions());
  267. // cleanup
  268. $this->shareManager->deleteShare($folder1Share);
  269. $this->shareManager->deleteShare($folder2Share);
  270. $this->shareManager->deleteShare($file1Share);
  271. $this->shareManager->deleteShare($subfolder1Share);
  272. $this->shareManager->deleteShare($file2Share);
  273. }
  274. }