sharedmount.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. <?php
  2. /**
  3. * ownCloud
  4. *
  5. * @author Bjoern Schiessle
  6. * @copyright 2014 Bjoern Schiessle <schiessle@owncloud.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. /**
  23. * Class Test_Files_Sharing_Api
  24. */
  25. class Test_Files_Sharing_Mount extends OCA\Files_sharing\Tests\TestCase {
  26. protected function setUp() {
  27. parent::setUp();
  28. $this->folder = '/folder_share_storage_test';
  29. $this->filename = '/share-api-storage.txt';
  30. $this->view->mkdir($this->folder);
  31. // save file with content
  32. $this->view->file_put_contents($this->filename, "root file");
  33. $this->view->file_put_contents($this->folder . $this->filename, "file in subfolder");
  34. }
  35. protected function tearDown() {
  36. $this->view->unlink($this->folder);
  37. $this->view->unlink($this->filename);
  38. parent::tearDown();
  39. }
  40. /**
  41. * test if the mount point moves up if the parent folder no longer exists
  42. */
  43. function testShareMountLoseParentFolder() {
  44. // share to user
  45. $fileinfo = $this->view->getFileInfo($this->folder);
  46. $result = \OCP\Share::shareItem('folder', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  47. self::TEST_FILES_SHARING_API_USER2, 31);
  48. $statement = "UPDATE `*PREFIX*share` SET `file_target` = ? where `share_with` = ?";
  49. $query = \OC_DB::prepare($statement);
  50. $arguments = array('/foo/bar' . $this->folder, self::TEST_FILES_SHARING_API_USER2);
  51. $query->execute($arguments);
  52. $query = \OC_DB::prepare('SELECT * FROM `*PREFIX*share`');
  53. $result = $query->execute();
  54. $shares = $result->fetchAll();
  55. $this->assertSame(1, count($shares));
  56. $share = reset($shares);
  57. $this->assertSame('/foo/bar' . $this->folder, $share['file_target']);
  58. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  59. // share should have moved up
  60. $query = \OC_DB::prepare('SELECT * FROM `*PREFIX*share`');
  61. $result = $query->execute();
  62. $shares = $result->fetchAll();
  63. $this->assertSame(1, count($shares));
  64. $share = reset($shares);
  65. $this->assertSame($this->folder, $share['file_target']);
  66. //cleanup
  67. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  68. \OCP\Share::unshare('folder', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_USER, self::TEST_FILES_SHARING_API_USER2);
  69. $this->view->unlink($this->folder);
  70. }
  71. /**
  72. * @medium
  73. */
  74. function testDeleteParentOfMountPoint() {
  75. // share to user
  76. $fileinfo = $this->view->getFileInfo($this->folder);
  77. $result = \OCP\Share::shareItem('folder', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  78. self::TEST_FILES_SHARING_API_USER2, 31);
  79. $this->assertTrue($result);
  80. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  81. $user2View = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER2 . '/files');
  82. $this->assertTrue($user2View->file_exists($this->folder));
  83. // create a local folder
  84. $result = $user2View->mkdir('localfolder');
  85. $this->assertTrue($result);
  86. // move mount point to local folder
  87. $result = $user2View->rename($this->folder, '/localfolder/' . $this->folder);
  88. $this->assertTrue($result);
  89. // mount point in the root folder should no longer exist
  90. $this->assertFalse($user2View->is_dir($this->folder));
  91. // delete the local folder
  92. $result = $user2View->unlink('/localfolder');
  93. $this->assertTrue($result);
  94. //enforce reload of the mount points
  95. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  96. //mount point should be back at the root
  97. $this->assertTrue($user2View->is_dir($this->folder));
  98. //cleanup
  99. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  100. $this->view->unlink($this->folder);
  101. }
  102. function testMoveSharedFile() {
  103. $fileinfo = $this->view->getFileInfo($this->filename);
  104. $result = \OCP\Share::shareItem('file', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  105. self::TEST_FILES_SHARING_API_USER2, 31);
  106. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  107. \OC\Files\Filesystem::rename($this->filename, "newFileName");
  108. $this->assertTrue(\OC\Files\Filesystem::file_exists('newFileName'));
  109. $this->assertFalse(\OC\Files\Filesystem::file_exists($this->filename));
  110. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  111. $this->assertTrue(\OC\Files\Filesystem::file_exists($this->filename));
  112. $this->assertFalse(\OC\Files\Filesystem::file_exists("newFileName"));
  113. //cleanup
  114. \OCP\Share::unshare('file', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_USER, self::TEST_FILES_SHARING_API_USER2);
  115. }
  116. /**
  117. * share file with a group if a user renames the file the filename should not change
  118. * for the other users
  119. */
  120. function testMoveGroupShare () {
  121. \OC_Group::createGroup('testGroup');
  122. \OC_Group::addToGroup(self::TEST_FILES_SHARING_API_USER1, 'testGroup');
  123. \OC_Group::addToGroup(self::TEST_FILES_SHARING_API_USER2, 'testGroup');
  124. \OC_Group::addToGroup(self::TEST_FILES_SHARING_API_USER3, 'testGroup');
  125. $fileinfo = $this->view->getFileInfo($this->filename);
  126. $result = \OCP\Share::shareItem('file', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_GROUP,
  127. "testGroup", 31);
  128. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  129. $this->assertTrue(\OC\Files\Filesystem::file_exists($this->filename));
  130. \OC\Files\Filesystem::rename($this->filename, "newFileName");
  131. $this->assertTrue(\OC\Files\Filesystem::file_exists('newFileName'));
  132. $this->assertFalse(\OC\Files\Filesystem::file_exists($this->filename));
  133. self::loginHelper(self::TEST_FILES_SHARING_API_USER3);
  134. $this->assertTrue(\OC\Files\Filesystem::file_exists($this->filename));
  135. $this->assertFalse(\OC\Files\Filesystem::file_exists("newFileName"));
  136. self::loginHelper(self::TEST_FILES_SHARING_API_USER3);
  137. $this->assertTrue(\OC\Files\Filesystem::file_exists($this->filename));
  138. $this->assertFalse(\OC\Files\Filesystem::file_exists("newFileName"));
  139. //cleanup
  140. \OCP\Share::unshare('file', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_GROUP, 'testGroup');
  141. \OC_Group::removeFromGroup(self::TEST_FILES_SHARING_API_USER1, 'testGroup');
  142. \OC_Group::removeFromGroup(self::TEST_FILES_SHARING_API_USER2, 'testGroup');
  143. \OC_Group::removeFromGroup(self::TEST_FILES_SHARING_API_USER3, 'testGroup');
  144. }
  145. /**
  146. * @dataProvider dataProviderTestStripUserFilesPath
  147. * @param string $path
  148. * @param string $expectedResult
  149. * @param bool $exception if a exception is expected
  150. */
  151. function testStripUserFilesPath($path, $expectedResult, $exception) {
  152. $testClass = new DummyTestClassSharedMount(null, null);
  153. try {
  154. $result = $testClass->stripUserFilesPathDummy($path);
  155. $this->assertSame($expectedResult, $result);
  156. } catch (\Exception $e) {
  157. if ($exception) {
  158. $this->assertSame(10, $e->getCode());
  159. } else {
  160. $this->assertTrue(false, "Exception catched, but expected: " . $expectedResult);
  161. }
  162. }
  163. }
  164. function dataProviderTestStripUserFilesPath() {
  165. return array(
  166. array('/user/files/foo.txt', '/foo.txt', false),
  167. array('/user/files/folder/foo.txt', '/folder/foo.txt', false),
  168. array('/data/user/files/foo.txt', null, true),
  169. array('/data/user/files/', null, true),
  170. array('/files/foo.txt', null, true),
  171. array('/foo.txt', null, true),
  172. );
  173. }
  174. }
  175. class DummyTestClassSharedMount extends \OCA\Files_Sharing\SharedMount {
  176. public function __construct($storage, $mountpoint, $arguments = null, $loader = null){
  177. // noop
  178. }
  179. public function stripUserFilesPathDummy($path) {
  180. return $this->stripUserFilesPath($path);
  181. }
  182. }