DefaultShareProviderTest.php 103 KB

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