updater.php 7.4 KB

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