1
0

UpdaterTest.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Björn Schießle <bjoern@schiessle.org>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Lukas Reschke <lukas@statuscode.ch>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Robin Appelman <robin@icewind.nl>
  10. * @author Roeland Jago Douma <roeland@famdouma.nl>
  11. * @author Thomas Müller <thomas.mueller@tmit.eu>
  12. * @author Tobia De Koninck <tobia@ledfan.be>
  13. * @author Vincent Petry <pvince81@owncloud.com>
  14. *
  15. * @license AGPL-3.0
  16. *
  17. * This code is free software: you can redistribute it and/or modify
  18. * it under the terms of the GNU Affero General Public License, version 3,
  19. * as published by the Free Software Foundation.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU Affero General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU Affero General Public License, version 3,
  27. * along with this program. If not, see <http://www.gnu.org/licenses/>
  28. *
  29. */
  30. namespace OCA\Files_Sharing\Tests;
  31. /**
  32. * Class UpdaterTest
  33. *
  34. * @group DB
  35. */
  36. class UpdaterTest extends TestCase {
  37. const TEST_FOLDER_NAME = '/folder_share_updater_test';
  38. public static function setUpBeforeClass(): void {
  39. parent::setUpBeforeClass();
  40. \OCA\Files_Sharing\Helper::registerHooks();
  41. }
  42. protected function setUp(): void {
  43. parent::setUp();
  44. $this->folder = self::TEST_FOLDER_NAME;
  45. $this->filename = '/share-updater-test.txt';
  46. // save file with content
  47. $this->view->file_put_contents($this->filename, $this->data);
  48. $this->view->mkdir($this->folder);
  49. $this->view->file_put_contents($this->folder . '/' . $this->filename, $this->data);
  50. }
  51. protected function tearDown(): void {
  52. if ($this->view) {
  53. $this->view->unlink($this->filename);
  54. $this->view->deleteAll($this->folder);
  55. }
  56. parent::tearDown();
  57. }
  58. /**
  59. * test deletion of a folder which contains share mount points. Share mount
  60. * points should be unshared before the folder gets deleted so
  61. * that the mount point doesn't end up at the trash bin
  62. */
  63. public function testDeleteParentFolder() {
  64. $status = \OC::$server->getAppManager()->isEnabledForUser('files_trashbin');
  65. (new \OC_App())->enable('files_trashbin');
  66. \OCA\Files_Trashbin\Trashbin::registerHooks();
  67. $fileinfo = \OC\Files\Filesystem::getFileInfo($this->folder);
  68. $this->assertTrue($fileinfo instanceof \OC\Files\FileInfo);
  69. $this->share(
  70. \OCP\Share::SHARE_TYPE_USER,
  71. $this->folder,
  72. self::TEST_FILES_SHARING_API_USER1,
  73. self::TEST_FILES_SHARING_API_USER2,
  74. \OCP\Constants::PERMISSION_ALL
  75. );
  76. $this->loginHelper(self::TEST_FILES_SHARING_API_USER2);
  77. $view = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER2 . '/files');
  78. // check if user2 can see the shared folder
  79. $this->assertTrue($view->file_exists($this->folder));
  80. $foldersShared = \OC\Share\Share::getItemsSharedWith('folder');
  81. $this->assertSame(1, count($foldersShared));
  82. $view->mkdir('localFolder');
  83. $view->file_put_contents('localFolder/localFile.txt', 'local file');
  84. $view->rename($this->folder, 'localFolder/' . $this->folder);
  85. // share mount point should now be moved to the subfolder
  86. $this->assertFalse($view->file_exists($this->folder));
  87. $this->assertTrue($view->file_exists('localFolder/' .$this->folder));
  88. $view->unlink('localFolder');
  89. $this->loginHelper(self::TEST_FILES_SHARING_API_USER2);
  90. // shared folder should be unshared
  91. $foldersShared = \OC\Share\Share::getItemsSharedWith('folder');
  92. $this->assertTrue(empty($foldersShared));
  93. // trashbin should contain the local file but not the mount point
  94. $rootView = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER2);
  95. $trashContent = \OCA\Files_Trashbin\Helper::getTrashFiles('/', self::TEST_FILES_SHARING_API_USER2);
  96. $this->assertSame(1, count($trashContent));
  97. $firstElement = reset($trashContent);
  98. $timestamp = $firstElement['mtime'];
  99. $this->assertTrue($rootView->file_exists('files_trashbin/files/localFolder.d' . $timestamp . '/localFile.txt'));
  100. $this->assertFalse($rootView->file_exists('files_trashbin/files/localFolder.d' . $timestamp . '/' . $this->folder));
  101. //cleanup
  102. $rootView->deleteAll('files_trashin');
  103. if ($status === false) {
  104. \OC::$server->getAppManager()->disableApp('files_trashbin');
  105. }
  106. \OC\Files\Filesystem::getLoader()->removeStorageWrapper('oc_trashbin');
  107. }
  108. public function shareFolderProvider() {
  109. return [
  110. ['/'],
  111. ['/my_shares'],
  112. ];
  113. }
  114. /**
  115. * if a file gets shared the etag for the recipients root should change
  116. *
  117. * @dataProvider shareFolderProvider
  118. *
  119. * @param string $shareFolder share folder to use
  120. */
  121. public function testShareFile($shareFolder) {
  122. $config = \OC::$server->getConfig();
  123. $oldShareFolder = $config->getSystemValue('share_folder');
  124. $config->setSystemValue('share_folder', $shareFolder);
  125. $this->loginHelper(self::TEST_FILES_SHARING_API_USER2);
  126. $beforeShareRoot = \OC\Files\Filesystem::getFileInfo('');
  127. $etagBeforeShareRoot = $beforeShareRoot->getEtag();
  128. \OC\Files\Filesystem::mkdir($shareFolder);
  129. $beforeShareDir = \OC\Files\Filesystem::getFileInfo($shareFolder);
  130. $etagBeforeShareDir = $beforeShareDir->getEtag();
  131. $this->loginHelper(self::TEST_FILES_SHARING_API_USER1);
  132. $share = $this->share(
  133. \OCP\Share::SHARE_TYPE_USER,
  134. $this->folder,
  135. self::TEST_FILES_SHARING_API_USER1,
  136. self::TEST_FILES_SHARING_API_USER2,
  137. \OCP\Constants::PERMISSION_ALL
  138. );
  139. $this->loginHelper(self::TEST_FILES_SHARING_API_USER2);
  140. $afterShareRoot = \OC\Files\Filesystem::getFileInfo('');
  141. $etagAfterShareRoot = $afterShareRoot->getEtag();
  142. $afterShareDir = \OC\Files\Filesystem::getFileInfo($shareFolder);
  143. $etagAfterShareDir = $afterShareDir->getEtag();
  144. $this->assertTrue(is_string($etagBeforeShareRoot));
  145. $this->assertTrue(is_string($etagBeforeShareDir));
  146. $this->assertTrue(is_string($etagAfterShareRoot));
  147. $this->assertTrue(is_string($etagAfterShareDir));
  148. $this->assertTrue($etagBeforeShareRoot !== $etagAfterShareRoot);
  149. $this->assertTrue($etagBeforeShareDir !== $etagAfterShareDir);
  150. // cleanup
  151. $this->loginHelper(self::TEST_FILES_SHARING_API_USER1);
  152. $this->shareManager->deleteShare($share);
  153. $config->setSystemValue('share_folder', $oldShareFolder);
  154. }
  155. /**
  156. * if a folder gets renamed all children mount points should be renamed too
  157. */
  158. public function testRename() {
  159. $fileinfo = \OC\Files\Filesystem::getFileInfo($this->folder);
  160. $share = $this->share(
  161. \OCP\Share::SHARE_TYPE_USER,
  162. $this->folder,
  163. self::TEST_FILES_SHARING_API_USER1,
  164. self::TEST_FILES_SHARING_API_USER2,
  165. \OCP\Constants::PERMISSION_ALL
  166. );
  167. $this->loginHelper(self::TEST_FILES_SHARING_API_USER2);
  168. // make sure that the shared folder exists
  169. $this->assertTrue(\OC\Files\Filesystem::file_exists($this->folder));
  170. \OC\Files\Filesystem::mkdir('oldTarget');
  171. \OC\Files\Filesystem::mkdir('oldTarget/subfolder');
  172. \OC\Files\Filesystem::mkdir('newTarget');
  173. \OC\Files\Filesystem::rename($this->folder, 'oldTarget/subfolder/' . $this->folder);
  174. // re-login to make sure that the new mount points are initialized
  175. $this->loginHelper(self::TEST_FILES_SHARING_API_USER2);
  176. \OC\Files\Filesystem::rename('/oldTarget', '/newTarget/oldTarget');
  177. // re-login to make sure that the new mount points are initialized
  178. $this->loginHelper(self::TEST_FILES_SHARING_API_USER2);
  179. $this->assertTrue(\OC\Files\Filesystem::file_exists('/newTarget/oldTarget/subfolder/' . $this->folder));
  180. // cleanup
  181. $this->shareManager->deleteShare($share);
  182. }
  183. }