1
0

UpdaterTest.php 7.4 KB

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