You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

234 lines
8.2 KiB

  1. <?php
  2. /**
  3. * ownCloud
  4. *
  5. * @author Morris Jobke
  6. * @copyright 2014 Morris Jobke <morris.jobke@gmail.com>
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  10. * License as published by the Free Software Foundation; either
  11. * version 3 of the License, or any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public
  19. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. require_once __DIR__ . '/../appinfo/update.php';
  23. require_once __DIR__ . '/base.php';
  24. /**
  25. * Class Test_Files_Sharing_Updater
  26. */
  27. class Test_Files_Sharing_Updater extends Test_Files_Sharing_Base {
  28. const TEST_FOLDER_NAME = '/folder_share_api_test';
  29. function setUp() {
  30. parent::setUp();
  31. $this->folder = self::TEST_FOLDER_NAME;
  32. $this->filename = '/share-api-test.txt';
  33. // save file with content
  34. $this->view->file_put_contents($this->filename, $this->data);
  35. $this->view->mkdir($this->folder);
  36. $this->view->file_put_contents($this->folder . '/' . $this->filename, $this->data);
  37. }
  38. function tearDown() {
  39. $this->view->unlink($this->filename);
  40. $this->view->deleteAll($this->folder);
  41. $removeShares = \OC_DB::prepare('DELETE FROM `*PREFIX*share`');
  42. $removeShares->execute();
  43. $removeItems = \OC_DB::prepare('DELETE FROM `*PREFIX*filecache`');
  44. $removeItems->execute();
  45. parent::tearDown();
  46. }
  47. /**
  48. * test deletion of a folder which contains share mount points. Share mount
  49. * points should move up to the parent 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. OC_FileProxy::register(new OCA\Files\Share\Proxy());
  57. $fileinfo = \OC\Files\Filesystem::getFileInfo($this->folder);
  58. $this->assertTrue($fileinfo instanceof \OC\Files\FileInfo);
  59. \OCP\Share::shareItem('folder', $fileinfo->getId(), \OCP\Share::SHARE_TYPE_USER, self::TEST_FILES_SHARING_API_USER2, 31);
  60. $this->loginHelper(self::TEST_FILES_SHARING_API_USER2);
  61. $view = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER2 . '/files');
  62. // check if user2 can see the shared folder
  63. $this->assertTrue($view->file_exists($this->folder));
  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. // mount point should move up again
  73. $this->assertTrue($view->file_exists($this->folder));
  74. // trashbin should contain the local file but not the mount point
  75. $rootView = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER2);
  76. $dirContent = $rootView->getDirectoryContent('files_trashbin/files');
  77. $this->assertSame(1, count($dirContent));
  78. $firstElement = reset($dirContent);
  79. $ext = pathinfo($firstElement['path'], PATHINFO_EXTENSION);
  80. $this->assertTrue($rootView->file_exists('files_trashbin/files/localFolder.' . $ext . '/localFile.txt'));
  81. $this->assertFalse($rootView->file_exists('files_trashbin/files/localFolder.' . $ext . '/' . $this->folder));
  82. //cleanup
  83. $rootView->deleteAll('files_trashin');
  84. if ($status === false) {
  85. \OC_App::disable('files_trashbin');
  86. }
  87. }
  88. /**
  89. * @medium
  90. */
  91. function testRemoveBrokenShares() {
  92. $this->prepareFileCache();
  93. // check if there are just 3 shares (see setUp - precondition: empty table)
  94. $countShares = \OC_DB::prepare('SELECT COUNT(`id`) FROM `*PREFIX*share`');
  95. $result = $countShares->execute()->fetchOne();
  96. $this->assertEquals(3, $result);
  97. // check if there are just 2 items (see setUp - precondition: empty table)
  98. $countItems = \OC_DB::prepare('SELECT COUNT(`fileid`) FROM `*PREFIX*filecache`');
  99. $result = $countItems->execute()->fetchOne();
  100. $this->assertEquals(2, $result);
  101. // execute actual code which should be tested
  102. \OC\Files\Cache\Shared_Updater::fixBrokenSharesOnAppUpdate();
  103. // check if there are just 2 shares (one gets killed by the code as there is no filecache entry for this)
  104. $countShares = \OC_DB::prepare('SELECT COUNT(`id`) FROM `*PREFIX*share`');
  105. $result = $countShares->execute()->fetchOne();
  106. $this->assertEquals(2, $result);
  107. // check if the share of file '200' is removed as there is no entry for this in filecache table
  108. $countShares = \OC_DB::prepare('SELECT COUNT(`id`) FROM `*PREFIX*share` WHERE `file_source` = 200');
  109. $result = $countShares->execute()->fetchOne();
  110. $this->assertEquals(0, $result);
  111. // check if there are just 2 items
  112. $countItems = \OC_DB::prepare('SELECT COUNT(`fileid`) FROM `*PREFIX*filecache`');
  113. $result = $countItems->execute()->fetchOne();
  114. $this->assertEquals(2, $result);
  115. }
  116. /**
  117. * test update for the removal of the logical "Shared" folder. It should update
  118. * the file_target for every share and create a physical "Shared" folder for each user
  119. */
  120. function testRemoveSharedFolder() {
  121. self::prepareDB();
  122. // run the update routine to remove the shared folder and replace it with a real folder
  123. removeSharedFolder(false, 2);
  124. // verify results
  125. $query = \OC_DB::prepare('SELECT * FROM `*PREFIX*share`');
  126. $result = $query->execute(array());
  127. $newDBContent = $result->fetchAll();
  128. foreach ($newDBContent as $row) {
  129. if ((int)$row['share_type'] === \OCP\Share::SHARE_TYPE_USER) {
  130. $this->assertSame('/Shared', substr($row['file_target'], 0, strlen('/Shared')));
  131. } else {
  132. $this->assertSame('/ShouldNotChange', $row['file_target']);
  133. }
  134. }
  135. // cleanup
  136. $this->cleanupSharedTable();
  137. }
  138. private function cleanupSharedTable() {
  139. $query = \OC_DB::prepare('DELETE FROM `*PREFIX*share`');
  140. $query->execute();
  141. }
  142. /**
  143. * prepare sharing table for testRemoveSharedFolder()
  144. */
  145. private function prepareDB() {
  146. $this->cleanupSharedTable();
  147. // add items except one - because this is the test case for the broken share table
  148. $addItems = \OC_DB::prepare('INSERT INTO `*PREFIX*share` (`share_type`, `item_type`, ' .
  149. '`share_with`, `uid_owner` , `file_target`) ' .
  150. 'VALUES (?, ?, ?, ?, ?)');
  151. $items = array(
  152. array(\OCP\Share::SHARE_TYPE_USER, 'file', 'user1', 'admin' , '/foo'),
  153. array(\OCP\Share::SHARE_TYPE_USER, 'folder', 'user2', 'admin', '/foo2'),
  154. array(\OCP\Share::SHARE_TYPE_USER, 'file', 'user3', 'admin', '/foo3'),
  155. array(\OCP\Share::SHARE_TYPE_USER, 'folder', 'user4', 'admin', '/foo4'),
  156. array(\OCP\Share::SHARE_TYPE_LINK, 'file', 'user1', 'admin', '/ShouldNotChange'),
  157. array(\OCP\Share::SHARE_TYPE_CONTACT, 'contact', 'admin', 'user1', '/ShouldNotChange'),
  158. );
  159. foreach($items as $item) {
  160. // the number is used as path_hash
  161. $addItems->execute($item);
  162. }
  163. }
  164. /**
  165. * prepare file cache for testRemoveBrokenShares()
  166. */
  167. private function prepareFileCache() {
  168. // some previous tests didn't clean up and therefore this has to be done here
  169. // FIXME: DIRTY HACK - TODO: find tests, that don't clean up and fix it there
  170. $this->tearDown();
  171. // add items except one - because this is the test case for the broken share table
  172. $addItems = \OC_DB::prepare('INSERT INTO `*PREFIX*filecache` (`storage`, `path_hash`, ' .
  173. '`parent`, `mimetype`, `mimepart`, `size`, `mtime`, `storage_mtime`) ' .
  174. 'VALUES (1, ?, 1, 1, 1, 1, 1, 1)');
  175. $items = array(1, 3);
  176. $fileIds = array();
  177. foreach($items as $item) {
  178. // the number is used as path_hash
  179. $addItems->execute(array($item));
  180. $fileIds[] = \OC_DB::insertId('*PREFIX*filecache');
  181. }
  182. $addShares = \OC_DB::prepare('INSERT INTO `*PREFIX*share` (`file_source`, `item_type`, `uid_owner`) VALUES (?, \'file\', 1)');
  183. // the number is used as item_source
  184. $addShares->execute(array($fileIds[0]));
  185. $addShares->execute(array(200)); // id of "deleted" file
  186. $addShares->execute(array($fileIds[1]));
  187. }
  188. }