share.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. <?php
  2. /**
  3. * ownCloud
  4. *
  5. * @author Michael Gapczynski
  6. * @copyright 2012 Michael Gapczynski mtgap@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. class Test_Share extends PHPUnit_Framework_TestCase {
  22. protected $itemType;
  23. protected $userBackend;
  24. protected $user1;
  25. protected $user2;
  26. protected $groupBackend;
  27. protected $group1;
  28. protected $group2;
  29. protected $resharing;
  30. public function setUp() {
  31. OC_User::clearBackends();
  32. OC_User::useBackend('dummy');
  33. $this->user1 = uniqid('user1_');
  34. $this->user2 = uniqid('user2_');
  35. $this->user3 = uniqid('user3_');
  36. $this->user4 = uniqid('user4_');
  37. OC_User::createUser($this->user1, 'pass');
  38. OC_User::createUser($this->user2, 'pass');
  39. OC_User::createUser($this->user3, 'pass');
  40. OC_User::createUser($this->user4, 'pass');
  41. OC_User::setUserId($this->user1);
  42. OC_Group::clearBackends();
  43. OC_Group::useBackend(new OC_Group_Dummy);
  44. $this->group1 = uniqid('group_');
  45. $this->group2 = uniqid('group_');
  46. OC_Group::createGroup($this->group1);
  47. OC_Group::createGroup($this->group2);
  48. OC_Group::addToGroup($this->user1, $this->group1);
  49. OC_Group::addToGroup($this->user2, $this->group1);
  50. OC_Group::addToGroup($this->user3, $this->group1);
  51. OC_Group::addToGroup($this->user2, $this->group2);
  52. OC_Group::addToGroup($this->user4, $this->group2);
  53. OCP\Share::registerBackend('test', 'Test_Share_Backend');
  54. OC_Hook::clear('OCP\\Share');
  55. OC::registerShareHooks();
  56. $this->resharing = OC_Appconfig::getValue('core', 'shareapi_allow_resharing', 'yes');
  57. OC_Appconfig::setValue('core', 'shareapi_allow_resharing', 'yes');
  58. }
  59. public function tearDown() {
  60. $query = OC_DB::prepare('DELETE FROM `*PREFIX*share` WHERE `item_type` = ?');
  61. $query->execute(array('test'));
  62. OC_Appconfig::setValue('core', 'shareapi_allow_resharing', $this->resharing);
  63. }
  64. public function testShareInvalidShareType() {
  65. $message = 'Share type foobar is not valid for test.txt';
  66. try {
  67. OCP\Share::shareItem('test', 'test.txt', 'foobar', $this->user2, OCP\PERMISSION_READ);
  68. } catch (Exception $exception) {
  69. $this->assertEquals($message, $exception->getMessage());
  70. }
  71. }
  72. public function testInvalidItemType() {
  73. $message = 'Sharing backend for foobar not found';
  74. try {
  75. OCP\Share::shareItem('foobar', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user2, OCP\PERMISSION_READ);
  76. $this->fail('Exception was expected: '.$message);
  77. } catch (Exception $exception) {
  78. $this->assertEquals($message, $exception->getMessage());
  79. }
  80. try {
  81. OCP\Share::getItemsSharedWith('foobar');
  82. $this->fail('Exception was expected: '.$message);
  83. } catch (Exception $exception) {
  84. $this->assertEquals($message, $exception->getMessage());
  85. }
  86. try {
  87. OCP\Share::getItemSharedWith('foobar', 'test.txt');
  88. $this->fail('Exception was expected: '.$message);
  89. } catch (Exception $exception) {
  90. $this->assertEquals($message, $exception->getMessage());
  91. }
  92. try {
  93. OCP\Share::getItemSharedWithBySource('foobar', 'test.txt');
  94. $this->fail('Exception was expected: '.$message);
  95. } catch (Exception $exception) {
  96. $this->assertEquals($message, $exception->getMessage());
  97. }
  98. try {
  99. OCP\Share::getItemShared('foobar', 'test.txt');
  100. $this->fail('Exception was expected: '.$message);
  101. } catch (Exception $exception) {
  102. $this->assertEquals($message, $exception->getMessage());
  103. }
  104. try {
  105. OCP\Share::unshare('foobar', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user2);
  106. $this->fail('Exception was expected: '.$message);
  107. } catch (Exception $exception) {
  108. $this->assertEquals($message, $exception->getMessage());
  109. }
  110. try {
  111. OCP\Share::setPermissions('foobar', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user2, OCP\PERMISSION_UPDATE);
  112. $this->fail('Exception was expected: '.$message);
  113. } catch (Exception $exception) {
  114. $this->assertEquals($message, $exception->getMessage());
  115. }
  116. }
  117. public function testShareWithUser() {
  118. // Invalid shares
  119. $message = 'Sharing test.txt failed, because the user '.$this->user1.' is the item owner';
  120. try {
  121. OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user1, OCP\PERMISSION_READ);
  122. $this->fail('Exception was expected: '.$message);
  123. } catch (Exception $exception) {
  124. $this->assertEquals($message, $exception->getMessage());
  125. }
  126. $message = 'Sharing test.txt failed, because the user foobar does not exist';
  127. try {
  128. OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, 'foobar', OCP\PERMISSION_READ);
  129. $this->fail('Exception was expected: '.$message);
  130. } catch (Exception $exception) {
  131. $this->assertEquals($message, $exception->getMessage());
  132. }
  133. $message = 'Sharing foobar failed, because the sharing backend for test could not find its source';
  134. try {
  135. OCP\Share::shareItem('test', 'foobar', OCP\Share::SHARE_TYPE_USER, $this->user2, OCP\PERMISSION_READ);
  136. $this->fail('Exception was expected: '.$message);
  137. } catch (Exception $exception) {
  138. $this->assertEquals($message, $exception->getMessage());
  139. }
  140. // Valid share
  141. $this->assertTrue(OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user2, OCP\PERMISSION_READ));
  142. $this->assertEquals(array('test.txt'), OCP\Share::getItemShared('test', 'test.txt', Test_Share_Backend::FORMAT_SOURCE));
  143. OC_User::setUserId($this->user2);
  144. $this->assertEquals(array('test.txt'), OCP\Share::getItemSharedWith('test', 'test.txt', Test_Share_Backend::FORMAT_SOURCE));
  145. // Attempt to share again
  146. OC_User::setUserId($this->user1);
  147. $message = 'Sharing test.txt failed, because this item is already shared with '.$this->user2;
  148. try {
  149. OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user2, OCP\PERMISSION_READ);
  150. $this->fail('Exception was expected: '.$message);
  151. } catch (Exception $exception) {
  152. $this->assertEquals($message, $exception->getMessage());
  153. }
  154. // Attempt to share back
  155. OC_User::setUserId($this->user2);
  156. $message = 'Sharing test.txt failed, because the user '.$this->user1.' is the original sharer';
  157. try {
  158. OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user1, OCP\PERMISSION_READ);
  159. $this->fail('Exception was expected: '.$message);
  160. } catch (Exception $exception) {
  161. $this->assertEquals($message, $exception->getMessage());
  162. }
  163. // Unshare
  164. OC_User::setUserId($this->user1);
  165. $this->assertTrue(OCP\Share::unshare('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user2));
  166. // Attempt reshare without share permission
  167. $this->assertTrue(OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user2, OCP\PERMISSION_READ));
  168. OC_User::setUserId($this->user2);
  169. $message = 'Sharing test.txt failed, because resharing is not allowed';
  170. try {
  171. OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user3, OCP\PERMISSION_READ);
  172. $this->fail('Exception was expected: '.$message);
  173. } catch (Exception $exception) {
  174. $this->assertEquals($message, $exception->getMessage());
  175. }
  176. // Owner grants share and update permission
  177. OC_User::setUserId($this->user1);
  178. $this->assertTrue(OCP\Share::setPermissions('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user2, OCP\PERMISSION_READ | OCP\PERMISSION_UPDATE | OCP\PERMISSION_SHARE));
  179. // Attempt reshare with escalated permissions
  180. OC_User::setUserId($this->user2);
  181. $message = 'Sharing test.txt failed, because the permissions exceed permissions granted to '.$this->user2;
  182. try {
  183. OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user3, OCP\PERMISSION_READ | OCP\PERMISSION_DELETE);
  184. $this->fail('Exception was expected: '.$message);
  185. } catch (Exception $exception) {
  186. $this->assertEquals($message, $exception->getMessage());
  187. }
  188. // Valid reshare
  189. $this->assertTrue(OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user3, OCP\PERMISSION_READ | OCP\PERMISSION_UPDATE));
  190. $this->assertEquals(array('test.txt'), OCP\Share::getItemShared('test', 'test.txt', Test_Share_Backend::FORMAT_SOURCE));
  191. OC_User::setUserId($this->user3);
  192. $this->assertEquals(array('test.txt'), OCP\Share::getItemSharedWith('test', 'test.txt', Test_Share_Backend::FORMAT_SOURCE));
  193. $this->assertEquals(array(OCP\PERMISSION_READ | OCP\PERMISSION_UPDATE), OCP\Share::getItemSharedWith('test', 'test.txt', Test_Share_Backend::FORMAT_PERMISSIONS));
  194. // Attempt to escalate permissions
  195. OC_User::setUserId($this->user2);
  196. $message = 'Setting permissions for test.txt failed, because the permissions exceed permissions granted to '.$this->user2;
  197. try {
  198. OCP\Share::setPermissions('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user3, OCP\PERMISSION_READ | OCP\PERMISSION_DELETE);
  199. $this->fail('Exception was expected: '.$message);
  200. } catch (Exception $exception) {
  201. $this->assertEquals($message, $exception->getMessage());
  202. }
  203. // Remove update permission
  204. OC_User::setUserId($this->user1);
  205. $this->assertTrue(OCP\Share::setPermissions('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user2, OCP\PERMISSION_READ | OCP\PERMISSION_SHARE));
  206. OC_User::setUserId($this->user2);
  207. $this->assertEquals(array(OCP\PERMISSION_READ | OCP\PERMISSION_SHARE), OCP\Share::getItemSharedWith('test', 'test.txt', Test_Share_Backend::FORMAT_PERMISSIONS));
  208. OC_User::setUserId($this->user3);
  209. $this->assertEquals(array(OCP\PERMISSION_READ), OCP\Share::getItemSharedWith('test', 'test.txt', Test_Share_Backend::FORMAT_PERMISSIONS));
  210. // Remove share permission
  211. OC_User::setUserId($this->user1);
  212. $this->assertTrue(OCP\Share::setPermissions('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user2, OCP\PERMISSION_READ));
  213. OC_User::setUserId($this->user2);
  214. $this->assertEquals(array(OCP\PERMISSION_READ), OCP\Share::getItemSharedWith('test', 'test.txt', Test_Share_Backend::FORMAT_PERMISSIONS));
  215. OC_User::setUserId($this->user3);
  216. $this->assertFalse(OCP\Share::getItemSharedWith('test', 'test.txt'));
  217. // Reshare again, and then have owner unshare
  218. OC_User::setUserId($this->user1);
  219. $this->assertTrue(OCP\Share::setPermissions('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user2, OCP\PERMISSION_READ | OCP\PERMISSION_SHARE));
  220. OC_User::setUserId($this->user2);
  221. $this->assertTrue(OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user3, OCP\PERMISSION_READ));
  222. OC_User::setUserId($this->user1);
  223. $this->assertTrue(OCP\Share::unshare('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user2));
  224. OC_User::setUserId($this->user2);
  225. $this->assertFalse(OCP\Share::getItemSharedWith('test', 'test.txt'));
  226. OC_User::setUserId($this->user3);
  227. $this->assertFalse(OCP\Share::getItemSharedWith('test', 'test.txt'));
  228. // Attempt target conflict
  229. OC_User::setUserId($this->user1);
  230. $this->assertTrue(OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user2, OCP\PERMISSION_READ));
  231. OC_User::setUserId($this->user3);
  232. $this->assertTrue(OCP\Share::shareItem('test', 'share.txt', OCP\Share::SHARE_TYPE_USER, $this->user2, OCP\PERMISSION_READ));
  233. OC_User::setUserId($this->user2);
  234. $to_test = OCP\Share::getItemsSharedWith('test', Test_Share_Backend::FORMAT_TARGET);
  235. $this->assertEquals(2, count($to_test));
  236. $this->assertTrue(in_array('test.txt', $to_test));
  237. $this->assertTrue(in_array('test1.txt', $to_test));
  238. // Remove user
  239. OC_User::setUserId($this->user1);
  240. OC_User::deleteUser($this->user1);
  241. OC_User::setUserId($this->user2);
  242. $this->assertEquals(array('test1.txt'), OCP\Share::getItemsSharedWith('test', Test_Share_Backend::FORMAT_TARGET));
  243. }
  244. public function testShareWithGroup() {
  245. // Invalid shares
  246. $message = 'Sharing test.txt failed, because the group foobar does not exist';
  247. try {
  248. OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_GROUP, 'foobar', OCP\PERMISSION_READ);
  249. $this->fail('Exception was expected: '.$message);
  250. } catch (Exception $exception) {
  251. $this->assertEquals($message, $exception->getMessage());
  252. }
  253. $policy = OC_Appconfig::getValue('core', 'shareapi_share_policy', 'global');
  254. OC_Appconfig::setValue('core', 'shareapi_share_policy', 'groups_only');
  255. $message = 'Sharing test.txt failed, because '.$this->user1.' is not a member of the group '.$this->group2;
  256. try {
  257. OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_GROUP, $this->group2, OCP\PERMISSION_READ);
  258. $this->fail('Exception was expected: '.$message);
  259. } catch (Exception $exception) {
  260. $this->assertEquals($message, $exception->getMessage());
  261. }
  262. OC_Appconfig::setValue('core', 'shareapi_share_policy', $policy);
  263. // Valid share
  264. $this->assertTrue(OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_GROUP, $this->group1, OCP\PERMISSION_READ));
  265. $this->assertEquals(array('test.txt'), OCP\Share::getItemShared('test', 'test.txt', Test_Share_Backend::FORMAT_SOURCE));
  266. OC_User::setUserId($this->user2);
  267. $this->assertEquals(array('test.txt'), OCP\Share::getItemSharedWith('test', 'test.txt', Test_Share_Backend::FORMAT_SOURCE));
  268. OC_User::setUserId($this->user3);
  269. $this->assertEquals(array('test.txt'), OCP\Share::getItemSharedWith('test', 'test.txt', Test_Share_Backend::FORMAT_SOURCE));
  270. // Attempt to share again
  271. OC_User::setUserId($this->user1);
  272. $message = 'Sharing test.txt failed, because this item is already shared with '.$this->group1;
  273. try {
  274. OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_GROUP, $this->group1, OCP\PERMISSION_READ);
  275. $this->fail('Exception was expected: '.$message);
  276. } catch (Exception $exception) {
  277. $this->assertEquals($message, $exception->getMessage());
  278. }
  279. // Attempt to share back to owner of group share
  280. OC_User::setUserId($this->user2);
  281. $message = 'Sharing test.txt failed, because the user '.$this->user1.' is the original sharer';
  282. try {
  283. OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user1, OCP\PERMISSION_READ);
  284. $this->fail('Exception was expected: '.$message);
  285. } catch (Exception $exception) {
  286. $this->assertEquals($message, $exception->getMessage());
  287. }
  288. // Attempt to share back to group
  289. $message = 'Sharing test.txt failed, because this item is already shared with '.$this->group1;
  290. try {
  291. OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_GROUP, $this->group1, OCP\PERMISSION_READ);
  292. $this->fail('Exception was expected: '.$message);
  293. } catch (Exception $exception) {
  294. $this->assertEquals($message, $exception->getMessage());
  295. }
  296. // Attempt to share back to member of group
  297. $message ='Sharing test.txt failed, because this item is already shared with '.$this->user3;
  298. try {
  299. OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user3, OCP\PERMISSION_READ);
  300. $this->fail('Exception was expected: '.$message);
  301. } catch (Exception $exception) {
  302. $this->assertEquals($message, $exception->getMessage());
  303. }
  304. // Unshare
  305. OC_User::setUserId($this->user1);
  306. $this->assertTrue(OCP\Share::unshare('test', 'test.txt', OCP\Share::SHARE_TYPE_GROUP, $this->group1));
  307. // Valid share with same person - user then group
  308. $this->assertTrue(OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user2, OCP\PERMISSION_READ | OCP\PERMISSION_DELETE | OCP\PERMISSION_SHARE));
  309. $this->assertTrue(OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_GROUP, $this->group1, OCP\PERMISSION_READ | OCP\PERMISSION_UPDATE));
  310. OC_User::setUserId($this->user2);
  311. $this->assertEquals(array('test.txt'), OCP\Share::getItemsSharedWith('test', Test_Share_Backend::FORMAT_TARGET));
  312. $this->assertEquals(array(OCP\PERMISSION_READ | OCP\PERMISSION_UPDATE | OCP\PERMISSION_DELETE | OCP\PERMISSION_SHARE), OCP\Share::getItemSharedWith('test', 'test.txt', Test_Share_Backend::FORMAT_PERMISSIONS));
  313. OC_User::setUserId($this->user3);
  314. $this->assertEquals(array('test.txt'), OCP\Share::getItemsSharedWith('test', Test_Share_Backend::FORMAT_TARGET));
  315. $this->assertEquals(array(OCP\PERMISSION_READ | OCP\PERMISSION_UPDATE), OCP\Share::getItemSharedWith('test', 'test.txt', Test_Share_Backend::FORMAT_PERMISSIONS));
  316. // Valid reshare
  317. OC_User::setUserId($this->user2);
  318. $this->assertTrue(OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user4, OCP\PERMISSION_READ));
  319. OC_User::setUserId($this->user4);
  320. $this->assertEquals(array('test.txt'), OCP\Share::getItemsSharedWith('test', Test_Share_Backend::FORMAT_TARGET));
  321. // Unshare from user only
  322. OC_User::setUserId($this->user1);
  323. $this->assertTrue(OCP\Share::unshare('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user2));
  324. OC_User::setUserId($this->user2);
  325. $this->assertEquals(array(OCP\PERMISSION_READ | OCP\PERMISSION_UPDATE), OCP\Share::getItemSharedWith('test', 'test.txt', Test_Share_Backend::FORMAT_PERMISSIONS));
  326. OC_User::setUserId($this->user4);
  327. $this->assertEquals(array(), OCP\Share::getItemsSharedWith('test', Test_Share_Backend::FORMAT_TARGET));
  328. // Valid share with same person - group then user
  329. OC_User::setUserId($this->user1);
  330. $this->assertTrue(OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user2, OCP\PERMISSION_READ | OCP\PERMISSION_DELETE));
  331. OC_User::setUserId($this->user2);
  332. $this->assertEquals(array('test.txt'), OCP\Share::getItemsSharedWith('test', Test_Share_Backend::FORMAT_TARGET));
  333. $this->assertEquals(array(OCP\PERMISSION_READ | OCP\PERMISSION_UPDATE | OCP\PERMISSION_DELETE), OCP\Share::getItemSharedWith('test', 'test.txt', Test_Share_Backend::FORMAT_PERMISSIONS));
  334. // Unshare from group only
  335. OC_User::setUserId($this->user1);
  336. $this->assertTrue(OCP\Share::unshare('test', 'test.txt', OCP\Share::SHARE_TYPE_GROUP, $this->group1));
  337. OC_User::setUserId($this->user2);
  338. $this->assertEquals(array(OCP\PERMISSION_READ | OCP\PERMISSION_DELETE), OCP\Share::getItemSharedWith('test', 'test.txt', Test_Share_Backend::FORMAT_PERMISSIONS));
  339. // Attempt user specific target conflict
  340. OC_User::setUserId($this->user3);
  341. $this->assertTrue(OCP\Share::shareItem('test', 'share.txt', OCP\Share::SHARE_TYPE_GROUP, $this->group1, OCP\PERMISSION_READ | OCP\PERMISSION_SHARE));
  342. OC_User::setUserId($this->user2);
  343. $to_test = OCP\Share::getItemsSharedWith('test', Test_Share_Backend::FORMAT_TARGET);
  344. $this->assertEquals(2, count($to_test));
  345. $this->assertTrue(in_array('test.txt', $to_test));
  346. $this->assertTrue(in_array('test1.txt', $to_test));
  347. // Valid reshare
  348. $this->assertTrue(OCP\Share::shareItem('test', 'share.txt', OCP\Share::SHARE_TYPE_USER, $this->user4, OCP\PERMISSION_READ | OCP\PERMISSION_SHARE));
  349. OC_User::setUserId($this->user4);
  350. $this->assertEquals(array('test1.txt'), OCP\Share::getItemsSharedWith('test', Test_Share_Backend::FORMAT_TARGET));
  351. // Remove user from group
  352. OC_Group::removeFromGroup($this->user2, $this->group1);
  353. OC_User::setUserId($this->user2);
  354. $this->assertEquals(array('test.txt'), OCP\Share::getItemsSharedWith('test', Test_Share_Backend::FORMAT_TARGET));
  355. OC_User::setUserId($this->user4);
  356. $this->assertEquals(array(), OCP\Share::getItemsSharedWith('test', Test_Share_Backend::FORMAT_TARGET));
  357. // Add user to group
  358. OC_Group::addToGroup($this->user4, $this->group1);
  359. $this->assertEquals(array('test.txt'), OCP\Share::getItemsSharedWith('test', Test_Share_Backend::FORMAT_TARGET));
  360. // Unshare from self
  361. $this->assertTrue(OCP\Share::unshareFromSelf('test', 'test.txt'));
  362. $this->assertEquals(array(), OCP\Share::getItemsSharedWith('test', Test_Share_Backend::FORMAT_TARGET));
  363. OC_User::setUserId($this->user2);
  364. $this->assertEquals(array('test.txt'), OCP\Share::getItemsSharedWith('test', Test_Share_Backend::FORMAT_TARGET));
  365. // Remove group
  366. OC_Group::deleteGroup($this->group1);
  367. OC_User::setUserId($this->user4);
  368. $this->assertEquals(array(), OCP\Share::getItemsSharedWith('test', Test_Share_Backend::FORMAT_TARGET));
  369. OC_User::setUserId($this->user3);
  370. $this->assertEquals(array(), OCP\Share::getItemsShared('test'));
  371. }
  372. }