DefaultShareProviderTest.php 96 KB

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