DefaultShareProviderTest.php 102 KB

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