DefaultShareProviderTest.php 101 KB

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