DefaultShareProviderTest.php 101 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace Test\Share20;
  8. use OC\Share20\DefaultShareProvider;
  9. use OC\Share20\ShareAttributes;
  10. use OCP\AppFramework\Utility\ITimeFactory;
  11. use OCP\DB\QueryBuilder\IQueryBuilder;
  12. use OCP\Defaults;
  13. use OCP\Files\File;
  14. use OCP\Files\Folder;
  15. use OCP\Files\IRootFolder;
  16. use OCP\IDBConnection;
  17. use OCP\IGroup;
  18. use OCP\IGroupManager;
  19. use OCP\IL10N;
  20. use OCP\IURLGenerator;
  21. use OCP\IUser;
  22. use OCP\IUserManager;
  23. use OCP\L10N\IFactory;
  24. use OCP\Mail\IMailer;
  25. use OCP\Share\IManager as IShareManager;
  26. use OCP\Share\IShare;
  27. use PHPUnit\Framework\MockObject\MockObject;
  28. /**
  29. * Class DefaultShareProviderTest
  30. *
  31. * @package Test\Share20
  32. * @group DB
  33. */
  34. class DefaultShareProviderTest extends \Test\TestCase {
  35. /** @var IDBConnection */
  36. protected $dbConn;
  37. /** @var IUserManager | MockObject */
  38. protected $userManager;
  39. /** @var IGroupManager | MockObject */
  40. protected $groupManager;
  41. /** @var IRootFolder | MockObject */
  42. protected $rootFolder;
  43. /** @var DefaultShareProvider */
  44. protected $provider;
  45. /** @var MockObject|IMailer */
  46. protected $mailer;
  47. /** @var IFactory|MockObject */
  48. protected $l10nFactory;
  49. /** @var MockObject|IL10N */
  50. protected $l10n;
  51. /** @var MockObject|Defaults */
  52. protected $defaults;
  53. /** @var MockObject|IURLGenerator */
  54. protected $urlGenerator;
  55. /** @var ITimeFactory|MockObject */
  56. protected $timeFactory;
  57. protected function setUp(): void {
  58. $this->dbConn = \OC::$server->getDatabaseConnection();
  59. $this->userManager = $this->createMock(IUserManager::class);
  60. $this->groupManager = $this->createMock(IGroupManager::class);
  61. $this->rootFolder = $this->createMock(IRootFolder::class);
  62. $this->mailer = $this->createMock(IMailer::class);
  63. $this->l10nFactory = $this->createMock(IFactory::class);
  64. $this->l10n = $this->createMock(IL10N::class);
  65. $this->defaults = $this->getMockBuilder(Defaults::class)->disableOriginalConstructor()->getMock();
  66. $this->urlGenerator = $this->createMock(IURLGenerator::class);
  67. $this->timeFactory = $this->createMock(ITimeFactory::class);
  68. $this->userManager->expects($this->any())->method('userExists')->willReturn(true);
  69. $this->timeFactory->expects($this->any())->method('now')->willReturn(new \DateTimeImmutable("2023-05-04 00:00 Europe/Berlin"));
  70. //Empty share table
  71. $this->dbConn->getQueryBuilder()->delete('share')->execute();
  72. $this->provider = new DefaultShareProvider(
  73. $this->dbConn,
  74. $this->userManager,
  75. $this->groupManager,
  76. $this->rootFolder,
  77. $this->mailer,
  78. $this->defaults,
  79. $this->l10nFactory,
  80. $this->urlGenerator,
  81. $this->timeFactory
  82. );
  83. }
  84. protected function tearDown(): void {
  85. $this->dbConn->getQueryBuilder()->delete('share')->execute();
  86. $this->dbConn->getQueryBuilder()->delete('filecache')->execute();
  87. $this->dbConn->getQueryBuilder()->delete('storages')->execute();
  88. }
  89. /**
  90. * @param int $shareType
  91. * @param string $sharedWith
  92. * @param string $sharedBy
  93. * @param string $shareOwner
  94. * @param string $itemType
  95. * @param int $fileSource
  96. * @param string $fileTarget
  97. * @param int $permissions
  98. * @param $token
  99. * @param $expiration
  100. * @return int
  101. */
  102. private function addShareToDB($shareType, $sharedWith, $sharedBy, $shareOwner,
  103. $itemType, $fileSource, $fileTarget, $permissions, $token, $expiration,
  104. $parent = null) {
  105. $qb = $this->dbConn->getQueryBuilder();
  106. $qb->insert('share');
  107. if ($shareType) {
  108. $qb->setValue('share_type', $qb->expr()->literal($shareType));
  109. }
  110. if ($sharedWith) {
  111. $qb->setValue('share_with', $qb->expr()->literal($sharedWith));
  112. }
  113. if ($sharedBy) {
  114. $qb->setValue('uid_initiator', $qb->expr()->literal($sharedBy));
  115. }
  116. if ($shareOwner) {
  117. $qb->setValue('uid_owner', $qb->expr()->literal($shareOwner));
  118. }
  119. if ($itemType) {
  120. $qb->setValue('item_type', $qb->expr()->literal($itemType));
  121. }
  122. if ($fileSource) {
  123. $qb->setValue('file_source', $qb->expr()->literal($fileSource));
  124. }
  125. if ($fileTarget) {
  126. $qb->setValue('file_target', $qb->expr()->literal($fileTarget));
  127. }
  128. if ($permissions) {
  129. $qb->setValue('permissions', $qb->expr()->literal($permissions));
  130. }
  131. if ($token) {
  132. $qb->setValue('token', $qb->expr()->literal($token));
  133. }
  134. if ($expiration) {
  135. $qb->setValue('expiration', $qb->createNamedParameter($expiration, IQueryBuilder::PARAM_DATE));
  136. }
  137. if ($parent) {
  138. $qb->setValue('parent', $qb->expr()->literal($parent));
  139. }
  140. $this->assertEquals(1, $qb->execute());
  141. return$qb->getLastInsertId();
  142. }
  143. public function testGetShareByIdNotExist() {
  144. $this->expectException(\OCP\Share\Exceptions\ShareNotFound::class);
  145. $this->provider->getShareById(1);
  146. }
  147. public function testGetShareByIdUserShare() {
  148. $qb = $this->dbConn->getQueryBuilder();
  149. $qb->insert('share')
  150. ->values([
  151. 'share_type' => $qb->expr()->literal(IShare::TYPE_USER),
  152. 'share_with' => $qb->expr()->literal('sharedWith'),
  153. 'uid_owner' => $qb->expr()->literal('shareOwner'),
  154. 'uid_initiator' => $qb->expr()->literal('sharedBy'),
  155. 'item_type' => $qb->expr()->literal('file'),
  156. 'file_source' => $qb->expr()->literal(42),
  157. 'file_target' => $qb->expr()->literal('myTarget'),
  158. 'permissions' => $qb->expr()->literal(13),
  159. ]);
  160. $qb->execute();
  161. $id = $qb->getLastInsertId();
  162. $sharedBy = $this->createMock(IUser::class);
  163. $sharedBy->method('getUID')->willReturn('sharedBy');
  164. $shareOwner = $this->createMock(IUser::class);
  165. $shareOwner->method('getUID')->willReturn('shareOwner');
  166. $ownerPath = $this->createMock(File::class);
  167. $shareOwnerFolder = $this->createMock(Folder::class);
  168. $shareOwnerFolder->method('getFirstNodeById')->with(42)->willReturn($ownerPath);
  169. $this->rootFolder
  170. ->method('getUserFolder')
  171. ->willReturnMap([
  172. ['shareOwner', $shareOwnerFolder],
  173. ]);
  174. $share = $this->provider->getShareById($id);
  175. $this->assertEquals($id, $share->getId());
  176. $this->assertEquals(IShare::TYPE_USER, $share->getShareType());
  177. $this->assertEquals('sharedWith', $share->getSharedWith());
  178. $this->assertEquals('sharedBy', $share->getSharedBy());
  179. $this->assertEquals('shareOwner', $share->getShareOwner());
  180. $this->assertEquals($ownerPath, $share->getNode());
  181. $this->assertEquals(13, $share->getPermissions());
  182. $this->assertEquals(null, $share->getToken());
  183. $this->assertEquals(null, $share->getExpirationDate());
  184. $this->assertEquals('myTarget', $share->getTarget());
  185. }
  186. public function testGetShareByIdLazy() {
  187. $qb = $this->dbConn->getQueryBuilder();
  188. $qb->insert('share')
  189. ->values([
  190. 'share_type' => $qb->expr()->literal(IShare::TYPE_USER),
  191. 'share_with' => $qb->expr()->literal('sharedWith'),
  192. 'uid_owner' => $qb->expr()->literal('shareOwner'),
  193. 'uid_initiator' => $qb->expr()->literal('sharedBy'),
  194. 'item_type' => $qb->expr()->literal('file'),
  195. 'file_source' => $qb->expr()->literal(42),
  196. 'file_target' => $qb->expr()->literal('myTarget'),
  197. 'permissions' => $qb->expr()->literal(13),
  198. ]);
  199. $qb->execute();
  200. $id = $qb->getLastInsertId();
  201. $this->rootFolder->expects($this->never())->method('getUserFolder');
  202. $share = $this->provider->getShareById($id);
  203. // We do not fetch the node so the rootfolder is never called.
  204. $this->assertEquals($id, $share->getId());
  205. $this->assertEquals(IShare::TYPE_USER, $share->getShareType());
  206. $this->assertEquals('sharedWith', $share->getSharedWith());
  207. $this->assertEquals('sharedBy', $share->getSharedBy());
  208. $this->assertEquals('shareOwner', $share->getShareOwner());
  209. $this->assertEquals(13, $share->getPermissions());
  210. $this->assertEquals(null, $share->getToken());
  211. $this->assertEquals(null, $share->getExpirationDate());
  212. $this->assertEquals('myTarget', $share->getTarget());
  213. }
  214. public function testGetShareByIdLazy2() {
  215. $qb = $this->dbConn->getQueryBuilder();
  216. $qb->insert('share')
  217. ->values([
  218. 'share_type' => $qb->expr()->literal(IShare::TYPE_USER),
  219. 'share_with' => $qb->expr()->literal('sharedWith'),
  220. 'uid_owner' => $qb->expr()->literal('shareOwner'),
  221. 'uid_initiator' => $qb->expr()->literal('sharedBy'),
  222. 'item_type' => $qb->expr()->literal('file'),
  223. 'file_source' => $qb->expr()->literal(42),
  224. 'file_target' => $qb->expr()->literal('myTarget'),
  225. 'permissions' => $qb->expr()->literal(13),
  226. ]);
  227. $qb->execute();
  228. $id = $qb->getLastInsertId();
  229. $ownerPath = $this->createMock(File::class);
  230. $shareOwnerFolder = $this->createMock(Folder::class);
  231. $shareOwnerFolder->method('getFirstNodeById')->with(42)->willReturn($ownerPath);
  232. $this->rootFolder
  233. ->method('getUserFolder')
  234. ->with('shareOwner')
  235. ->willReturn($shareOwnerFolder);
  236. $share = $this->provider->getShareById($id);
  237. // We fetch the node so the root folder is eventually called
  238. $this->assertEquals($id, $share->getId());
  239. $this->assertEquals(IShare::TYPE_USER, $share->getShareType());
  240. $this->assertEquals('sharedWith', $share->getSharedWith());
  241. $this->assertEquals('sharedBy', $share->getSharedBy());
  242. $this->assertEquals('shareOwner', $share->getShareOwner());
  243. $this->assertEquals($ownerPath, $share->getNode());
  244. $this->assertEquals(13, $share->getPermissions());
  245. $this->assertEquals(null, $share->getToken());
  246. $this->assertEquals(null, $share->getExpirationDate());
  247. $this->assertEquals('myTarget', $share->getTarget());
  248. }
  249. public function testGetShareByIdGroupShare() {
  250. $qb = $this->dbConn->getQueryBuilder();
  251. $qb->insert('share')
  252. ->values([
  253. 'share_type' => $qb->expr()->literal(IShare::TYPE_GROUP),
  254. 'share_with' => $qb->expr()->literal('sharedWith'),
  255. 'uid_owner' => $qb->expr()->literal('shareOwner'),
  256. 'uid_initiator' => $qb->expr()->literal('sharedBy'),
  257. 'item_type' => $qb->expr()->literal('file'),
  258. 'file_source' => $qb->expr()->literal(42),
  259. 'file_target' => $qb->expr()->literal('myTarget'),
  260. 'permissions' => $qb->expr()->literal(13),
  261. ]);
  262. $this->assertEquals(1, $qb->execute());
  263. // Get the id
  264. $id = $qb->getLastInsertId();
  265. $ownerPath = $this->createMock(Folder::class);
  266. $shareOwnerFolder = $this->createMock(Folder::class);
  267. $shareOwnerFolder->method('getFirstNodeById')->with(42)->willReturn($ownerPath);
  268. $this->rootFolder
  269. ->method('getUserFolder')
  270. ->willReturnMap([
  271. ['shareOwner', $shareOwnerFolder],
  272. ]);
  273. $share = $this->provider->getShareById($id);
  274. $this->assertEquals($id, $share->getId());
  275. $this->assertEquals(IShare::TYPE_GROUP, $share->getShareType());
  276. $this->assertEquals('sharedWith', $share->getSharedWith());
  277. $this->assertEquals('sharedBy', $share->getSharedBy());
  278. $this->assertEquals('shareOwner', $share->getShareOwner());
  279. $this->assertEquals($ownerPath, $share->getNode());
  280. $this->assertEquals(13, $share->getPermissions());
  281. $this->assertEquals(null, $share->getToken());
  282. $this->assertEquals(null, $share->getExpirationDate());
  283. $this->assertEquals('myTarget', $share->getTarget());
  284. }
  285. public function testGetShareByIdUserGroupShare() {
  286. $id = $this->addShareToDB(IShare::TYPE_GROUP, 'group0', 'user0', 'user0', 'file', 42, 'myTarget', 31, null, null);
  287. $this->addShareToDB(2, 'user1', 'user0', 'user0', 'file', 42, 'userTarget', 0, null, null, $id);
  288. $user0 = $this->createMock(IUser::class);
  289. $user0->method('getUID')->willReturn('user0');
  290. $user1 = $this->createMock(IUser::class);
  291. $user1->method('getUID')->willReturn('user1');
  292. $group0 = $this->createMock(IGroup::class);
  293. $group0->method('inGroup')->with($user1)->willReturn(true);
  294. $group0->method('getDisplayName')->willReturn('g0-displayname');
  295. $node = $this->createMock(Folder::class);
  296. $node->method('getId')->willReturn(42);
  297. $this->rootFolder->method('getUserFolder')->with('user0')->willReturnSelf();
  298. $this->rootFolder->method('getFirstNodeById')->willReturn($node);
  299. $this->userManager->method('get')->willReturnMap([
  300. ['user0', $user0],
  301. ['user1', $user1],
  302. ]);
  303. $this->groupManager->method('get')->with('group0')->willReturn($group0);
  304. $share = $this->provider->getShareById($id, 'user1');
  305. $this->assertEquals($id, $share->getId());
  306. $this->assertEquals(IShare::TYPE_GROUP, $share->getShareType());
  307. $this->assertSame('group0', $share->getSharedWith());
  308. $this->assertSame('user0', $share->getSharedBy());
  309. $this->assertSame('user0', $share->getShareOwner());
  310. $this->assertSame($node, $share->getNode());
  311. $this->assertEquals(0, $share->getPermissions());
  312. $this->assertEquals(null, $share->getToken());
  313. $this->assertEquals(null, $share->getExpirationDate());
  314. $this->assertEquals('userTarget', $share->getTarget());
  315. }
  316. public function testGetShareByIdLinkShare() {
  317. $qb = $this->dbConn->getQueryBuilder();
  318. $qb->insert('share')
  319. ->values([
  320. 'share_type' => $qb->expr()->literal(IShare::TYPE_LINK),
  321. 'password' => $qb->expr()->literal('password'),
  322. 'password_by_talk' => $qb->expr()->literal(true),
  323. 'uid_owner' => $qb->expr()->literal('shareOwner'),
  324. 'uid_initiator' => $qb->expr()->literal('sharedBy'),
  325. 'item_type' => $qb->expr()->literal('file'),
  326. 'file_source' => $qb->expr()->literal(42),
  327. 'file_target' => $qb->expr()->literal('myTarget'),
  328. 'permissions' => $qb->expr()->literal(13),
  329. 'token' => $qb->expr()->literal('token'),
  330. 'expiration' => $qb->expr()->literal('2000-01-02 00:00:00'),
  331. ]);
  332. $this->assertEquals(1, $qb->execute());
  333. $id = $qb->getLastInsertId();
  334. $ownerPath = $this->createMock(Folder::class);
  335. $shareOwnerFolder = $this->createMock(Folder::class);
  336. $shareOwnerFolder->method('getFirstNodeById')->with(42)->willReturn($ownerPath);
  337. $this->rootFolder
  338. ->method('getUserFolder')
  339. ->willReturnMap([
  340. ['shareOwner', $shareOwnerFolder],
  341. ]);
  342. $share = $this->provider->getShareById($id);
  343. $this->assertEquals($id, $share->getId());
  344. $this->assertEquals(IShare::TYPE_LINK, $share->getShareType());
  345. $this->assertNull($share->getSharedWith());
  346. $this->assertEquals('password', $share->getPassword());
  347. $this->assertEquals(true, $share->getSendPasswordByTalk());
  348. $this->assertEquals('sharedBy', $share->getSharedBy());
  349. $this->assertEquals('shareOwner', $share->getShareOwner());
  350. $this->assertEquals($ownerPath, $share->getNode());
  351. $this->assertEquals(13, $share->getPermissions());
  352. $this->assertEquals('token', $share->getToken());
  353. $this->assertEquals(\DateTime::createFromFormat('Y-m-d H:i:s', '2000-01-02 00:00:00'), $share->getExpirationDate());
  354. $this->assertEquals('myTarget', $share->getTarget());
  355. }
  356. public function testDeleteSingleShare() {
  357. $qb = $this->dbConn->getQueryBuilder();
  358. $qb->insert('share')
  359. ->values([
  360. 'share_type' => $qb->expr()->literal(IShare::TYPE_USER),
  361. 'share_with' => $qb->expr()->literal('sharedWith'),
  362. 'uid_owner' => $qb->expr()->literal('sharedBy'),
  363. 'item_type' => $qb->expr()->literal('file'),
  364. 'file_source' => $qb->expr()->literal(42),
  365. 'file_target' => $qb->expr()->literal('myTarget'),
  366. 'permissions' => $qb->expr()->literal(13),
  367. ]);
  368. $this->assertEquals(1, $qb->execute());
  369. $id = $qb->getLastInsertId();
  370. $share = $this->createMock(IShare::class);
  371. $share->method('getId')->willReturn($id);
  372. /** @var DefaultShareProvider $provider */
  373. $provider = $this->getMockBuilder(DefaultShareProvider::class)
  374. ->setConstructorArgs([
  375. $this->dbConn,
  376. $this->userManager,
  377. $this->groupManager,
  378. $this->rootFolder,
  379. $this->mailer,
  380. $this->defaults,
  381. $this->l10nFactory,
  382. $this->urlGenerator,
  383. $this->timeFactory
  384. ])
  385. ->setMethods(['getShareById'])
  386. ->getMock();
  387. $provider->delete($share);
  388. $qb = $this->dbConn->getQueryBuilder();
  389. $qb->select('*')
  390. ->from('share');
  391. $cursor = $qb->execute();
  392. $result = $cursor->fetchAll();
  393. $cursor->closeCursor();
  394. $this->assertEmpty($result);
  395. }
  396. public function testDeleteSingleShareLazy() {
  397. $qb = $this->dbConn->getQueryBuilder();
  398. $qb->insert('share')
  399. ->values([
  400. 'share_type' => $qb->expr()->literal(IShare::TYPE_USER),
  401. 'share_with' => $qb->expr()->literal('sharedWith'),
  402. 'uid_owner' => $qb->expr()->literal('shareOwner'),
  403. 'uid_initiator' => $qb->expr()->literal('sharedBy'),
  404. 'item_type' => $qb->expr()->literal('file'),
  405. 'file_source' => $qb->expr()->literal(42),
  406. 'file_target' => $qb->expr()->literal('myTarget'),
  407. 'permissions' => $qb->expr()->literal(13),
  408. ]);
  409. $this->assertEquals(1, $qb->execute());
  410. $id = $qb->getLastInsertId();
  411. $this->rootFolder->expects($this->never())->method($this->anything());
  412. $share = $this->provider->getShareById($id);
  413. $this->provider->delete($share);
  414. $qb = $this->dbConn->getQueryBuilder();
  415. $qb->select('*')
  416. ->from('share');
  417. $cursor = $qb->execute();
  418. $result = $cursor->fetchAll();
  419. $cursor->closeCursor();
  420. $this->assertEmpty($result);
  421. }
  422. public function testDeleteGroupShareWithUserGroupShares() {
  423. $qb = $this->dbConn->getQueryBuilder();
  424. $qb->insert('share')
  425. ->values([
  426. 'share_type' => $qb->expr()->literal(IShare::TYPE_GROUP),
  427. 'share_with' => $qb->expr()->literal('sharedWith'),
  428. 'uid_owner' => $qb->expr()->literal('sharedBy'),
  429. 'item_type' => $qb->expr()->literal('file'),
  430. 'file_source' => $qb->expr()->literal(42),
  431. 'file_target' => $qb->expr()->literal('myTarget'),
  432. 'permissions' => $qb->expr()->literal(13),
  433. ]);
  434. $this->assertEquals(1, $qb->execute());
  435. $id = $qb->getLastInsertId();
  436. $qb = $this->dbConn->getQueryBuilder();
  437. $qb->insert('share')
  438. ->values([
  439. 'share_type' => $qb->expr()->literal(2),
  440. 'share_with' => $qb->expr()->literal('sharedWithUser'),
  441. 'uid_owner' => $qb->expr()->literal('sharedBy'),
  442. 'item_type' => $qb->expr()->literal('file'),
  443. 'file_source' => $qb->expr()->literal(42),
  444. 'file_target' => $qb->expr()->literal('myTarget'),
  445. 'permissions' => $qb->expr()->literal(13),
  446. 'parent' => $qb->expr()->literal($id),
  447. ]);
  448. $this->assertEquals(1, $qb->execute());
  449. $share = $this->createMock(IShare::class);
  450. $share->method('getId')->willReturn($id);
  451. $share->method('getShareType')->willReturn(IShare::TYPE_GROUP);
  452. /** @var DefaultShareProvider $provider */
  453. $provider = $this->getMockBuilder(DefaultShareProvider::class)
  454. ->setConstructorArgs([
  455. $this->dbConn,
  456. $this->userManager,
  457. $this->groupManager,
  458. $this->rootFolder,
  459. $this->mailer,
  460. $this->defaults,
  461. $this->l10nFactory,
  462. $this->urlGenerator,
  463. $this->timeFactory
  464. ])
  465. ->setMethods(['getShareById'])
  466. ->getMock();
  467. $provider->delete($share);
  468. $qb = $this->dbConn->getQueryBuilder();
  469. $qb->select('*')
  470. ->from('share');
  471. $cursor = $qb->execute();
  472. $result = $cursor->fetchAll();
  473. $cursor->closeCursor();
  474. $this->assertEmpty($result);
  475. }
  476. public function testGetChildren() {
  477. $qb = $this->dbConn->getQueryBuilder();
  478. $qb->insert('share')
  479. ->values([
  480. 'share_type' => $qb->expr()->literal(IShare::TYPE_USER),
  481. 'share_with' => $qb->expr()->literal('sharedWith'),
  482. 'uid_owner' => $qb->expr()->literal('shareOwner'),
  483. 'uid_initiator' => $qb->expr()->literal('sharedBy'),
  484. 'item_type' => $qb->expr()->literal('file'),
  485. 'file_source' => $qb->expr()->literal(42),
  486. 'file_target' => $qb->expr()->literal('myTarget'),
  487. 'permissions' => $qb->expr()->literal(13),
  488. ]);
  489. $qb->execute();
  490. // Get the id
  491. $id = $qb->getLastInsertId();
  492. $qb = $this->dbConn->getQueryBuilder();
  493. $qb->insert('share')
  494. ->values([
  495. 'share_type' => $qb->expr()->literal(IShare::TYPE_USER),
  496. 'share_with' => $qb->expr()->literal('user1'),
  497. 'uid_owner' => $qb->expr()->literal('shareOwner'),
  498. 'uid_initiator' => $qb->expr()->literal('user2'),
  499. 'item_type' => $qb->expr()->literal('file'),
  500. 'file_source' => $qb->expr()->literal(1),
  501. 'file_target' => $qb->expr()->literal('myTarget1'),
  502. 'permissions' => $qb->expr()->literal(2),
  503. 'parent' => $qb->expr()->literal($id),
  504. ]);
  505. $qb->execute();
  506. $qb = $this->dbConn->getQueryBuilder();
  507. $qb->insert('share')
  508. ->values([
  509. 'share_type' => $qb->expr()->literal(IShare::TYPE_GROUP),
  510. 'share_with' => $qb->expr()->literal('group1'),
  511. 'uid_owner' => $qb->expr()->literal('shareOwner'),
  512. 'uid_initiator' => $qb->expr()->literal('user3'),
  513. 'item_type' => $qb->expr()->literal('folder'),
  514. 'file_source' => $qb->expr()->literal(3),
  515. 'file_target' => $qb->expr()->literal('myTarget2'),
  516. 'permissions' => $qb->expr()->literal(4),
  517. 'parent' => $qb->expr()->literal($id),
  518. ]);
  519. $qb->execute();
  520. $ownerPath = $this->createMock(Folder::class);
  521. $ownerFolder = $this->createMock(Folder::class);
  522. $ownerFolder->method('getFirstNodeById')->willReturn($ownerPath);
  523. $this->rootFolder
  524. ->method('getUserFolder')
  525. ->willReturnMap([
  526. ['shareOwner', $ownerFolder],
  527. ]);
  528. $share = $this->createMock(IShare::class);
  529. $share->method('getId')->willReturn($id);
  530. $children = $this->provider->getChildren($share);
  531. $this->assertCount(2, $children);
  532. //Child1
  533. $this->assertEquals(IShare::TYPE_USER, $children[0]->getShareType());
  534. $this->assertEquals('user1', $children[0]->getSharedWith());
  535. $this->assertEquals('user2', $children[0]->getSharedBy());
  536. $this->assertEquals('shareOwner', $children[0]->getShareOwner());
  537. $this->assertEquals($ownerPath, $children[0]->getNode());
  538. $this->assertEquals(2, $children[0]->getPermissions());
  539. $this->assertEquals(null, $children[0]->getToken());
  540. $this->assertEquals(null, $children[0]->getExpirationDate());
  541. $this->assertEquals('myTarget1', $children[0]->getTarget());
  542. //Child2
  543. $this->assertEquals(IShare::TYPE_GROUP, $children[1]->getShareType());
  544. $this->assertEquals('group1', $children[1]->getSharedWith());
  545. $this->assertEquals('user3', $children[1]->getSharedBy());
  546. $this->assertEquals('shareOwner', $children[1]->getShareOwner());
  547. $this->assertEquals($ownerPath, $children[1]->getNode());
  548. $this->assertEquals(4, $children[1]->getPermissions());
  549. $this->assertEquals(null, $children[1]->getToken());
  550. $this->assertEquals(null, $children[1]->getExpirationDate());
  551. $this->assertEquals('myTarget2', $children[1]->getTarget());
  552. }
  553. public function testCreateUserShare() {
  554. $share = new \OC\Share20\Share($this->rootFolder, $this->userManager);
  555. $shareOwner = $this->createMock(IUser::class);
  556. $shareOwner->method('getUID')->willReturn('shareOwner');
  557. $path = $this->createMock(File::class);
  558. $path->method('getId')->willReturn(100);
  559. $path->method('getOwner')->willReturn($shareOwner);
  560. $ownerFolder = $this->createMock(Folder::class);
  561. $userFolder = $this->createMock(Folder::class);
  562. $this->rootFolder
  563. ->method('getUserFolder')
  564. ->willReturnMap([
  565. ['sharedBy', $userFolder],
  566. ['shareOwner', $ownerFolder],
  567. ]);
  568. $userFolder->method('getFirstNodeById')
  569. ->with(100)
  570. ->willReturn($path);
  571. $ownerFolder->method('getFirstNodeById')
  572. ->with(100)
  573. ->willReturn($path);
  574. $share->setShareType(IShare::TYPE_USER);
  575. $share->setSharedWith('sharedWith');
  576. $share->setSharedBy('sharedBy');
  577. $share->setShareOwner('shareOwner');
  578. $share->setNode($path);
  579. $share->setSharedWithDisplayName('Displayed Name');
  580. $share->setSharedWithAvatar('/path/to/image.svg');
  581. $share->setPermissions(1);
  582. $attrs = new ShareAttributes();
  583. $attrs->setAttribute('permissions', 'download', true);
  584. $share->setAttributes($attrs);
  585. $share->setTarget('/target');
  586. $share2 = $this->provider->create($share);
  587. $this->assertNotNull($share2->getId());
  588. $this->assertSame('ocinternal:'.$share2->getId(), $share2->getFullId());
  589. $this->assertSame(IShare::TYPE_USER, $share2->getShareType());
  590. $this->assertSame('sharedWith', $share2->getSharedWith());
  591. $this->assertSame('sharedBy', $share2->getSharedBy());
  592. $this->assertSame('shareOwner', $share2->getShareOwner());
  593. $this->assertSame(1, $share2->getPermissions());
  594. $this->assertSame('/target', $share2->getTarget());
  595. $this->assertLessThanOrEqual(new \DateTime(), $share2->getShareTime());
  596. $this->assertSame($path, $share2->getNode());
  597. // Data is kept after creation
  598. $this->assertSame('Displayed Name', $share->getSharedWithDisplayName());
  599. $this->assertSame('/path/to/image.svg', $share->getSharedWithAvatar());
  600. $this->assertSame('Displayed Name', $share2->getSharedWithDisplayName());
  601. $this->assertSame('/path/to/image.svg', $share2->getSharedWithAvatar());
  602. $this->assertSame(
  603. [
  604. [
  605. 'scope' => 'permissions',
  606. 'key' => 'download',
  607. 'enabled' => true
  608. ]
  609. ],
  610. $share->getAttributes()->toArray()
  611. );
  612. }
  613. public function testCreateGroupShare() {
  614. $share = new \OC\Share20\Share($this->rootFolder, $this->userManager);
  615. $shareOwner = $this->createMock(IUser::class);
  616. $shareOwner->method('getUID')->willReturn('shareOwner');
  617. $path = $this->createMock(Folder::class);
  618. $path->method('getId')->willReturn(100);
  619. $path->method('getOwner')->willReturn($shareOwner);
  620. $ownerFolder = $this->createMock(Folder::class);
  621. $userFolder = $this->createMock(Folder::class);
  622. $this->rootFolder
  623. ->method('getUserFolder')
  624. ->willReturnMap([
  625. ['sharedBy', $userFolder],
  626. ['shareOwner', $ownerFolder],
  627. ]);
  628. $userFolder->method('getFirstNodeById')
  629. ->with(100)
  630. ->willReturn($path);
  631. $ownerFolder->method('getFirstNodeById')
  632. ->with(100)
  633. ->willReturn($path);
  634. $share->setShareType(IShare::TYPE_GROUP);
  635. $share->setSharedWith('sharedWith');
  636. $share->setSharedBy('sharedBy');
  637. $share->setShareOwner('shareOwner');
  638. $share->setNode($path);
  639. $share->setPermissions(1);
  640. $share->setSharedWithDisplayName('Displayed Name');
  641. $share->setSharedWithAvatar('/path/to/image.svg');
  642. $share->setTarget('/target');
  643. $attrs = new ShareAttributes();
  644. $attrs->setAttribute('permissions', 'download', true);
  645. $share->setAttributes($attrs);
  646. $share2 = $this->provider->create($share);
  647. $this->assertNotNull($share2->getId());
  648. $this->assertSame('ocinternal:'.$share2->getId(), $share2->getFullId());
  649. $this->assertSame(IShare::TYPE_GROUP, $share2->getShareType());
  650. $this->assertSame('sharedWith', $share2->getSharedWith());
  651. $this->assertSame('sharedBy', $share2->getSharedBy());
  652. $this->assertSame('shareOwner', $share2->getShareOwner());
  653. $this->assertSame(1, $share2->getPermissions());
  654. $this->assertSame('/target', $share2->getTarget());
  655. $this->assertLessThanOrEqual(new \DateTime(), $share2->getShareTime());
  656. $this->assertSame($path, $share2->getNode());
  657. // Data is kept after creation
  658. $this->assertSame('Displayed Name', $share->getSharedWithDisplayName());
  659. $this->assertSame('/path/to/image.svg', $share->getSharedWithAvatar());
  660. $this->assertSame('Displayed Name', $share2->getSharedWithDisplayName());
  661. $this->assertSame('/path/to/image.svg', $share2->getSharedWithAvatar());
  662. $this->assertSame(
  663. [
  664. [
  665. 'scope' => 'permissions',
  666. 'key' => 'download',
  667. 'enabled' => true
  668. ]
  669. ],
  670. $share->getAttributes()->toArray()
  671. );
  672. }
  673. public function testCreateLinkShare() {
  674. $share = new \OC\Share20\Share($this->rootFolder, $this->userManager);
  675. $shareOwner = $this->createMock(IUser::class);
  676. $shareOwner->method('getUID')->willReturn('shareOwner');
  677. $path = $this->createMock(Folder::class);
  678. $path->method('getId')->willReturn(100);
  679. $path->method('getOwner')->willReturn($shareOwner);
  680. $ownerFolder = $this->createMock(Folder::class);
  681. $userFolder = $this->createMock(Folder::class);
  682. $this->rootFolder
  683. ->method('getUserFolder')
  684. ->willReturnMap([
  685. ['sharedBy', $userFolder],
  686. ['shareOwner', $ownerFolder],
  687. ]);
  688. $userFolder->method('getFirstNodeById')
  689. ->with(100)
  690. ->willReturn($path);
  691. $ownerFolder->method('getFirstNodeById')
  692. ->with(100)
  693. ->willReturn($path);
  694. $share->setShareType(IShare::TYPE_LINK);
  695. $share->setSharedBy('sharedBy');
  696. $share->setShareOwner('shareOwner');
  697. $share->setNode($path);
  698. $share->setPermissions(1);
  699. $share->setPassword('password');
  700. $share->setSendPasswordByTalk(true);
  701. $share->setToken('token');
  702. $expireDate = new \DateTime();
  703. $share->setExpirationDate($expireDate);
  704. $share->setTarget('/target');
  705. $share2 = $this->provider->create($share);
  706. $this->assertNotNull($share2->getId());
  707. $this->assertSame('ocinternal:'.$share2->getId(), $share2->getFullId());
  708. $this->assertSame(IShare::TYPE_LINK, $share2->getShareType());
  709. $this->assertSame('sharedBy', $share2->getSharedBy());
  710. $this->assertSame('shareOwner', $share2->getShareOwner());
  711. $this->assertSame(1, $share2->getPermissions());
  712. $this->assertSame('/target', $share2->getTarget());
  713. $this->assertLessThanOrEqual(new \DateTime(), $share2->getShareTime());
  714. $this->assertSame($path, $share2->getNode());
  715. $this->assertSame('password', $share2->getPassword());
  716. $this->assertSame(true, $share2->getSendPasswordByTalk());
  717. $this->assertSame('token', $share2->getToken());
  718. $this->assertEquals($expireDate->getTimestamp(), $share2->getExpirationDate()->getTimestamp());
  719. }
  720. public function testGetShareByToken() {
  721. $qb = $this->dbConn->getQueryBuilder();
  722. $qb->insert('share')
  723. ->values([
  724. 'share_type' => $qb->expr()->literal(IShare::TYPE_LINK),
  725. 'password' => $qb->expr()->literal('password'),
  726. 'password_by_talk' => $qb->expr()->literal(true),
  727. 'uid_owner' => $qb->expr()->literal('shareOwner'),
  728. 'uid_initiator' => $qb->expr()->literal('sharedBy'),
  729. 'item_type' => $qb->expr()->literal('file'),
  730. 'file_source' => $qb->expr()->literal(42),
  731. 'file_target' => $qb->expr()->literal('myTarget'),
  732. 'permissions' => $qb->expr()->literal(13),
  733. 'token' => $qb->expr()->literal('secrettoken'),
  734. ]);
  735. $qb->execute();
  736. $id = $qb->getLastInsertId();
  737. $file = $this->createMock(File::class);
  738. $this->rootFolder->method('getUserFolder')->with('shareOwner')->willReturnSelf();
  739. $this->rootFolder->method('getFirstNodeById')->with(42)->willReturn($file);
  740. $share = $this->provider->getShareByToken('secrettoken');
  741. $this->assertEquals($id, $share->getId());
  742. $this->assertSame('shareOwner', $share->getShareOwner());
  743. $this->assertSame('sharedBy', $share->getSharedBy());
  744. $this->assertSame('secrettoken', $share->getToken());
  745. $this->assertSame('password', $share->getPassword());
  746. $this->assertSame(true, $share->getSendPasswordByTalk());
  747. $this->assertSame(null, $share->getSharedWith());
  748. }
  749. public function testGetShareByTokenNotFound() {
  750. $this->expectException(\OCP\Share\Exceptions\ShareNotFound::class);
  751. $this->provider->getShareByToken('invalidtoken');
  752. }
  753. private function createTestStorageEntry($storageStringId) {
  754. $qb = $this->dbConn->getQueryBuilder();
  755. $qb->insert('storages')
  756. ->values([
  757. 'id' => $qb->expr()->literal($storageStringId),
  758. ]);
  759. $this->assertEquals(1, $qb->execute());
  760. return $qb->getLastInsertId();
  761. }
  762. private function createTestFileEntry($path, $storage = 1) {
  763. $qb = $this->dbConn->getQueryBuilder();
  764. $qb->insert('filecache')
  765. ->values([
  766. 'storage' => $qb->expr()->literal($storage),
  767. 'path' => $qb->expr()->literal($path),
  768. 'path_hash' => $qb->expr()->literal(md5($path)),
  769. 'name' => $qb->expr()->literal(basename($path)),
  770. ]);
  771. $this->assertEquals(1, $qb->execute());
  772. return $qb->getLastInsertId();
  773. }
  774. public function storageAndFileNameProvider() {
  775. return [
  776. // regular file on regular storage
  777. ['home::shareOwner', 'files/test.txt', 'files/test2.txt'],
  778. // regular file on external storage
  779. ['smb::whatever', 'files/test.txt', 'files/test2.txt'],
  780. // regular file on external storage in trashbin-like folder,
  781. ['smb::whatever', 'files_trashbin/files/test.txt', 'files_trashbin/files/test2.txt'],
  782. ];
  783. }
  784. /**
  785. * @dataProvider storageAndFileNameProvider
  786. */
  787. public function testGetSharedWithUser($storageStringId, $fileName1, $fileName2) {
  788. $storageId = $this->createTestStorageEntry($storageStringId);
  789. $fileId = $this->createTestFileEntry($fileName1, $storageId);
  790. $fileId2 = $this->createTestFileEntry($fileName2, $storageId);
  791. $qb = $this->dbConn->getQueryBuilder();
  792. $qb->insert('share')
  793. ->values([
  794. 'share_type' => $qb->expr()->literal(IShare::TYPE_USER),
  795. 'share_with' => $qb->expr()->literal('sharedWith'),
  796. 'uid_owner' => $qb->expr()->literal('shareOwner'),
  797. 'uid_initiator' => $qb->expr()->literal('sharedBy'),
  798. 'item_type' => $qb->expr()->literal('file'),
  799. 'file_source' => $qb->expr()->literal($fileId),
  800. 'file_target' => $qb->expr()->literal('myTarget'),
  801. 'permissions' => $qb->expr()->literal(13),
  802. ]);
  803. $this->assertEquals(1, $qb->execute());
  804. $id = $qb->getLastInsertId();
  805. $qb = $this->dbConn->getQueryBuilder();
  806. $qb->insert('share')
  807. ->values([
  808. 'share_type' => $qb->expr()->literal(IShare::TYPE_USER),
  809. 'share_with' => $qb->expr()->literal('sharedWith2'),
  810. 'uid_owner' => $qb->expr()->literal('shareOwner2'),
  811. 'uid_initiator' => $qb->expr()->literal('sharedBy2'),
  812. 'item_type' => $qb->expr()->literal('file2'),
  813. 'file_source' => $qb->expr()->literal($fileId2),
  814. 'file_target' => $qb->expr()->literal('myTarget2'),
  815. 'permissions' => $qb->expr()->literal(14),
  816. ]);
  817. $this->assertEquals(1, $qb->execute());
  818. $file = $this->createMock(File::class);
  819. $this->rootFolder->method('getUserFolder')->with('shareOwner')->willReturnSelf();
  820. $this->rootFolder->method('getFirstNodeById')->with($fileId)->willReturn($file);
  821. $share = $this->provider->getSharedWith('sharedWith', IShare::TYPE_USER, null, 1, 0);
  822. $this->assertCount(1, $share);
  823. $share = $share[0];
  824. $this->assertEquals($id, $share->getId());
  825. $this->assertEquals('sharedWith', $share->getSharedWith());
  826. $this->assertEquals('shareOwner', $share->getShareOwner());
  827. $this->assertEquals('sharedBy', $share->getSharedBy());
  828. $this->assertEquals(IShare::TYPE_USER, $share->getShareType());
  829. }
  830. /**
  831. * @dataProvider storageAndFileNameProvider
  832. */
  833. public function testGetSharedWithGroup($storageStringId, $fileName1, $fileName2) {
  834. $storageId = $this->createTestStorageEntry($storageStringId);
  835. $fileId = $this->createTestFileEntry($fileName1, $storageId);
  836. $fileId2 = $this->createTestFileEntry($fileName2, $storageId);
  837. $qb = $this->dbConn->getQueryBuilder();
  838. $qb->insert('share')
  839. ->values([
  840. 'share_type' => $qb->expr()->literal(IShare::TYPE_GROUP),
  841. 'share_with' => $qb->expr()->literal('sharedWith'),
  842. 'uid_owner' => $qb->expr()->literal('shareOwner2'),
  843. 'uid_initiator' => $qb->expr()->literal('sharedBy2'),
  844. 'item_type' => $qb->expr()->literal('file'),
  845. 'file_source' => $qb->expr()->literal($fileId2),
  846. 'file_target' => $qb->expr()->literal('myTarget2'),
  847. 'permissions' => $qb->expr()->literal(14),
  848. ]);
  849. $this->assertEquals(1, $qb->execute());
  850. $qb = $this->dbConn->getQueryBuilder();
  851. $qb->insert('share')
  852. ->values([
  853. 'share_type' => $qb->expr()->literal(IShare::TYPE_GROUP),
  854. 'share_with' => $qb->expr()->literal('sharedWith'),
  855. 'uid_owner' => $qb->expr()->literal('shareOwner'),
  856. 'uid_initiator' => $qb->expr()->literal('sharedBy'),
  857. 'item_type' => $qb->expr()->literal('file'),
  858. 'file_source' => $qb->expr()->literal($fileId),
  859. 'file_target' => $qb->expr()->literal('myTarget'),
  860. 'permissions' => $qb->expr()->literal(13),
  861. ]);
  862. $this->assertEquals(1, $qb->execute());
  863. $id = $qb->getLastInsertId();
  864. $groups = [];
  865. foreach (range(0, 100) as $i) {
  866. $groups[] = 'group'.$i;
  867. }
  868. $groups[] = 'sharedWith';
  869. $user = $this->createMock(IUser::class);
  870. $user->method('getUID')->willReturn('sharedWith');
  871. $owner = $this->createMock(IUser::class);
  872. $owner->method('getUID')->willReturn('shareOwner');
  873. $initiator = $this->createMock(IUser::class);
  874. $initiator->method('getUID')->willReturn('sharedBy');
  875. $this->userManager->method('get')->willReturnMap([
  876. ['sharedWith', $user],
  877. ['shareOwner', $owner],
  878. ['sharedBy', $initiator],
  879. ]);
  880. $this->groupManager
  881. ->method('getUserGroupIds')
  882. ->willReturnCallback(fn (IUser $user) => ($user->getUID() === 'sharedWith' ? $groups : []));
  883. $file = $this->createMock(File::class);
  884. $this->rootFolder->method('getUserFolder')->with('shareOwner')->willReturnSelf();
  885. $this->rootFolder->method('getFirstNodeById')->with($fileId)->willReturn($file);
  886. $share = $this->provider->getSharedWith('sharedWith', IShare::TYPE_GROUP, null, 20, 1);
  887. $this->assertCount(1, $share);
  888. $share = $share[0];
  889. $this->assertEquals($id, $share->getId());
  890. $this->assertEquals('sharedWith', $share->getSharedWith());
  891. $this->assertEquals('shareOwner', $share->getShareOwner());
  892. $this->assertEquals('sharedBy', $share->getSharedBy());
  893. $this->assertEquals(IShare::TYPE_GROUP, $share->getShareType());
  894. }
  895. /**
  896. * @dataProvider storageAndFileNameProvider
  897. */
  898. public function testGetSharedWithGroupUserModified($storageStringId, $fileName1, $fileName2) {
  899. $storageId = $this->createTestStorageEntry($storageStringId);
  900. $fileId = $this->createTestFileEntry($fileName1, $storageId);
  901. $qb = $this->dbConn->getQueryBuilder();
  902. $qb->insert('share')
  903. ->values([
  904. 'share_type' => $qb->expr()->literal(IShare::TYPE_GROUP),
  905. 'share_with' => $qb->expr()->literal('sharedWith'),
  906. 'uid_owner' => $qb->expr()->literal('shareOwner'),
  907. 'uid_initiator' => $qb->expr()->literal('sharedBy'),
  908. 'item_type' => $qb->expr()->literal('file'),
  909. 'file_source' => $qb->expr()->literal($fileId),
  910. 'file_target' => $qb->expr()->literal('myTarget'),
  911. 'permissions' => $qb->expr()->literal(13),
  912. ]);
  913. $this->assertEquals(1, $qb->execute());
  914. $id = $qb->getLastInsertId();
  915. /*
  916. * Wrong share. Should not be taken by code.
  917. */
  918. $qb = $this->dbConn->getQueryBuilder();
  919. $qb->insert('share')
  920. ->values([
  921. 'share_type' => $qb->expr()->literal(2),
  922. 'share_with' => $qb->expr()->literal('user2'),
  923. 'uid_owner' => $qb->expr()->literal('shareOwner'),
  924. 'uid_initiator' => $qb->expr()->literal('sharedBy'),
  925. 'item_type' => $qb->expr()->literal('file'),
  926. 'file_source' => $qb->expr()->literal($fileId),
  927. 'file_target' => $qb->expr()->literal('wrongTarget'),
  928. 'permissions' => $qb->expr()->literal(31),
  929. 'parent' => $qb->expr()->literal($id),
  930. ]);
  931. $this->assertEquals(1, $qb->execute());
  932. /*
  933. * Correct share. should be taken by code path.
  934. */
  935. $qb = $this->dbConn->getQueryBuilder();
  936. $qb->insert('share')
  937. ->values([
  938. 'share_type' => $qb->expr()->literal(2),
  939. 'share_with' => $qb->expr()->literal('user'),
  940. 'uid_owner' => $qb->expr()->literal('shareOwner'),
  941. 'uid_initiator' => $qb->expr()->literal('sharedBy'),
  942. 'item_type' => $qb->expr()->literal('file'),
  943. 'file_source' => $qb->expr()->literal($fileId),
  944. 'file_target' => $qb->expr()->literal('userTarget'),
  945. 'permissions' => $qb->expr()->literal(0),
  946. 'parent' => $qb->expr()->literal($id),
  947. ]);
  948. $this->assertEquals(1, $qb->execute());
  949. $groups = ['sharedWith'];
  950. $user = $this->createMock(IUser::class);
  951. $user->method('getUID')->willReturn('user');
  952. $owner = $this->createMock(IUser::class);
  953. $owner->method('getUID')->willReturn('shareOwner');
  954. $initiator = $this->createMock(IUser::class);
  955. $initiator->method('getUID')->willReturn('sharedBy');
  956. $this->userManager->method('get')->willReturnMap([
  957. ['user', $user],
  958. ['shareOwner', $owner],
  959. ['sharedBy', $initiator],
  960. ]);
  961. $this->groupManager
  962. ->method('getUserGroupIds')
  963. ->willReturnCallback(fn (IUser $user) => ($user->getUID() === 'user' ? $groups : []));
  964. $file = $this->createMock(File::class);
  965. $this->rootFolder->method('getUserFolder')->with('shareOwner')->willReturnSelf();
  966. $this->rootFolder->method('getFirstNodeById')->with($fileId)->willReturn($file);
  967. $share = $this->provider->getSharedWith('user', IShare::TYPE_GROUP, null, -1, 0);
  968. $this->assertCount(1, $share);
  969. $share = $share[0];
  970. $this->assertSame((string)$id, $share->getId());
  971. $this->assertSame('sharedWith', $share->getSharedWith());
  972. $this->assertSame('shareOwner', $share->getShareOwner());
  973. $this->assertSame('sharedBy', $share->getSharedBy());
  974. $this->assertSame(IShare::TYPE_GROUP, $share->getShareType());
  975. $this->assertSame(0, $share->getPermissions());
  976. $this->assertSame('userTarget', $share->getTarget());
  977. }
  978. /**
  979. * @dataProvider storageAndFileNameProvider
  980. */
  981. public function testGetSharedWithUserWithNode($storageStringId, $fileName1, $fileName2) {
  982. $storageId = $this->createTestStorageEntry($storageStringId);
  983. $fileId = $this->createTestFileEntry($fileName1, $storageId);
  984. $fileId2 = $this->createTestFileEntry($fileName2, $storageId);
  985. $this->addShareToDB(IShare::TYPE_USER, 'user0', 'user1', 'user1',
  986. 'file', $fileId, 'myTarget', 31, null, null, null);
  987. $id = $this->addShareToDB(IShare::TYPE_USER, 'user0', 'user1', 'user1',
  988. 'file', $fileId2, 'myTarget', 31, null, null, null);
  989. $user0 = $this->createMock(IUser::class);
  990. $user0->method('getUID')->willReturn('user0');
  991. $user0->method('getDisplayName')->willReturn('user0');
  992. $user1 = $this->createMock(IUser::class);
  993. $user1->method('getUID')->willReturn('user1');
  994. $user0->method('getDisplayName')->willReturn('user0');
  995. $this->userManager->method('get')->willReturnMap([
  996. ['user0', $user0],
  997. ['user1', $user1],
  998. ]);
  999. $file = $this->createMock(File::class);
  1000. $file->method('getId')->willReturn($fileId2);
  1001. $this->rootFolder->method('getUserFolder')->with('user1')->willReturnSelf();
  1002. $this->rootFolder->method('getFirstNodeById')->with($fileId2)->willReturn($file);
  1003. $share = $this->provider->getSharedWith('user0', IShare::TYPE_USER, $file, -1, 0);
  1004. $this->assertCount(1, $share);
  1005. $share = $share[0];
  1006. $this->assertEquals($id, $share->getId());
  1007. $this->assertSame('user0', $share->getSharedWith());
  1008. $this->assertSame('user1', $share->getShareOwner());
  1009. $this->assertSame('user1', $share->getSharedBy());
  1010. $this->assertSame($file, $share->getNode());
  1011. $this->assertEquals(IShare::TYPE_USER, $share->getShareType());
  1012. }
  1013. /**
  1014. * @dataProvider storageAndFileNameProvider
  1015. */
  1016. public function testGetSharedWithGroupWithNode($storageStringId, $fileName1, $fileName2) {
  1017. $storageId = $this->createTestStorageEntry($storageStringId);
  1018. $fileId = $this->createTestFileEntry($fileName1, $storageId);
  1019. $fileId2 = $this->createTestFileEntry($fileName2, $storageId);
  1020. $this->addShareToDB(IShare::TYPE_GROUP, 'group0', 'user1', 'user1',
  1021. 'file', $fileId, 'myTarget', 31, null, null, null);
  1022. $id = $this->addShareToDB(IShare::TYPE_GROUP, 'group0', 'user1', 'user1',
  1023. 'file', $fileId2, 'myTarget', 31, null, null, null);
  1024. $user0 = $this->createMock(IUser::class);
  1025. $user0->method('getUID')->willReturn('user0');
  1026. $user1 = $this->createMock(IUser::class);
  1027. $user1->method('getUID')->willReturn('user1');
  1028. $this->userManager->method('get')->willReturnMap([
  1029. ['user0', $user0],
  1030. ['user1', $user1],
  1031. ]);
  1032. $this->groupManager
  1033. ->method('getUserGroupIds')
  1034. ->willReturnCallback(fn (IUser $user) => ($user->getUID() === 'user0' ? ['group0'] : []));
  1035. $node = $this->createMock(Folder::class);
  1036. $node->method('getId')->willReturn($fileId2);
  1037. $this->rootFolder->method('getUserFolder')->with('user1')->willReturnSelf();
  1038. $this->rootFolder->method('getFirstNodeById')->with($fileId2)->willReturn($node);
  1039. $share = $this->provider->getSharedWith('user0', IShare::TYPE_GROUP, $node, -1, 0);
  1040. $this->assertCount(1, $share);
  1041. $share = $share[0];
  1042. $this->assertEquals($id, $share->getId());
  1043. $this->assertSame('group0', $share->getSharedWith());
  1044. $this->assertSame('user1', $share->getShareOwner());
  1045. $this->assertSame('user1', $share->getSharedBy());
  1046. $this->assertSame($node, $share->getNode());
  1047. $this->assertEquals(IShare::TYPE_GROUP, $share->getShareType());
  1048. }
  1049. public function shareTypesProvider() {
  1050. return [
  1051. [IShare::TYPE_USER, false],
  1052. [IShare::TYPE_GROUP, false],
  1053. [IShare::TYPE_USER, true],
  1054. [IShare::TYPE_GROUP, true],
  1055. ];
  1056. }
  1057. /**
  1058. * @dataProvider shareTypesProvider
  1059. */
  1060. public function testGetSharedWithWithDeletedFile($shareType, $trashed) {
  1061. if ($trashed) {
  1062. // exists in database but is in trash
  1063. $storageId = $this->createTestStorageEntry('home::shareOwner');
  1064. $deletedFileId = $this->createTestFileEntry('files_trashbin/files/test.txt.d1465553223', $storageId);
  1065. } else {
  1066. // fileid that doesn't exist in the database
  1067. $deletedFileId = 123;
  1068. }
  1069. $qb = $this->dbConn->getQueryBuilder();
  1070. $qb->insert('share')
  1071. ->values([
  1072. 'share_type' => $qb->expr()->literal($shareType),
  1073. 'share_with' => $qb->expr()->literal('sharedWith'),
  1074. 'uid_owner' => $qb->expr()->literal('shareOwner'),
  1075. 'uid_initiator' => $qb->expr()->literal('sharedBy'),
  1076. 'item_type' => $qb->expr()->literal('file'),
  1077. 'file_source' => $qb->expr()->literal($deletedFileId),
  1078. 'file_target' => $qb->expr()->literal('myTarget'),
  1079. 'permissions' => $qb->expr()->literal(13),
  1080. ]);
  1081. $this->assertEquals(1, $qb->execute());
  1082. $file = $this->createMock(File::class);
  1083. $this->rootFolder->method('getUserFolder')->with('shareOwner')->willReturnSelf();
  1084. $this->rootFolder->method('getFirstNodeById')->with($deletedFileId)->willReturn($file);
  1085. $groups = [];
  1086. foreach (range(0, 100) as $i) {
  1087. $groups[] = 'group'.$i;
  1088. }
  1089. $groups[] = 'sharedWith';
  1090. $user = $this->createMock(IUser::class);
  1091. $user->method('getUID')->willReturn('sharedWith');
  1092. $owner = $this->createMock(IUser::class);
  1093. $owner->method('getUID')->willReturn('shareOwner');
  1094. $initiator = $this->createMock(IUser::class);
  1095. $initiator->method('getUID')->willReturn('sharedBy');
  1096. $this->userManager->method('get')->willReturnMap([
  1097. ['sharedWith', $user],
  1098. ['shareOwner', $owner],
  1099. ['sharedBy', $initiator],
  1100. ]);
  1101. $this->groupManager
  1102. ->method('getUserGroupIds')
  1103. ->willReturnCallback(fn (IUser $user) => ($user->getUID() === 'sharedWith' ? $groups : []));
  1104. $share = $this->provider->getSharedWith('sharedWith', $shareType, null, 1, 0);
  1105. $this->assertCount(0, $share);
  1106. }
  1107. public function testGetSharesBy() {
  1108. $qb = $this->dbConn->getQueryBuilder();
  1109. $qb->insert('share')
  1110. ->values([
  1111. 'share_type' => $qb->expr()->literal(IShare::TYPE_USER),
  1112. 'share_with' => $qb->expr()->literal('sharedWith'),
  1113. 'uid_owner' => $qb->expr()->literal('shareOwner'),
  1114. 'uid_initiator' => $qb->expr()->literal('sharedBy'),
  1115. 'item_type' => $qb->expr()->literal('file'),
  1116. 'file_source' => $qb->expr()->literal(42),
  1117. 'file_target' => $qb->expr()->literal('myTarget'),
  1118. 'permissions' => $qb->expr()->literal(13),
  1119. ]);
  1120. $this->assertEquals(1, $qb->execute());
  1121. $id = $qb->getLastInsertId();
  1122. $qb = $this->dbConn->getQueryBuilder();
  1123. $qb->insert('share')
  1124. ->values([
  1125. 'share_type' => $qb->expr()->literal(IShare::TYPE_USER),
  1126. 'share_with' => $qb->expr()->literal('sharedWith'),
  1127. 'uid_owner' => $qb->expr()->literal('shareOwner'),
  1128. 'uid_initiator' => $qb->expr()->literal('sharedBy2'),
  1129. 'item_type' => $qb->expr()->literal('file'),
  1130. 'file_source' => $qb->expr()->literal(42),
  1131. 'file_target' => $qb->expr()->literal('userTarget'),
  1132. 'permissions' => $qb->expr()->literal(0),
  1133. 'parent' => $qb->expr()->literal($id),
  1134. ]);
  1135. $this->assertEquals(1, $qb->execute());
  1136. $file = $this->createMock(File::class);
  1137. $this->rootFolder->method('getUserFolder')->with('shareOwner')->willReturnSelf();
  1138. $this->rootFolder->method('getFirstNodeById')->with(42)->willReturn($file);
  1139. $share = $this->provider->getSharesBy('sharedBy', IShare::TYPE_USER, null, false, 1, 0);
  1140. $this->assertCount(1, $share);
  1141. /** @var IShare $share */
  1142. $share = $share[0];
  1143. $this->assertEquals($id, $share->getId());
  1144. $this->assertEquals('sharedWith', $share->getSharedWith());
  1145. $this->assertEquals('shareOwner', $share->getShareOwner());
  1146. $this->assertEquals('sharedBy', $share->getSharedBy());
  1147. $this->assertEquals(IShare::TYPE_USER, $share->getShareType());
  1148. $this->assertEquals(13, $share->getPermissions());
  1149. $this->assertEquals('myTarget', $share->getTarget());
  1150. }
  1151. public function testGetSharesNode() {
  1152. $qb = $this->dbConn->getQueryBuilder();
  1153. $qb->insert('share')
  1154. ->values([
  1155. 'share_type' => $qb->expr()->literal(IShare::TYPE_USER),
  1156. 'share_with' => $qb->expr()->literal('sharedWith'),
  1157. 'uid_owner' => $qb->expr()->literal('shareOwner'),
  1158. 'uid_initiator' => $qb->expr()->literal('sharedBy'),
  1159. 'item_type' => $qb->expr()->literal('file'),
  1160. 'file_source' => $qb->expr()->literal(42),
  1161. 'file_target' => $qb->expr()->literal('myTarget'),
  1162. 'permissions' => $qb->expr()->literal(13),
  1163. ]);
  1164. $this->assertEquals(1, $qb->execute());
  1165. $id = $qb->getLastInsertId();
  1166. $qb = $this->dbConn->getQueryBuilder();
  1167. $qb->insert('share')
  1168. ->values([
  1169. 'share_type' => $qb->expr()->literal(IShare::TYPE_USER),
  1170. 'share_with' => $qb->expr()->literal('sharedWith'),
  1171. 'uid_owner' => $qb->expr()->literal('shareOwner'),
  1172. 'uid_initiator' => $qb->expr()->literal('sharedBy'),
  1173. 'item_type' => $qb->expr()->literal('file'),
  1174. 'file_source' => $qb->expr()->literal(43),
  1175. 'file_target' => $qb->expr()->literal('userTarget'),
  1176. 'permissions' => $qb->expr()->literal(0),
  1177. 'parent' => $qb->expr()->literal($id),
  1178. ]);
  1179. $this->assertEquals(1, $qb->execute());
  1180. $file = $this->createMock(File::class);
  1181. $file->method('getId')->willReturn(42);
  1182. $this->rootFolder->method('getUserFolder')->with('shareOwner')->willReturnSelf();
  1183. $this->rootFolder->method('getFirstNodeById')->with(42)->willReturn($file);
  1184. $share = $this->provider->getSharesBy('sharedBy', IShare::TYPE_USER, $file, false, 1, 0);
  1185. $this->assertCount(1, $share);
  1186. /** @var IShare $share */
  1187. $share = $share[0];
  1188. $this->assertEquals($id, $share->getId());
  1189. $this->assertEquals('sharedWith', $share->getSharedWith());
  1190. $this->assertEquals('shareOwner', $share->getShareOwner());
  1191. $this->assertEquals('sharedBy', $share->getSharedBy());
  1192. $this->assertEquals(IShare::TYPE_USER, $share->getShareType());
  1193. $this->assertEquals(13, $share->getPermissions());
  1194. $this->assertEquals('myTarget', $share->getTarget());
  1195. }
  1196. public function testGetSharesReshare() {
  1197. $qb = $this->dbConn->getQueryBuilder();
  1198. $qb->insert('share')
  1199. ->values([
  1200. 'share_type' => $qb->expr()->literal(IShare::TYPE_USER),
  1201. 'share_with' => $qb->expr()->literal('sharedWith'),
  1202. 'uid_owner' => $qb->expr()->literal('shareOwner'),
  1203. 'uid_initiator' => $qb->expr()->literal('shareOwner'),
  1204. 'item_type' => $qb->expr()->literal('file'),
  1205. 'file_source' => $qb->expr()->literal(42),
  1206. 'file_target' => $qb->expr()->literal('myTarget'),
  1207. 'permissions' => $qb->expr()->literal(13),
  1208. ]);
  1209. $this->assertEquals(1, $qb->execute());
  1210. $id1 = $qb->getLastInsertId();
  1211. $qb = $this->dbConn->getQueryBuilder();
  1212. $qb->insert('share')
  1213. ->values([
  1214. 'share_type' => $qb->expr()->literal(IShare::TYPE_USER),
  1215. 'share_with' => $qb->expr()->literal('sharedWith'),
  1216. 'uid_owner' => $qb->expr()->literal('shareOwner'),
  1217. 'uid_initiator' => $qb->expr()->literal('sharedBy'),
  1218. 'item_type' => $qb->expr()->literal('file'),
  1219. 'file_source' => $qb->expr()->literal(42),
  1220. 'file_target' => $qb->expr()->literal('userTarget'),
  1221. 'permissions' => $qb->expr()->literal(0),
  1222. ]);
  1223. $this->assertEquals(1, $qb->execute());
  1224. $id2 = $qb->getLastInsertId();
  1225. $file = $this->createMock(File::class);
  1226. $file->method('getId')->willReturn(42);
  1227. $this->rootFolder->method('getUserFolder')->with('shareOwner')->willReturnSelf();
  1228. $this->rootFolder->method('getFirstNodeById')->with(42)->willReturn($file);
  1229. $shares = $this->provider->getSharesBy('shareOwner', IShare::TYPE_USER, null, true, -1, 0);
  1230. $this->assertCount(2, $shares);
  1231. /** @var IShare $share */
  1232. $share = $shares[0];
  1233. $this->assertEquals($id1, $share->getId());
  1234. $this->assertSame('sharedWith', $share->getSharedWith());
  1235. $this->assertSame('shareOwner', $share->getShareOwner());
  1236. $this->assertSame('shareOwner', $share->getSharedBy());
  1237. $this->assertEquals(IShare::TYPE_USER, $share->getShareType());
  1238. $this->assertEquals(13, $share->getPermissions());
  1239. $this->assertEquals('myTarget', $share->getTarget());
  1240. $share = $shares[1];
  1241. $this->assertEquals($id2, $share->getId());
  1242. $this->assertSame('sharedWith', $share->getSharedWith());
  1243. $this->assertSame('shareOwner', $share->getShareOwner());
  1244. $this->assertSame('sharedBy', $share->getSharedBy());
  1245. $this->assertEquals(IShare::TYPE_USER, $share->getShareType());
  1246. $this->assertEquals(0, $share->getPermissions());
  1247. $this->assertEquals('userTarget', $share->getTarget());
  1248. }
  1249. public function testDeleteFromSelfGroupNoCustomShare() {
  1250. $qb = $this->dbConn->getQueryBuilder();
  1251. $stmt = $qb->insert('share')
  1252. ->values([
  1253. 'share_type' => $qb->expr()->literal(IShare::TYPE_GROUP),
  1254. 'share_with' => $qb->expr()->literal('group'),
  1255. 'uid_owner' => $qb->expr()->literal('user1'),
  1256. 'uid_initiator' => $qb->expr()->literal('user1'),
  1257. 'item_type' => $qb->expr()->literal('file'),
  1258. 'file_source' => $qb->expr()->literal(1),
  1259. 'file_target' => $qb->expr()->literal('myTarget1'),
  1260. 'permissions' => $qb->expr()->literal(2)
  1261. ])->execute();
  1262. $this->assertEquals(1, $stmt);
  1263. $id = $qb->getLastInsertId();
  1264. $user1 = $this->createMock(IUser::class);
  1265. $user1->method('getUID')->willReturn('user1');
  1266. $user2 = $this->createMock(IUser::class);
  1267. $user2->method('getUID')->willReturn('user2');
  1268. $this->userManager->method('get')->willReturnMap([
  1269. ['user1', $user1],
  1270. ['user2', $user2],
  1271. ]);
  1272. $group = $this->createMock(IGroup::class);
  1273. $group->method('getGID')->willReturn('group');
  1274. $group->method('inGroup')->with($user2)->willReturn(true);
  1275. $group->method('getDisplayName')->willReturn('group-displayname');
  1276. $this->groupManager->method('get')->with('group')->willReturn($group);
  1277. $file = $this->createMock(File::class);
  1278. $file->method('getId')->willReturn(1);
  1279. $this->rootFolder->method('getUserFolder')->with('user1')->willReturnSelf();
  1280. $this->rootFolder->method('getFirstNodeById')->with(1)->willReturn($file);
  1281. $share = $this->provider->getShareById($id);
  1282. $this->provider->deleteFromSelf($share, 'user2');
  1283. $qb = $this->dbConn->getQueryBuilder();
  1284. $stmt = $qb->select('*')
  1285. ->from('share')
  1286. ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(2)))
  1287. ->execute();
  1288. $shares = $stmt->fetchAll();
  1289. $stmt->closeCursor();
  1290. $this->assertCount(1, $shares);
  1291. $share2 = $shares[0];
  1292. $this->assertEquals($id, $share2['parent']);
  1293. $this->assertEquals(0, $share2['permissions']);
  1294. $this->assertEquals('user2', $share2['share_with']);
  1295. }
  1296. public function testDeleteFromSelfGroupAlreadyCustomShare() {
  1297. $qb = $this->dbConn->getQueryBuilder();
  1298. $stmt = $qb->insert('share')
  1299. ->values([
  1300. 'share_type' => $qb->expr()->literal(IShare::TYPE_GROUP),
  1301. 'share_with' => $qb->expr()->literal('group'),
  1302. 'uid_owner' => $qb->expr()->literal('user1'),
  1303. 'uid_initiator' => $qb->expr()->literal('user1'),
  1304. 'item_type' => $qb->expr()->literal('file'),
  1305. 'file_source' => $qb->expr()->literal(1),
  1306. 'file_target' => $qb->expr()->literal('myTarget1'),
  1307. 'permissions' => $qb->expr()->literal(2)
  1308. ])->execute();
  1309. $this->assertEquals(1, $stmt);
  1310. $id = $qb->getLastInsertId();
  1311. $qb = $this->dbConn->getQueryBuilder();
  1312. $stmt = $qb->insert('share')
  1313. ->values([
  1314. 'share_type' => $qb->expr()->literal(2),
  1315. 'share_with' => $qb->expr()->literal('user2'),
  1316. 'uid_owner' => $qb->expr()->literal('user1'),
  1317. 'uid_initiator' => $qb->expr()->literal('user1'),
  1318. 'item_type' => $qb->expr()->literal('file'),
  1319. 'file_source' => $qb->expr()->literal(1),
  1320. 'file_target' => $qb->expr()->literal('myTarget1'),
  1321. 'permissions' => $qb->expr()->literal(2),
  1322. 'parent' => $qb->expr()->literal($id),
  1323. ])->execute();
  1324. $this->assertEquals(1, $stmt);
  1325. $user1 = $this->createMock(IUser::class);
  1326. $user1->method('getUID')->willReturn('user1');
  1327. $user2 = $this->createMock(IUser::class);
  1328. $user2->method('getUID')->willReturn('user2');
  1329. $this->userManager->method('get')->willReturnMap([
  1330. ['user1', $user1],
  1331. ['user2', $user2],
  1332. ]);
  1333. $group = $this->createMock(IGroup::class);
  1334. $group->method('getGID')->willReturn('group');
  1335. $group->method('inGroup')->with($user2)->willReturn(true);
  1336. $group->method('getDisplayName')->willReturn('group-displayname');
  1337. $this->groupManager->method('get')->with('group')->willReturn($group);
  1338. $file = $this->createMock(File::class);
  1339. $file->method('getId')->willReturn(1);
  1340. $this->rootFolder->method('getUserFolder')->with('user1')->willReturnSelf();
  1341. $this->rootFolder->method('getFirstNodeById')->with(1)->willReturn($file);
  1342. $share = $this->provider->getShareById($id);
  1343. $this->provider->deleteFromSelf($share, 'user2');
  1344. $qb = $this->dbConn->getQueryBuilder();
  1345. $stmt = $qb->select('*')
  1346. ->from('share')
  1347. ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(2)))
  1348. ->execute();
  1349. $shares = $stmt->fetchAll();
  1350. $stmt->closeCursor();
  1351. $this->assertCount(1, $shares);
  1352. $share2 = $shares[0];
  1353. $this->assertEquals($id, $share2['parent']);
  1354. $this->assertEquals(0, $share2['permissions']);
  1355. $this->assertEquals('user2', $share2['share_with']);
  1356. }
  1357. public function testDeleteFromSelfGroupUserNotInGroup() {
  1358. $qb = $this->dbConn->getQueryBuilder();
  1359. $stmt = $qb->insert('share')
  1360. ->values([
  1361. 'share_type' => $qb->expr()->literal(IShare::TYPE_GROUP),
  1362. 'share_with' => $qb->expr()->literal('group'),
  1363. 'uid_owner' => $qb->expr()->literal('user1'),
  1364. 'uid_initiator' => $qb->expr()->literal('user1'),
  1365. 'item_type' => $qb->expr()->literal('file'),
  1366. 'file_source' => $qb->expr()->literal(1),
  1367. 'file_target' => $qb->expr()->literal('myTarget1'),
  1368. 'permissions' => $qb->expr()->literal(2)
  1369. ])->execute();
  1370. $this->assertEquals(1, $stmt);
  1371. $id = $qb->getLastInsertId();
  1372. $user1 = $this->createMock(IUser::class);
  1373. $user1->method('getUID')->willReturn('user1');
  1374. $user2 = $this->createMock(IUser::class);
  1375. $user2->method('getUID')->willReturn('user2');
  1376. $this->userManager->method('get')->willReturnMap([
  1377. ['user1', $user1],
  1378. ['user2', $user2],
  1379. ]);
  1380. $group = $this->createMock(IGroup::class);
  1381. $group->method('getGID')->willReturn('group');
  1382. $group->method('inGroup')->with($user2)->willReturn(false);
  1383. $group->method('getDisplayName')->willReturn('group-displayname');
  1384. $this->groupManager->method('get')->with('group')->willReturn($group);
  1385. $file = $this->createMock(File::class);
  1386. $file->method('getId')->willReturn(1);
  1387. $this->rootFolder->method('getUserFolder')->with('user1')->willReturnSelf();
  1388. $this->rootFolder->method('getFirstNodeById')->with(1)->willReturn($file);
  1389. $share = $this->provider->getShareById($id);
  1390. $this->provider->deleteFromSelf($share, 'user2');
  1391. }
  1392. public function testDeleteFromSelfGroupDoesNotExist() {
  1393. $this->expectException(\OC\Share20\Exception\ProviderException::class);
  1394. $this->expectExceptionMessage('Group "group" does not exist');
  1395. $qb = $this->dbConn->getQueryBuilder();
  1396. $stmt = $qb->insert('share')
  1397. ->values([
  1398. 'share_type' => $qb->expr()->literal(IShare::TYPE_GROUP),
  1399. 'share_with' => $qb->expr()->literal('group'),
  1400. 'uid_owner' => $qb->expr()->literal('user1'),
  1401. 'uid_initiator' => $qb->expr()->literal('user1'),
  1402. 'item_type' => $qb->expr()->literal('file'),
  1403. 'file_source' => $qb->expr()->literal(1),
  1404. 'file_target' => $qb->expr()->literal('myTarget1'),
  1405. 'permissions' => $qb->expr()->literal(2)
  1406. ])->execute();
  1407. $this->assertEquals(1, $stmt);
  1408. $id = $qb->getLastInsertId();
  1409. $user1 = $this->createMock(IUser::class);
  1410. $user1->method('getUID')->willReturn('user1');
  1411. $user2 = $this->createMock(IUser::class);
  1412. $user2->method('getUID')->willReturn('user2');
  1413. $this->userManager->method('get')->willReturnMap([
  1414. ['user1', $user1],
  1415. ['user2', $user2],
  1416. ]);
  1417. $this->groupManager->method('get')->with('group')->willReturn(null);
  1418. $file = $this->createMock(File::class);
  1419. $file->method('getId')->willReturn(1);
  1420. $this->rootFolder->method('getUserFolder')->with('user1')->willReturnSelf();
  1421. $this->rootFolder->method('getFirstNodeById')->with(1)->willReturn($file);
  1422. $share = $this->provider->getShareById($id);
  1423. $this->provider->deleteFromSelf($share, 'user2');
  1424. }
  1425. public function testDeleteFromSelfUser() {
  1426. $qb = $this->dbConn->getQueryBuilder();
  1427. $stmt = $qb->insert('share')
  1428. ->values([
  1429. 'share_type' => $qb->expr()->literal(IShare::TYPE_USER),
  1430. 'share_with' => $qb->expr()->literal('user2'),
  1431. 'uid_owner' => $qb->expr()->literal('user1'),
  1432. 'uid_initiator' => $qb->expr()->literal('user1'),
  1433. 'item_type' => $qb->expr()->literal('file'),
  1434. 'file_source' => $qb->expr()->literal(1),
  1435. 'file_target' => $qb->expr()->literal('myTarget1'),
  1436. 'permissions' => $qb->expr()->literal(2)
  1437. ])->execute();
  1438. $this->assertEquals(1, $stmt);
  1439. $id = $qb->getLastInsertId();
  1440. $user1 = $this->createMock(IUser::class);
  1441. $user1->method('getUID')->willReturn('user1');
  1442. $user1->method('getDisplayName')->willReturn('user1');
  1443. $user2 = $this->createMock(IUser::class);
  1444. $user2->method('getUID')->willReturn('user2');
  1445. $user2->method('getDisplayName')->willReturn('user2');
  1446. $this->userManager->method('get')->willReturnMap([
  1447. ['user1', $user1],
  1448. ['user2', $user2],
  1449. ]);
  1450. $file = $this->createMock(File::class);
  1451. $file->method('getId')->willReturn(1);
  1452. $this->rootFolder->method('getUserFolder')->with('user1')->willReturnSelf();
  1453. $this->rootFolder->method('getFirstNodeById')->with(1)->willReturn($file);
  1454. $share = $this->provider->getShareById($id);
  1455. $this->provider->deleteFromSelf($share, 'user2');
  1456. $qb = $this->dbConn->getQueryBuilder();
  1457. $stmt = $qb->select('*')
  1458. ->from('share')
  1459. ->where($qb->expr()->eq('id', $qb->createNamedParameter($id)))
  1460. ->execute();
  1461. $shares = $stmt->fetchAll();
  1462. $stmt->closeCursor();
  1463. $this->assertCount(0, $shares);
  1464. }
  1465. public function testDeleteFromSelfUserNotRecipient() {
  1466. $this->expectException(\OC\Share20\Exception\ProviderException::class);
  1467. $this->expectExceptionMessage('Recipient does not match');
  1468. $qb = $this->dbConn->getQueryBuilder();
  1469. $stmt = $qb->insert('share')
  1470. ->values([
  1471. 'share_type' => $qb->expr()->literal(IShare::TYPE_USER),
  1472. 'share_with' => $qb->expr()->literal('user2'),
  1473. 'uid_owner' => $qb->expr()->literal('user1'),
  1474. 'uid_initiator' => $qb->expr()->literal('user1'),
  1475. 'item_type' => $qb->expr()->literal('file'),
  1476. 'file_source' => $qb->expr()->literal(1),
  1477. 'file_target' => $qb->expr()->literal('myTarget1'),
  1478. 'permissions' => $qb->expr()->literal(2)
  1479. ])->execute();
  1480. $this->assertEquals(1, $stmt);
  1481. $id = $qb->getLastInsertId();
  1482. $user1 = $this->createMock(IUser::class);
  1483. $user1->method('getUID')->willReturn('user1');
  1484. $user1->method('getDisplayName')->willReturn('user1');
  1485. $user2 = $this->createMock(IUser::class);
  1486. $user2->method('getUID')->willReturn('user2');
  1487. $user2->method('getDisplayName')->willReturn('user2');
  1488. $user3 = $this->createMock(IUser::class);
  1489. $this->userManager->method('get')->willReturnMap([
  1490. ['user1', $user1],
  1491. ['user2', $user2],
  1492. ]);
  1493. $file = $this->createMock(File::class);
  1494. $file->method('getId')->willReturn(1);
  1495. $this->rootFolder->method('getUserFolder')->with('user1')->willReturnSelf();
  1496. $this->rootFolder->method('getFirstNodeById')->with(1)->willReturn($file);
  1497. $share = $this->provider->getShareById($id);
  1498. $this->provider->deleteFromSelf($share, $user3);
  1499. }
  1500. public function testDeleteFromSelfLink() {
  1501. $this->expectException(\OC\Share20\Exception\ProviderException::class);
  1502. $this->expectExceptionMessage('Invalid shareType');
  1503. $qb = $this->dbConn->getQueryBuilder();
  1504. $stmt = $qb->insert('share')
  1505. ->values([
  1506. 'share_type' => $qb->expr()->literal(IShare::TYPE_LINK),
  1507. 'uid_owner' => $qb->expr()->literal('user1'),
  1508. 'uid_initiator' => $qb->expr()->literal('user1'),
  1509. 'item_type' => $qb->expr()->literal('file'),
  1510. 'file_source' => $qb->expr()->literal(1),
  1511. 'file_target' => $qb->expr()->literal('myTarget1'),
  1512. 'permissions' => $qb->expr()->literal(2),
  1513. 'token' => $qb->expr()->literal('token'),
  1514. ])->execute();
  1515. $this->assertEquals(1, $stmt);
  1516. $id = $qb->getLastInsertId();
  1517. $user1 = $this->createMock(IUser::class);
  1518. $user1->method('getUID')->willReturn('user1');
  1519. $this->userManager->method('get')->willReturnMap([
  1520. ['user1', $user1],
  1521. ]);
  1522. $file = $this->createMock(File::class);
  1523. $file->method('getId')->willReturn(1);
  1524. $this->rootFolder->method('getUserFolder')->with('user1')->willReturnSelf();
  1525. $this->rootFolder->method('getFirstNodeById')->with(1)->willReturn($file);
  1526. $share = $this->provider->getShareById($id);
  1527. $this->provider->deleteFromSelf($share, $user1);
  1528. }
  1529. public function testUpdateUser() {
  1530. $id = $this->addShareToDB(IShare::TYPE_USER, 'user0', 'user1', 'user2',
  1531. 'file', 42, 'target', 31, null, null);
  1532. $users = [];
  1533. for ($i = 0; $i < 6; $i++) {
  1534. $user = $this->createMock(IUser::class);
  1535. $user->method('getUID')->willReturn('user'.$i);
  1536. $user->method('getDisplayName')->willReturn('user' . $i);
  1537. $users['user'.$i] = $user;
  1538. }
  1539. $this->userManager->method('get')->willReturnCallback(
  1540. function ($userId) use ($users) {
  1541. return $users[$userId];
  1542. }
  1543. );
  1544. $file1 = $this->createMock(File::class);
  1545. $file1->method('getId')->willReturn(42);
  1546. $file2 = $this->createMock(File::class);
  1547. $file2->method('getId')->willReturn(43);
  1548. $folder1 = $this->createMock(Folder::class);
  1549. $folder1->method('getFirstNodeById')->with(42)->willReturn($file1);
  1550. $folder2 = $this->createMock(Folder::class);
  1551. $folder2->method('getFirstNodeById')->with(43)->willReturn($file2);
  1552. $this->rootFolder->method('getUserFolder')->willReturnMap([
  1553. ['user2', $folder1],
  1554. ['user5', $folder2],
  1555. ]);
  1556. $share = $this->provider->getShareById($id);
  1557. $share->setSharedWith('user3');
  1558. $share->setSharedBy('user4');
  1559. $share->setShareOwner('user5');
  1560. $share->setNode($file2);
  1561. $share->setPermissions(1);
  1562. $share2 = $this->provider->update($share);
  1563. $this->assertEquals($id, $share2->getId());
  1564. $this->assertSame('user3', $share2->getSharedWith());
  1565. $this->assertSame('user4', $share2->getSharedBy());
  1566. $this->assertSame('user5', $share2->getShareOwner());
  1567. $this->assertSame(1, $share2->getPermissions());
  1568. $share2 = $this->provider->getShareById($id);
  1569. $this->assertEquals($id, $share2->getId());
  1570. $this->assertSame('user3', $share2->getSharedWith());
  1571. $this->assertSame('user4', $share2->getSharedBy());
  1572. $this->assertSame('user5', $share2->getShareOwner());
  1573. $this->assertSame(1, $share2->getPermissions());
  1574. }
  1575. public function testUpdateLink() {
  1576. $id = $this->addShareToDB(IShare::TYPE_LINK, null, 'user1', 'user2',
  1577. 'file', 42, 'target', 31, null, null);
  1578. $users = [];
  1579. for ($i = 0; $i < 6; $i++) {
  1580. $user = $this->createMock(IUser::class);
  1581. $user->method('getUID')->willReturn('user'.$i);
  1582. $users['user'.$i] = $user;
  1583. }
  1584. $this->userManager->method('get')->willReturnCallback(
  1585. function ($userId) use ($users) {
  1586. return $users[$userId];
  1587. }
  1588. );
  1589. $file1 = $this->createMock(File::class);
  1590. $file1->method('getId')->willReturn(42);
  1591. $file2 = $this->createMock(File::class);
  1592. $file2->method('getId')->willReturn(43);
  1593. $folder1 = $this->createMock(Folder::class);
  1594. $folder1->method('getFirstNodeById')->with(42)->willReturn($file1);
  1595. $folder2 = $this->createMock(Folder::class);
  1596. $folder2->method('getFirstNodeById')->with(43)->willReturn($file2);
  1597. $this->rootFolder->method('getUserFolder')->willReturnMap([
  1598. ['user2', $folder1],
  1599. ['user5', $folder2],
  1600. ]);
  1601. $share = $this->provider->getShareById($id);
  1602. $share->setPassword('password');
  1603. $share->setSendPasswordByTalk(true);
  1604. $share->setSharedBy('user4');
  1605. $share->setShareOwner('user5');
  1606. $share->setNode($file2);
  1607. $share->setPermissions(1);
  1608. $share2 = $this->provider->update($share);
  1609. $this->assertEquals($id, $share2->getId());
  1610. $this->assertEquals('password', $share2->getPassword());
  1611. $this->assertSame(true, $share2->getSendPasswordByTalk());
  1612. $this->assertSame('user4', $share2->getSharedBy());
  1613. $this->assertSame('user5', $share2->getShareOwner());
  1614. $this->assertSame(1, $share2->getPermissions());
  1615. $share2 = $this->provider->getShareById($id);
  1616. $this->assertEquals($id, $share2->getId());
  1617. $this->assertEquals('password', $share2->getPassword());
  1618. $this->assertSame(true, $share2->getSendPasswordByTalk());
  1619. $this->assertSame('user4', $share2->getSharedBy());
  1620. $this->assertSame('user5', $share2->getShareOwner());
  1621. $this->assertSame(1, $share2->getPermissions());
  1622. }
  1623. public function testUpdateLinkRemovePassword() {
  1624. $id = $this->addShareToDB(IShare::TYPE_LINK, 'foo', 'user1', 'user2',
  1625. 'file', 42, 'target', 31, null, null);
  1626. $qb = $this->dbConn->getQueryBuilder();
  1627. $qb->update('share');
  1628. $qb->where($qb->expr()->eq('id', $qb->createNamedParameter($id)));
  1629. $qb->set('password', $qb->createNamedParameter('password'));
  1630. $this->assertEquals(1, $qb->execute());
  1631. $users = [];
  1632. for ($i = 0; $i < 6; $i++) {
  1633. $user = $this->createMock(IUser::class);
  1634. $user->method('getUID')->willReturn('user'.$i);
  1635. $users['user'.$i] = $user;
  1636. }
  1637. $this->userManager->method('get')->willReturnCallback(
  1638. function ($userId) use ($users) {
  1639. return $users[$userId];
  1640. }
  1641. );
  1642. $file1 = $this->createMock(File::class);
  1643. $file1->method('getId')->willReturn(42);
  1644. $file2 = $this->createMock(File::class);
  1645. $file2->method('getId')->willReturn(43);
  1646. $folder1 = $this->createMock(Folder::class);
  1647. $folder1->method('getFirstNodeById')->with(42)->willReturn($file1);
  1648. $folder2 = $this->createMock(Folder::class);
  1649. $folder2->method('getFirstNodeById')->with(43)->willReturn($file2);
  1650. $this->rootFolder->method('getUserFolder')->willReturnMap([
  1651. ['user2', $folder1],
  1652. ['user5', $folder2],
  1653. ]);
  1654. $share = $this->provider->getShareById($id);
  1655. $share->setPassword(null);
  1656. $share->setSharedBy('user4');
  1657. $share->setShareOwner('user5');
  1658. $share->setNode($file2);
  1659. $share->setPermissions(1);
  1660. $share2 = $this->provider->update($share);
  1661. $this->assertEquals($id, $share2->getId());
  1662. $this->assertEquals(null, $share2->getPassword());
  1663. $this->assertSame('user4', $share2->getSharedBy());
  1664. $this->assertSame('user5', $share2->getShareOwner());
  1665. $this->assertSame(1, $share2->getPermissions());
  1666. $share2 = $this->provider->getShareById($id);
  1667. $this->assertEquals($id, $share2->getId());
  1668. $this->assertEquals(null, $share2->getPassword());
  1669. $this->assertSame('user4', $share2->getSharedBy());
  1670. $this->assertSame('user5', $share2->getShareOwner());
  1671. $this->assertSame(1, $share2->getPermissions());
  1672. }
  1673. public function testUpdateGroupNoSub() {
  1674. $id = $this->addShareToDB(IShare::TYPE_GROUP, 'group0', 'user1', 'user2',
  1675. 'file', 42, 'target', 31, null, null);
  1676. $users = [];
  1677. for ($i = 0; $i < 6; $i++) {
  1678. $user = $this->createMock(IUser::class);
  1679. $user->method('getUID')->willReturn('user'.$i);
  1680. $users['user'.$i] = $user;
  1681. }
  1682. $this->userManager->method('get')->willReturnCallback(
  1683. function ($userId) use ($users) {
  1684. return $users[$userId];
  1685. }
  1686. );
  1687. $groups = [];
  1688. for ($i = 0; $i < 2; $i++) {
  1689. $group = $this->createMock(IGroup::class);
  1690. $group->method('getGID')->willReturn('group'.$i);
  1691. $group->method('getDisplayName')->willReturn('group-displayname' . $i);
  1692. $groups['group'.$i] = $group;
  1693. }
  1694. $this->groupManager->method('get')->willReturnCallback(
  1695. function ($groupId) use ($groups) {
  1696. return $groups[$groupId];
  1697. }
  1698. );
  1699. $file1 = $this->createMock(File::class);
  1700. $file1->method('getId')->willReturn(42);
  1701. $file2 = $this->createMock(File::class);
  1702. $file2->method('getId')->willReturn(43);
  1703. $folder1 = $this->createMock(Folder::class);
  1704. $folder1->method('getFirstNodeById')->with(42)->willReturn($file1);
  1705. $folder2 = $this->createMock(Folder::class);
  1706. $folder2->method('getFirstNodeById')->with(43)->willReturn($file2);
  1707. $this->rootFolder->method('getUserFolder')->willReturnMap([
  1708. ['user2', $folder1],
  1709. ['user5', $folder2],
  1710. ]);
  1711. $share = $this->provider->getShareById($id);
  1712. $share->setSharedWith('group0');
  1713. $share->setSharedBy('user4');
  1714. $share->setShareOwner('user5');
  1715. $share->setNode($file2);
  1716. $share->setPermissions(1);
  1717. $share2 = $this->provider->update($share);
  1718. $this->assertEquals($id, $share2->getId());
  1719. // Group shares do not allow updating the recipient
  1720. $this->assertSame('group0', $share2->getSharedWith());
  1721. $this->assertSame('user4', $share2->getSharedBy());
  1722. $this->assertSame('user5', $share2->getShareOwner());
  1723. $this->assertSame(1, $share2->getPermissions());
  1724. $share2 = $this->provider->getShareById($id);
  1725. $this->assertEquals($id, $share2->getId());
  1726. // Group shares do not allow updating the recipient
  1727. $this->assertSame('group0', $share2->getSharedWith());
  1728. $this->assertSame('user4', $share2->getSharedBy());
  1729. $this->assertSame('user5', $share2->getShareOwner());
  1730. $this->assertSame(1, $share2->getPermissions());
  1731. }
  1732. public function testUpdateGroupSubShares() {
  1733. $id = $this->addShareToDB(IShare::TYPE_GROUP, 'group0', 'user1', 'user2',
  1734. 'file', 42, 'target', 31, null, null);
  1735. $id2 = $this->addShareToDB(2, 'user0', 'user1', 'user2',
  1736. 'file', 42, 'mytarget', 31, null, null, $id);
  1737. $id3 = $this->addShareToDB(2, 'user3', 'user1', 'user2',
  1738. 'file', 42, 'mytarget2', 0, null, null, $id);
  1739. $users = [];
  1740. for ($i = 0; $i < 6; $i++) {
  1741. $user = $this->createMock(IUser::class);
  1742. $user->method('getUID')->willReturn('user'.$i);
  1743. $users['user'.$i] = $user;
  1744. }
  1745. $this->userManager->method('get')->willReturnCallback(
  1746. function ($userId) use ($users) {
  1747. return $users[$userId];
  1748. }
  1749. );
  1750. $groups = [];
  1751. for ($i = 0; $i < 2; $i++) {
  1752. $group = $this->createMock(IGroup::class);
  1753. $group->method('getGID')->willReturn('group'.$i);
  1754. $group->method('getDisplayName')->willReturn('group-displayname'.$i);
  1755. $groups['group'.$i] = $group;
  1756. }
  1757. $this->groupManager->method('get')->willReturnCallback(
  1758. function ($groupId) use ($groups) {
  1759. return $groups[$groupId];
  1760. }
  1761. );
  1762. $file1 = $this->createMock(File::class);
  1763. $file1->method('getId')->willReturn(42);
  1764. $file2 = $this->createMock(File::class);
  1765. $file2->method('getId')->willReturn(43);
  1766. $folder1 = $this->createMock(Folder::class);
  1767. $folder1->method('getFirstNodeById')->with(42)->willReturn($file1);
  1768. $folder2 = $this->createMock(Folder::class);
  1769. $folder2->method('getFirstNodeById')->with(43)->willReturn($file2);
  1770. $this->rootFolder->method('getUserFolder')->willReturnMap([
  1771. ['user2', $folder1],
  1772. ['user5', $folder2],
  1773. ]);
  1774. $share = $this->provider->getShareById($id);
  1775. $share->setSharedWith('group0');
  1776. $share->setSharedBy('user4');
  1777. $share->setShareOwner('user5');
  1778. $share->setNode($file2);
  1779. $share->setPermissions(1);
  1780. $share2 = $this->provider->update($share);
  1781. $this->assertEquals($id, $share2->getId());
  1782. // Group shares do not allow updating the recipient
  1783. $this->assertSame('group0', $share2->getSharedWith());
  1784. $this->assertSame('user4', $share2->getSharedBy());
  1785. $this->assertSame('user5', $share2->getShareOwner());
  1786. $this->assertSame(1, $share2->getPermissions());
  1787. $share2 = $this->provider->getShareById($id);
  1788. $this->assertEquals($id, $share2->getId());
  1789. // Group shares do not allow updating the recipient
  1790. $this->assertSame('group0', $share2->getSharedWith());
  1791. $this->assertSame('user4', $share2->getSharedBy());
  1792. $this->assertSame('user5', $share2->getShareOwner());
  1793. $this->assertSame(1, $share2->getPermissions());
  1794. $qb = $this->dbConn->getQueryBuilder();
  1795. $stmt = $qb->select('*')
  1796. ->from('share')
  1797. ->where($qb->expr()->eq('parent', $qb->createNamedParameter($id)))
  1798. ->orderBy('id')
  1799. ->execute();
  1800. $shares = $stmt->fetchAll();
  1801. $this->assertSame('user0', $shares[0]['share_with']);
  1802. $this->assertSame('user4', $shares[0]['uid_initiator']);
  1803. $this->assertSame('user5', $shares[0]['uid_owner']);
  1804. $this->assertSame(1, (int)$shares[0]['permissions']);
  1805. $this->assertSame('user3', $shares[1]['share_with']);
  1806. $this->assertSame('user4', $shares[1]['uid_initiator']);
  1807. $this->assertSame('user5', $shares[1]['uid_owner']);
  1808. $this->assertSame(0, (int)$shares[1]['permissions']);
  1809. $stmt->closeCursor();
  1810. }
  1811. public function testMoveUserShare() {
  1812. $id = $this->addShareToDB(IShare::TYPE_USER, 'user0', 'user1', 'user1', 'file',
  1813. 42, 'mytaret', 31, null, null);
  1814. $user0 = $this->createMock(IUser::class);
  1815. $user0->method('getUID')->willReturn('user0');
  1816. $user0->method('getDisplayName')->willReturn('user0');
  1817. $user1 = $this->createMock(IUser::class);
  1818. $user1->method('getUID')->willReturn('user1');
  1819. $user1->method('getDisplayName')->willReturn('user1');
  1820. $this->userManager->method('get')->willReturnMap([
  1821. ['user0', $user0],
  1822. ['user1', $user1],
  1823. ]);
  1824. $file = $this->createMock(File::class);
  1825. $file->method('getId')->willReturn(42);
  1826. $this->rootFolder->method('getUserFolder')->with('user1')->willReturnSelf();
  1827. $this->rootFolder->method('getFirstNodeById')->willReturn($file);
  1828. $share = $this->provider->getShareById($id, null);
  1829. $share->setTarget('/newTarget');
  1830. $this->provider->move($share, $user0);
  1831. $share = $this->provider->getShareById($id, null);
  1832. $this->assertSame('/newTarget', $share->getTarget());
  1833. }
  1834. public function testMoveGroupShare() {
  1835. $id = $this->addShareToDB(IShare::TYPE_GROUP, 'group0', 'user1', 'user1', 'file',
  1836. 42, 'mytaret', 31, null, null);
  1837. $user0 = $this->createMock(IUser::class);
  1838. $user0->method('getUID')->willReturn('user0');
  1839. $user1 = $this->createMock(IUser::class);
  1840. $user1->method('getUID')->willReturn('user1');
  1841. $group0 = $this->createMock(IGroup::class);
  1842. $group0->method('getGID')->willReturn('group0');
  1843. $group0->method('inGroup')->with($user0)->willReturn(true);
  1844. $group0->method('getDisplayName')->willReturn('group0-displayname');
  1845. $this->groupManager->method('get')->with('group0')->willReturn($group0);
  1846. $this->userManager->method('get')->willReturnMap([
  1847. ['user0', $user0],
  1848. ['user1', $user1],
  1849. ]);
  1850. $folder = $this->createMock(Folder::class);
  1851. $folder->method('getId')->willReturn(42);
  1852. $this->rootFolder->method('getUserFolder')->with('user1')->willReturnSelf();
  1853. $this->rootFolder->method('getFirstNodeById')->willReturn($folder);
  1854. $share = $this->provider->getShareById($id, 'user0');
  1855. $share->setTarget('/newTarget');
  1856. $this->provider->move($share, 'user0');
  1857. $share = $this->provider->getShareById($id, 'user0');
  1858. $this->assertSame('/newTarget', $share->getTarget());
  1859. $share->setTarget('/ultraNewTarget');
  1860. $this->provider->move($share, 'user0');
  1861. $share = $this->provider->getShareById($id, 'user0');
  1862. $this->assertSame('/ultraNewTarget', $share->getTarget());
  1863. }
  1864. public function dataDeleteUser() {
  1865. return [
  1866. [IShare::TYPE_USER, 'a', 'b', 'c', 'a', true],
  1867. [IShare::TYPE_USER, 'a', 'b', 'c', 'b', false],
  1868. [IShare::TYPE_USER, 'a', 'b', 'c', 'c', true],
  1869. [IShare::TYPE_USER, 'a', 'b', 'c', 'd', false],
  1870. [IShare::TYPE_GROUP, 'a', 'b', 'c', 'a', true],
  1871. [IShare::TYPE_GROUP, 'a', 'b', 'c', 'b', false],
  1872. // The group c is still valid but user c is deleted so group share stays
  1873. [IShare::TYPE_GROUP, 'a', 'b', 'c', 'c', false],
  1874. [IShare::TYPE_GROUP, 'a', 'b', 'c', 'd', false],
  1875. [IShare::TYPE_LINK, 'a', 'b', 'c', 'a', true],
  1876. // To avoid invisible link shares delete initiated link shares as well (see #22327)
  1877. [IShare::TYPE_LINK, 'a', 'b', 'c', 'b', true],
  1878. [IShare::TYPE_LINK, 'a', 'b', 'c', 'c', false],
  1879. [IShare::TYPE_LINK, 'a', 'b', 'c', 'd', false],
  1880. ];
  1881. }
  1882. /**
  1883. * @dataProvider dataDeleteUser
  1884. *
  1885. * @param int $type The shareType (user/group/link)
  1886. * @param string $owner The owner of the share (uid)
  1887. * @param string $initiator The initiator of the share (uid)
  1888. * @param string $recipient The recipient of the share (uid/gid/pass)
  1889. * @param string $deletedUser The user that is deleted
  1890. * @param bool $rowDeleted Is the row deleted in this setup
  1891. */
  1892. public function testDeleteUser($type, $owner, $initiator, $recipient, $deletedUser, $rowDeleted) {
  1893. $qb = $this->dbConn->getQueryBuilder();
  1894. $qb->insert('share')
  1895. ->setValue('share_type', $qb->createNamedParameter($type))
  1896. ->setValue('uid_owner', $qb->createNamedParameter($owner))
  1897. ->setValue('uid_initiator', $qb->createNamedParameter($initiator))
  1898. ->setValue('share_with', $qb->createNamedParameter($recipient))
  1899. ->setValue('item_type', $qb->createNamedParameter('file'))
  1900. ->setValue('item_source', $qb->createNamedParameter(42))
  1901. ->setValue('file_source', $qb->createNamedParameter(42))
  1902. ->execute();
  1903. $id = $qb->getLastInsertId();
  1904. $this->provider->userDeleted($deletedUser, $type);
  1905. $qb = $this->dbConn->getQueryBuilder();
  1906. $qb->select('*')
  1907. ->from('share')
  1908. ->where(
  1909. $qb->expr()->eq('id', $qb->createNamedParameter($id))
  1910. );
  1911. $cursor = $qb->execute();
  1912. $data = $cursor->fetchAll();
  1913. $cursor->closeCursor();
  1914. $this->assertCount($rowDeleted ? 0 : 1, $data);
  1915. }
  1916. public function dataDeleteUserGroup() {
  1917. return [
  1918. ['a', 'b', 'c', 'a', true, true],
  1919. ['a', 'b', 'c', 'b', false, false],
  1920. ['a', 'b', 'c', 'c', false, true],
  1921. ['a', 'b', 'c', 'd', false, false],
  1922. ];
  1923. }
  1924. /**
  1925. * @dataProvider dataDeleteUserGroup
  1926. *
  1927. * @param string $owner The owner of the share (uid)
  1928. * @param string $initiator The initiator of the share (uid)
  1929. * @param string $recipient The recipient of the usergroup share (uid)
  1930. * @param string $deletedUser The user that is deleted
  1931. * @param bool $groupShareDeleted
  1932. * @param bool $userGroupShareDeleted
  1933. */
  1934. public function testDeleteUserGroup($owner, $initiator, $recipient, $deletedUser, $groupShareDeleted, $userGroupShareDeleted) {
  1935. $qb = $this->dbConn->getQueryBuilder();
  1936. $qb->insert('share')
  1937. ->setValue('share_type', $qb->createNamedParameter(IShare::TYPE_GROUP))
  1938. ->setValue('uid_owner', $qb->createNamedParameter($owner))
  1939. ->setValue('uid_initiator', $qb->createNamedParameter($initiator))
  1940. ->setValue('share_with', $qb->createNamedParameter('group'))
  1941. ->setValue('item_type', $qb->createNamedParameter('file'))
  1942. ->setValue('item_source', $qb->createNamedParameter(42))
  1943. ->setValue('file_source', $qb->createNamedParameter(42))
  1944. ->execute();
  1945. $groupId = $qb->getLastInsertId();
  1946. $qb = $this->dbConn->getQueryBuilder();
  1947. $qb->insert('share')
  1948. ->setValue('share_type', $qb->createNamedParameter(2))
  1949. ->setValue('uid_owner', $qb->createNamedParameter($owner))
  1950. ->setValue('uid_initiator', $qb->createNamedParameter($initiator))
  1951. ->setValue('share_with', $qb->createNamedParameter($recipient))
  1952. ->setValue('item_type', $qb->createNamedParameter('file'))
  1953. ->setValue('item_source', $qb->createNamedParameter(42))
  1954. ->setValue('file_source', $qb->createNamedParameter(42))
  1955. ->execute();
  1956. $userGroupId = $qb->getLastInsertId();
  1957. $this->provider->userDeleted($deletedUser, IShare::TYPE_GROUP);
  1958. $qb = $this->dbConn->getQueryBuilder();
  1959. $qb->select('*')
  1960. ->from('share')
  1961. ->where(
  1962. $qb->expr()->eq('id', $qb->createNamedParameter($userGroupId))
  1963. );
  1964. $cursor = $qb->execute();
  1965. $data = $cursor->fetchAll();
  1966. $cursor->closeCursor();
  1967. $this->assertCount($userGroupShareDeleted ? 0 : 1, $data);
  1968. $qb = $this->dbConn->getQueryBuilder();
  1969. $qb->select('*')
  1970. ->from('share')
  1971. ->where(
  1972. $qb->expr()->eq('id', $qb->createNamedParameter($groupId))
  1973. );
  1974. $cursor = $qb->execute();
  1975. $data = $cursor->fetchAll();
  1976. $cursor->closeCursor();
  1977. $this->assertCount($groupShareDeleted ? 0 : 1, $data);
  1978. }
  1979. public function dataGroupDeleted() {
  1980. return [
  1981. [
  1982. [
  1983. 'type' => IShare::TYPE_USER,
  1984. 'recipient' => 'user',
  1985. 'children' => []
  1986. ], 'group', false
  1987. ],
  1988. [
  1989. [
  1990. 'type' => IShare::TYPE_USER,
  1991. 'recipient' => 'user',
  1992. 'children' => []
  1993. ], 'user', false
  1994. ],
  1995. [
  1996. [
  1997. 'type' => IShare::TYPE_LINK,
  1998. 'recipient' => 'user',
  1999. 'children' => []
  2000. ], 'group', false
  2001. ],
  2002. [
  2003. [
  2004. 'type' => IShare::TYPE_GROUP,
  2005. 'recipient' => 'group1',
  2006. 'children' => [
  2007. 'foo',
  2008. 'bar'
  2009. ]
  2010. ], 'group2', false
  2011. ],
  2012. [
  2013. [
  2014. 'type' => IShare::TYPE_GROUP,
  2015. 'recipient' => 'group1',
  2016. 'children' => [
  2017. 'foo',
  2018. 'bar'
  2019. ]
  2020. ], 'group1', true
  2021. ],
  2022. ];
  2023. }
  2024. /**
  2025. * @dataProvider dataGroupDeleted
  2026. *
  2027. * @param $shares
  2028. * @param $groupToDelete
  2029. * @param $shouldBeDeleted
  2030. */
  2031. public function testGroupDeleted($shares, $groupToDelete, $shouldBeDeleted) {
  2032. $qb = $this->dbConn->getQueryBuilder();
  2033. $qb->insert('share')
  2034. ->setValue('share_type', $qb->createNamedParameter($shares['type']))
  2035. ->setValue('uid_owner', $qb->createNamedParameter('owner'))
  2036. ->setValue('uid_initiator', $qb->createNamedParameter('initiator'))
  2037. ->setValue('share_with', $qb->createNamedParameter($shares['recipient']))
  2038. ->setValue('item_type', $qb->createNamedParameter('file'))
  2039. ->setValue('item_source', $qb->createNamedParameter(42))
  2040. ->setValue('file_source', $qb->createNamedParameter(42))
  2041. ->execute();
  2042. $ids = [$qb->getLastInsertId()];
  2043. foreach ($shares['children'] as $child) {
  2044. $qb = $this->dbConn->getQueryBuilder();
  2045. $qb->insert('share')
  2046. ->setValue('share_type', $qb->createNamedParameter(2))
  2047. ->setValue('uid_owner', $qb->createNamedParameter('owner'))
  2048. ->setValue('uid_initiator', $qb->createNamedParameter('initiator'))
  2049. ->setValue('share_with', $qb->createNamedParameter($child))
  2050. ->setValue('item_type', $qb->createNamedParameter('file'))
  2051. ->setValue('item_source', $qb->createNamedParameter(42))
  2052. ->setValue('file_source', $qb->createNamedParameter(42))
  2053. ->setValue('parent', $qb->createNamedParameter($ids[0]))
  2054. ->execute();
  2055. $ids[] = $qb->getLastInsertId();
  2056. }
  2057. $this->provider->groupDeleted($groupToDelete);
  2058. $qb = $this->dbConn->getQueryBuilder();
  2059. $cursor = $qb->select('*')
  2060. ->from('share')
  2061. ->where($qb->expr()->in('id', $qb->createNamedParameter($ids, IQueryBuilder::PARAM_INT_ARRAY)))
  2062. ->execute();
  2063. $data = $cursor->fetchAll();
  2064. $cursor->closeCursor();
  2065. $this->assertCount($shouldBeDeleted ? 0 : count($ids), $data);
  2066. }
  2067. public function dataUserDeletedFromGroup() {
  2068. return [
  2069. ['group1', 'user1', true],
  2070. ['group1', 'user2', false],
  2071. ['group2', 'user1', false],
  2072. ];
  2073. }
  2074. /**
  2075. * Given a group share with 'group1'
  2076. * And a user specific group share with 'user1'.
  2077. * User $user is deleted from group $gid.
  2078. *
  2079. * @dataProvider dataUserDeletedFromGroup
  2080. *
  2081. * @param string $group
  2082. * @param string $user
  2083. * @param bool $toDelete
  2084. */
  2085. public function testUserDeletedFromGroup($group, $user, $toDelete) {
  2086. $qb = $this->dbConn->getQueryBuilder();
  2087. $qb->insert('share')
  2088. ->setValue('share_type', $qb->createNamedParameter(IShare::TYPE_GROUP))
  2089. ->setValue('uid_owner', $qb->createNamedParameter('owner'))
  2090. ->setValue('uid_initiator', $qb->createNamedParameter('initiator'))
  2091. ->setValue('share_with', $qb->createNamedParameter('group1'))
  2092. ->setValue('item_type', $qb->createNamedParameter('file'))
  2093. ->setValue('item_source', $qb->createNamedParameter(42))
  2094. ->setValue('file_source', $qb->createNamedParameter(42));
  2095. $qb->execute();
  2096. $id1 = $qb->getLastInsertId();
  2097. $qb = $this->dbConn->getQueryBuilder();
  2098. $qb->insert('share')
  2099. ->setValue('share_type', $qb->createNamedParameter(2))
  2100. ->setValue('uid_owner', $qb->createNamedParameter('owner'))
  2101. ->setValue('uid_initiator', $qb->createNamedParameter('initiator'))
  2102. ->setValue('share_with', $qb->createNamedParameter('user1'))
  2103. ->setValue('item_type', $qb->createNamedParameter('file'))
  2104. ->setValue('item_source', $qb->createNamedParameter(42))
  2105. ->setValue('file_source', $qb->createNamedParameter(42))
  2106. ->setValue('parent', $qb->createNamedParameter($id1));
  2107. $qb->execute();
  2108. $id2 = $qb->getLastInsertId();
  2109. $this->provider->userDeletedFromGroup($user, $group);
  2110. $qb = $this->dbConn->getQueryBuilder();
  2111. $qb->select('*')
  2112. ->from('share')
  2113. ->where($qb->expr()->eq('id', $qb->createNamedParameter($id2)));
  2114. $cursor = $qb->execute();
  2115. $data = $cursor->fetchAll();
  2116. $cursor->closeCursor();
  2117. $this->assertCount($toDelete ? 0 : 1, $data);
  2118. }
  2119. public function testGetSharesInFolder() {
  2120. $userManager = \OC::$server->getUserManager();
  2121. $groupManager = \OC::$server->getGroupManager();
  2122. $rootFolder = \OC::$server->get(IRootFolder::class);
  2123. $provider = new DefaultShareProvider(
  2124. $this->dbConn,
  2125. $userManager,
  2126. $groupManager,
  2127. $rootFolder,
  2128. $this->mailer,
  2129. $this->defaults,
  2130. $this->l10nFactory,
  2131. $this->urlGenerator,
  2132. $this->timeFactory
  2133. );
  2134. $password = md5(time());
  2135. $u1 = $userManager->createUser('testShare1', $password);
  2136. $u2 = $userManager->createUser('testShare2', $password);
  2137. $u3 = $userManager->createUser('testShare3', $password);
  2138. $g1 = $groupManager->createGroup('group1');
  2139. $u1Folder = $rootFolder->getUserFolder($u1->getUID());
  2140. $folder1 = $u1Folder->newFolder('foo');
  2141. $file1 = $folder1->newFile('bar');
  2142. $folder2 = $folder1->newFolder('baz');
  2143. $shareManager = \OC::$server->get(IShareManager::class);
  2144. $share1 = $shareManager->newShare();
  2145. $share1->setNode($folder1)
  2146. ->setSharedBy($u1->getUID())
  2147. ->setSharedWith($u2->getUID())
  2148. ->setShareOwner($u1->getUID())
  2149. ->setShareType(IShare::TYPE_USER)
  2150. ->setPermissions(\OCP\Constants::PERMISSION_ALL);
  2151. $share1 = $this->provider->create($share1);
  2152. $share2 = $shareManager->newShare();
  2153. $share2->setNode($file1)
  2154. ->setSharedBy($u2->getUID())
  2155. ->setSharedWith($u3->getUID())
  2156. ->setShareOwner($u1->getUID())
  2157. ->setShareType(IShare::TYPE_USER)
  2158. ->setPermissions(\OCP\Constants::PERMISSION_READ);
  2159. $share2 = $this->provider->create($share2);
  2160. $share3 = $shareManager->newShare();
  2161. $share3->setNode($folder2)
  2162. ->setSharedBy($u2->getUID())
  2163. ->setShareOwner($u1->getUID())
  2164. ->setShareType(IShare::TYPE_LINK)
  2165. ->setPermissions(\OCP\Constants::PERMISSION_READ);
  2166. $share3 = $this->provider->create($share3);
  2167. $share4 = $shareManager->newShare();
  2168. $share4->setNode($folder2)
  2169. ->setSharedBy($u1->getUID())
  2170. ->setSharedWith($g1->getGID())
  2171. ->setShareOwner($u1->getUID())
  2172. ->setShareType(IShare::TYPE_GROUP)
  2173. ->setPermissions(\OCP\Constants::PERMISSION_READ);
  2174. $share4 = $this->provider->create($share4);
  2175. $result = $provider->getSharesInFolder($u1->getUID(), $folder1, false);
  2176. $this->assertCount(1, $result);
  2177. $shares = array_pop($result);
  2178. $this->assertCount(1, $shares);
  2179. $this->assertSame($folder2->getId(), $shares[0]->getNodeId());
  2180. $result = $provider->getSharesInFolder($u1->getUID(), $folder1, true);
  2181. $this->assertCount(2, $result);
  2182. $file_shares = $result[$file1->getId()];
  2183. $this->assertCount(1, $file_shares);
  2184. $this->assertSame($file1->getId(), $file_shares[0]->getNodeId());
  2185. $this->assertSame(IShare::TYPE_USER, $file_shares[0]->getShareType());
  2186. $folder_shares = $result[$folder2->getId()];
  2187. $this->assertCount(2, $folder_shares);
  2188. $this->assertSame($folder2->getId(), $folder_shares[0]->getNodeId());
  2189. $this->assertSame($folder2->getId(), $folder_shares[1]->getNodeId());
  2190. $this->assertSame(IShare::TYPE_LINK, $folder_shares[0]->getShareType());
  2191. $this->assertSame(IShare::TYPE_GROUP, $folder_shares[1]->getShareType());
  2192. $provider->delete($share1);
  2193. $provider->delete($share2);
  2194. $provider->delete($share3);
  2195. $provider->delete($share4);
  2196. $u1->delete();
  2197. $u2->delete();
  2198. $u3->delete();
  2199. $g1->delete();
  2200. }
  2201. public function testGetAccessListNoCurrentAccessRequired() {
  2202. $userManager = \OC::$server->getUserManager();
  2203. $groupManager = \OC::$server->getGroupManager();
  2204. $rootFolder = \OC::$server->get(IRootFolder::class);
  2205. $provider = new DefaultShareProvider(
  2206. $this->dbConn,
  2207. $userManager,
  2208. $groupManager,
  2209. $rootFolder,
  2210. $this->mailer,
  2211. $this->defaults,
  2212. $this->l10nFactory,
  2213. $this->urlGenerator,
  2214. $this->timeFactory
  2215. );
  2216. $u1 = $userManager->createUser('testShare1', 'test');
  2217. $u2 = $userManager->createUser('testShare2', 'test');
  2218. $u3 = $userManager->createUser('testShare3', 'test');
  2219. $u4 = $userManager->createUser('testShare4', 'test');
  2220. $u5 = $userManager->createUser('testShare5', 'test');
  2221. $g1 = $groupManager->createGroup('group1');
  2222. $g1->addUser($u3);
  2223. $g1->addUser($u4);
  2224. $u1Folder = $rootFolder->getUserFolder($u1->getUID());
  2225. $folder1 = $u1Folder->newFolder('foo');
  2226. $folder2 = $folder1->newFolder('baz');
  2227. $file1 = $folder2->newFile('bar');
  2228. $result = $provider->getAccessList([$folder1, $folder2, $file1], false);
  2229. $this->assertCount(0, $result['users']);
  2230. $this->assertFalse($result['public']);
  2231. $shareManager = \OC::$server->get(IShareManager::class);
  2232. $share1 = $shareManager->newShare();
  2233. $share1->setNode($folder1)
  2234. ->setSharedBy($u1->getUID())
  2235. ->setSharedWith($u2->getUID())
  2236. ->setShareOwner($u1->getUID())
  2237. ->setShareType(IShare::TYPE_USER)
  2238. ->setPermissions(\OCP\Constants::PERMISSION_ALL);
  2239. $share1 = $this->provider->create($share1);
  2240. $share2 = $shareManager->newShare();
  2241. $share2->setNode($folder2)
  2242. ->setSharedBy($u2->getUID())
  2243. ->setSharedWith($g1->getGID())
  2244. ->setShareOwner($u1->getUID())
  2245. ->setShareType(IShare::TYPE_GROUP)
  2246. ->setPermissions(\OCP\Constants::PERMISSION_ALL);
  2247. $share2 = $this->provider->create($share2);
  2248. $shareManager->deleteFromSelf($share2, $u4->getUID());
  2249. $share3 = $shareManager->newShare();
  2250. $share3->setNode($file1)
  2251. ->setSharedBy($u3->getUID())
  2252. ->setShareOwner($u1->getUID())
  2253. ->setShareType(IShare::TYPE_LINK)
  2254. ->setPermissions(\OCP\Constants::PERMISSION_READ);
  2255. $share3 = $this->provider->create($share3);
  2256. $share4 = $shareManager->newShare();
  2257. $share4->setNode($file1)
  2258. ->setSharedBy($u3->getUID())
  2259. ->setSharedWith($u5->getUID())
  2260. ->setShareOwner($u1->getUID())
  2261. ->setShareType(IShare::TYPE_USER)
  2262. ->setPermissions(\OCP\Constants::PERMISSION_READ);
  2263. $share4 = $this->provider->create($share4);
  2264. $result = $provider->getAccessList([$folder1, $folder2, $file1], false);
  2265. $this->assertCount(4, $result['users']);
  2266. $this->assertContains('testShare2', $result['users']);
  2267. $this->assertContains('testShare3', $result['users']);
  2268. $this->assertContains('testShare4', $result['users']);
  2269. $this->assertContains('testShare5', $result['users']);
  2270. $this->assertTrue($result['public']);
  2271. $provider->delete($share1);
  2272. $provider->delete($share2);
  2273. $provider->delete($share3);
  2274. $provider->delete($share4);
  2275. $u1->delete();
  2276. $u2->delete();
  2277. $u3->delete();
  2278. $u4->delete();
  2279. $u5->delete();
  2280. $g1->delete();
  2281. }
  2282. public function testGetAccessListCurrentAccessRequired() {
  2283. $userManager = \OC::$server->getUserManager();
  2284. $groupManager = \OC::$server->getGroupManager();
  2285. $rootFolder = \OC::$server->get(IRootFolder::class);
  2286. $provider = new DefaultShareProvider(
  2287. $this->dbConn,
  2288. $userManager,
  2289. $groupManager,
  2290. $rootFolder,
  2291. $this->mailer,
  2292. $this->defaults,
  2293. $this->l10nFactory,
  2294. $this->urlGenerator,
  2295. $this->timeFactory
  2296. );
  2297. $u1 = $userManager->createUser('testShare1', 'test');
  2298. $u2 = $userManager->createUser('testShare2', 'test');
  2299. $u3 = $userManager->createUser('testShare3', 'test');
  2300. $u4 = $userManager->createUser('testShare4', 'test');
  2301. $u5 = $userManager->createUser('testShare5', 'test');
  2302. $g1 = $groupManager->createGroup('group1');
  2303. $g1->addUser($u3);
  2304. $g1->addUser($u4);
  2305. $u1Folder = $rootFolder->getUserFolder($u1->getUID());
  2306. $folder1 = $u1Folder->newFolder('foo');
  2307. $folder2 = $folder1->newFolder('baz');
  2308. $file1 = $folder2->newFile('bar');
  2309. $result = $provider->getAccessList([$folder1, $folder2, $file1], false);
  2310. $this->assertCount(0, $result['users']);
  2311. $this->assertFalse($result['public']);
  2312. $shareManager = \OC::$server->get(IShareManager::class);
  2313. $share1 = $shareManager->newShare();
  2314. $share1->setNode($folder1)
  2315. ->setSharedBy($u1->getUID())
  2316. ->setSharedWith($u2->getUID())
  2317. ->setShareOwner($u1->getUID())
  2318. ->setShareType(IShare::TYPE_USER)
  2319. ->setPermissions(\OCP\Constants::PERMISSION_ALL);
  2320. $share1 = $this->provider->create($share1);
  2321. $share2 = $shareManager->newShare();
  2322. $share2->setNode($folder2)
  2323. ->setSharedBy($u2->getUID())
  2324. ->setSharedWith($g1->getGID())
  2325. ->setShareOwner($u1->getUID())
  2326. ->setShareType(IShare::TYPE_GROUP)
  2327. ->setPermissions(\OCP\Constants::PERMISSION_ALL);
  2328. $share2 = $this->provider->create($share2);
  2329. $shareManager->deleteFromSelf($share2, $u4->getUID());
  2330. $share3 = $shareManager->newShare();
  2331. $share3->setNode($file1)
  2332. ->setSharedBy($u3->getUID())
  2333. ->setShareOwner($u1->getUID())
  2334. ->setShareType(IShare::TYPE_LINK)
  2335. ->setPermissions(\OCP\Constants::PERMISSION_READ);
  2336. $share3 = $this->provider->create($share3);
  2337. $share4 = $shareManager->newShare();
  2338. $share4->setNode($file1)
  2339. ->setSharedBy($u3->getUID())
  2340. ->setSharedWith($u5->getUID())
  2341. ->setShareOwner($u1->getUID())
  2342. ->setShareType(IShare::TYPE_USER)
  2343. ->setPermissions(\OCP\Constants::PERMISSION_READ);
  2344. $share4 = $this->provider->create($share4);
  2345. $result = $provider->getAccessList([$folder1, $folder2, $file1], true);
  2346. $this->assertCount(3, $result['users']);
  2347. $this->assertArrayHasKey('testShare2', $result['users']);
  2348. $this->assertArrayHasKey('testShare3', $result['users']);
  2349. $this->assertArrayHasKey('testShare5', $result['users']);
  2350. $this->assertTrue($result['public']);
  2351. $provider->delete($share1);
  2352. $provider->delete($share2);
  2353. $provider->delete($share3);
  2354. $provider->delete($share4);
  2355. $u1->delete();
  2356. $u2->delete();
  2357. $u3->delete();
  2358. $u4->delete();
  2359. $u5->delete();
  2360. $g1->delete();
  2361. }
  2362. public function testGetAllShares() {
  2363. $qb = $this->dbConn->getQueryBuilder();
  2364. $qb->insert('share')
  2365. ->values([
  2366. 'share_type' => $qb->expr()->literal(IShare::TYPE_USER),
  2367. 'share_with' => $qb->expr()->literal('sharedWith1'),
  2368. 'uid_owner' => $qb->expr()->literal('shareOwner1'),
  2369. 'uid_initiator' => $qb->expr()->literal('sharedBy1'),
  2370. 'item_type' => $qb->expr()->literal('file'),
  2371. 'file_source' => $qb->expr()->literal(42),
  2372. 'file_target' => $qb->expr()->literal('myTarget1'),
  2373. 'permissions' => $qb->expr()->literal(13),
  2374. ]);
  2375. $qb->execute();
  2376. $id1 = $qb->getLastInsertId();
  2377. $qb->insert('share')
  2378. ->values([
  2379. 'share_type' => $qb->expr()->literal(IShare::TYPE_GROUP),
  2380. 'share_with' => $qb->expr()->literal('sharedWith2'),
  2381. 'uid_owner' => $qb->expr()->literal('shareOwner2'),
  2382. 'uid_initiator' => $qb->expr()->literal('sharedBy2'),
  2383. 'item_type' => $qb->expr()->literal('file'),
  2384. 'file_source' => $qb->expr()->literal(43),
  2385. 'file_target' => $qb->expr()->literal('myTarget2'),
  2386. 'permissions' => $qb->expr()->literal(14),
  2387. ]);
  2388. $qb->execute();
  2389. $id2 = $qb->getLastInsertId();
  2390. $qb->insert('share')
  2391. ->values([
  2392. 'share_type' => $qb->expr()->literal(IShare::TYPE_LINK),
  2393. 'token' => $qb->expr()->literal('token3'),
  2394. 'uid_owner' => $qb->expr()->literal('shareOwner3'),
  2395. 'uid_initiator' => $qb->expr()->literal('sharedBy3'),
  2396. 'item_type' => $qb->expr()->literal('file'),
  2397. 'file_source' => $qb->expr()->literal(44),
  2398. 'file_target' => $qb->expr()->literal('myTarget3'),
  2399. 'permissions' => $qb->expr()->literal(15),
  2400. ]);
  2401. $qb->execute();
  2402. $id3 = $qb->getLastInsertId();
  2403. $qb->insert('share')
  2404. ->values([
  2405. 'share_type' => $qb->expr()->literal(IShare::TYPE_EMAIL),
  2406. 'share_with' => $qb->expr()->literal('shareOwner4'),
  2407. 'token' => $qb->expr()->literal('token4'),
  2408. 'uid_owner' => $qb->expr()->literal('shareOwner4'),
  2409. 'uid_initiator' => $qb->expr()->literal('sharedBy4'),
  2410. 'item_type' => $qb->expr()->literal('file'),
  2411. 'file_source' => $qb->expr()->literal(45),
  2412. 'file_target' => $qb->expr()->literal('myTarget4'),
  2413. 'permissions' => $qb->expr()->literal(16),
  2414. ]);
  2415. $qb->execute();
  2416. $id4 = $qb->getLastInsertId();
  2417. $qb->insert('share')
  2418. ->values([
  2419. 'share_type' => $qb->expr()->literal(IShare::TYPE_LINK),
  2420. 'token' => $qb->expr()->literal('token5'),
  2421. 'uid_owner' => $qb->expr()->literal('shareOwner5'),
  2422. 'uid_initiator' => $qb->expr()->literal('sharedBy5'),
  2423. 'item_type' => $qb->expr()->literal('file'),
  2424. 'file_source' => $qb->expr()->literal(46),
  2425. 'file_target' => $qb->expr()->literal('myTarget5'),
  2426. 'permissions' => $qb->expr()->literal(17),
  2427. ]);
  2428. $qb->execute();
  2429. $id5 = $qb->getLastInsertId();
  2430. $ownerPath1 = $this->createMock(File::class);
  2431. $shareOwner1Folder = $this->createMock(Folder::class);
  2432. $shareOwner1Folder->method('getFirstNodeById')->willReturn($ownerPath1);
  2433. $ownerPath2 = $this->createMock(File::class);
  2434. $shareOwner2Folder = $this->createMock(Folder::class);
  2435. $shareOwner2Folder->method('getFirstNodeById')->willReturn($ownerPath2);
  2436. $ownerPath3 = $this->createMock(File::class);
  2437. $shareOwner3Folder = $this->createMock(Folder::class);
  2438. $shareOwner3Folder->method('getFirstNodeById')->willReturn($ownerPath3);
  2439. $ownerPath4 = $this->createMock(File::class);
  2440. $shareOwner4Folder = $this->createMock(Folder::class);
  2441. $shareOwner4Folder->method('getFirstNodeById')->willReturn($ownerPath4);
  2442. $ownerPath5 = $this->createMock(File::class);
  2443. $shareOwner5Folder = $this->createMock(Folder::class);
  2444. $shareOwner5Folder->method('getFirstNodeById')->willReturn($ownerPath5);
  2445. $this->rootFolder
  2446. ->method('getUserFolder')
  2447. ->willReturnMap(
  2448. [
  2449. ['shareOwner1', $shareOwner1Folder],
  2450. ['shareOwner2', $shareOwner2Folder],
  2451. ['shareOwner3', $shareOwner3Folder],
  2452. ['shareOwner4', $shareOwner4Folder],
  2453. ['shareOwner5', $shareOwner5Folder],
  2454. ]
  2455. );
  2456. $shares = iterator_to_array($this->provider->getAllShares());
  2457. $this->assertEquals(4, count($shares));
  2458. $share = $shares[0];
  2459. // We fetch the node so the root folder is eventually called
  2460. $this->assertEquals($id1, $share->getId());
  2461. $this->assertEquals(IShare::TYPE_USER, $share->getShareType());
  2462. $this->assertEquals('sharedWith1', $share->getSharedWith());
  2463. $this->assertEquals('sharedBy1', $share->getSharedBy());
  2464. $this->assertEquals('shareOwner1', $share->getShareOwner());
  2465. $this->assertEquals($ownerPath1, $share->getNode());
  2466. $this->assertEquals(13, $share->getPermissions());
  2467. $this->assertEquals(null, $share->getToken());
  2468. $this->assertEquals('myTarget1', $share->getTarget());
  2469. $share = $shares[1];
  2470. $this->assertEquals($id2, $share->getId());
  2471. $this->assertEquals(IShare::TYPE_GROUP, $share->getShareType());
  2472. $this->assertEquals('sharedWith2', $share->getSharedWith());
  2473. $this->assertEquals('sharedBy2', $share->getSharedBy());
  2474. $this->assertEquals('shareOwner2', $share->getShareOwner());
  2475. $this->assertEquals($ownerPath2, $share->getNode());
  2476. $this->assertEquals(14, $share->getPermissions());
  2477. $this->assertEquals(null, $share->getToken());
  2478. $this->assertEquals('myTarget2', $share->getTarget());
  2479. $share = $shares[2];
  2480. $this->assertEquals($id3, $share->getId());
  2481. $this->assertEquals(IShare::TYPE_LINK, $share->getShareType());
  2482. $this->assertEquals(null, $share->getSharedWith());
  2483. $this->assertEquals('sharedBy3', $share->getSharedBy());
  2484. $this->assertEquals('shareOwner3', $share->getShareOwner());
  2485. $this->assertEquals($ownerPath3, $share->getNode());
  2486. $this->assertEquals(15, $share->getPermissions());
  2487. $this->assertEquals('token3', $share->getToken());
  2488. $this->assertEquals('myTarget3', $share->getTarget());
  2489. $share = $shares[3];
  2490. $this->assertEquals($id5, $share->getId());
  2491. $this->assertEquals(IShare::TYPE_LINK, $share->getShareType());
  2492. $this->assertEquals(null, $share->getSharedWith());
  2493. $this->assertEquals('sharedBy5', $share->getSharedBy());
  2494. $this->assertEquals('shareOwner5', $share->getShareOwner());
  2495. $this->assertEquals($ownerPath5, $share->getNode());
  2496. $this->assertEquals(17, $share->getPermissions());
  2497. $this->assertEquals('token5', $share->getToken());
  2498. $this->assertEquals('myTarget5', $share->getTarget());
  2499. }
  2500. }