UpdaterTest.php 7.3 KB

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