share.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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 Thomas Müller <thomas.mueller@tmit.eu>
  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. use OCA\Files\Share;
  25. /**
  26. * Class Test_Files_Sharing
  27. */
  28. class Test_Files_Sharing extends OCA\Files_sharing\Tests\TestCase {
  29. const TEST_FOLDER_NAME = '/folder_share_api_test';
  30. private static $tempStorage;
  31. protected function setUp() {
  32. parent::setUp();
  33. $this->folder = self::TEST_FOLDER_NAME;
  34. $this->subfolder = '/subfolder_share_api_test';
  35. $this->subsubfolder = '/subsubfolder_share_api_test';
  36. $this->filename = '/share-api-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->mkdir($this->folder . $this->subfolder);
  41. $this->view->mkdir($this->folder . $this->subfolder . $this->subsubfolder);
  42. $this->view->file_put_contents($this->folder.$this->filename, $this->data);
  43. $this->view->file_put_contents($this->folder . $this->subfolder . $this->filename, $this->data);
  44. }
  45. protected function tearDown() {
  46. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  47. $this->view->unlink($this->filename);
  48. $this->view->deleteAll($this->folder);
  49. self::$tempStorage = null;
  50. // clear database table
  51. $query = \OCP\DB::prepare('DELETE FROM `*PREFIX*share`');
  52. $query->execute();
  53. parent::tearDown();
  54. }
  55. public function testUnshareFromSelf() {
  56. \OC_Group::createGroup('testGroup');
  57. \OC_Group::addToGroup(self::TEST_FILES_SHARING_API_USER2, 'testGroup');
  58. \OC_Group::addToGroup(self::TEST_FILES_SHARING_API_USER3, 'testGroup');
  59. $fileinfo = $this->view->getFileInfo($this->filename);
  60. $result = \OCP\Share::shareItem('file', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  61. \Test_Files_Sharing::TEST_FILES_SHARING_API_USER2, 31);
  62. $this->assertTrue($result);
  63. $result = \OCP\Share::shareItem('file', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_GROUP,
  64. 'testGroup', 31);
  65. $this->assertTrue($result);
  66. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  67. $this->assertTrue(\OC\Files\Filesystem::file_exists($this->filename));
  68. self::loginHelper(self::TEST_FILES_SHARING_API_USER3);
  69. $this->assertTrue(\OC\Files\Filesystem::file_exists($this->filename));
  70. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  71. \OC\Files\Filesystem::unlink($this->filename);
  72. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  73. // both group share and user share should be gone
  74. $this->assertFalse(\OC\Files\Filesystem::file_exists($this->filename));
  75. // for user3 nothing should change
  76. self::loginHelper(self::TEST_FILES_SHARING_API_USER3);
  77. $this->assertTrue(\OC\Files\Filesystem::file_exists($this->filename));
  78. }
  79. /**
  80. * if a file was shared as group share and as individual share they should be grouped
  81. */
  82. public function testGroupingOfShares() {
  83. $fileinfo = $this->view->getFileInfo($this->filename);
  84. $result = \OCP\Share::shareItem('file', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_GROUP,
  85. \Test_Files_Sharing::TEST_FILES_SHARING_API_GROUP1, \OCP\Constants::PERMISSION_READ);
  86. $this->assertTrue($result);
  87. $result = \OCP\Share::shareItem('file', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  88. \Test_Files_Sharing::TEST_FILES_SHARING_API_USER2, \OCP\Constants::PERMISSION_UPDATE);
  89. $this->assertTrue($result);
  90. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  91. $result = \OCP\Share::getItemSharedWith('file', null);
  92. $this->assertTrue(is_array($result));
  93. // test should return exactly one shares created from testCreateShare()
  94. $this->assertSame(1, count($result));
  95. $share = reset($result);
  96. $this->assertSame(\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_UPDATE, $share['permissions']);
  97. \OC\Files\Filesystem::rename($this->filename, $this->filename . '-renamed');
  98. $result = \OCP\Share::getItemSharedWith('file', null);
  99. $this->assertTrue(is_array($result));
  100. // test should return exactly one shares created from testCreateShare()
  101. $this->assertSame(1, count($result));
  102. $share = reset($result);
  103. $this->assertSame(\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_UPDATE, $share['permissions']);
  104. $this->assertSame($this->filename . '-renamed', $share['file_target']);
  105. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  106. // unshare user share
  107. $result = \OCP\Share::unshare('file', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  108. \Test_Files_Sharing::TEST_FILES_SHARING_API_USER2);
  109. $this->assertTrue($result);
  110. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  111. $result = \OCP\Share::getItemSharedWith('file', null);
  112. $this->assertTrue(is_array($result));
  113. // test should return the remaining group share
  114. $this->assertSame(1, count($result));
  115. $share = reset($result);
  116. // only the group share permissions should be available now
  117. $this->assertSame(\OCP\Constants::PERMISSION_READ, $share['permissions']);
  118. $this->assertSame($this->filename . '-renamed', $share['file_target']);
  119. }
  120. /**
  121. * user1 share file to a group and to a user2 in the same group. Then user2
  122. * unshares the file from self. Afterwards user1 should no longer see the
  123. * single user share to user2. If he re-shares the file to user2 the same target
  124. * then the group share should be used to group the item
  125. */
  126. public function testShareAndUnshareFromSelf() {
  127. $fileinfo = $this->view->getFileInfo($this->filename);
  128. // share the file to group1 (user2 is a member of this group) and explicitely to user2
  129. \OCP\Share::shareItem('file', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_GROUP, self::TEST_FILES_SHARING_API_GROUP1, \OCP\Constants::PERMISSION_ALL);
  130. \OCP\Share::shareItem('file', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_USER, self::TEST_FILES_SHARING_API_USER2, \OCP\Constants::PERMISSION_ALL);
  131. // user1 should have to shared files
  132. $shares = \OCP\Share::getItemsShared('file');
  133. $this->assertSame(2, count($shares));
  134. // user2 should have two files "welcome.txt" and the shared file,
  135. // both the group share and the single share of the same file should be
  136. // grouped to one file
  137. \Test_Files_Sharing::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  138. $dirContent = \OC\Files\Filesystem::getDirectoryContent('/');
  139. $this->assertSame(2, count($dirContent));
  140. $this->verifyDirContent($dirContent, array('welcome.txt', ltrim($this->filename, '/')));
  141. // now user2 deletes the share (= unshare from self)
  142. \OC\Files\Filesystem::unlink($this->filename);
  143. // only welcome.txt should exists
  144. $dirContent = \OC\Files\Filesystem::getDirectoryContent('/');
  145. $this->assertSame(1, count($dirContent));
  146. $this->verifyDirContent($dirContent, array('welcome.txt'));
  147. // login as user1...
  148. \Test_Files_Sharing::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  149. // ... now user1 should have only one shared file, the group share
  150. $shares = \OCP\Share::getItemsShared('file');
  151. $this->assertSame(1, count($shares));
  152. // user1 shares a gain the file directly to user2
  153. \OCP\Share::shareItem('file', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_USER, self::TEST_FILES_SHARING_API_USER2, \OCP\Constants::PERMISSION_ALL);
  154. // user2 should see again welcome.txt and the shared file
  155. \Test_Files_Sharing::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  156. $dirContent = \OC\Files\Filesystem::getDirectoryContent('/');
  157. $this->assertSame(2, count($dirContent));
  158. $this->verifyDirContent($dirContent, array('welcome.txt', ltrim($this->filename, '/')));
  159. }
  160. public function verifyDirContent($content, $expected) {
  161. foreach ($content as $c) {
  162. if (!in_array($c['name'], $expected)) {
  163. $this->assertTrue(false, "folder should only contain '" . implode(',', $expected) . "', found: " .$c['name']);
  164. }
  165. }
  166. }
  167. public function testShareWithDifferentShareFolder() {
  168. $fileinfo = $this->view->getFileInfo($this->filename);
  169. $folderinfo = $this->view->getFileInfo($this->folder);
  170. $fileShare = \OCP\Share::shareItem('file', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  171. self::TEST_FILES_SHARING_API_USER2, 31);
  172. $this->assertTrue($fileShare);
  173. \OCA\Files_Sharing\Helper::setShareFolder('/Shared/subfolder');
  174. $folderShare = \OCP\Share::shareItem('folder', $folderinfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  175. self::TEST_FILES_SHARING_API_USER2, 31);
  176. $this->assertTrue($folderShare);
  177. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  178. $this->assertTrue(\OC\Files\Filesystem::file_exists($this->filename));
  179. $this->assertTrue(\OC\Files\Filesystem::file_exists('/Shared/subfolder/' . $this->folder));
  180. //cleanup
  181. \OC::$server->getConfig()->deleteSystemValue('share_folder');
  182. }
  183. public function testShareWithGroupUniqueName() {
  184. $this->loginHelper(self::TEST_FILES_SHARING_API_USER1);
  185. \OC\Files\Filesystem::file_put_contents('test.txt', 'test');
  186. $fileInfo = \OC\Files\Filesystem::getFileInfo('test.txt');
  187. $this->assertTrue(
  188. \OCP\Share::shareItem('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_GROUP, self::TEST_FILES_SHARING_API_GROUP1, 23)
  189. );
  190. $this->loginHelper(self::TEST_FILES_SHARING_API_USER2);
  191. $items = \OCP\Share::getItemsSharedWith('file');
  192. $this->assertSame('/test.txt' ,$items[0]['file_target']);
  193. $this->assertSame(23, $items[0]['permissions']);
  194. \OC\Files\Filesystem::rename('test.txt', 'new test.txt');
  195. $items = \OCP\Share::getItemsSharedWith('file');
  196. $this->assertSame('/new test.txt' ,$items[0]['file_target']);
  197. $this->assertSame(23, $items[0]['permissions']);
  198. $this->loginHelper(self::TEST_FILES_SHARING_API_USER1);
  199. \OCP\Share::setPermissions('file', $items[0]['item_source'], $items[0]['share_type'], $items[0]['share_with'], 3);
  200. $this->loginHelper(self::TEST_FILES_SHARING_API_USER2);
  201. $items = \OCP\Share::getItemsSharedWith('file');
  202. $this->assertSame('/new test.txt' ,$items[0]['file_target']);
  203. $this->assertSame(3, $items[0]['permissions']);
  204. }
  205. /**
  206. * shared files should never have delete permissions
  207. * @dataProvider dataProviderTestFileSharePermissions
  208. */
  209. public function testFileSharePermissions($permission, $expectedPermissions) {
  210. $fileinfo = $this->view->getFileInfo($this->filename);
  211. $result = \OCP\Share::shareItem('file', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  212. \Test_Files_Sharing::TEST_FILES_SHARING_API_USER2, $permission);
  213. $this->assertTrue($result);
  214. $result = \OCP\Share::getItemShared('file', null);
  215. $this->assertTrue(is_array($result));
  216. // test should return exactly one shares created from testCreateShare()
  217. $this->assertSame(1, count($result), 'more then one share found');
  218. $share = reset($result);
  219. $this->assertSame($expectedPermissions, $share['permissions']);
  220. }
  221. public function dataProviderTestFileSharePermissions() {
  222. $permission1 = \OCP\Constants::PERMISSION_ALL;
  223. $permission3 = \OCP\Constants::PERMISSION_READ;
  224. $permission4 = \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_UPDATE;
  225. $permission5 = \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_DELETE;
  226. $permission6 = \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE;
  227. return array(
  228. array($permission1, \OCP\Constants::PERMISSION_ALL & ~\OCP\Constants::PERMISSION_DELETE),
  229. array($permission3, $permission3),
  230. array($permission4, $permission4),
  231. array($permission5, $permission3),
  232. array($permission6, $permission4),
  233. );
  234. }
  235. /**
  236. * @dataProvider dataProviderGetUsersSharingFile
  237. *
  238. * @param string $groupName name of group to share with
  239. * @param bool $includeOwner whether to include the owner in the result
  240. * @param bool $includePaths whether to include paths in the result
  241. * @param array $expectedResult expected result of the API call
  242. */
  243. public function testGetUsersSharingFile($groupName, $includeOwner, $includePaths, $expectedResult) {
  244. $fileinfo = $this->view->getFileInfo($this->folder);
  245. $result = \OCP\Share::shareItem('folder', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_GROUP,
  246. $groupName, \OCP\Constants::PERMISSION_READ);
  247. $this->assertTrue($result);
  248. // public share
  249. $result = \OCP\Share::shareItem('folder', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_LINK,
  250. null, \OCP\Constants::PERMISSION_READ);
  251. $this->assertNotNull($result); // returns the token!
  252. // owner renames after sharing
  253. $this->view->rename($this->folder, $this->folder . '_owner_renamed');
  254. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  255. $user2View = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER2 . '/files');
  256. $user2View->rename($this->folder, $this->folder . '_renamed');
  257. $ownerPath = $this->folder . '_owner_renamed';
  258. $owner = self::TEST_FILES_SHARING_API_USER1;
  259. $result = \OCP\Share::getUsersSharingFile($ownerPath, $owner, $includeOwner, $includePaths);
  260. // sort users to make sure it matches
  261. if ($includePaths) {
  262. ksort($result);
  263. } else {
  264. sort($result['users']);
  265. }
  266. $this->assertEquals(
  267. $expectedResult,
  268. $result
  269. );
  270. }
  271. public function dataProviderGetUsersSharingFile() {
  272. // note: "group" contains user1 (the owner), user2 and user3
  273. // and self::TEST_FILES_SHARING_API_GROUP1 contains only user2
  274. return [
  275. // share with group that contains owner
  276. [
  277. 'group',
  278. false,
  279. false,
  280. [
  281. 'users' =>
  282. [
  283. // because user1 was in group
  284. self::TEST_FILES_SHARING_API_USER1,
  285. self::TEST_FILES_SHARING_API_USER2,
  286. self::TEST_FILES_SHARING_API_USER3,
  287. ],
  288. 'public' => true,
  289. 'remote' => false,
  290. ],
  291. ],
  292. // share with group that does not contain owner
  293. [
  294. self::TEST_FILES_SHARING_API_GROUP1,
  295. false,
  296. false,
  297. [
  298. 'users' =>
  299. [
  300. self::TEST_FILES_SHARING_API_USER2,
  301. ],
  302. 'public' => true,
  303. 'remote' => false,
  304. ],
  305. ],
  306. // share with group that does not contain owner, include owner
  307. [
  308. self::TEST_FILES_SHARING_API_GROUP1,
  309. true,
  310. false,
  311. [
  312. 'users' =>
  313. [
  314. self::TEST_FILES_SHARING_API_USER1,
  315. self::TEST_FILES_SHARING_API_USER2,
  316. ],
  317. 'public' => true,
  318. 'remote' => false,
  319. ],
  320. ],
  321. // include paths, with owner
  322. [
  323. 'group',
  324. true,
  325. true,
  326. [
  327. self::TEST_FILES_SHARING_API_USER1 => self::TEST_FOLDER_NAME . '_owner_renamed',
  328. self::TEST_FILES_SHARING_API_USER2 => self::TEST_FOLDER_NAME . '_renamed',
  329. self::TEST_FILES_SHARING_API_USER3 => self::TEST_FOLDER_NAME,
  330. ],
  331. ],
  332. // include paths, group without owner
  333. [
  334. self::TEST_FILES_SHARING_API_GROUP1,
  335. false,
  336. true,
  337. [
  338. self::TEST_FILES_SHARING_API_USER2 => self::TEST_FOLDER_NAME. '_renamed',
  339. ],
  340. ],
  341. // include paths, include owner, group without owner
  342. [
  343. self::TEST_FILES_SHARING_API_GROUP1,
  344. true,
  345. true,
  346. [
  347. self::TEST_FILES_SHARING_API_USER1 => self::TEST_FOLDER_NAME . '_owner_renamed',
  348. self::TEST_FILES_SHARING_API_USER2 => self::TEST_FOLDER_NAME . '_renamed',
  349. ],
  350. ],
  351. ];
  352. }
  353. }