sharedmount.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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 Robin Appelman <icewind@owncloud.com>
  7. * @author Thomas Müller <thomas.mueller@tmit.eu>
  8. * @author Vincent Petry <pvince81@owncloud.com>
  9. *
  10. * @copyright Copyright (c) 2015, ownCloud, Inc.
  11. * @license AGPL-3.0
  12. *
  13. * This code is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License, version 3,
  15. * as published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License, version 3,
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>
  24. *
  25. */
  26. /**
  27. * Class Test_Files_Sharing_Api
  28. */
  29. class Test_Files_Sharing_Mount extends OCA\Files_sharing\Tests\TestCase {
  30. protected function setUp() {
  31. parent::setUp();
  32. $this->folder = '/folder_share_storage_test';
  33. $this->filename = '/share-api-storage.txt';
  34. $this->view->mkdir($this->folder);
  35. // save file with content
  36. $this->view->file_put_contents($this->filename, "root file");
  37. $this->view->file_put_contents($this->folder . $this->filename, "file in subfolder");
  38. }
  39. protected function tearDown() {
  40. $this->view->unlink($this->folder);
  41. $this->view->unlink($this->filename);
  42. parent::tearDown();
  43. }
  44. /**
  45. * test if the mount point moves up if the parent folder no longer exists
  46. */
  47. function testShareMountLoseParentFolder() {
  48. // share to user
  49. $fileinfo = $this->view->getFileInfo($this->folder);
  50. $result = \OCP\Share::shareItem('folder', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  51. self::TEST_FILES_SHARING_API_USER2, 31);
  52. $statement = "UPDATE `*PREFIX*share` SET `file_target` = ? where `share_with` = ?";
  53. $query = \OCP\DB::prepare($statement);
  54. $arguments = array('/foo/bar' . $this->folder, self::TEST_FILES_SHARING_API_USER2);
  55. $query->execute($arguments);
  56. $query = \OCP\DB::prepare('SELECT * FROM `*PREFIX*share`');
  57. $result = $query->execute();
  58. $shares = $result->fetchAll();
  59. $this->assertSame(1, count($shares));
  60. $share = reset($shares);
  61. $this->assertSame('/foo/bar' . $this->folder, $share['file_target']);
  62. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  63. // share should have moved up
  64. $query = \OCP\DB::prepare('SELECT * FROM `*PREFIX*share`');
  65. $result = $query->execute();
  66. $shares = $result->fetchAll();
  67. $this->assertSame(1, count($shares));
  68. $share = reset($shares);
  69. $this->assertSame($this->folder, $share['file_target']);
  70. //cleanup
  71. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  72. \OCP\Share::unshare('folder', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_USER, self::TEST_FILES_SHARING_API_USER2);
  73. $this->view->unlink($this->folder);
  74. }
  75. /**
  76. * @medium
  77. */
  78. function testDeleteParentOfMountPoint() {
  79. // share to user
  80. $fileinfo = $this->view->getFileInfo($this->folder);
  81. $result = \OCP\Share::shareItem('folder', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  82. self::TEST_FILES_SHARING_API_USER2, 31);
  83. $this->assertTrue($result);
  84. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  85. $user2View = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER2 . '/files');
  86. $this->assertTrue($user2View->file_exists($this->folder));
  87. // create a local folder
  88. $result = $user2View->mkdir('localfolder');
  89. $this->assertTrue($result);
  90. // move mount point to local folder
  91. $result = $user2View->rename($this->folder, '/localfolder/' . $this->folder);
  92. $this->assertTrue($result);
  93. // mount point in the root folder should no longer exist
  94. $this->assertFalse($user2View->is_dir($this->folder));
  95. // delete the local folder
  96. $result = $user2View->unlink('/localfolder');
  97. $this->assertTrue($result);
  98. //enforce reload of the mount points
  99. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  100. //mount point should be back at the root
  101. $this->assertTrue($user2View->is_dir($this->folder));
  102. //cleanup
  103. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  104. $this->view->unlink($this->folder);
  105. }
  106. function testMoveSharedFile() {
  107. $fileinfo = $this->view->getFileInfo($this->filename);
  108. $result = \OCP\Share::shareItem('file', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  109. self::TEST_FILES_SHARING_API_USER2, 31);
  110. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  111. \OC\Files\Filesystem::rename($this->filename, $this->filename . '_renamed');
  112. $this->assertTrue(\OC\Files\Filesystem::file_exists($this->filename . '_renamed'));
  113. $this->assertFalse(\OC\Files\Filesystem::file_exists($this->filename));
  114. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  115. $this->assertTrue(\OC\Files\Filesystem::file_exists($this->filename));
  116. $this->assertFalse(\OC\Files\Filesystem::file_exists($this->filename . '_renamed'));
  117. // rename back to original name
  118. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  119. \OC\Files\Filesystem::rename($this->filename . '_renamed', $this->filename);
  120. $this->assertFalse(\OC\Files\Filesystem::file_exists($this->filename . '_renamed'));
  121. $this->assertTrue(\OC\Files\Filesystem::file_exists($this->filename));
  122. //cleanup
  123. \OCP\Share::unshare('file', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_USER, self::TEST_FILES_SHARING_API_USER2);
  124. }
  125. /**
  126. * share file with a group if a user renames the file the filename should not change
  127. * for the other users
  128. */
  129. function testMoveGroupShare () {
  130. \OC_Group::createGroup('testGroup');
  131. \OC_Group::addToGroup(self::TEST_FILES_SHARING_API_USER1, 'testGroup');
  132. \OC_Group::addToGroup(self::TEST_FILES_SHARING_API_USER2, 'testGroup');
  133. \OC_Group::addToGroup(self::TEST_FILES_SHARING_API_USER3, 'testGroup');
  134. $fileinfo = $this->view->getFileInfo($this->filename);
  135. $result = \OCP\Share::shareItem('file', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_GROUP,
  136. "testGroup", 31);
  137. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  138. $this->assertTrue(\OC\Files\Filesystem::file_exists($this->filename));
  139. \OC\Files\Filesystem::rename($this->filename, "newFileName");
  140. $this->assertTrue(\OC\Files\Filesystem::file_exists('newFileName'));
  141. $this->assertFalse(\OC\Files\Filesystem::file_exists($this->filename));
  142. self::loginHelper(self::TEST_FILES_SHARING_API_USER3);
  143. $this->assertTrue(\OC\Files\Filesystem::file_exists($this->filename));
  144. $this->assertFalse(\OC\Files\Filesystem::file_exists("newFileName"));
  145. self::loginHelper(self::TEST_FILES_SHARING_API_USER3);
  146. $this->assertTrue(\OC\Files\Filesystem::file_exists($this->filename));
  147. $this->assertFalse(\OC\Files\Filesystem::file_exists("newFileName"));
  148. //cleanup
  149. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  150. \OCP\Share::unshare('file', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_GROUP, 'testGroup');
  151. \OC_Group::removeFromGroup(self::TEST_FILES_SHARING_API_USER1, 'testGroup');
  152. \OC_Group::removeFromGroup(self::TEST_FILES_SHARING_API_USER2, 'testGroup');
  153. \OC_Group::removeFromGroup(self::TEST_FILES_SHARING_API_USER3, 'testGroup');
  154. }
  155. /**
  156. * @dataProvider dataProviderTestStripUserFilesPath
  157. * @param string $path
  158. * @param string $expectedResult
  159. * @param bool $exception if a exception is expected
  160. */
  161. function testStripUserFilesPath($path, $expectedResult, $exception) {
  162. $testClass = new DummyTestClassSharedMount(null, null);
  163. try {
  164. $result = $testClass->stripUserFilesPathDummy($path);
  165. $this->assertSame($expectedResult, $result);
  166. } catch (\Exception $e) {
  167. if ($exception) {
  168. $this->assertSame(10, $e->getCode());
  169. } else {
  170. $this->assertTrue(false, "Exception catched, but expected: " . $expectedResult);
  171. }
  172. }
  173. }
  174. function dataProviderTestStripUserFilesPath() {
  175. return array(
  176. array('/user/files/foo.txt', '/foo.txt', false),
  177. array('/user/files/folder/foo.txt', '/folder/foo.txt', false),
  178. array('/data/user/files/foo.txt', null, true),
  179. array('/data/user/files/', null, true),
  180. array('/files/foo.txt', null, true),
  181. array('/foo.txt', null, true),
  182. );
  183. }
  184. }
  185. class DummyTestClassSharedMount extends \OCA\Files_Sharing\SharedMount {
  186. public function __construct($storage, $mountpoint, $arguments = null, $loader = null){
  187. // noop
  188. }
  189. public function stripUserFilesPathDummy($path) {
  190. return $this->stripUserFilesPath($path);
  191. }
  192. }