DefaultShareProviderTest.php 76 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161
  1. <?php
  2. /**
  3. * @author Roeland Jago Douma <rullzer@owncloud.com>
  4. *
  5. * @copyright Copyright (c) 2015, ownCloud, Inc.
  6. * @license AGPL-3.0
  7. *
  8. * This code is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License, version 3,
  10. * as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License, version 3,
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>
  19. *
  20. */
  21. namespace Test\Share20;
  22. use OC\Share20\Exception\ProviderException;
  23. use OCP\DB\QueryBuilder\IQueryBuilder;
  24. use OCP\Files\File;
  25. use OCP\Files\Folder;
  26. use OCP\IDBConnection;
  27. use OCP\IGroup;
  28. use OCP\IUser;
  29. use OCP\IUserManager;
  30. use OCP\IGroupManager;
  31. use OCP\Files\IRootFolder;
  32. use OC\Share20\DefaultShareProvider;
  33. use OCP\Share\IShare;
  34. /**
  35. * Class DefaultShareProviderTest
  36. *
  37. * @package Test\Share20
  38. * @group DB
  39. */
  40. class DefaultShareProviderTest extends \Test\TestCase {
  41. /** @var IDBConnection */
  42. protected $dbConn;
  43. /** @var IUserManager | \PHPUnit_Framework_MockObject_MockObject */
  44. protected $userManager;
  45. /** @var IGroupManager | \PHPUnit_Framework_MockObject_MockObject */
  46. protected $groupManager;
  47. /** @var IRootFolder | \PHPUnit_Framework_MockObject_MockObject */
  48. protected $rootFolder;
  49. /** @var DefaultShareProvider */
  50. protected $provider;
  51. public function setUp() {
  52. $this->dbConn = \OC::$server->getDatabaseConnection();
  53. $this->userManager = $this->createMock(IUserManager::class);
  54. $this->groupManager = $this->createMock(IGroupManager::class);
  55. $this->rootFolder = $this->createMock(IRootFolder::class);
  56. $this->userManager->expects($this->any())->method('userExists')->willReturn(true);
  57. //Empty share table
  58. $this->dbConn->getQueryBuilder()->delete('share')->execute();
  59. $this->provider = new DefaultShareProvider(
  60. $this->dbConn,
  61. $this->userManager,
  62. $this->groupManager,
  63. $this->rootFolder
  64. );
  65. }
  66. public function tearDown() {
  67. $this->dbConn->getQueryBuilder()->delete('share')->execute();
  68. }
  69. /**
  70. * @param int $shareType
  71. * @param string $sharedWith
  72. * @param string $sharedBy
  73. * @param string $shareOwner
  74. * @param string $itemType
  75. * @param int $fileSource
  76. * @param string $fileTarget
  77. * @param int $permissions
  78. * @param $token
  79. * @param $expiration
  80. * @return int
  81. */
  82. private function addShareToDB($shareType, $sharedWith, $sharedBy, $shareOwner,
  83. $itemType, $fileSource, $fileTarget, $permissions, $token, $expiration,
  84. $parent = null) {
  85. $qb = $this->dbConn->getQueryBuilder();
  86. $qb->insert('share');
  87. if ($shareType) $qb->setValue('share_type', $qb->expr()->literal($shareType));
  88. if ($sharedWith) $qb->setValue('share_with', $qb->expr()->literal($sharedWith));
  89. if ($sharedBy) $qb->setValue('uid_initiator', $qb->expr()->literal($sharedBy));
  90. if ($shareOwner) $qb->setValue('uid_owner', $qb->expr()->literal($shareOwner));
  91. if ($itemType) $qb->setValue('item_type', $qb->expr()->literal($itemType));
  92. if ($fileSource) $qb->setValue('file_source', $qb->expr()->literal($fileSource));
  93. if ($fileTarget) $qb->setValue('file_target', $qb->expr()->literal($fileTarget));
  94. if ($permissions) $qb->setValue('permissions', $qb->expr()->literal($permissions));
  95. if ($token) $qb->setValue('token', $qb->expr()->literal($token));
  96. if ($expiration) $qb->setValue('expiration', $qb->createNamedParameter($expiration, IQueryBuilder::PARAM_DATE));
  97. if ($parent) $qb->setValue('parent', $qb->expr()->literal($parent));
  98. $this->assertEquals(1, $qb->execute());
  99. return$qb->getLastInsertId();
  100. }
  101. /**
  102. * @expectedException \OCP\Share\Exceptions\ShareNotFound
  103. */
  104. public function testGetShareByIdNotExist() {
  105. $this->provider->getShareById(1);
  106. }
  107. public function testGetShareByIdUserShare() {
  108. $qb = $this->dbConn->getQueryBuilder();
  109. $qb->insert('share')
  110. ->values([
  111. 'share_type' => $qb->expr()->literal(\OCP\Share::SHARE_TYPE_USER),
  112. 'share_with' => $qb->expr()->literal('sharedWith'),
  113. 'uid_owner' => $qb->expr()->literal('shareOwner'),
  114. 'uid_initiator' => $qb->expr()->literal('sharedBy'),
  115. 'item_type' => $qb->expr()->literal('file'),
  116. 'file_source' => $qb->expr()->literal(42),
  117. 'file_target' => $qb->expr()->literal('myTarget'),
  118. 'permissions' => $qb->expr()->literal(13),
  119. ]);
  120. $qb->execute();
  121. $id = $qb->getLastInsertId();
  122. $sharedBy = $this->createMock(IUser::class);
  123. $sharedBy->method('getUID')->willReturn('sharedBy');
  124. $shareOwner = $this->createMock(IUser::class);
  125. $shareOwner->method('getUID')->willReturn('shareOwner');
  126. $ownerPath = $this->createMock(File::class);
  127. $shareOwnerFolder = $this->createMock(Folder::class);
  128. $shareOwnerFolder->method('getById')->with(42)->willReturn([$ownerPath]);
  129. $this->rootFolder
  130. ->method('getUserFolder')
  131. ->will($this->returnValueMap([
  132. ['shareOwner', $shareOwnerFolder],
  133. ]));
  134. $share = $this->provider->getShareById($id);
  135. $this->assertEquals($id, $share->getId());
  136. $this->assertEquals(\OCP\Share::SHARE_TYPE_USER, $share->getShareType());
  137. $this->assertEquals('sharedWith', $share->getSharedWith());
  138. $this->assertEquals('sharedBy', $share->getSharedBy());
  139. $this->assertEquals('shareOwner', $share->getShareOwner());
  140. $this->assertEquals($ownerPath, $share->getNode());
  141. $this->assertEquals(13, $share->getPermissions());
  142. $this->assertEquals(null, $share->getToken());
  143. $this->assertEquals(null, $share->getExpirationDate());
  144. $this->assertEquals('myTarget', $share->getTarget());
  145. }
  146. public function testGetShareByIdLazy() {
  147. $qb = $this->dbConn->getQueryBuilder();
  148. $qb->insert('share')
  149. ->values([
  150. 'share_type' => $qb->expr()->literal(\OCP\Share::SHARE_TYPE_USER),
  151. 'share_with' => $qb->expr()->literal('sharedWith'),
  152. 'uid_owner' => $qb->expr()->literal('shareOwner'),
  153. 'uid_initiator' => $qb->expr()->literal('sharedBy'),
  154. 'item_type' => $qb->expr()->literal('file'),
  155. 'file_source' => $qb->expr()->literal(42),
  156. 'file_target' => $qb->expr()->literal('myTarget'),
  157. 'permissions' => $qb->expr()->literal(13),
  158. ]);
  159. $qb->execute();
  160. $id = $qb->getLastInsertId();
  161. $this->rootFolder->expects($this->never())->method('getUserFolder');
  162. $share = $this->provider->getShareById($id);
  163. // We do not fetch the node so the rootfolder is never called.
  164. $this->assertEquals($id, $share->getId());
  165. $this->assertEquals(\OCP\Share::SHARE_TYPE_USER, $share->getShareType());
  166. $this->assertEquals('sharedWith', $share->getSharedWith());
  167. $this->assertEquals('sharedBy', $share->getSharedBy());
  168. $this->assertEquals('shareOwner', $share->getShareOwner());
  169. $this->assertEquals(13, $share->getPermissions());
  170. $this->assertEquals(null, $share->getToken());
  171. $this->assertEquals(null, $share->getExpirationDate());
  172. $this->assertEquals('myTarget', $share->getTarget());
  173. }
  174. public function testGetShareByIdLazy2() {
  175. $qb = $this->dbConn->getQueryBuilder();
  176. $qb->insert('share')
  177. ->values([
  178. 'share_type' => $qb->expr()->literal(\OCP\Share::SHARE_TYPE_USER),
  179. 'share_with' => $qb->expr()->literal('sharedWith'),
  180. 'uid_owner' => $qb->expr()->literal('shareOwner'),
  181. 'uid_initiator' => $qb->expr()->literal('sharedBy'),
  182. 'item_type' => $qb->expr()->literal('file'),
  183. 'file_source' => $qb->expr()->literal(42),
  184. 'file_target' => $qb->expr()->literal('myTarget'),
  185. 'permissions' => $qb->expr()->literal(13),
  186. ]);
  187. $qb->execute();
  188. $id = $qb->getLastInsertId();
  189. $ownerPath = $this->createMock(File::class);
  190. $shareOwnerFolder = $this->createMock(Folder::class);
  191. $shareOwnerFolder->method('getById')->with(42)->willReturn([$ownerPath]);
  192. $this->rootFolder
  193. ->method('getUserFolder')
  194. ->with('shareOwner')
  195. ->willReturn($shareOwnerFolder);
  196. $share = $this->provider->getShareById($id);
  197. // We fetch the node so the root folder is eventually called
  198. $this->assertEquals($id, $share->getId());
  199. $this->assertEquals(\OCP\Share::SHARE_TYPE_USER, $share->getShareType());
  200. $this->assertEquals('sharedWith', $share->getSharedWith());
  201. $this->assertEquals('sharedBy', $share->getSharedBy());
  202. $this->assertEquals('shareOwner', $share->getShareOwner());
  203. $this->assertEquals($ownerPath, $share->getNode());
  204. $this->assertEquals(13, $share->getPermissions());
  205. $this->assertEquals(null, $share->getToken());
  206. $this->assertEquals(null, $share->getExpirationDate());
  207. $this->assertEquals('myTarget', $share->getTarget());
  208. }
  209. public function testGetShareByIdGroupShare() {
  210. $qb = $this->dbConn->getQueryBuilder();
  211. $qb->insert('share')
  212. ->values([
  213. 'share_type' => $qb->expr()->literal(\OCP\Share::SHARE_TYPE_GROUP),
  214. 'share_with' => $qb->expr()->literal('sharedWith'),
  215. 'uid_owner' => $qb->expr()->literal('shareOwner'),
  216. 'uid_initiator' => $qb->expr()->literal('sharedBy'),
  217. 'item_type' => $qb->expr()->literal('file'),
  218. 'file_source' => $qb->expr()->literal(42),
  219. 'file_target' => $qb->expr()->literal('myTarget'),
  220. 'permissions' => $qb->expr()->literal(13),
  221. ]);
  222. $this->assertEquals(1, $qb->execute());
  223. // Get the id
  224. $id = $qb->getLastInsertId();
  225. $ownerPath = $this->createMock(Folder::class);
  226. $shareOwnerFolder = $this->createMock(Folder::class);
  227. $shareOwnerFolder->method('getById')->with(42)->willReturn([$ownerPath]);
  228. $this->rootFolder
  229. ->method('getUserFolder')
  230. ->will($this->returnValueMap([
  231. ['shareOwner', $shareOwnerFolder],
  232. ]));
  233. $share = $this->provider->getShareById($id);
  234. $this->assertEquals($id, $share->getId());
  235. $this->assertEquals(\OCP\Share::SHARE_TYPE_GROUP, $share->getShareType());
  236. $this->assertEquals('sharedWith', $share->getSharedWith());
  237. $this->assertEquals('sharedBy', $share->getSharedBy());
  238. $this->assertEquals('shareOwner', $share->getShareOwner());
  239. $this->assertEquals($ownerPath, $share->getNode());
  240. $this->assertEquals(13, $share->getPermissions());
  241. $this->assertEquals(null, $share->getToken());
  242. $this->assertEquals(null, $share->getExpirationDate());
  243. $this->assertEquals('myTarget', $share->getTarget());
  244. }
  245. public function testGetShareByIdUserGroupShare() {
  246. $id = $this->addShareToDB(\OCP\Share::SHARE_TYPE_GROUP, 'group0', 'user0', 'user0', 'file', 42, 'myTarget', 31, null, null);
  247. $this->addShareToDB(2, 'user1', 'user0', 'user0', 'file', 42, 'userTarget', 0, null, null, $id);
  248. $user0 = $this->createMock(IUser::class);
  249. $user0->method('getUID')->willReturn('user0');
  250. $user1 = $this->createMock(IUser::class);
  251. $user1->method('getUID')->willReturn('user1');
  252. $group0 = $this->createMock(IGroup::class);
  253. $group0->method('inGroup')->with($user1)->willReturn(true);
  254. $node = $this->createMock(Folder::class);
  255. $node->method('getId')->willReturn(42);
  256. $this->rootFolder->method('getUserFolder')->with('user0')->will($this->returnSelf());
  257. $this->rootFolder->method('getById')->willReturn([$node]);
  258. $this->userManager->method('get')->will($this->returnValueMap([
  259. ['user0', $user0],
  260. ['user1', $user1],
  261. ]));
  262. $this->groupManager->method('get')->with('group0')->willReturn($group0);
  263. $share = $this->provider->getShareById($id, 'user1');
  264. $this->assertEquals($id, $share->getId());
  265. $this->assertEquals(\OCP\Share::SHARE_TYPE_GROUP, $share->getShareType());
  266. $this->assertSame('group0', $share->getSharedWith());
  267. $this->assertSame('user0', $share->getSharedBy());
  268. $this->assertSame('user0', $share->getShareOwner());
  269. $this->assertSame($node, $share->getNode());
  270. $this->assertEquals(0, $share->getPermissions());
  271. $this->assertEquals(null, $share->getToken());
  272. $this->assertEquals(null, $share->getExpirationDate());
  273. $this->assertEquals('userTarget', $share->getTarget());
  274. }
  275. public function testGetShareByIdLinkShare() {
  276. $qb = $this->dbConn->getQueryBuilder();
  277. $qb->insert('share')
  278. ->values([
  279. 'share_type' => $qb->expr()->literal(\OCP\Share::SHARE_TYPE_LINK),
  280. 'share_with' => $qb->expr()->literal('sharedWith'),
  281. 'uid_owner' => $qb->expr()->literal('shareOwner'),
  282. 'uid_initiator' => $qb->expr()->literal('sharedBy'),
  283. 'item_type' => $qb->expr()->literal('file'),
  284. 'file_source' => $qb->expr()->literal(42),
  285. 'file_target' => $qb->expr()->literal('myTarget'),
  286. 'permissions' => $qb->expr()->literal(13),
  287. 'token' => $qb->expr()->literal('token'),
  288. 'expiration' => $qb->expr()->literal('2000-01-02 00:00:00'),
  289. ]);
  290. $this->assertEquals(1, $qb->execute());
  291. $id = $qb->getLastInsertId();
  292. $ownerPath = $this->createMock(Folder::class);
  293. $shareOwnerFolder = $this->createMock(Folder::class);
  294. $shareOwnerFolder->method('getById')->with(42)->willReturn([$ownerPath]);
  295. $this->rootFolder
  296. ->method('getUserFolder')
  297. ->will($this->returnValueMap([
  298. ['shareOwner', $shareOwnerFolder],
  299. ]));
  300. $share = $this->provider->getShareById($id);
  301. $this->assertEquals($id, $share->getId());
  302. $this->assertEquals(\OCP\Share::SHARE_TYPE_LINK, $share->getShareType());
  303. $this->assertEquals('sharedWith', $share->getPassword());
  304. $this->assertEquals('sharedBy', $share->getSharedBy());
  305. $this->assertEquals('shareOwner', $share->getShareOwner());
  306. $this->assertEquals($ownerPath, $share->getNode());
  307. $this->assertEquals(13, $share->getPermissions());
  308. $this->assertEquals('token', $share->getToken());
  309. $this->assertEquals(\DateTime::createFromFormat('Y-m-d H:i:s', '2000-01-02 00:00:00'), $share->getExpirationDate());
  310. $this->assertEquals('myTarget', $share->getTarget());
  311. }
  312. public function testDeleteSingleShare() {
  313. $qb = $this->dbConn->getQueryBuilder();
  314. $qb->insert('share')
  315. ->values([
  316. 'share_type' => $qb->expr()->literal(\OCP\Share::SHARE_TYPE_USER),
  317. 'share_with' => $qb->expr()->literal('sharedWith'),
  318. 'uid_owner' => $qb->expr()->literal('sharedBy'),
  319. 'item_type' => $qb->expr()->literal('file'),
  320. 'file_source' => $qb->expr()->literal(42),
  321. 'file_target' => $qb->expr()->literal('myTarget'),
  322. 'permissions' => $qb->expr()->literal(13),
  323. ]);
  324. $this->assertEquals(1, $qb->execute());
  325. $id = $qb->getLastInsertId();
  326. $share = $this->createMock(IShare::class);
  327. $share->method('getId')->willReturn($id);
  328. $provider = $this->getMockBuilder('OC\Share20\DefaultShareProvider')
  329. ->setConstructorArgs([
  330. $this->dbConn,
  331. $this->userManager,
  332. $this->groupManager,
  333. $this->rootFolder,
  334. ])
  335. ->setMethods(['getShareById'])
  336. ->getMock();
  337. $provider->delete($share);
  338. $qb = $this->dbConn->getQueryBuilder();
  339. $qb->select('*')
  340. ->from('share');
  341. $cursor = $qb->execute();
  342. $result = $cursor->fetchAll();
  343. $cursor->closeCursor();
  344. $this->assertEmpty($result);
  345. }
  346. public function testDeleteSingleShareLazy() {
  347. $qb = $this->dbConn->getQueryBuilder();
  348. $qb->insert('share')
  349. ->values([
  350. 'share_type' => $qb->expr()->literal(\OCP\Share::SHARE_TYPE_USER),
  351. 'share_with' => $qb->expr()->literal('sharedWith'),
  352. 'uid_owner' => $qb->expr()->literal('shareOwner'),
  353. 'uid_initiator' => $qb->expr()->literal('sharedBy'),
  354. 'item_type' => $qb->expr()->literal('file'),
  355. 'file_source' => $qb->expr()->literal(42),
  356. 'file_target' => $qb->expr()->literal('myTarget'),
  357. 'permissions' => $qb->expr()->literal(13),
  358. ]);
  359. $this->assertEquals(1, $qb->execute());
  360. $id = $qb->getLastInsertId();
  361. $this->rootFolder->expects($this->never())->method($this->anything());
  362. $share = $this->provider->getShareById($id);
  363. $this->provider->delete($share);
  364. $qb = $this->dbConn->getQueryBuilder();
  365. $qb->select('*')
  366. ->from('share');
  367. $cursor = $qb->execute();
  368. $result = $cursor->fetchAll();
  369. $cursor->closeCursor();
  370. $this->assertEmpty($result);
  371. }
  372. public function testDeleteGroupShareWithUserGroupShares() {
  373. $qb = $this->dbConn->getQueryBuilder();
  374. $qb->insert('share')
  375. ->values([
  376. 'share_type' => $qb->expr()->literal(\OCP\Share::SHARE_TYPE_GROUP),
  377. 'share_with' => $qb->expr()->literal('sharedWith'),
  378. 'uid_owner' => $qb->expr()->literal('sharedBy'),
  379. 'item_type' => $qb->expr()->literal('file'),
  380. 'file_source' => $qb->expr()->literal(42),
  381. 'file_target' => $qb->expr()->literal('myTarget'),
  382. 'permissions' => $qb->expr()->literal(13),
  383. ]);
  384. $this->assertEquals(1, $qb->execute());
  385. $id = $qb->getLastInsertId();
  386. $qb = $this->dbConn->getQueryBuilder();
  387. $qb->insert('share')
  388. ->values([
  389. 'share_type' => $qb->expr()->literal(2),
  390. 'share_with' => $qb->expr()->literal('sharedWithUser'),
  391. 'uid_owner' => $qb->expr()->literal('sharedBy'),
  392. 'item_type' => $qb->expr()->literal('file'),
  393. 'file_source' => $qb->expr()->literal(42),
  394. 'file_target' => $qb->expr()->literal('myTarget'),
  395. 'permissions' => $qb->expr()->literal(13),
  396. 'parent' => $qb->expr()->literal($id),
  397. ]);
  398. $this->assertEquals(1, $qb->execute());
  399. $share = $this->createMock(IShare::class);
  400. $share->method('getId')->willReturn($id);
  401. $share->method('getShareType')->willReturn(\OCP\Share::SHARE_TYPE_GROUP);
  402. $provider = $this->getMockBuilder('OC\Share20\DefaultShareProvider')
  403. ->setConstructorArgs([
  404. $this->dbConn,
  405. $this->userManager,
  406. $this->groupManager,
  407. $this->rootFolder,
  408. ])
  409. ->setMethods(['getShareById'])
  410. ->getMock();
  411. $provider->delete($share);
  412. $qb = $this->dbConn->getQueryBuilder();
  413. $qb->select('*')
  414. ->from('share');
  415. $cursor = $qb->execute();
  416. $result = $cursor->fetchAll();
  417. $cursor->closeCursor();
  418. $this->assertEmpty($result);
  419. }
  420. public function testGetChildren() {
  421. $qb = $this->dbConn->getQueryBuilder();
  422. $qb->insert('share')
  423. ->values([
  424. 'share_type' => $qb->expr()->literal(\OCP\Share::SHARE_TYPE_USER),
  425. 'share_with' => $qb->expr()->literal('sharedWith'),
  426. 'uid_owner' => $qb->expr()->literal('shareOwner'),
  427. 'uid_initiator' => $qb->expr()->literal('sharedBy'),
  428. 'item_type' => $qb->expr()->literal('file'),
  429. 'file_source' => $qb->expr()->literal(42),
  430. 'file_target' => $qb->expr()->literal('myTarget'),
  431. 'permissions' => $qb->expr()->literal(13),
  432. ]);
  433. $qb->execute();
  434. // Get the id
  435. $id = $qb->getLastInsertId();
  436. $qb = $this->dbConn->getQueryBuilder();
  437. $qb->insert('share')
  438. ->values([
  439. 'share_type' => $qb->expr()->literal(\OCP\Share::SHARE_TYPE_USER),
  440. 'share_with' => $qb->expr()->literal('user1'),
  441. 'uid_owner' => $qb->expr()->literal('shareOwner'),
  442. 'uid_initiator' => $qb->expr()->literal('user2'),
  443. 'item_type' => $qb->expr()->literal('file'),
  444. 'file_source' => $qb->expr()->literal(1),
  445. 'file_target' => $qb->expr()->literal('myTarget1'),
  446. 'permissions' => $qb->expr()->literal(2),
  447. 'parent' => $qb->expr()->literal($id),
  448. ]);
  449. $qb->execute();
  450. $qb = $this->dbConn->getQueryBuilder();
  451. $qb->insert('share')
  452. ->values([
  453. 'share_type' => $qb->expr()->literal(\OCP\Share::SHARE_TYPE_GROUP),
  454. 'share_with' => $qb->expr()->literal('group1'),
  455. 'uid_owner' => $qb->expr()->literal('shareOwner'),
  456. 'uid_initiator' => $qb->expr()->literal('user3'),
  457. 'item_type' => $qb->expr()->literal('folder'),
  458. 'file_source' => $qb->expr()->literal(3),
  459. 'file_target' => $qb->expr()->literal('myTarget2'),
  460. 'permissions' => $qb->expr()->literal(4),
  461. 'parent' => $qb->expr()->literal($id),
  462. ]);
  463. $qb->execute();
  464. $ownerPath = $this->createMock(Folder::class);
  465. $ownerFolder = $this->createMock(Folder::class);
  466. $ownerFolder->method('getById')->willReturn([$ownerPath]);
  467. $this->rootFolder
  468. ->method('getUserFolder')
  469. ->will($this->returnValueMap([
  470. ['shareOwner', $ownerFolder],
  471. ]));
  472. $share = $this->createMock(IShare::class);
  473. $share->method('getId')->willReturn($id);
  474. $children = $this->provider->getChildren($share);
  475. $this->assertCount(2, $children);
  476. //Child1
  477. $this->assertEquals(\OCP\Share::SHARE_TYPE_USER, $children[0]->getShareType());
  478. $this->assertEquals('user1', $children[0]->getSharedWith());
  479. $this->assertEquals('user2', $children[0]->getSharedBy());
  480. $this->assertEquals('shareOwner', $children[0]->getShareOwner());
  481. $this->assertEquals($ownerPath, $children[0]->getNode());
  482. $this->assertEquals(2, $children[0]->getPermissions());
  483. $this->assertEquals(null, $children[0]->getToken());
  484. $this->assertEquals(null, $children[0]->getExpirationDate());
  485. $this->assertEquals('myTarget1', $children[0]->getTarget());
  486. //Child2
  487. $this->assertEquals(\OCP\Share::SHARE_TYPE_GROUP, $children[1]->getShareType());
  488. $this->assertEquals('group1', $children[1]->getSharedWith());
  489. $this->assertEquals('user3', $children[1]->getSharedBy());
  490. $this->assertEquals('shareOwner', $children[1]->getShareOwner());
  491. $this->assertEquals($ownerPath, $children[1]->getNode());
  492. $this->assertEquals(4, $children[1]->getPermissions());
  493. $this->assertEquals(null, $children[1]->getToken());
  494. $this->assertEquals(null, $children[1]->getExpirationDate());
  495. $this->assertEquals('myTarget2', $children[1]->getTarget());
  496. }
  497. public function testCreateUserShare() {
  498. $share = new \OC\Share20\Share($this->rootFolder, $this->userManager);
  499. $shareOwner = $this->createMock(IUser::class);
  500. $shareOwner->method('getUID')->WillReturn('shareOwner');
  501. $path = $this->createMock(File::class);
  502. $path->method('getId')->willReturn(100);
  503. $path->method('getOwner')->willReturn($shareOwner);
  504. $ownerFolder = $this->createMock(Folder::class);
  505. $userFolder = $this->createMock(Folder::class);
  506. $this->rootFolder
  507. ->method('getUserFolder')
  508. ->will($this->returnValueMap([
  509. ['sharedBy', $userFolder],
  510. ['shareOwner', $ownerFolder],
  511. ]));
  512. $userFolder->method('getById')
  513. ->with(100)
  514. ->willReturn([$path]);
  515. $ownerFolder->method('getById')
  516. ->with(100)
  517. ->willReturn([$path]);
  518. $share->setShareType(\OCP\Share::SHARE_TYPE_USER);
  519. $share->setSharedWith('sharedWith');
  520. $share->setSharedBy('sharedBy');
  521. $share->setShareOwner('shareOwner');
  522. $share->setNode($path);
  523. $share->setPermissions(1);
  524. $share->setTarget('/target');
  525. $share2 = $this->provider->create($share);
  526. $this->assertNotNull($share2->getId());
  527. $this->assertSame('ocinternal:'.$share2->getId(), $share2->getFullId());
  528. $this->assertSame(\OCP\Share::SHARE_TYPE_USER, $share2->getShareType());
  529. $this->assertSame('sharedWith', $share2->getSharedWith());
  530. $this->assertSame('sharedBy', $share2->getSharedBy());
  531. $this->assertSame('shareOwner', $share2->getShareOwner());
  532. $this->assertSame(1, $share2->getPermissions());
  533. $this->assertSame('/target', $share2->getTarget());
  534. $this->assertLessThanOrEqual(new \DateTime(), $share2->getShareTime());
  535. $this->assertSame($path, $share2->getNode());
  536. }
  537. public function testCreateGroupShare() {
  538. $share = new \OC\Share20\Share($this->rootFolder, $this->userManager);
  539. $shareOwner = $this->createMock(IUser::class);
  540. $shareOwner->method('getUID')->willReturn('shareOwner');
  541. $path = $this->createMock(Folder::class);
  542. $path->method('getId')->willReturn(100);
  543. $path->method('getOwner')->willReturn($shareOwner);
  544. $ownerFolder = $this->createMock(Folder::class);
  545. $userFolder = $this->createMock(Folder::class);
  546. $this->rootFolder
  547. ->method('getUserFolder')
  548. ->will($this->returnValueMap([
  549. ['sharedBy', $userFolder],
  550. ['shareOwner', $ownerFolder],
  551. ]));
  552. $userFolder->method('getById')
  553. ->with(100)
  554. ->willReturn([$path]);
  555. $ownerFolder->method('getById')
  556. ->with(100)
  557. ->willReturn([$path]);
  558. $share->setShareType(\OCP\Share::SHARE_TYPE_GROUP);
  559. $share->setSharedWith('sharedWith');
  560. $share->setSharedBy('sharedBy');
  561. $share->setShareOwner('shareOwner');
  562. $share->setNode($path);
  563. $share->setPermissions(1);
  564. $share->setTarget('/target');
  565. $share2 = $this->provider->create($share);
  566. $this->assertNotNull($share2->getId());
  567. $this->assertSame('ocinternal:'.$share2->getId(), $share2->getFullId());
  568. $this->assertSame(\OCP\Share::SHARE_TYPE_GROUP, $share2->getShareType());
  569. $this->assertSame('sharedWith', $share2->getSharedWith());
  570. $this->assertSame('sharedBy', $share2->getSharedBy());
  571. $this->assertSame('shareOwner', $share2->getShareOwner());
  572. $this->assertSame(1, $share2->getPermissions());
  573. $this->assertSame('/target', $share2->getTarget());
  574. $this->assertLessThanOrEqual(new \DateTime(), $share2->getShareTime());
  575. $this->assertSame($path, $share2->getNode());
  576. }
  577. public function testCreateLinkShare() {
  578. $share = new \OC\Share20\Share($this->rootFolder, $this->userManager);
  579. $shareOwner = $this->createMock(IUser::class);
  580. $shareOwner->method('getUID')->willReturn('shareOwner');
  581. $path = $this->createMock(Folder::class);
  582. $path->method('getId')->willReturn(100);
  583. $path->method('getOwner')->willReturn($shareOwner);
  584. $ownerFolder = $this->createMock(Folder::class);
  585. $userFolder = $this->createMock(Folder::class);
  586. $this->rootFolder
  587. ->method('getUserFolder')
  588. ->will($this->returnValueMap([
  589. ['sharedBy', $userFolder],
  590. ['shareOwner', $ownerFolder],
  591. ]));
  592. $userFolder->method('getById')
  593. ->with(100)
  594. ->willReturn([$path]);
  595. $ownerFolder->method('getById')
  596. ->with(100)
  597. ->willReturn([$path]);
  598. $share->setShareType(\OCP\Share::SHARE_TYPE_LINK);
  599. $share->setSharedBy('sharedBy');
  600. $share->setShareOwner('shareOwner');
  601. $share->setNode($path);
  602. $share->setPermissions(1);
  603. $share->setPassword('password');
  604. $share->setToken('token');
  605. $expireDate = new \DateTime();
  606. $share->setExpirationDate($expireDate);
  607. $share->setTarget('/target');
  608. $share2 = $this->provider->create($share);
  609. $this->assertNotNull($share2->getId());
  610. $this->assertSame('ocinternal:'.$share2->getId(), $share2->getFullId());
  611. $this->assertSame(\OCP\Share::SHARE_TYPE_LINK, $share2->getShareType());
  612. $this->assertSame('sharedBy', $share2->getSharedBy());
  613. $this->assertSame('shareOwner', $share2->getShareOwner());
  614. $this->assertSame(1, $share2->getPermissions());
  615. $this->assertSame('/target', $share2->getTarget());
  616. $this->assertLessThanOrEqual(new \DateTime(), $share2->getShareTime());
  617. $this->assertSame($path, $share2->getNode());
  618. $this->assertSame('password', $share2->getPassword());
  619. $this->assertSame('token', $share2->getToken());
  620. $this->assertEquals($expireDate, $share2->getExpirationDate());
  621. }
  622. public function testGetShareByToken() {
  623. $qb = $this->dbConn->getQueryBuilder();
  624. $qb->insert('share')
  625. ->values([
  626. 'share_type' => $qb->expr()->literal(\OCP\Share::SHARE_TYPE_LINK),
  627. 'share_with' => $qb->expr()->literal('password'),
  628. 'uid_owner' => $qb->expr()->literal('shareOwner'),
  629. 'uid_initiator' => $qb->expr()->literal('sharedBy'),
  630. 'item_type' => $qb->expr()->literal('file'),
  631. 'file_source' => $qb->expr()->literal(42),
  632. 'file_target' => $qb->expr()->literal('myTarget'),
  633. 'permissions' => $qb->expr()->literal(13),
  634. 'token' => $qb->expr()->literal('secrettoken'),
  635. ]);
  636. $qb->execute();
  637. $id = $qb->getLastInsertId();
  638. $file = $this->createMock(File::class);
  639. $this->rootFolder->method('getUserFolder')->with('shareOwner')->will($this->returnSelf());
  640. $this->rootFolder->method('getById')->with(42)->willReturn([$file]);
  641. $share = $this->provider->getShareByToken('secrettoken');
  642. $this->assertEquals($id, $share->getId());
  643. $this->assertSame('shareOwner', $share->getShareOwner());
  644. $this->assertSame('sharedBy', $share->getSharedBy());
  645. $this->assertSame('secrettoken', $share->getToken());
  646. $this->assertSame('password', $share->getPassword());
  647. $this->assertSame(null, $share->getSharedWith());
  648. }
  649. /**
  650. * @expectedException \OCP\Share\Exceptions\ShareNotFound
  651. */
  652. public function testGetShareByTokenNotFound() {
  653. $this->provider->getShareByToken('invalidtoken');
  654. }
  655. public function testGetSharedWithUser() {
  656. $qb = $this->dbConn->getQueryBuilder();
  657. $qb->insert('share')
  658. ->values([
  659. 'share_type' => $qb->expr()->literal(\OCP\Share::SHARE_TYPE_USER),
  660. 'share_with' => $qb->expr()->literal('sharedWith'),
  661. 'uid_owner' => $qb->expr()->literal('shareOwner'),
  662. 'uid_initiator' => $qb->expr()->literal('sharedBy'),
  663. 'item_type' => $qb->expr()->literal('file'),
  664. 'file_source' => $qb->expr()->literal(42),
  665. 'file_target' => $qb->expr()->literal('myTarget'),
  666. 'permissions' => $qb->expr()->literal(13),
  667. ]);
  668. $this->assertEquals(1, $qb->execute());
  669. $id = $qb->getLastInsertId();
  670. $qb = $this->dbConn->getQueryBuilder();
  671. $qb->insert('share')
  672. ->values([
  673. 'share_type' => $qb->expr()->literal(\OCP\Share::SHARE_TYPE_USER),
  674. 'share_with' => $qb->expr()->literal('sharedWith2'),
  675. 'uid_owner' => $qb->expr()->literal('shareOwner2'),
  676. 'uid_initiator' => $qb->expr()->literal('sharedBy2'),
  677. 'item_type' => $qb->expr()->literal('file2'),
  678. 'file_source' => $qb->expr()->literal(43),
  679. 'file_target' => $qb->expr()->literal('myTarget2'),
  680. 'permissions' => $qb->expr()->literal(14),
  681. ]);
  682. $this->assertEquals(1, $qb->execute());
  683. $file = $this->createMock(File::class);
  684. $this->rootFolder->method('getUserFolder')->with('shareOwner')->will($this->returnSelf());
  685. $this->rootFolder->method('getById')->with(42)->willReturn([$file]);
  686. $share = $this->provider->getSharedWith('sharedWith', \OCP\Share::SHARE_TYPE_USER, null, 1 , 0);
  687. $this->assertCount(1, $share);
  688. $share = $share[0];
  689. $this->assertEquals($id, $share->getId());
  690. $this->assertEquals('sharedWith', $share->getSharedWith());
  691. $this->assertEquals('shareOwner', $share->getShareOwner());
  692. $this->assertEquals('sharedBy', $share->getSharedBy());
  693. $this->assertEquals(\OCP\Share::SHARE_TYPE_USER, $share->getShareType());
  694. }
  695. public function testGetSharedWithGroup() {
  696. $qb = $this->dbConn->getQueryBuilder();
  697. $qb->insert('share')
  698. ->values([
  699. 'share_type' => $qb->expr()->literal(\OCP\Share::SHARE_TYPE_GROUP),
  700. 'share_with' => $qb->expr()->literal('sharedWith'),
  701. 'uid_owner' => $qb->expr()->literal('shareOwner2'),
  702. 'uid_initiator' => $qb->expr()->literal('sharedBy2'),
  703. 'item_type' => $qb->expr()->literal('file'),
  704. 'file_source' => $qb->expr()->literal(43),
  705. 'file_target' => $qb->expr()->literal('myTarget2'),
  706. 'permissions' => $qb->expr()->literal(14),
  707. ]);
  708. $this->assertEquals(1, $qb->execute());
  709. $qb = $this->dbConn->getQueryBuilder();
  710. $qb->insert('share')
  711. ->values([
  712. 'share_type' => $qb->expr()->literal(\OCP\Share::SHARE_TYPE_GROUP),
  713. 'share_with' => $qb->expr()->literal('sharedWith'),
  714. 'uid_owner' => $qb->expr()->literal('shareOwner'),
  715. 'uid_initiator' => $qb->expr()->literal('sharedBy'),
  716. 'item_type' => $qb->expr()->literal('file'),
  717. 'file_source' => $qb->expr()->literal(42),
  718. 'file_target' => $qb->expr()->literal('myTarget'),
  719. 'permissions' => $qb->expr()->literal(13),
  720. ]);
  721. $this->assertEquals(1, $qb->execute());
  722. $id = $qb->getLastInsertId();
  723. $groups = [];
  724. foreach(range(0, 100) as $i) {
  725. $group = $this->createMock(IGroup::class);
  726. $group->method('getGID')->willReturn('group'.$i);
  727. $groups[] = $group;
  728. }
  729. $group = $this->createMock(IGroup::class);
  730. $group->method('getGID')->willReturn('sharedWith');
  731. $groups[] = $group;
  732. $user = $this->createMock(IUser::class);
  733. $user->method('getUID')->willReturn('sharedWith');
  734. $owner = $this->createMock(IUser::class);
  735. $owner->method('getUID')->willReturn('shareOwner');
  736. $initiator = $this->createMock(IUser::class);
  737. $initiator->method('getUID')->willReturn('sharedBy');
  738. $this->userManager->method('get')->willReturnMap([
  739. ['sharedWith', $user],
  740. ['shareOwner', $owner],
  741. ['sharedBy', $initiator],
  742. ]);
  743. $this->groupManager->method('getUserGroups')->with($user)->willReturn($groups);
  744. $this->groupManager->method('get')->with('sharedWith')->willReturn($group);
  745. $file = $this->createMock(File::class);
  746. $this->rootFolder->method('getUserFolder')->with('shareOwner')->will($this->returnSelf());
  747. $this->rootFolder->method('getById')->with(42)->willReturn([$file]);
  748. $share = $this->provider->getSharedWith('sharedWith', \OCP\Share::SHARE_TYPE_GROUP, null, 20 , 1);
  749. $this->assertCount(1, $share);
  750. $share = $share[0];
  751. $this->assertEquals($id, $share->getId());
  752. $this->assertEquals('sharedWith', $share->getSharedWith());
  753. $this->assertEquals('shareOwner', $share->getShareOwner());
  754. $this->assertEquals('sharedBy', $share->getSharedBy());
  755. $this->assertEquals(\OCP\Share::SHARE_TYPE_GROUP, $share->getShareType());
  756. }
  757. public function testGetSharedWithGroupUserModified() {
  758. $qb = $this->dbConn->getQueryBuilder();
  759. $qb->insert('share')
  760. ->values([
  761. 'share_type' => $qb->expr()->literal(\OCP\Share::SHARE_TYPE_GROUP),
  762. 'share_with' => $qb->expr()->literal('sharedWith'),
  763. 'uid_owner' => $qb->expr()->literal('shareOwner'),
  764. 'uid_initiator' => $qb->expr()->literal('sharedBy'),
  765. 'item_type' => $qb->expr()->literal('file'),
  766. 'file_source' => $qb->expr()->literal(42),
  767. 'file_target' => $qb->expr()->literal('myTarget'),
  768. 'permissions' => $qb->expr()->literal(13),
  769. ]);
  770. $this->assertEquals(1, $qb->execute());
  771. $id = $qb->getLastInsertId();
  772. /*
  773. * Wrong share. Should not be taken by code.
  774. */
  775. $qb = $this->dbConn->getQueryBuilder();
  776. $qb->insert('share')
  777. ->values([
  778. 'share_type' => $qb->expr()->literal(2),
  779. 'share_with' => $qb->expr()->literal('user2'),
  780. 'uid_owner' => $qb->expr()->literal('shareOwner'),
  781. 'uid_initiator' => $qb->expr()->literal('sharedBy'),
  782. 'item_type' => $qb->expr()->literal('file'),
  783. 'file_source' => $qb->expr()->literal(42),
  784. 'file_target' => $qb->expr()->literal('wrongTarget'),
  785. 'permissions' => $qb->expr()->literal(31),
  786. 'parent' => $qb->expr()->literal($id),
  787. ]);
  788. $this->assertEquals(1, $qb->execute());
  789. /*
  790. * Correct share. should be taken by code path.
  791. */
  792. $qb = $this->dbConn->getQueryBuilder();
  793. $qb->insert('share')
  794. ->values([
  795. 'share_type' => $qb->expr()->literal(2),
  796. 'share_with' => $qb->expr()->literal('user'),
  797. 'uid_owner' => $qb->expr()->literal('shareOwner'),
  798. 'uid_initiator' => $qb->expr()->literal('sharedBy'),
  799. 'item_type' => $qb->expr()->literal('file'),
  800. 'file_source' => $qb->expr()->literal(42),
  801. 'file_target' => $qb->expr()->literal('userTarget'),
  802. 'permissions' => $qb->expr()->literal(0),
  803. 'parent' => $qb->expr()->literal($id),
  804. ]);
  805. $this->assertEquals(1, $qb->execute());
  806. $group = $this->createMock(IGroup::class);
  807. $group->method('getGID')->willReturn('sharedWith');
  808. $groups = [$group];
  809. $user = $this->createMock(IUser::class);
  810. $user->method('getUID')->willReturn('user');
  811. $owner = $this->createMock(IUser::class);
  812. $owner->method('getUID')->willReturn('shareOwner');
  813. $initiator = $this->createMock(IUser::class);
  814. $initiator->method('getUID')->willReturn('sharedBy');
  815. $this->userManager->method('get')->willReturnMap([
  816. ['user', $user],
  817. ['shareOwner', $owner],
  818. ['sharedBy', $initiator],
  819. ]);
  820. $this->groupManager->method('getUserGroups')->with($user)->willReturn($groups);
  821. $this->groupManager->method('get')->with('sharedWith')->willReturn($group);
  822. $file = $this->createMock(File::class);
  823. $this->rootFolder->method('getUserFolder')->with('shareOwner')->will($this->returnSelf());
  824. $this->rootFolder->method('getById')->with(42)->willReturn([$file]);
  825. $share = $this->provider->getSharedWith('user', \OCP\Share::SHARE_TYPE_GROUP, null, -1, 0);
  826. $this->assertCount(1, $share);
  827. $share = $share[0];
  828. $this->assertSame((string)$id, $share->getId());
  829. $this->assertSame('sharedWith', $share->getSharedWith());
  830. $this->assertSame('shareOwner', $share->getShareOwner());
  831. $this->assertSame('sharedBy', $share->getSharedBy());
  832. $this->assertSame(\OCP\Share::SHARE_TYPE_GROUP, $share->getShareType());
  833. $this->assertSame(0, $share->getPermissions());
  834. $this->assertSame('userTarget', $share->getTarget());
  835. }
  836. public function testGetSharedWithUserWithNode() {
  837. $this->addShareToDB(\OCP\Share::SHARE_TYPE_USER, 'user0', 'user1', 'user1',
  838. 'file', 42, 'myTarget', 31, null, null, null);
  839. $id = $this->addShareToDB(\OCP\Share::SHARE_TYPE_USER, 'user0', 'user1', 'user1',
  840. 'file', 43, 'myTarget', 31, null, null, null);
  841. $user0 = $this->createMock(IUser::class);
  842. $user0->method('getUID')->willReturn('user0');
  843. $user1 = $this->createMock(IUser::class);
  844. $user1->method('getUID')->willReturn('user1');
  845. $this->userManager->method('get')->willReturnMap([
  846. ['user0', $user0],
  847. ['user1', $user1],
  848. ]);
  849. $file = $this->createMock(File::class);
  850. $file->method('getId')->willReturn(43);
  851. $this->rootFolder->method('getUserFolder')->with('user1')->will($this->returnSelf());
  852. $this->rootFolder->method('getById')->with(43)->willReturn([$file]);
  853. $share = $this->provider->getSharedWith('user0', \OCP\Share::SHARE_TYPE_USER, $file, -1, 0);
  854. $this->assertCount(1, $share);
  855. $share = $share[0];
  856. $this->assertEquals($id, $share->getId());
  857. $this->assertSame('user0', $share->getSharedWith());
  858. $this->assertSame('user1', $share->getShareOwner());
  859. $this->assertSame('user1', $share->getSharedBy());
  860. $this->assertSame($file, $share->getNode());
  861. $this->assertEquals(\OCP\Share::SHARE_TYPE_USER, $share->getShareType());
  862. }
  863. public function testGetSharedWithGroupWithNode() {
  864. $this->addShareToDB(\OCP\Share::SHARE_TYPE_GROUP, 'group0', 'user1', 'user1',
  865. 'file', 42, 'myTarget', 31, null, null, null);
  866. $id = $this->addShareToDB(\OCP\Share::SHARE_TYPE_GROUP, 'group0', 'user1', 'user1',
  867. 'file', 43, 'myTarget', 31, null, null, null);
  868. $user0 = $this->createMock(IUser::class);
  869. $user0->method('getUID')->willReturn('user0');
  870. $user1 = $this->createMock(IUser::class);
  871. $user1->method('getUID')->willReturn('user1');
  872. $this->userManager->method('get')->willReturnMap([
  873. ['user0', $user0],
  874. ['user1', $user1],
  875. ]);
  876. $group0 = $this->createMock(IGroup::class);
  877. $group0->method('getGID')->willReturn('group0');
  878. $this->groupManager->method('get')->with('group0')->willReturn($group0);
  879. $this->groupManager->method('getUserGroups')->with($user0)->willReturn([$group0]);
  880. $node = $this->createMock(Folder::class);
  881. $node->method('getId')->willReturn(43);
  882. $this->rootFolder->method('getUserFolder')->with('user1')->will($this->returnSelf());
  883. $this->rootFolder->method('getById')->with(43)->willReturn([$node]);
  884. $share = $this->provider->getSharedWith('user0', \OCP\Share::SHARE_TYPE_GROUP, $node, -1, 0);
  885. $this->assertCount(1, $share);
  886. $share = $share[0];
  887. $this->assertEquals($id, $share->getId());
  888. $this->assertSame('group0', $share->getSharedWith());
  889. $this->assertSame('user1', $share->getShareOwner());
  890. $this->assertSame('user1', $share->getSharedBy());
  891. $this->assertSame($node, $share->getNode());
  892. $this->assertEquals(\OCP\Share::SHARE_TYPE_GROUP, $share->getShareType());
  893. }
  894. public function testGetSharesBy() {
  895. $qb = $this->dbConn->getQueryBuilder();
  896. $qb->insert('share')
  897. ->values([
  898. 'share_type' => $qb->expr()->literal(\OCP\Share::SHARE_TYPE_USER),
  899. 'share_with' => $qb->expr()->literal('sharedWith'),
  900. 'uid_owner' => $qb->expr()->literal('shareOwner'),
  901. 'uid_initiator' => $qb->expr()->literal('sharedBy'),
  902. 'item_type' => $qb->expr()->literal('file'),
  903. 'file_source' => $qb->expr()->literal(42),
  904. 'file_target' => $qb->expr()->literal('myTarget'),
  905. 'permissions' => $qb->expr()->literal(13),
  906. ]);
  907. $this->assertEquals(1, $qb->execute());
  908. $id = $qb->getLastInsertId();
  909. $qb = $this->dbConn->getQueryBuilder();
  910. $qb->insert('share')
  911. ->values([
  912. 'share_type' => $qb->expr()->literal(\OCP\Share::SHARE_TYPE_USER),
  913. 'share_with' => $qb->expr()->literal('sharedWith'),
  914. 'uid_owner' => $qb->expr()->literal('shareOwner'),
  915. 'uid_initiator' => $qb->expr()->literal('sharedBy2'),
  916. 'item_type' => $qb->expr()->literal('file'),
  917. 'file_source' => $qb->expr()->literal(42),
  918. 'file_target' => $qb->expr()->literal('userTarget'),
  919. 'permissions' => $qb->expr()->literal(0),
  920. 'parent' => $qb->expr()->literal($id),
  921. ]);
  922. $this->assertEquals(1, $qb->execute());
  923. $file = $this->createMock(File::class);
  924. $this->rootFolder->method('getUserFolder')->with('shareOwner')->will($this->returnSelf());
  925. $this->rootFolder->method('getById')->with(42)->willReturn([$file]);
  926. $share = $this->provider->getSharesBy('sharedBy', \OCP\Share::SHARE_TYPE_USER, null, false, 1, 0);
  927. $this->assertCount(1, $share);
  928. $share = $share[0];
  929. $this->assertEquals($id, $share->getId());
  930. $this->assertEquals('sharedWith', $share->getSharedWith());
  931. $this->assertEquals('shareOwner', $share->getShareOwner());
  932. $this->assertEquals('sharedBy', $share->getSharedBy());
  933. $this->assertEquals(\OCP\Share::SHARE_TYPE_USER, $share->getShareType());
  934. $this->assertEquals(13, $share->getPermissions());
  935. $this->assertEquals('myTarget', $share->getTarget());
  936. }
  937. public function testGetSharesNode() {
  938. $qb = $this->dbConn->getQueryBuilder();
  939. $qb->insert('share')
  940. ->values([
  941. 'share_type' => $qb->expr()->literal(\OCP\Share::SHARE_TYPE_USER),
  942. 'share_with' => $qb->expr()->literal('sharedWith'),
  943. 'uid_owner' => $qb->expr()->literal('shareOwner'),
  944. 'uid_initiator' => $qb->expr()->literal('sharedBy'),
  945. 'item_type' => $qb->expr()->literal('file'),
  946. 'file_source' => $qb->expr()->literal(42),
  947. 'file_target' => $qb->expr()->literal('myTarget'),
  948. 'permissions' => $qb->expr()->literal(13),
  949. ]);
  950. $this->assertEquals(1, $qb->execute());
  951. $id = $qb->getLastInsertId();
  952. $qb = $this->dbConn->getQueryBuilder();
  953. $qb->insert('share')
  954. ->values([
  955. 'share_type' => $qb->expr()->literal(\OCP\Share::SHARE_TYPE_USER),
  956. 'share_with' => $qb->expr()->literal('sharedWith'),
  957. 'uid_owner' => $qb->expr()->literal('shareOwner'),
  958. 'uid_initiator' => $qb->expr()->literal('sharedBy'),
  959. 'item_type' => $qb->expr()->literal('file'),
  960. 'file_source' => $qb->expr()->literal(43),
  961. 'file_target' => $qb->expr()->literal('userTarget'),
  962. 'permissions' => $qb->expr()->literal(0),
  963. 'parent' => $qb->expr()->literal($id),
  964. ]);
  965. $this->assertEquals(1, $qb->execute());
  966. $file = $this->createMock(File::class);
  967. $file->method('getId')->willReturn(42);
  968. $this->rootFolder->method('getUserFolder')->with('shareOwner')->will($this->returnSelf());
  969. $this->rootFolder->method('getById')->with(42)->willReturn([$file]);
  970. $share = $this->provider->getSharesBy('sharedBy', \OCP\Share::SHARE_TYPE_USER, $file, false, 1, 0);
  971. $this->assertCount(1, $share);
  972. $share = $share[0];
  973. $this->assertEquals($id, $share->getId());
  974. $this->assertEquals('sharedWith', $share->getSharedWith());
  975. $this->assertEquals('shareOwner', $share->getShareOwner());
  976. $this->assertEquals('sharedBy', $share->getSharedBy());
  977. $this->assertEquals(\OCP\Share::SHARE_TYPE_USER, $share->getShareType());
  978. $this->assertEquals(13, $share->getPermissions());
  979. $this->assertEquals('myTarget', $share->getTarget());
  980. }
  981. public function testGetSharesReshare() {
  982. $qb = $this->dbConn->getQueryBuilder();
  983. $qb->insert('share')
  984. ->values([
  985. 'share_type' => $qb->expr()->literal(\OCP\Share::SHARE_TYPE_USER),
  986. 'share_with' => $qb->expr()->literal('sharedWith'),
  987. 'uid_owner' => $qb->expr()->literal('shareOwner'),
  988. 'uid_initiator' => $qb->expr()->literal('shareOwner'),
  989. 'item_type' => $qb->expr()->literal('file'),
  990. 'file_source' => $qb->expr()->literal(42),
  991. 'file_target' => $qb->expr()->literal('myTarget'),
  992. 'permissions' => $qb->expr()->literal(13),
  993. ]);
  994. $this->assertEquals(1, $qb->execute());
  995. $id1 = $qb->getLastInsertId();
  996. $qb = $this->dbConn->getQueryBuilder();
  997. $qb->insert('share')
  998. ->values([
  999. 'share_type' => $qb->expr()->literal(\OCP\Share::SHARE_TYPE_USER),
  1000. 'share_with' => $qb->expr()->literal('sharedWith'),
  1001. 'uid_owner' => $qb->expr()->literal('shareOwner'),
  1002. 'uid_initiator' => $qb->expr()->literal('sharedBy'),
  1003. 'item_type' => $qb->expr()->literal('file'),
  1004. 'file_source' => $qb->expr()->literal(42),
  1005. 'file_target' => $qb->expr()->literal('userTarget'),
  1006. 'permissions' => $qb->expr()->literal(0),
  1007. ]);
  1008. $this->assertEquals(1, $qb->execute());
  1009. $id2 = $qb->getLastInsertId();
  1010. $file = $this->createMock(File::class);
  1011. $file->method('getId')->willReturn(42);
  1012. $this->rootFolder->method('getUserFolder')->with('shareOwner')->will($this->returnSelf());
  1013. $this->rootFolder->method('getById')->with(42)->willReturn([$file]);
  1014. $shares = $this->provider->getSharesBy('shareOwner', \OCP\Share::SHARE_TYPE_USER, null, true, -1, 0);
  1015. $this->assertCount(2, $shares);
  1016. $share = $shares[0];
  1017. $this->assertEquals($id1, $share->getId());
  1018. $this->assertSame('sharedWith', $share->getSharedWith());
  1019. $this->assertSame('shareOwner', $share->getShareOwner());
  1020. $this->assertSame('shareOwner', $share->getSharedBy());
  1021. $this->assertEquals(\OCP\Share::SHARE_TYPE_USER, $share->getShareType());
  1022. $this->assertEquals(13, $share->getPermissions());
  1023. $this->assertEquals('myTarget', $share->getTarget());
  1024. $share = $shares[1];
  1025. $this->assertEquals($id2, $share->getId());
  1026. $this->assertSame('sharedWith', $share->getSharedWith());
  1027. $this->assertSame('shareOwner', $share->getShareOwner());
  1028. $this->assertSame('sharedBy', $share->getSharedBy());
  1029. $this->assertEquals(\OCP\Share::SHARE_TYPE_USER, $share->getShareType());
  1030. $this->assertEquals(0, $share->getPermissions());
  1031. $this->assertEquals('userTarget', $share->getTarget());
  1032. }
  1033. public function testDeleteFromSelfGroupNoCustomShare() {
  1034. $qb = $this->dbConn->getQueryBuilder();
  1035. $stmt = $qb->insert('share')
  1036. ->values([
  1037. 'share_type' => $qb->expr()->literal(\OCP\Share::SHARE_TYPE_GROUP),
  1038. 'share_with' => $qb->expr()->literal('group'),
  1039. 'uid_owner' => $qb->expr()->literal('user1'),
  1040. 'uid_initiator' => $qb->expr()->literal('user1'),
  1041. 'item_type' => $qb->expr()->literal('file'),
  1042. 'file_source' => $qb->expr()->literal(1),
  1043. 'file_target' => $qb->expr()->literal('myTarget1'),
  1044. 'permissions' => $qb->expr()->literal(2)
  1045. ])->execute();
  1046. $this->assertEquals(1, $stmt);
  1047. $id = $qb->getLastInsertId();
  1048. $user1 = $this->createMock(IUser::class);
  1049. $user1->method('getUID')->willReturn('user1');
  1050. $user2 = $this->createMock(IUser::class);
  1051. $user2->method('getUID')->willReturn('user2');
  1052. $this->userManager->method('get')->will($this->returnValueMap([
  1053. ['user1', $user1],
  1054. ['user2', $user2],
  1055. ]));
  1056. $group = $this->createMock(IGroup::class);
  1057. $group->method('getGID')->willReturn('group');
  1058. $group->method('inGroup')->with($user2)->willReturn(true);
  1059. $this->groupManager->method('get')->with('group')->willReturn($group);
  1060. $file = $this->createMock(File::class);
  1061. $file->method('getId')->willReturn(1);
  1062. $this->rootFolder->method('getUserFolder')->with('user1')->will($this->returnSelf());
  1063. $this->rootFolder->method('getById')->with(1)->willReturn([$file]);
  1064. $share = $this->provider->getShareById($id);
  1065. $this->provider->deleteFromSelf($share, 'user2');
  1066. $qb = $this->dbConn->getQueryBuilder();
  1067. $stmt = $qb->select('*')
  1068. ->from('share')
  1069. ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(2)))
  1070. ->execute();
  1071. $shares = $stmt->fetchAll();
  1072. $stmt->closeCursor();
  1073. $this->assertCount(1, $shares);
  1074. $share2 = $shares[0];
  1075. $this->assertEquals($id, $share2['parent']);
  1076. $this->assertEquals(0, $share2['permissions']);
  1077. $this->assertEquals('user2', $share2['share_with']);
  1078. }
  1079. public function testDeleteFromSelfGroupAlreadyCustomShare() {
  1080. $qb = $this->dbConn->getQueryBuilder();
  1081. $stmt = $qb->insert('share')
  1082. ->values([
  1083. 'share_type' => $qb->expr()->literal(\OCP\Share::SHARE_TYPE_GROUP),
  1084. 'share_with' => $qb->expr()->literal('group'),
  1085. 'uid_owner' => $qb->expr()->literal('user1'),
  1086. 'uid_initiator' => $qb->expr()->literal('user1'),
  1087. 'item_type' => $qb->expr()->literal('file'),
  1088. 'file_source' => $qb->expr()->literal(1),
  1089. 'file_target' => $qb->expr()->literal('myTarget1'),
  1090. 'permissions' => $qb->expr()->literal(2)
  1091. ])->execute();
  1092. $this->assertEquals(1, $stmt);
  1093. $id = $qb->getLastInsertId();
  1094. $qb = $this->dbConn->getQueryBuilder();
  1095. $stmt = $qb->insert('share')
  1096. ->values([
  1097. 'share_type' => $qb->expr()->literal(2),
  1098. 'share_with' => $qb->expr()->literal('user2'),
  1099. 'uid_owner' => $qb->expr()->literal('user1'),
  1100. 'uid_initiator' => $qb->expr()->literal('user1'),
  1101. 'item_type' => $qb->expr()->literal('file'),
  1102. 'file_source' => $qb->expr()->literal(1),
  1103. 'file_target' => $qb->expr()->literal('myTarget1'),
  1104. 'permissions' => $qb->expr()->literal(2),
  1105. 'parent' => $qb->expr()->literal($id),
  1106. ])->execute();
  1107. $this->assertEquals(1, $stmt);
  1108. $user1 = $this->createMock(IUser::class);
  1109. $user1->method('getUID')->willReturn('user1');
  1110. $user2 = $this->createMock(IUser::class);
  1111. $user2->method('getUID')->willReturn('user2');
  1112. $this->userManager->method('get')->will($this->returnValueMap([
  1113. ['user1', $user1],
  1114. ['user2', $user2],
  1115. ]));
  1116. $group = $this->createMock(IGroup::class);
  1117. $group->method('getGID')->willReturn('group');
  1118. $group->method('inGroup')->with($user2)->willReturn(true);
  1119. $this->groupManager->method('get')->with('group')->willReturn($group);
  1120. $file = $this->createMock(File::class);
  1121. $file->method('getId')->willReturn(1);
  1122. $this->rootFolder->method('getUserFolder')->with('user1')->will($this->returnSelf());
  1123. $this->rootFolder->method('getById')->with(1)->willReturn([$file]);
  1124. $share = $this->provider->getShareById($id);
  1125. $this->provider->deleteFromSelf($share, 'user2');
  1126. $qb = $this->dbConn->getQueryBuilder();
  1127. $stmt = $qb->select('*')
  1128. ->from('share')
  1129. ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(2)))
  1130. ->execute();
  1131. $shares = $stmt->fetchAll();
  1132. $stmt->closeCursor();
  1133. $this->assertCount(1, $shares);
  1134. $share2 = $shares[0];
  1135. $this->assertEquals($id, $share2['parent']);
  1136. $this->assertEquals(0, $share2['permissions']);
  1137. $this->assertEquals('user2', $share2['share_with']);
  1138. }
  1139. /**
  1140. * @expectedException \OC\Share20\Exception\ProviderException
  1141. * @expectedExceptionMessage Recipient not in receiving group
  1142. */
  1143. public function testDeleteFromSelfGroupUserNotInGroup() {
  1144. $qb = $this->dbConn->getQueryBuilder();
  1145. $stmt = $qb->insert('share')
  1146. ->values([
  1147. 'share_type' => $qb->expr()->literal(\OCP\Share::SHARE_TYPE_GROUP),
  1148. 'share_with' => $qb->expr()->literal('group'),
  1149. 'uid_owner' => $qb->expr()->literal('user1'),
  1150. 'uid_initiator' => $qb->expr()->literal('user1'),
  1151. 'item_type' => $qb->expr()->literal('file'),
  1152. 'file_source' => $qb->expr()->literal(1),
  1153. 'file_target' => $qb->expr()->literal('myTarget1'),
  1154. 'permissions' => $qb->expr()->literal(2)
  1155. ])->execute();
  1156. $this->assertEquals(1, $stmt);
  1157. $id = $qb->getLastInsertId();
  1158. $user1 = $this->createMock(IUser::class);
  1159. $user1->method('getUID')->willReturn('user1');
  1160. $user2 = $this->createMock(IUser::class);
  1161. $user2->method('getUID')->willReturn('user2');
  1162. $this->userManager->method('get')->will($this->returnValueMap([
  1163. ['user1', $user1],
  1164. ['user2', $user2],
  1165. ]));
  1166. $group = $this->createMock(IGroup::class);
  1167. $group->method('getGID')->willReturn('group');
  1168. $group->method('inGroup')->with($user2)->willReturn(false);
  1169. $this->groupManager->method('get')->with('group')->willReturn($group);
  1170. $file = $this->createMock(File::class);
  1171. $file->method('getId')->willReturn(1);
  1172. $this->rootFolder->method('getUserFolder')->with('user1')->will($this->returnSelf());
  1173. $this->rootFolder->method('getById')->with(1)->willReturn([$file]);
  1174. $share = $this->provider->getShareById($id);
  1175. $this->provider->deleteFromSelf($share, 'user2');
  1176. }
  1177. public function testDeleteFromSelfUser() {
  1178. $qb = $this->dbConn->getQueryBuilder();
  1179. $stmt = $qb->insert('share')
  1180. ->values([
  1181. 'share_type' => $qb->expr()->literal(\OCP\Share::SHARE_TYPE_USER),
  1182. 'share_with' => $qb->expr()->literal('user2'),
  1183. 'uid_owner' => $qb->expr()->literal('user1'),
  1184. 'uid_initiator' => $qb->expr()->literal('user1'),
  1185. 'item_type' => $qb->expr()->literal('file'),
  1186. 'file_source' => $qb->expr()->literal(1),
  1187. 'file_target' => $qb->expr()->literal('myTarget1'),
  1188. 'permissions' => $qb->expr()->literal(2)
  1189. ])->execute();
  1190. $this->assertEquals(1, $stmt);
  1191. $id = $qb->getLastInsertId();
  1192. $user1 = $this->createMock(IUser::class);
  1193. $user1->method('getUID')->willReturn('user1');
  1194. $user2 = $this->createMock(IUser::class);
  1195. $user2->method('getUID')->willReturn('user2');
  1196. $this->userManager->method('get')->will($this->returnValueMap([
  1197. ['user1', $user1],
  1198. ['user2', $user2],
  1199. ]));
  1200. $file = $this->createMock(File::class);
  1201. $file->method('getId')->willReturn(1);
  1202. $this->rootFolder->method('getUserFolder')->with('user1')->will($this->returnSelf());
  1203. $this->rootFolder->method('getById')->with(1)->willReturn([$file]);
  1204. $share = $this->provider->getShareById($id);
  1205. $this->provider->deleteFromSelf($share, 'user2');
  1206. $qb = $this->dbConn->getQueryBuilder();
  1207. $stmt = $qb->select('*')
  1208. ->from('share')
  1209. ->where($qb->expr()->eq('id', $qb->createNamedParameter($id)))
  1210. ->execute();
  1211. $shares = $stmt->fetchAll();
  1212. $stmt->closeCursor();
  1213. $this->assertCount(0, $shares);
  1214. }
  1215. /**
  1216. * @expectedException \OC\Share20\Exception\ProviderException
  1217. * @expectedExceptionMessage Recipient does not match
  1218. */
  1219. public function testDeleteFromSelfUserNotRecipient() {
  1220. $qb = $this->dbConn->getQueryBuilder();
  1221. $stmt = $qb->insert('share')
  1222. ->values([
  1223. 'share_type' => $qb->expr()->literal(\OCP\Share::SHARE_TYPE_USER),
  1224. 'share_with' => $qb->expr()->literal('user2'),
  1225. 'uid_owner' => $qb->expr()->literal('user1'),
  1226. 'uid_initiator' => $qb->expr()->literal('user1'),
  1227. 'item_type' => $qb->expr()->literal('file'),
  1228. 'file_source' => $qb->expr()->literal(1),
  1229. 'file_target' => $qb->expr()->literal('myTarget1'),
  1230. 'permissions' => $qb->expr()->literal(2)
  1231. ])->execute();
  1232. $this->assertEquals(1, $stmt);
  1233. $id = $qb->getLastInsertId();
  1234. $user1 = $this->createMock(IUser::class);
  1235. $user1->method('getUID')->willReturn('user1');
  1236. $user2 = $this->createMock(IUser::class);
  1237. $user2->method('getUID')->willReturn('user2');
  1238. $user3 = $this->createMock(IUser::class);
  1239. $this->userManager->method('get')->will($this->returnValueMap([
  1240. ['user1', $user1],
  1241. ['user2', $user2],
  1242. ]));
  1243. $file = $this->createMock(File::class);
  1244. $file->method('getId')->willReturn(1);
  1245. $this->rootFolder->method('getUserFolder')->with('user1')->will($this->returnSelf());
  1246. $this->rootFolder->method('getById')->with(1)->willReturn([$file]);
  1247. $share = $this->provider->getShareById($id);
  1248. $this->provider->deleteFromSelf($share, $user3);
  1249. }
  1250. /**
  1251. * @expectedException \OC\Share20\Exception\ProviderException
  1252. * @expectedExceptionMessage Invalid shareType
  1253. */
  1254. public function testDeleteFromSelfLink() {
  1255. $qb = $this->dbConn->getQueryBuilder();
  1256. $stmt = $qb->insert('share')
  1257. ->values([
  1258. 'share_type' => $qb->expr()->literal(\OCP\Share::SHARE_TYPE_LINK),
  1259. 'uid_owner' => $qb->expr()->literal('user1'),
  1260. 'uid_initiator' => $qb->expr()->literal('user1'),
  1261. 'item_type' => $qb->expr()->literal('file'),
  1262. 'file_source' => $qb->expr()->literal(1),
  1263. 'file_target' => $qb->expr()->literal('myTarget1'),
  1264. 'permissions' => $qb->expr()->literal(2),
  1265. 'token' => $qb->expr()->literal('token'),
  1266. ])->execute();
  1267. $this->assertEquals(1, $stmt);
  1268. $id = $qb->getLastInsertId();
  1269. $user1 = $this->createMock(IUser::class);
  1270. $user1->method('getUID')->willReturn('user1');
  1271. $this->userManager->method('get')->will($this->returnValueMap([
  1272. ['user1', $user1],
  1273. ]));
  1274. $file = $this->createMock(File::class);
  1275. $file->method('getId')->willReturn(1);
  1276. $this->rootFolder->method('getUserFolder')->with('user1')->will($this->returnSelf());
  1277. $this->rootFolder->method('getById')->with(1)->willReturn([$file]);
  1278. $share = $this->provider->getShareById($id);
  1279. $this->provider->deleteFromSelf($share, $user1);
  1280. }
  1281. public function testUpdateUser() {
  1282. $id = $this->addShareToDB(\OCP\Share::SHARE_TYPE_USER, 'user0', 'user1', 'user2',
  1283. 'file', 42, 'target', 31, null, null);
  1284. $users = [];
  1285. for($i = 0; $i < 6; $i++) {
  1286. $user = $this->createMock(IUser::class);
  1287. $user->method('getUID')->willReturn('user'.$i);
  1288. $users['user'.$i] = $user;
  1289. }
  1290. $this->userManager->method('get')->will(
  1291. $this->returnCallback(function($userId) use ($users) {
  1292. return $users[$userId];
  1293. })
  1294. );
  1295. $file1 = $this->createMock(File::class);
  1296. $file1->method('getId')->willReturn(42);
  1297. $file2 = $this->createMock(File::class);
  1298. $file2->method('getId')->willReturn(43);
  1299. $folder1 = $this->createMock(Folder::class);
  1300. $folder1->method('getById')->with(42)->willReturn([$file1]);
  1301. $folder2 = $this->createMock(Folder::class);
  1302. $folder2->method('getById')->with(43)->willReturn([$file2]);
  1303. $this->rootFolder->method('getUserFolder')->will($this->returnValueMap([
  1304. ['user2', $folder1],
  1305. ['user5', $folder2],
  1306. ]));
  1307. $share = $this->provider->getShareById($id);
  1308. $share->setSharedWith('user3');
  1309. $share->setSharedBy('user4');
  1310. $share->setShareOwner('user5');
  1311. $share->setNode($file2);
  1312. $share->setPermissions(1);
  1313. $share2 = $this->provider->update($share);
  1314. $this->assertEquals($id, $share2->getId());
  1315. $this->assertSame('user3', $share2->getSharedWith());
  1316. $this->assertSame('user4', $share2->getSharedBy());
  1317. $this->assertSame('user5', $share2->getShareOwner());
  1318. $this->assertSame(1, $share2->getPermissions());
  1319. }
  1320. public function testUpdateLink() {
  1321. $id = $this->addShareToDB(\OCP\Share::SHARE_TYPE_LINK, null, 'user1', 'user2',
  1322. 'file', 42, 'target', 31, null, null);
  1323. $users = [];
  1324. for($i = 0; $i < 6; $i++) {
  1325. $user = $this->createMock(IUser::class);
  1326. $user->method('getUID')->willReturn('user'.$i);
  1327. $users['user'.$i] = $user;
  1328. }
  1329. $this->userManager->method('get')->will(
  1330. $this->returnCallback(function($userId) use ($users) {
  1331. return $users[$userId];
  1332. })
  1333. );
  1334. $file1 = $this->createMock(File::class);
  1335. $file1->method('getId')->willReturn(42);
  1336. $file2 = $this->createMock(File::class);
  1337. $file2->method('getId')->willReturn(43);
  1338. $folder1 = $this->createMock(Folder::class);
  1339. $folder1->method('getById')->with(42)->willReturn([$file1]);
  1340. $folder2 = $this->createMock(Folder::class);
  1341. $folder2->method('getById')->with(43)->willReturn([$file2]);
  1342. $this->rootFolder->method('getUserFolder')->will($this->returnValueMap([
  1343. ['user2', $folder1],
  1344. ['user5', $folder2],
  1345. ]));
  1346. $share = $this->provider->getShareById($id);
  1347. $share->setPassword('password');
  1348. $share->setSharedBy('user4');
  1349. $share->setShareOwner('user5');
  1350. $share->setNode($file2);
  1351. $share->setPermissions(1);
  1352. $share2 = $this->provider->update($share);
  1353. $this->assertEquals($id, $share2->getId());
  1354. $this->assertEquals('password', $share->getPassword());
  1355. $this->assertSame('user4', $share2->getSharedBy());
  1356. $this->assertSame('user5', $share2->getShareOwner());
  1357. $this->assertSame(1, $share2->getPermissions());
  1358. }
  1359. public function testUpdateLinkRemovePassword() {
  1360. $id = $this->addShareToDB(\OCP\Share::SHARE_TYPE_LINK, 'foo', 'user1', 'user2',
  1361. 'file', 42, 'target', 31, null, null);
  1362. $users = [];
  1363. for($i = 0; $i < 6; $i++) {
  1364. $user = $this->createMock(IUser::class);
  1365. $user->method('getUID')->willReturn('user'.$i);
  1366. $users['user'.$i] = $user;
  1367. }
  1368. $this->userManager->method('get')->will(
  1369. $this->returnCallback(function($userId) use ($users) {
  1370. return $users[$userId];
  1371. })
  1372. );
  1373. $file1 = $this->createMock(File::class);
  1374. $file1->method('getId')->willReturn(42);
  1375. $file2 = $this->createMock(File::class);
  1376. $file2->method('getId')->willReturn(43);
  1377. $folder1 = $this->createMock(Folder::class);
  1378. $folder1->method('getById')->with(42)->willReturn([$file1]);
  1379. $folder2 = $this->createMock(Folder::class);
  1380. $folder2->method('getById')->with(43)->willReturn([$file2]);
  1381. $this->rootFolder->method('getUserFolder')->will($this->returnValueMap([
  1382. ['user2', $folder1],
  1383. ['user5', $folder2],
  1384. ]));
  1385. $share = $this->provider->getShareById($id);
  1386. $share->setPassword(null);
  1387. $share->setSharedBy('user4');
  1388. $share->setShareOwner('user5');
  1389. $share->setNode($file2);
  1390. $share->setPermissions(1);
  1391. $share2 = $this->provider->update($share);
  1392. $this->assertEquals($id, $share2->getId());
  1393. $this->assertEquals(null, $share->getPassword());
  1394. $this->assertSame('user4', $share2->getSharedBy());
  1395. $this->assertSame('user5', $share2->getShareOwner());
  1396. $this->assertSame(1, $share2->getPermissions());
  1397. }
  1398. public function testUpdateGroupNoSub() {
  1399. $id = $this->addShareToDB(\OCP\Share::SHARE_TYPE_GROUP, 'group0', 'user1', 'user2',
  1400. 'file', 42, 'target', 31, null, null);
  1401. $users = [];
  1402. for($i = 0; $i < 6; $i++) {
  1403. $user = $this->createMock(IUser::class);
  1404. $user->method('getUID')->willReturn('user'.$i);
  1405. $users['user'.$i] = $user;
  1406. }
  1407. $this->userManager->method('get')->will(
  1408. $this->returnCallback(function($userId) use ($users) {
  1409. return $users[$userId];
  1410. })
  1411. );
  1412. $groups = [];
  1413. for($i = 0; $i < 2; $i++) {
  1414. $group = $this->createMock(IGroup::class);
  1415. $group->method('getGID')->willReturn('group'.$i);
  1416. $groups['group'.$i] = $group;
  1417. }
  1418. $this->groupManager->method('get')->will(
  1419. $this->returnCallback(function($groupId) use ($groups) {
  1420. return $groups[$groupId];
  1421. })
  1422. );
  1423. $file1 = $this->createMock(File::class);
  1424. $file1->method('getId')->willReturn(42);
  1425. $file2 = $this->createMock(File::class);
  1426. $file2->method('getId')->willReturn(43);
  1427. $folder1 = $this->createMock(Folder::class);
  1428. $folder1->method('getById')->with(42)->willReturn([$file1]);
  1429. $folder2 = $this->createMock(Folder::class);
  1430. $folder2->method('getById')->with(43)->willReturn([$file2]);
  1431. $this->rootFolder->method('getUserFolder')->will($this->returnValueMap([
  1432. ['user2', $folder1],
  1433. ['user5', $folder2],
  1434. ]));
  1435. $share = $this->provider->getShareById($id);
  1436. $share->setSharedWith('group0');
  1437. $share->setSharedBy('user4');
  1438. $share->setShareOwner('user5');
  1439. $share->setNode($file2);
  1440. $share->setPermissions(1);
  1441. $share2 = $this->provider->update($share);
  1442. $this->assertEquals($id, $share2->getId());
  1443. // Group shares do not allow updating the recipient
  1444. $this->assertSame('group0', $share2->getSharedWith());
  1445. $this->assertSame('user4', $share2->getSharedBy());
  1446. $this->assertSame('user5', $share2->getShareOwner());
  1447. $this->assertSame(1, $share2->getPermissions());
  1448. }
  1449. public function testUpdateGroupSubShares() {
  1450. $id = $this->addShareToDB(\OCP\Share::SHARE_TYPE_GROUP, 'group0', 'user1', 'user2',
  1451. 'file', 42, 'target', 31, null, null);
  1452. $id2 = $this->addShareToDB(2, 'user0', 'user1', 'user2',
  1453. 'file', 42, 'mytarget', 31, null, null, $id);
  1454. $id3 = $this->addShareToDB(2, 'user3', 'user1', 'user2',
  1455. 'file', 42, 'mytarget2', 0, null, null, $id);
  1456. $users = [];
  1457. for($i = 0; $i < 6; $i++) {
  1458. $user = $this->createMock(IUser::class);
  1459. $user->method('getUID')->willReturn('user'.$i);
  1460. $users['user'.$i] = $user;
  1461. }
  1462. $this->userManager->method('get')->will(
  1463. $this->returnCallback(function($userId) use ($users) {
  1464. return $users[$userId];
  1465. })
  1466. );
  1467. $groups = [];
  1468. for($i = 0; $i < 2; $i++) {
  1469. $group = $this->createMock(IGroup::class);
  1470. $group->method('getGID')->willReturn('group'.$i);
  1471. $groups['group'.$i] = $group;
  1472. }
  1473. $this->groupManager->method('get')->will(
  1474. $this->returnCallback(function($groupId) use ($groups) {
  1475. return $groups[$groupId];
  1476. })
  1477. );
  1478. $file1 = $this->createMock(File::class);
  1479. $file1->method('getId')->willReturn(42);
  1480. $file2 = $this->createMock(File::class);
  1481. $file2->method('getId')->willReturn(43);
  1482. $folder1 = $this->createMock(Folder::class);
  1483. $folder1->method('getById')->with(42)->willReturn([$file1]);
  1484. $folder2 = $this->createMock(Folder::class);
  1485. $folder2->method('getById')->with(43)->willReturn([$file2]);
  1486. $this->rootFolder->method('getUserFolder')->will($this->returnValueMap([
  1487. ['user2', $folder1],
  1488. ['user5', $folder2],
  1489. ]));
  1490. $share = $this->provider->getShareById($id);
  1491. $share->setSharedWith('group0');
  1492. $share->setSharedBy('user4');
  1493. $share->setShareOwner('user5');
  1494. $share->setNode($file2);
  1495. $share->setPermissions(1);
  1496. $share2 = $this->provider->update($share);
  1497. $this->assertEquals($id, $share2->getId());
  1498. // Group shares do not allow updating the recipient
  1499. $this->assertSame('group0', $share2->getSharedWith());
  1500. $this->assertSame('user4', $share2->getSharedBy());
  1501. $this->assertSame('user5', $share2->getShareOwner());
  1502. $this->assertSame(1, $share2->getPermissions());
  1503. $qb = $this->dbConn->getQueryBuilder();
  1504. $stmt = $qb->select('*')
  1505. ->from('share')
  1506. ->where($qb->expr()->eq('parent', $qb->createNamedParameter($id)))
  1507. ->orderBy('id')
  1508. ->execute();
  1509. $shares = $stmt->fetchAll();
  1510. $this->assertSame('user0', $shares[0]['share_with']);
  1511. $this->assertSame('user4', $shares[0]['uid_initiator']);
  1512. $this->assertSame('user5', $shares[0]['uid_owner']);
  1513. $this->assertSame(1, (int)$shares[0]['permissions']);
  1514. $this->assertSame('user3', $shares[1]['share_with']);
  1515. $this->assertSame('user4', $shares[1]['uid_initiator']);
  1516. $this->assertSame('user5', $shares[1]['uid_owner']);
  1517. $this->assertSame(0, (int)$shares[1]['permissions']);
  1518. $stmt->closeCursor();
  1519. }
  1520. public function testMoveUserShare() {
  1521. $id = $this->addShareToDB(\OCP\Share::SHARE_TYPE_USER, 'user0', 'user1', 'user1', 'file',
  1522. 42, 'mytaret', 31, null, null);
  1523. $user0 = $this->createMock(IUser::class);
  1524. $user0->method('getUID')->willReturn('user0');
  1525. $user1 = $this->createMock(IUser::class);
  1526. $user1->method('getUID')->willReturn('user1');
  1527. $this->userManager->method('get')->will($this->returnValueMap([
  1528. ['user0', $user0],
  1529. ['user1', $user1],
  1530. ]));
  1531. $file = $this->createMock(File::class);
  1532. $file->method('getId')->willReturn(42);
  1533. $this->rootFolder->method('getUserFolder')->with('user1')->will($this->returnSelf());
  1534. $this->rootFolder->method('getById')->willReturn([$file]);
  1535. $share = $this->provider->getShareById($id, null);
  1536. $share->setTarget('/newTarget');
  1537. $this->provider->move($share, $user0);
  1538. $share = $this->provider->getShareById($id, null);
  1539. $this->assertSame('/newTarget', $share->getTarget());
  1540. }
  1541. public function testMoveGroupShare() {
  1542. $id = $this->addShareToDB(\OCP\Share::SHARE_TYPE_GROUP, 'group0', 'user1', 'user1', 'file',
  1543. 42, 'mytaret', 31, null, null);
  1544. $user0 = $this->createMock(IUser::class);
  1545. $user0->method('getUID')->willReturn('user0');
  1546. $user1 = $this->createMock(IUser::class);
  1547. $user1->method('getUID')->willReturn('user1');
  1548. $group0 = $this->createMock(IGroup::class);
  1549. $group0->method('getGID')->willReturn('group0');
  1550. $group0->method('inGroup')->with($user0)->willReturn(true);
  1551. $this->groupManager->method('get')->with('group0')->willReturn($group0);
  1552. $this->userManager->method('get')->will($this->returnValueMap([
  1553. ['user0', $user0],
  1554. ['user1', $user1],
  1555. ]));
  1556. $folder = $this->createMock(Folder::class);
  1557. $folder->method('getId')->willReturn(42);
  1558. $this->rootFolder->method('getUserFolder')->with('user1')->will($this->returnSelf());
  1559. $this->rootFolder->method('getById')->willReturn([$folder]);
  1560. $share = $this->provider->getShareById($id, 'user0');
  1561. $share->setTarget('/newTarget');
  1562. $this->provider->move($share, 'user0');
  1563. $share = $this->provider->getShareById($id, 'user0');
  1564. $this->assertSame('/newTarget', $share->getTarget());
  1565. $share->setTarget('/ultraNewTarget');
  1566. $this->provider->move($share, 'user0');
  1567. $share = $this->provider->getShareById($id, 'user0');
  1568. $this->assertSame('/ultraNewTarget', $share->getTarget());
  1569. }
  1570. public function dataDeleteUser() {
  1571. return [
  1572. [\OCP\Share::SHARE_TYPE_USER, 'a', 'b', 'c', 'a', true],
  1573. [\OCP\Share::SHARE_TYPE_USER, 'a', 'b', 'c', 'b', false],
  1574. [\OCP\Share::SHARE_TYPE_USER, 'a', 'b', 'c', 'c', true],
  1575. [\OCP\Share::SHARE_TYPE_USER, 'a', 'b', 'c', 'd', false],
  1576. [\OCP\Share::SHARE_TYPE_GROUP, 'a', 'b', 'c', 'a', true],
  1577. [\OCP\Share::SHARE_TYPE_GROUP, 'a', 'b', 'c', 'b', false],
  1578. // The group c is still valid but user c is deleted so group share stays
  1579. [\OCP\Share::SHARE_TYPE_GROUP, 'a', 'b', 'c', 'c', false],
  1580. [\OCP\Share::SHARE_TYPE_GROUP, 'a', 'b', 'c', 'd', false],
  1581. [\OCP\Share::SHARE_TYPE_LINK, 'a', 'b', 'c', 'a', true],
  1582. // To avoid invisible link shares delete initiated link shares as well (see #22327)
  1583. [\OCP\Share::SHARE_TYPE_LINK, 'a', 'b', 'c', 'b', true],
  1584. [\OCP\Share::SHARE_TYPE_LINK, 'a', 'b', 'c', 'c', false],
  1585. [\OCP\Share::SHARE_TYPE_LINK, 'a', 'b', 'c', 'd', false],
  1586. ];
  1587. }
  1588. /**
  1589. * @dataProvider dataDeleteUser
  1590. *
  1591. * @param int $type The shareType (user/group/link)
  1592. * @param string $owner The owner of the share (uid)
  1593. * @param string $initiator The initiator of the share (uid)
  1594. * @param string $recipient The recipient of the share (uid/gid/pass)
  1595. * @param string $deletedUser The user that is deleted
  1596. * @param bool $rowDeleted Is the row deleted in this setup
  1597. */
  1598. public function testDeleteUser($type, $owner, $initiator, $recipient, $deletedUser, $rowDeleted) {
  1599. $qb = $this->dbConn->getQueryBuilder();
  1600. $qb->insert('share')
  1601. ->setValue('share_type', $qb->createNamedParameter($type))
  1602. ->setValue('uid_owner', $qb->createNamedParameter($owner))
  1603. ->setValue('uid_initiator', $qb->createNamedParameter($initiator))
  1604. ->setValue('share_with', $qb->createNamedParameter($recipient))
  1605. ->setValue('item_type', $qb->createNamedParameter('file'))
  1606. ->setValue('item_source', $qb->createNamedParameter(42))
  1607. ->setValue('file_source', $qb->createNamedParameter(42))
  1608. ->execute();
  1609. $id = $qb->getLastInsertId();
  1610. $this->provider->userDeleted($deletedUser, $type);
  1611. $qb = $this->dbConn->getQueryBuilder();
  1612. $qb->select('*')
  1613. ->from('share')
  1614. ->where(
  1615. $qb->expr()->eq('id', $qb->createNamedParameter($id))
  1616. );
  1617. $cursor = $qb->execute();
  1618. $data = $cursor->fetchAll();
  1619. $cursor->closeCursor();
  1620. $this->assertCount($rowDeleted ? 0 : 1, $data);
  1621. }
  1622. public function dataDeleteUserGroup() {
  1623. return [
  1624. ['a', 'b', 'c', 'a', true, true],
  1625. ['a', 'b', 'c', 'b', false, false],
  1626. ['a', 'b', 'c', 'c', false, true],
  1627. ['a', 'b', 'c', 'd', false, false],
  1628. ];
  1629. }
  1630. /**
  1631. * @dataProvider dataDeleteUserGroup
  1632. *
  1633. * @param string $owner The owner of the share (uid)
  1634. * @param string $initiator The initiator of the share (uid)
  1635. * @param string $recipient The recipient of the usergroup share (uid)
  1636. * @param string $deletedUser The user that is deleted
  1637. * @param bool $groupShareDeleted
  1638. * @param bool $userGroupShareDeleted
  1639. */
  1640. public function testDeleteUserGroup($owner, $initiator, $recipient, $deletedUser, $groupShareDeleted, $userGroupShareDeleted) {
  1641. $qb = $this->dbConn->getQueryBuilder();
  1642. $qb->insert('share')
  1643. ->setValue('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_GROUP))
  1644. ->setValue('uid_owner', $qb->createNamedParameter($owner))
  1645. ->setValue('uid_initiator', $qb->createNamedParameter($initiator))
  1646. ->setValue('share_with', $qb->createNamedParameter('group'))
  1647. ->setValue('item_type', $qb->createNamedParameter('file'))
  1648. ->setValue('item_source', $qb->createNamedParameter(42))
  1649. ->setValue('file_source', $qb->createNamedParameter(42))
  1650. ->execute();
  1651. $groupId = $qb->getLastInsertId();
  1652. $qb = $this->dbConn->getQueryBuilder();
  1653. $qb->insert('share')
  1654. ->setValue('share_type', $qb->createNamedParameter(2))
  1655. ->setValue('uid_owner', $qb->createNamedParameter($owner))
  1656. ->setValue('uid_initiator', $qb->createNamedParameter($initiator))
  1657. ->setValue('share_with', $qb->createNamedParameter($recipient))
  1658. ->setValue('item_type', $qb->createNamedParameter('file'))
  1659. ->setValue('item_source', $qb->createNamedParameter(42))
  1660. ->setValue('file_source', $qb->createNamedParameter(42))
  1661. ->execute();
  1662. $userGroupId = $qb->getLastInsertId();
  1663. $this->provider->userDeleted($deletedUser, \OCP\Share::SHARE_TYPE_GROUP);
  1664. $qb = $this->dbConn->getQueryBuilder();
  1665. $qb->select('*')
  1666. ->from('share')
  1667. ->where(
  1668. $qb->expr()->eq('id', $qb->createNamedParameter($userGroupId))
  1669. );
  1670. $cursor = $qb->execute();
  1671. $data = $cursor->fetchAll();
  1672. $cursor->closeCursor();
  1673. $this->assertCount($userGroupShareDeleted ? 0 : 1, $data);
  1674. $qb = $this->dbConn->getQueryBuilder();
  1675. $qb->select('*')
  1676. ->from('share')
  1677. ->where(
  1678. $qb->expr()->eq('id', $qb->createNamedParameter($groupId))
  1679. );
  1680. $cursor = $qb->execute();
  1681. $data = $cursor->fetchAll();
  1682. $cursor->closeCursor();
  1683. $this->assertCount($groupShareDeleted ? 0 : 1, $data);
  1684. }
  1685. public function dataGroupDeleted() {
  1686. return [
  1687. [
  1688. [
  1689. 'type' => \OCP\Share::SHARE_TYPE_USER,
  1690. 'recipient' => 'user',
  1691. 'children' => []
  1692. ], 'group', false
  1693. ],
  1694. [
  1695. [
  1696. 'type' => \OCP\Share::SHARE_TYPE_USER,
  1697. 'recipient' => 'user',
  1698. 'children' => []
  1699. ], 'user', false
  1700. ],
  1701. [
  1702. [
  1703. 'type' => \OCP\Share::SHARE_TYPE_LINK,
  1704. 'recipient' => 'user',
  1705. 'children' => []
  1706. ], 'group', false
  1707. ],
  1708. [
  1709. [
  1710. 'type' => \OCP\Share::SHARE_TYPE_GROUP,
  1711. 'recipient' => 'group1',
  1712. 'children' => [
  1713. 'foo',
  1714. 'bar'
  1715. ]
  1716. ], 'group2', false
  1717. ],
  1718. [
  1719. [
  1720. 'type' => \OCP\Share::SHARE_TYPE_GROUP,
  1721. 'recipient' => 'group1',
  1722. 'children' => [
  1723. 'foo',
  1724. 'bar'
  1725. ]
  1726. ], 'group1', true
  1727. ],
  1728. ];
  1729. }
  1730. /**
  1731. * @dataProvider dataGroupDeleted
  1732. *
  1733. * @param $shares
  1734. * @param $groupToDelete
  1735. * @param $shouldBeDeleted
  1736. */
  1737. public function testGroupDeleted($shares, $groupToDelete, $shouldBeDeleted) {
  1738. $qb = $this->dbConn->getQueryBuilder();
  1739. $qb->insert('share')
  1740. ->setValue('share_type', $qb->createNamedParameter($shares['type']))
  1741. ->setValue('uid_owner', $qb->createNamedParameter('owner'))
  1742. ->setValue('uid_initiator', $qb->createNamedParameter('initiator'))
  1743. ->setValue('share_with', $qb->createNamedParameter($shares['recipient']))
  1744. ->setValue('item_type', $qb->createNamedParameter('file'))
  1745. ->setValue('item_source', $qb->createNamedParameter(42))
  1746. ->setValue('file_source', $qb->createNamedParameter(42))
  1747. ->execute();
  1748. $ids = [$qb->getLastInsertId()];
  1749. foreach ($shares['children'] as $child) {
  1750. $qb = $this->dbConn->getQueryBuilder();
  1751. $qb->insert('share')
  1752. ->setValue('share_type', $qb->createNamedParameter(2))
  1753. ->setValue('uid_owner', $qb->createNamedParameter('owner'))
  1754. ->setValue('uid_initiator', $qb->createNamedParameter('initiator'))
  1755. ->setValue('share_with', $qb->createNamedParameter($child))
  1756. ->setValue('item_type', $qb->createNamedParameter('file'))
  1757. ->setValue('item_source', $qb->createNamedParameter(42))
  1758. ->setValue('file_source', $qb->createNamedParameter(42))
  1759. ->setValue('parent', $qb->createNamedParameter($ids[0]))
  1760. ->execute();
  1761. $ids[] = $qb->getLastInsertId();
  1762. }
  1763. $this->provider->groupDeleted($groupToDelete);
  1764. $qb = $this->dbConn->getQueryBuilder();
  1765. $cursor = $qb->select('*')
  1766. ->from('share')
  1767. ->where($qb->expr()->in('id', $qb->createNamedParameter($ids, IQueryBuilder::PARAM_INT_ARRAY)))
  1768. ->execute();
  1769. $data = $cursor->fetchAll();
  1770. $cursor->closeCursor();
  1771. $this->assertCount($shouldBeDeleted ? 0 : count($ids), $data);
  1772. }
  1773. public function dataUserDeletedFromGroup() {
  1774. return [
  1775. ['group1', 'user1', true],
  1776. ['group1', 'user2', false],
  1777. ['group2', 'user1', false],
  1778. ];
  1779. }
  1780. /**
  1781. * Given a group share with 'group1'
  1782. * And a user specific group share with 'user1'.
  1783. * User $user is deleted from group $gid.
  1784. *
  1785. * @dataProvider dataUserDeletedFromGroup
  1786. *
  1787. * @param string $group
  1788. * @param string $user
  1789. * @param bool $toDelete
  1790. */
  1791. public function testUserDeletedFromGroup($group, $user, $toDelete) {
  1792. $qb = $this->dbConn->getQueryBuilder();
  1793. $qb->insert('share')
  1794. ->setValue('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_GROUP))
  1795. ->setValue('uid_owner', $qb->createNamedParameter('owner'))
  1796. ->setValue('uid_initiator', $qb->createNamedParameter('initiator'))
  1797. ->setValue('share_with', $qb->createNamedParameter('group1'))
  1798. ->setValue('item_type', $qb->createNamedParameter('file'))
  1799. ->setValue('item_source', $qb->createNamedParameter(42))
  1800. ->setValue('file_source', $qb->createNamedParameter(42));
  1801. $qb->execute();
  1802. $id1 = $qb->getLastInsertId();
  1803. $qb = $this->dbConn->getQueryBuilder();
  1804. $qb->insert('share')
  1805. ->setValue('share_type', $qb->createNamedParameter(2))
  1806. ->setValue('uid_owner', $qb->createNamedParameter('owner'))
  1807. ->setValue('uid_initiator', $qb->createNamedParameter('initiator'))
  1808. ->setValue('share_with', $qb->createNamedParameter('user1'))
  1809. ->setValue('item_type', $qb->createNamedParameter('file'))
  1810. ->setValue('item_source', $qb->createNamedParameter(42))
  1811. ->setValue('file_source', $qb->createNamedParameter(42))
  1812. ->setValue('parent', $qb->createNamedParameter($id1));
  1813. $qb->execute();
  1814. $id2 = $qb->getLastInsertId();
  1815. $this->provider->userDeletedFromGroup($user, $group);
  1816. $qb = $this->dbConn->getQueryBuilder();
  1817. $qb->select('*')
  1818. ->from('share')
  1819. ->where($qb->expr()->eq('id', $qb->createNamedParameter($id2)));
  1820. $cursor = $qb->execute();
  1821. $data = $cursor->fetchAll();
  1822. $cursor->closeCursor();
  1823. $this->assertCount($toDelete ? 0 : 1, $data);
  1824. }
  1825. }