DefaultShareProviderTest.php 101 KB

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