defaultshareprovidertest.php 75 KB

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