You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DefaultShareProviderTest.php 96KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736
  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. $user1 = $this->createMock(IUser::class);
  951. $user1->method('getUID')->willReturn('user1');
  952. $this->userManager->method('get')->willReturnMap([
  953. ['user0', $user0],
  954. ['user1', $user1],
  955. ]);
  956. $file = $this->createMock(File::class);
  957. $file->method('getId')->willReturn($fileId2);
  958. $this->rootFolder->method('getUserFolder')->with('user1')->will($this->returnSelf());
  959. $this->rootFolder->method('getById')->with($fileId2)->willReturn([$file]);
  960. $share = $this->provider->getSharedWith('user0', \OCP\Share::SHARE_TYPE_USER, $file, -1, 0);
  961. $this->assertCount(1, $share);
  962. $share = $share[0];
  963. $this->assertEquals($id, $share->getId());
  964. $this->assertSame('user0', $share->getSharedWith());
  965. $this->assertSame('user1', $share->getShareOwner());
  966. $this->assertSame('user1', $share->getSharedBy());
  967. $this->assertSame($file, $share->getNode());
  968. $this->assertEquals(\OCP\Share::SHARE_TYPE_USER, $share->getShareType());
  969. }
  970. /**
  971. * @dataProvider storageAndFileNameProvider
  972. */
  973. public function testGetSharedWithGroupWithNode($storageStringId, $fileName1, $fileName2) {
  974. $storageId = $this->createTestStorageEntry($storageStringId);
  975. $fileId = $this->createTestFileEntry($fileName1, $storageId);
  976. $fileId2 = $this->createTestFileEntry($fileName2, $storageId);
  977. $this->addShareToDB(\OCP\Share::SHARE_TYPE_GROUP, 'group0', 'user1', 'user1',
  978. 'file', $fileId, 'myTarget', 31, null, null, null);
  979. $id = $this->addShareToDB(\OCP\Share::SHARE_TYPE_GROUP, 'group0', 'user1', 'user1',
  980. 'file', $fileId2, 'myTarget', 31, null, null, null);
  981. $user0 = $this->createMock(IUser::class);
  982. $user0->method('getUID')->willReturn('user0');
  983. $user1 = $this->createMock(IUser::class);
  984. $user1->method('getUID')->willReturn('user1');
  985. $this->userManager->method('get')->willReturnMap([
  986. ['user0', $user0],
  987. ['user1', $user1],
  988. ]);
  989. $group0 = $this->createMock(IGroup::class);
  990. $group0->method('getGID')->willReturn('group0');
  991. $this->groupManager->method('get')->with('group0')->willReturn($group0);
  992. $this->groupManager->method('getUserGroups')->with($user0)->willReturn([$group0]);
  993. $node = $this->createMock(Folder::class);
  994. $node->method('getId')->willReturn($fileId2);
  995. $this->rootFolder->method('getUserFolder')->with('user1')->will($this->returnSelf());
  996. $this->rootFolder->method('getById')->with($fileId2)->willReturn([$node]);
  997. $share = $this->provider->getSharedWith('user0', \OCP\Share::SHARE_TYPE_GROUP, $node, -1, 0);
  998. $this->assertCount(1, $share);
  999. $share = $share[0];
  1000. $this->assertEquals($id, $share->getId());
  1001. $this->assertSame('group0', $share->getSharedWith());
  1002. $this->assertSame('user1', $share->getShareOwner());
  1003. $this->assertSame('user1', $share->getSharedBy());
  1004. $this->assertSame($node, $share->getNode());
  1005. $this->assertEquals(\OCP\Share::SHARE_TYPE_GROUP, $share->getShareType());
  1006. }
  1007. public function shareTypesProvider() {
  1008. return [
  1009. [\OCP\Share::SHARE_TYPE_USER, false],
  1010. [\OCP\Share::SHARE_TYPE_GROUP, false],
  1011. [\OCP\Share::SHARE_TYPE_USER, true],
  1012. [\OCP\Share::SHARE_TYPE_GROUP, true],
  1013. ];
  1014. }
  1015. /**
  1016. * @dataProvider shareTypesProvider
  1017. */
  1018. public function testGetSharedWithWithDeletedFile($shareType, $trashed) {
  1019. if ($trashed) {
  1020. // exists in database but is in trash
  1021. $storageId = $this->createTestStorageEntry('home::shareOwner');
  1022. $deletedFileId = $this->createTestFileEntry('files_trashbin/files/test.txt.d1465553223', $storageId);
  1023. } else {
  1024. // fileid that doesn't exist in the database
  1025. $deletedFileId = 123;
  1026. }
  1027. $qb = $this->dbConn->getQueryBuilder();
  1028. $qb->insert('share')
  1029. ->values([
  1030. 'share_type' => $qb->expr()->literal($shareType),
  1031. 'share_with' => $qb->expr()->literal('sharedWith'),
  1032. 'uid_owner' => $qb->expr()->literal('shareOwner'),
  1033. 'uid_initiator' => $qb->expr()->literal('sharedBy'),
  1034. 'item_type' => $qb->expr()->literal('file'),
  1035. 'file_source' => $qb->expr()->literal($deletedFileId),
  1036. 'file_target' => $qb->expr()->literal('myTarget'),
  1037. 'permissions' => $qb->expr()->literal(13),
  1038. ]);
  1039. $this->assertEquals(1, $qb->execute());
  1040. $file = $this->createMock(File::class);
  1041. $this->rootFolder->method('getUserFolder')->with('shareOwner')->will($this->returnSelf());
  1042. $this->rootFolder->method('getById')->with($deletedFileId)->willReturn([$file]);
  1043. $groups = [];
  1044. foreach(range(0, 100) as $i) {
  1045. $group = $this->createMock(IGroup::class);
  1046. $group->method('getGID')->willReturn('group'.$i);
  1047. $groups[] = $group;
  1048. }
  1049. $group = $this->createMock(IGroup::class);
  1050. $group->method('getGID')->willReturn('sharedWith');
  1051. $groups[] = $group;
  1052. $user = $this->createMock(IUser::class);
  1053. $user->method('getUID')->willReturn('sharedWith');
  1054. $owner = $this->createMock(IUser::class);
  1055. $owner->method('getUID')->willReturn('shareOwner');
  1056. $initiator = $this->createMock(IUser::class);
  1057. $initiator->method('getUID')->willReturn('sharedBy');
  1058. $this->userManager->method('get')->willReturnMap([
  1059. ['sharedWith', $user],
  1060. ['shareOwner', $owner],
  1061. ['sharedBy', $initiator],
  1062. ]);
  1063. $this->groupManager->method('getUserGroups')->with($user)->willReturn($groups);
  1064. $this->groupManager->method('get')->with('sharedWith')->willReturn($group);
  1065. $share = $this->provider->getSharedWith('sharedWith', $shareType, null, 1 , 0);
  1066. $this->assertCount(0, $share);
  1067. }
  1068. public function testGetSharesBy() {
  1069. $qb = $this->dbConn->getQueryBuilder();
  1070. $qb->insert('share')
  1071. ->values([
  1072. 'share_type' => $qb->expr()->literal(\OCP\Share::SHARE_TYPE_USER),
  1073. 'share_with' => $qb->expr()->literal('sharedWith'),
  1074. 'uid_owner' => $qb->expr()->literal('shareOwner'),
  1075. 'uid_initiator' => $qb->expr()->literal('sharedBy'),
  1076. 'item_type' => $qb->expr()->literal('file'),
  1077. 'file_source' => $qb->expr()->literal(42),
  1078. 'file_target' => $qb->expr()->literal('myTarget'),
  1079. 'permissions' => $qb->expr()->literal(13),
  1080. ]);
  1081. $this->assertEquals(1, $qb->execute());
  1082. $id = $qb->getLastInsertId();
  1083. $qb = $this->dbConn->getQueryBuilder();
  1084. $qb->insert('share')
  1085. ->values([
  1086. 'share_type' => $qb->expr()->literal(\OCP\Share::SHARE_TYPE_USER),
  1087. 'share_with' => $qb->expr()->literal('sharedWith'),
  1088. 'uid_owner' => $qb->expr()->literal('shareOwner'),
  1089. 'uid_initiator' => $qb->expr()->literal('sharedBy2'),
  1090. 'item_type' => $qb->expr()->literal('file'),
  1091. 'file_source' => $qb->expr()->literal(42),
  1092. 'file_target' => $qb->expr()->literal('userTarget'),
  1093. 'permissions' => $qb->expr()->literal(0),
  1094. 'parent' => $qb->expr()->literal($id),
  1095. ]);
  1096. $this->assertEquals(1, $qb->execute());
  1097. $file = $this->createMock(File::class);
  1098. $this->rootFolder->method('getUserFolder')->with('shareOwner')->will($this->returnSelf());
  1099. $this->rootFolder->method('getById')->with(42)->willReturn([$file]);
  1100. $share = $this->provider->getSharesBy('sharedBy', \OCP\Share::SHARE_TYPE_USER, null, false, 1, 0);
  1101. $this->assertCount(1, $share);
  1102. /** @var IShare $share */
  1103. $share = $share[0];
  1104. $this->assertEquals($id, $share->getId());
  1105. $this->assertEquals('sharedWith', $share->getSharedWith());
  1106. $this->assertEquals('shareOwner', $share->getShareOwner());
  1107. $this->assertEquals('sharedBy', $share->getSharedBy());
  1108. $this->assertEquals(\OCP\Share::SHARE_TYPE_USER, $share->getShareType());
  1109. $this->assertEquals(13, $share->getPermissions());
  1110. $this->assertEquals('myTarget', $share->getTarget());
  1111. }
  1112. public function testGetSharesNode() {
  1113. $qb = $this->dbConn->getQueryBuilder();
  1114. $qb->insert('share')
  1115. ->values([
  1116. 'share_type' => $qb->expr()->literal(\OCP\Share::SHARE_TYPE_USER),
  1117. 'share_with' => $qb->expr()->literal('sharedWith'),
  1118. 'uid_owner' => $qb->expr()->literal('shareOwner'),
  1119. 'uid_initiator' => $qb->expr()->literal('sharedBy'),
  1120. 'item_type' => $qb->expr()->literal('file'),
  1121. 'file_source' => $qb->expr()->literal(42),
  1122. 'file_target' => $qb->expr()->literal('myTarget'),
  1123. 'permissions' => $qb->expr()->literal(13),
  1124. ]);
  1125. $this->assertEquals(1, $qb->execute());
  1126. $id = $qb->getLastInsertId();
  1127. $qb = $this->dbConn->getQueryBuilder();
  1128. $qb->insert('share')
  1129. ->values([
  1130. 'share_type' => $qb->expr()->literal(\OCP\Share::SHARE_TYPE_USER),
  1131. 'share_with' => $qb->expr()->literal('sharedWith'),
  1132. 'uid_owner' => $qb->expr()->literal('shareOwner'),
  1133. 'uid_initiator' => $qb->expr()->literal('sharedBy'),
  1134. 'item_type' => $qb->expr()->literal('file'),
  1135. 'file_source' => $qb->expr()->literal(43),
  1136. 'file_target' => $qb->expr()->literal('userTarget'),
  1137. 'permissions' => $qb->expr()->literal(0),
  1138. 'parent' => $qb->expr()->literal($id),
  1139. ]);
  1140. $this->assertEquals(1, $qb->execute());
  1141. $file = $this->createMock(File::class);
  1142. $file->method('getId')->willReturn(42);
  1143. $this->rootFolder->method('getUserFolder')->with('shareOwner')->will($this->returnSelf());
  1144. $this->rootFolder->method('getById')->with(42)->willReturn([$file]);
  1145. $share = $this->provider->getSharesBy('sharedBy', \OCP\Share::SHARE_TYPE_USER, $file, false, 1, 0);
  1146. $this->assertCount(1, $share);
  1147. /** @var IShare $share */
  1148. $share = $share[0];
  1149. $this->assertEquals($id, $share->getId());
  1150. $this->assertEquals('sharedWith', $share->getSharedWith());
  1151. $this->assertEquals('shareOwner', $share->getShareOwner());
  1152. $this->assertEquals('sharedBy', $share->getSharedBy());
  1153. $this->assertEquals(\OCP\Share::SHARE_TYPE_USER, $share->getShareType());
  1154. $this->assertEquals(13, $share->getPermissions());
  1155. $this->assertEquals('myTarget', $share->getTarget());
  1156. }
  1157. public function testGetSharesReshare() {
  1158. $qb = $this->dbConn->getQueryBuilder();
  1159. $qb->insert('share')
  1160. ->values([
  1161. 'share_type' => $qb->expr()->literal(\OCP\Share::SHARE_TYPE_USER),
  1162. 'share_with' => $qb->expr()->literal('sharedWith'),
  1163. 'uid_owner' => $qb->expr()->literal('shareOwner'),
  1164. 'uid_initiator' => $qb->expr()->literal('shareOwner'),
  1165. 'item_type' => $qb->expr()->literal('file'),
  1166. 'file_source' => $qb->expr()->literal(42),
  1167. 'file_target' => $qb->expr()->literal('myTarget'),
  1168. 'permissions' => $qb->expr()->literal(13),
  1169. ]);
  1170. $this->assertEquals(1, $qb->execute());
  1171. $id1 = $qb->getLastInsertId();
  1172. $qb = $this->dbConn->getQueryBuilder();
  1173. $qb->insert('share')
  1174. ->values([
  1175. 'share_type' => $qb->expr()->literal(\OCP\Share::SHARE_TYPE_USER),
  1176. 'share_with' => $qb->expr()->literal('sharedWith'),
  1177. 'uid_owner' => $qb->expr()->literal('shareOwner'),
  1178. 'uid_initiator' => $qb->expr()->literal('sharedBy'),
  1179. 'item_type' => $qb->expr()->literal('file'),
  1180. 'file_source' => $qb->expr()->literal(42),
  1181. 'file_target' => $qb->expr()->literal('userTarget'),
  1182. 'permissions' => $qb->expr()->literal(0),
  1183. ]);
  1184. $this->assertEquals(1, $qb->execute());
  1185. $id2 = $qb->getLastInsertId();
  1186. $file = $this->createMock(File::class);
  1187. $file->method('getId')->willReturn(42);
  1188. $this->rootFolder->method('getUserFolder')->with('shareOwner')->will($this->returnSelf());
  1189. $this->rootFolder->method('getById')->with(42)->willReturn([$file]);
  1190. $shares = $this->provider->getSharesBy('shareOwner', \OCP\Share::SHARE_TYPE_USER, null, true, -1, 0);
  1191. $this->assertCount(2, $shares);
  1192. /** @var IShare $share */
  1193. $share = $shares[0];
  1194. $this->assertEquals($id1, $share->getId());
  1195. $this->assertSame('sharedWith', $share->getSharedWith());
  1196. $this->assertSame('shareOwner', $share->getShareOwner());
  1197. $this->assertSame('shareOwner', $share->getSharedBy());
  1198. $this->assertEquals(\OCP\Share::SHARE_TYPE_USER, $share->getShareType());
  1199. $this->assertEquals(13, $share->getPermissions());
  1200. $this->assertEquals('myTarget', $share->getTarget());
  1201. $share = $shares[1];
  1202. $this->assertEquals($id2, $share->getId());
  1203. $this->assertSame('sharedWith', $share->getSharedWith());
  1204. $this->assertSame('shareOwner', $share->getShareOwner());
  1205. $this->assertSame('sharedBy', $share->getSharedBy());
  1206. $this->assertEquals(\OCP\Share::SHARE_TYPE_USER, $share->getShareType());
  1207. $this->assertEquals(0, $share->getPermissions());
  1208. $this->assertEquals('userTarget', $share->getTarget());
  1209. }
  1210. public function testDeleteFromSelfGroupNoCustomShare() {
  1211. $qb = $this->dbConn->getQueryBuilder();
  1212. $stmt = $qb->insert('share')
  1213. ->values([
  1214. 'share_type' => $qb->expr()->literal(\OCP\Share::SHARE_TYPE_GROUP),
  1215. 'share_with' => $qb->expr()->literal('group'),
  1216. 'uid_owner' => $qb->expr()->literal('user1'),
  1217. 'uid_initiator' => $qb->expr()->literal('user1'),
  1218. 'item_type' => $qb->expr()->literal('file'),
  1219. 'file_source' => $qb->expr()->literal(1),
  1220. 'file_target' => $qb->expr()->literal('myTarget1'),
  1221. 'permissions' => $qb->expr()->literal(2)
  1222. ])->execute();
  1223. $this->assertEquals(1, $stmt);
  1224. $id = $qb->getLastInsertId();
  1225. $user1 = $this->createMock(IUser::class);
  1226. $user1->method('getUID')->willReturn('user1');
  1227. $user2 = $this->createMock(IUser::class);
  1228. $user2->method('getUID')->willReturn('user2');
  1229. $this->userManager->method('get')->will($this->returnValueMap([
  1230. ['user1', $user1],
  1231. ['user2', $user2],
  1232. ]));
  1233. $group = $this->createMock(IGroup::class);
  1234. $group->method('getGID')->willReturn('group');
  1235. $group->method('inGroup')->with($user2)->willReturn(true);
  1236. $this->groupManager->method('get')->with('group')->willReturn($group);
  1237. $file = $this->createMock(File::class);
  1238. $file->method('getId')->willReturn(1);
  1239. $this->rootFolder->method('getUserFolder')->with('user1')->will($this->returnSelf());
  1240. $this->rootFolder->method('getById')->with(1)->willReturn([$file]);
  1241. $share = $this->provider->getShareById($id);
  1242. $this->provider->deleteFromSelf($share, 'user2');
  1243. $qb = $this->dbConn->getQueryBuilder();
  1244. $stmt = $qb->select('*')
  1245. ->from('share')
  1246. ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(2)))
  1247. ->execute();
  1248. $shares = $stmt->fetchAll();
  1249. $stmt->closeCursor();
  1250. $this->assertCount(1, $shares);
  1251. $share2 = $shares[0];
  1252. $this->assertEquals($id, $share2['parent']);
  1253. $this->assertEquals(0, $share2['permissions']);
  1254. $this->assertEquals('user2', $share2['share_with']);
  1255. }
  1256. public function testDeleteFromSelfGroupAlreadyCustomShare() {
  1257. $qb = $this->dbConn->getQueryBuilder();
  1258. $stmt = $qb->insert('share')
  1259. ->values([
  1260. 'share_type' => $qb->expr()->literal(\OCP\Share::SHARE_TYPE_GROUP),
  1261. 'share_with' => $qb->expr()->literal('group'),
  1262. 'uid_owner' => $qb->expr()->literal('user1'),
  1263. 'uid_initiator' => $qb->expr()->literal('user1'),
  1264. 'item_type' => $qb->expr()->literal('file'),
  1265. 'file_source' => $qb->expr()->literal(1),
  1266. 'file_target' => $qb->expr()->literal('myTarget1'),
  1267. 'permissions' => $qb->expr()->literal(2)
  1268. ])->execute();
  1269. $this->assertEquals(1, $stmt);
  1270. $id = $qb->getLastInsertId();
  1271. $qb = $this->dbConn->getQueryBuilder();
  1272. $stmt = $qb->insert('share')
  1273. ->values([
  1274. 'share_type' => $qb->expr()->literal(2),
  1275. 'share_with' => $qb->expr()->literal('user2'),
  1276. 'uid_owner' => $qb->expr()->literal('user1'),
  1277. 'uid_initiator' => $qb->expr()->literal('user1'),
  1278. 'item_type' => $qb->expr()->literal('file'),
  1279. 'file_source' => $qb->expr()->literal(1),
  1280. 'file_target' => $qb->expr()->literal('myTarget1'),
  1281. 'permissions' => $qb->expr()->literal(2),
  1282. 'parent' => $qb->expr()->literal($id),
  1283. ])->execute();
  1284. $this->assertEquals(1, $stmt);
  1285. $user1 = $this->createMock(IUser::class);
  1286. $user1->method('getUID')->willReturn('user1');
  1287. $user2 = $this->createMock(IUser::class);
  1288. $user2->method('getUID')->willReturn('user2');
  1289. $this->userManager->method('get')->will($this->returnValueMap([
  1290. ['user1', $user1],
  1291. ['user2', $user2],
  1292. ]));
  1293. $group = $this->createMock(IGroup::class);
  1294. $group->method('getGID')->willReturn('group');
  1295. $group->method('inGroup')->with($user2)->willReturn(true);
  1296. $this->groupManager->method('get')->with('group')->willReturn($group);
  1297. $file = $this->createMock(File::class);
  1298. $file->method('getId')->willReturn(1);
  1299. $this->rootFolder->method('getUserFolder')->with('user1')->will($this->returnSelf());
  1300. $this->rootFolder->method('getById')->with(1)->willReturn([$file]);
  1301. $share = $this->provider->getShareById($id);
  1302. $this->provider->deleteFromSelf($share, 'user2');
  1303. $qb = $this->dbConn->getQueryBuilder();
  1304. $stmt = $qb->select('*')
  1305. ->from('share')
  1306. ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(2)))
  1307. ->execute();
  1308. $shares = $stmt->fetchAll();
  1309. $stmt->closeCursor();
  1310. $this->assertCount(1, $shares);
  1311. $share2 = $shares[0];
  1312. $this->assertEquals($id, $share2['parent']);
  1313. $this->assertEquals(0, $share2['permissions']);
  1314. $this->assertEquals('user2', $share2['share_with']);
  1315. }
  1316. /**
  1317. * @expectedException \OC\Share20\Exception\ProviderException
  1318. * @expectedExceptionMessage Recipient not in receiving group
  1319. */
  1320. public function testDeleteFromSelfGroupUserNotInGroup() {
  1321. $qb = $this->dbConn->getQueryBuilder();
  1322. $stmt = $qb->insert('share')
  1323. ->values([
  1324. 'share_type' => $qb->expr()->literal(\OCP\Share::SHARE_TYPE_GROUP),
  1325. 'share_with' => $qb->expr()->literal('group'),
  1326. 'uid_owner' => $qb->expr()->literal('user1'),
  1327. 'uid_initiator' => $qb->expr()->literal('user1'),
  1328. 'item_type' => $qb->expr()->literal('file'),
  1329. 'file_source' => $qb->expr()->literal(1),
  1330. 'file_target' => $qb->expr()->literal('myTarget1'),
  1331. 'permissions' => $qb->expr()->literal(2)
  1332. ])->execute();
  1333. $this->assertEquals(1, $stmt);
  1334. $id = $qb->getLastInsertId();
  1335. $user1 = $this->createMock(IUser::class);
  1336. $user1->method('getUID')->willReturn('user1');
  1337. $user2 = $this->createMock(IUser::class);
  1338. $user2->method('getUID')->willReturn('user2');
  1339. $this->userManager->method('get')->will($this->returnValueMap([
  1340. ['user1', $user1],
  1341. ['user2', $user2],
  1342. ]));
  1343. $group = $this->createMock(IGroup::class);
  1344. $group->method('getGID')->willReturn('group');
  1345. $group->method('inGroup')->with($user2)->willReturn(false);
  1346. $this->groupManager->method('get')->with('group')->willReturn($group);
  1347. $file = $this->createMock(File::class);
  1348. $file->method('getId')->willReturn(1);
  1349. $this->rootFolder->method('getUserFolder')->with('user1')->will($this->returnSelf());
  1350. $this->rootFolder->method('getById')->with(1)->willReturn([$file]);
  1351. $share = $this->provider->getShareById($id);
  1352. $this->provider->deleteFromSelf($share, 'user2');
  1353. }
  1354. /**
  1355. * @expectedException \OC\Share20\Exception\ProviderException
  1356. * @expectedExceptionMessage Group "group" does not exist
  1357. */
  1358. public function testDeleteFromSelfGroupDoesNotExist() {
  1359. $qb = $this->dbConn->getQueryBuilder();
  1360. $stmt = $qb->insert('share')
  1361. ->values([
  1362. 'share_type' => $qb->expr()->literal(\OCP\Share::SHARE_TYPE_GROUP),
  1363. 'share_with' => $qb->expr()->literal('group'),
  1364. 'uid_owner' => $qb->expr()->literal('user1'),
  1365. 'uid_initiator' => $qb->expr()->literal('user1'),
  1366. 'item_type' => $qb->expr()->literal('file'),
  1367. 'file_source' => $qb->expr()->literal(1),
  1368. 'file_target' => $qb->expr()->literal('myTarget1'),
  1369. 'permissions' => $qb->expr()->literal(2)
  1370. ])->execute();
  1371. $this->assertEquals(1, $stmt);
  1372. $id = $qb->getLastInsertId();
  1373. $user1 = $this->createMock(IUser::class);
  1374. $user1->method('getUID')->willReturn('user1');
  1375. $user2 = $this->createMock(IUser::class);
  1376. $user2->method('getUID')->willReturn('user2');
  1377. $this->userManager->method('get')->will($this->returnValueMap([
  1378. ['user1', $user1],
  1379. ['user2', $user2],
  1380. ]));
  1381. $this->groupManager->method('get')->with('group')->willReturn(null);
  1382. $file = $this->createMock(File::class);
  1383. $file->method('getId')->willReturn(1);
  1384. $this->rootFolder->method('getUserFolder')->with('user1')->will($this->returnSelf());
  1385. $this->rootFolder->method('getById')->with(1)->willReturn([$file]);
  1386. $share = $this->provider->getShareById($id);
  1387. $this->provider->deleteFromSelf($share, 'user2');
  1388. }
  1389. public function testDeleteFromSelfUser() {
  1390. $qb = $this->dbConn->getQueryBuilder();
  1391. $stmt = $qb->insert('share')
  1392. ->values([
  1393. 'share_type' => $qb->expr()->literal(\OCP\Share::SHARE_TYPE_USER),
  1394. 'share_with' => $qb->expr()->literal('user2'),
  1395. 'uid_owner' => $qb->expr()->literal('user1'),
  1396. 'uid_initiator' => $qb->expr()->literal('user1'),
  1397. 'item_type' => $qb->expr()->literal('file'),
  1398. 'file_source' => $qb->expr()->literal(1),
  1399. 'file_target' => $qb->expr()->literal('myTarget1'),
  1400. 'permissions' => $qb->expr()->literal(2)
  1401. ])->execute();
  1402. $this->assertEquals(1, $stmt);
  1403. $id = $qb->getLastInsertId();
  1404. $user1 = $this->createMock(IUser::class);
  1405. $user1->method('getUID')->willReturn('user1');
  1406. $user2 = $this->createMock(IUser::class);
  1407. $user2->method('getUID')->willReturn('user2');
  1408. $this->userManager->method('get')->will($this->returnValueMap([
  1409. ['user1', $user1],
  1410. ['user2', $user2],
  1411. ]));
  1412. $file = $this->createMock(File::class);
  1413. $file->method('getId')->willReturn(1);
  1414. $this->rootFolder->method('getUserFolder')->with('user1')->will($this->returnSelf());
  1415. $this->rootFolder->method('getById')->with(1)->willReturn([$file]);
  1416. $share = $this->provider->getShareById($id);
  1417. $this->provider->deleteFromSelf($share, 'user2');
  1418. $qb = $this->dbConn->getQueryBuilder();
  1419. $stmt = $qb->select('*')
  1420. ->from('share')
  1421. ->where($qb->expr()->eq('id', $qb->createNamedParameter($id)))
  1422. ->execute();
  1423. $shares = $stmt->fetchAll();
  1424. $stmt->closeCursor();
  1425. $this->assertCount(0, $shares);
  1426. }
  1427. /**
  1428. * @expectedException \OC\Share20\Exception\ProviderException
  1429. * @expectedExceptionMessage Recipient does not match
  1430. */
  1431. public function testDeleteFromSelfUserNotRecipient() {
  1432. $qb = $this->dbConn->getQueryBuilder();
  1433. $stmt = $qb->insert('share')
  1434. ->values([
  1435. 'share_type' => $qb->expr()->literal(\OCP\Share::SHARE_TYPE_USER),
  1436. 'share_with' => $qb->expr()->literal('user2'),
  1437. 'uid_owner' => $qb->expr()->literal('user1'),
  1438. 'uid_initiator' => $qb->expr()->literal('user1'),
  1439. 'item_type' => $qb->expr()->literal('file'),
  1440. 'file_source' => $qb->expr()->literal(1),
  1441. 'file_target' => $qb->expr()->literal('myTarget1'),
  1442. 'permissions' => $qb->expr()->literal(2)
  1443. ])->execute();
  1444. $this->assertEquals(1, $stmt);
  1445. $id = $qb->getLastInsertId();
  1446. $user1 = $this->createMock(IUser::class);
  1447. $user1->method('getUID')->willReturn('user1');
  1448. $user2 = $this->createMock(IUser::class);
  1449. $user2->method('getUID')->willReturn('user2');
  1450. $user3 = $this->createMock(IUser::class);
  1451. $this->userManager->method('get')->will($this->returnValueMap([
  1452. ['user1', $user1],
  1453. ['user2', $user2],
  1454. ]));
  1455. $file = $this->createMock(File::class);
  1456. $file->method('getId')->willReturn(1);
  1457. $this->rootFolder->method('getUserFolder')->with('user1')->will($this->returnSelf());
  1458. $this->rootFolder->method('getById')->with(1)->willReturn([$file]);
  1459. $share = $this->provider->getShareById($id);
  1460. $this->provider->deleteFromSelf($share, $user3);
  1461. }
  1462. /**
  1463. * @expectedException \OC\Share20\Exception\ProviderException
  1464. * @expectedExceptionMessage Invalid shareType
  1465. */
  1466. public function testDeleteFromSelfLink() {
  1467. $qb = $this->dbConn->getQueryBuilder();
  1468. $stmt = $qb->insert('share')
  1469. ->values([
  1470. 'share_type' => $qb->expr()->literal(\OCP\Share::SHARE_TYPE_LINK),
  1471. 'uid_owner' => $qb->expr()->literal('user1'),
  1472. 'uid_initiator' => $qb->expr()->literal('user1'),
  1473. 'item_type' => $qb->expr()->literal('file'),
  1474. 'file_source' => $qb->expr()->literal(1),
  1475. 'file_target' => $qb->expr()->literal('myTarget1'),
  1476. 'permissions' => $qb->expr()->literal(2),
  1477. 'token' => $qb->expr()->literal('token'),
  1478. ])->execute();
  1479. $this->assertEquals(1, $stmt);
  1480. $id = $qb->getLastInsertId();
  1481. $user1 = $this->createMock(IUser::class);
  1482. $user1->method('getUID')->willReturn('user1');
  1483. $this->userManager->method('get')->will($this->returnValueMap([
  1484. ['user1', $user1],
  1485. ]));
  1486. $file = $this->createMock(File::class);
  1487. $file->method('getId')->willReturn(1);
  1488. $this->rootFolder->method('getUserFolder')->with('user1')->will($this->returnSelf());
  1489. $this->rootFolder->method('getById')->with(1)->willReturn([$file]);
  1490. $share = $this->provider->getShareById($id);
  1491. $this->provider->deleteFromSelf($share, $user1);
  1492. }
  1493. public function testUpdateUser() {
  1494. $id = $this->addShareToDB(\OCP\Share::SHARE_TYPE_USER, 'user0', 'user1', 'user2',
  1495. 'file', 42, 'target', 31, null, null);
  1496. $users = [];
  1497. for($i = 0; $i < 6; $i++) {
  1498. $user = $this->createMock(IUser::class);
  1499. $user->method('getUID')->willReturn('user'.$i);
  1500. $users['user'.$i] = $user;
  1501. }
  1502. $this->userManager->method('get')->will(
  1503. $this->returnCallback(function($userId) use ($users) {
  1504. return $users[$userId];
  1505. })
  1506. );
  1507. $file1 = $this->createMock(File::class);
  1508. $file1->method('getId')->willReturn(42);
  1509. $file2 = $this->createMock(File::class);
  1510. $file2->method('getId')->willReturn(43);
  1511. $folder1 = $this->createMock(Folder::class);
  1512. $folder1->method('getById')->with(42)->willReturn([$file1]);
  1513. $folder2 = $this->createMock(Folder::class);
  1514. $folder2->method('getById')->with(43)->willReturn([$file2]);
  1515. $this->rootFolder->method('getUserFolder')->will($this->returnValueMap([
  1516. ['user2', $folder1],
  1517. ['user5', $folder2],
  1518. ]));
  1519. $share = $this->provider->getShareById($id);
  1520. $share->setSharedWith('user3');
  1521. $share->setSharedBy('user4');
  1522. $share->setShareOwner('user5');
  1523. $share->setNode($file2);
  1524. $share->setPermissions(1);
  1525. $share2 = $this->provider->update($share);
  1526. $this->assertEquals($id, $share2->getId());
  1527. $this->assertSame('user3', $share2->getSharedWith());
  1528. $this->assertSame('user4', $share2->getSharedBy());
  1529. $this->assertSame('user5', $share2->getShareOwner());
  1530. $this->assertSame(1, $share2->getPermissions());
  1531. $share2 = $this->provider->getShareById($id);
  1532. $this->assertEquals($id, $share2->getId());
  1533. $this->assertSame('user3', $share2->getSharedWith());
  1534. $this->assertSame('user4', $share2->getSharedBy());
  1535. $this->assertSame('user5', $share2->getShareOwner());
  1536. $this->assertSame(1, $share2->getPermissions());
  1537. }
  1538. public function testUpdateLink() {
  1539. $id = $this->addShareToDB(\OCP\Share::SHARE_TYPE_LINK, null, 'user1', 'user2',
  1540. 'file', 42, 'target', 31, null, null);
  1541. $users = [];
  1542. for($i = 0; $i < 6; $i++) {
  1543. $user = $this->createMock(IUser::class);
  1544. $user->method('getUID')->willReturn('user'.$i);
  1545. $users['user'.$i] = $user;
  1546. }
  1547. $this->userManager->method('get')->will(
  1548. $this->returnCallback(function($userId) use ($users) {
  1549. return $users[$userId];
  1550. })
  1551. );
  1552. $file1 = $this->createMock(File::class);
  1553. $file1->method('getId')->willReturn(42);
  1554. $file2 = $this->createMock(File::class);
  1555. $file2->method('getId')->willReturn(43);
  1556. $folder1 = $this->createMock(Folder::class);
  1557. $folder1->method('getById')->with(42)->willReturn([$file1]);
  1558. $folder2 = $this->createMock(Folder::class);
  1559. $folder2->method('getById')->with(43)->willReturn([$file2]);
  1560. $this->rootFolder->method('getUserFolder')->will($this->returnValueMap([
  1561. ['user2', $folder1],
  1562. ['user5', $folder2],
  1563. ]));
  1564. $share = $this->provider->getShareById($id);
  1565. $share->setPassword('password');
  1566. $share->setSendPasswordByTalk(true);
  1567. $share->setSharedBy('user4');
  1568. $share->setShareOwner('user5');
  1569. $share->setNode($file2);
  1570. $share->setPermissions(1);
  1571. $share2 = $this->provider->update($share);
  1572. $this->assertEquals($id, $share2->getId());
  1573. $this->assertEquals('password', $share2->getPassword());
  1574. $this->assertSame(true, $share2->getSendPasswordByTalk());
  1575. $this->assertSame('user4', $share2->getSharedBy());
  1576. $this->assertSame('user5', $share2->getShareOwner());
  1577. $this->assertSame(1, $share2->getPermissions());
  1578. $share2 = $this->provider->getShareById($id);
  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. }
  1586. public function testUpdateLinkRemovePassword() {
  1587. $id = $this->addShareToDB(\OCP\Share::SHARE_TYPE_LINK, 'foo', 'user1', 'user2',
  1588. 'file', 42, 'target', 31, null, null);
  1589. $qb = $this->dbConn->getQueryBuilder();
  1590. $qb->update('share');
  1591. $qb->where($qb->expr()->eq('id', $qb->createNamedParameter($id)));
  1592. $qb->set('password', $qb->createNamedParameter('password'));
  1593. $this->assertEquals(1, $qb->execute());
  1594. $users = [];
  1595. for($i = 0; $i < 6; $i++) {
  1596. $user = $this->createMock(IUser::class);
  1597. $user->method('getUID')->willReturn('user'.$i);
  1598. $users['user'.$i] = $user;
  1599. }
  1600. $this->userManager->method('get')->will(
  1601. $this->returnCallback(function($userId) use ($users) {
  1602. return $users[$userId];
  1603. })
  1604. );
  1605. $file1 = $this->createMock(File::class);
  1606. $file1->method('getId')->willReturn(42);
  1607. $file2 = $this->createMock(File::class);
  1608. $file2->method('getId')->willReturn(43);
  1609. $folder1 = $this->createMock(Folder::class);
  1610. $folder1->method('getById')->with(42)->willReturn([$file1]);
  1611. $folder2 = $this->createMock(Folder::class);
  1612. $folder2->method('getById')->with(43)->willReturn([$file2]);
  1613. $this->rootFolder->method('getUserFolder')->will($this->returnValueMap([
  1614. ['user2', $folder1],
  1615. ['user5', $folder2],
  1616. ]));
  1617. $share = $this->provider->getShareById($id);
  1618. $share->setPassword(null);
  1619. $share->setSharedBy('user4');
  1620. $share->setShareOwner('user5');
  1621. $share->setNode($file2);
  1622. $share->setPermissions(1);
  1623. $share2 = $this->provider->update($share);
  1624. $this->assertEquals($id, $share2->getId());
  1625. $this->assertEquals(null, $share2->getPassword());
  1626. $this->assertSame('user4', $share2->getSharedBy());
  1627. $this->assertSame('user5', $share2->getShareOwner());
  1628. $this->assertSame(1, $share2->getPermissions());
  1629. $share2 = $this->provider->getShareById($id);
  1630. $this->assertEquals($id, $share2->getId());
  1631. $this->assertEquals(null, $share2->getPassword());
  1632. $this->assertSame('user4', $share2->getSharedBy());
  1633. $this->assertSame('user5', $share2->getShareOwner());
  1634. $this->assertSame(1, $share2->getPermissions());
  1635. }
  1636. public function testUpdateGroupNoSub() {
  1637. $id = $this->addShareToDB(\OCP\Share::SHARE_TYPE_GROUP, 'group0', 'user1', 'user2',
  1638. 'file', 42, 'target', 31, null, null);
  1639. $users = [];
  1640. for($i = 0; $i < 6; $i++) {
  1641. $user = $this->createMock(IUser::class);
  1642. $user->method('getUID')->willReturn('user'.$i);
  1643. $users['user'.$i] = $user;
  1644. }
  1645. $this->userManager->method('get')->will(
  1646. $this->returnCallback(function($userId) use ($users) {
  1647. return $users[$userId];
  1648. })
  1649. );
  1650. $groups = [];
  1651. for($i = 0; $i < 2; $i++) {
  1652. $group = $this->createMock(IGroup::class);
  1653. $group->method('getGID')->willReturn('group'.$i);
  1654. $groups['group'.$i] = $group;
  1655. }
  1656. $this->groupManager->method('get')->will(
  1657. $this->returnCallback(function($groupId) use ($groups) {
  1658. return $groups[$groupId];
  1659. })
  1660. );
  1661. $file1 = $this->createMock(File::class);
  1662. $file1->method('getId')->willReturn(42);
  1663. $file2 = $this->createMock(File::class);
  1664. $file2->method('getId')->willReturn(43);
  1665. $folder1 = $this->createMock(Folder::class);
  1666. $folder1->method('getById')->with(42)->willReturn([$file1]);
  1667. $folder2 = $this->createMock(Folder::class);
  1668. $folder2->method('getById')->with(43)->willReturn([$file2]);
  1669. $this->rootFolder->method('getUserFolder')->will($this->returnValueMap([
  1670. ['user2', $folder1],
  1671. ['user5', $folder2],
  1672. ]));
  1673. $share = $this->provider->getShareById($id);
  1674. $share->setSharedWith('group0');
  1675. $share->setSharedBy('user4');
  1676. $share->setShareOwner('user5');
  1677. $share->setNode($file2);
  1678. $share->setPermissions(1);
  1679. $share2 = $this->provider->update($share);
  1680. $this->assertEquals($id, $share2->getId());
  1681. // Group shares do not allow updating the recipient
  1682. $this->assertSame('group0', $share2->getSharedWith());
  1683. $this->assertSame('user4', $share2->getSharedBy());
  1684. $this->assertSame('user5', $share2->getShareOwner());
  1685. $this->assertSame(1, $share2->getPermissions());
  1686. $share2 = $this->provider->getShareById($id);
  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. }
  1694. public function testUpdateGroupSubShares() {
  1695. $id = $this->addShareToDB(\OCP\Share::SHARE_TYPE_GROUP, 'group0', 'user1', 'user2',
  1696. 'file', 42, 'target', 31, null, null);
  1697. $id2 = $this->addShareToDB(2, 'user0', 'user1', 'user2',
  1698. 'file', 42, 'mytarget', 31, null, null, $id);
  1699. $id3 = $this->addShareToDB(2, 'user3', 'user1', 'user2',
  1700. 'file', 42, 'mytarget2', 0, null, null, $id);
  1701. $users = [];
  1702. for($i = 0; $i < 6; $i++) {
  1703. $user = $this->createMock(IUser::class);
  1704. $user->method('getUID')->willReturn('user'.$i);
  1705. $users['user'.$i] = $user;
  1706. }
  1707. $this->userManager->method('get')->will(
  1708. $this->returnCallback(function($userId) use ($users) {
  1709. return $users[$userId];
  1710. })
  1711. );
  1712. $groups = [];
  1713. for($i = 0; $i < 2; $i++) {
  1714. $group = $this->createMock(IGroup::class);
  1715. $group->method('getGID')->willReturn('group'.$i);
  1716. $groups['group'.$i] = $group;
  1717. }
  1718. $this->groupManager->method('get')->will(
  1719. $this->returnCallback(function($groupId) use ($groups) {
  1720. return $groups[$groupId];
  1721. })
  1722. );
  1723. $file1 = $this->createMock(File::class);
  1724. $file1->method('getId')->willReturn(42);
  1725. $file2 = $this->createMock(File::class);
  1726. $file2->method('getId')->willReturn(43);
  1727. $folder1 = $this->createMock(Folder::class);
  1728. $folder1->method('getById')->with(42)->willReturn([$file1]);
  1729. $folder2 = $this->createMock(Folder::class);
  1730. $folder2->method('getById')->with(43)->willReturn([$file2]);
  1731. $this->rootFolder->method('getUserFolder')->will($this->returnValueMap([
  1732. ['user2', $folder1],
  1733. ['user5', $folder2],
  1734. ]));
  1735. $share = $this->provider->getShareById($id);
  1736. $share->setSharedWith('group0');
  1737. $share->setSharedBy('user4');
  1738. $share->setShareOwner('user5');
  1739. $share->setNode($file2);
  1740. $share->setPermissions(1);
  1741. $share2 = $this->provider->update($share);
  1742. $this->assertEquals($id, $share2->getId());
  1743. // Group shares do not allow updating the recipient
  1744. $this->assertSame('group0', $share2->getSharedWith());
  1745. $this->assertSame('user4', $share2->getSharedBy());
  1746. $this->assertSame('user5', $share2->getShareOwner());
  1747. $this->assertSame(1, $share2->getPermissions());
  1748. $share2 = $this->provider->getShareById($id);
  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. $qb = $this->dbConn->getQueryBuilder();
  1756. $stmt = $qb->select('*')
  1757. ->from('share')
  1758. ->where($qb->expr()->eq('parent', $qb->createNamedParameter($id)))
  1759. ->orderBy('id')
  1760. ->execute();
  1761. $shares = $stmt->fetchAll();
  1762. $this->assertSame('user0', $shares[0]['share_with']);
  1763. $this->assertSame('user4', $shares[0]['uid_initiator']);
  1764. $this->assertSame('user5', $shares[0]['uid_owner']);
  1765. $this->assertSame(1, (int)$shares[0]['permissions']);
  1766. $this->assertSame('user3', $shares[1]['share_with']);
  1767. $this->assertSame('user4', $shares[1]['uid_initiator']);
  1768. $this->assertSame('user5', $shares[1]['uid_owner']);
  1769. $this->assertSame(0, (int)$shares[1]['permissions']);
  1770. $stmt->closeCursor();
  1771. }
  1772. public function testMoveUserShare() {
  1773. $id = $this->addShareToDB(\OCP\Share::SHARE_TYPE_USER, 'user0', 'user1', 'user1', 'file',
  1774. 42, 'mytaret', 31, null, null);
  1775. $user0 = $this->createMock(IUser::class);
  1776. $user0->method('getUID')->willReturn('user0');
  1777. $user1 = $this->createMock(IUser::class);
  1778. $user1->method('getUID')->willReturn('user1');
  1779. $this->userManager->method('get')->will($this->returnValueMap([
  1780. ['user0', $user0],
  1781. ['user1', $user1],
  1782. ]));
  1783. $file = $this->createMock(File::class);
  1784. $file->method('getId')->willReturn(42);
  1785. $this->rootFolder->method('getUserFolder')->with('user1')->will($this->returnSelf());
  1786. $this->rootFolder->method('getById')->willReturn([$file]);
  1787. $share = $this->provider->getShareById($id, null);
  1788. $share->setTarget('/newTarget');
  1789. $this->provider->move($share, $user0);
  1790. $share = $this->provider->getShareById($id, null);
  1791. $this->assertSame('/newTarget', $share->getTarget());
  1792. }
  1793. public function testMoveGroupShare() {
  1794. $id = $this->addShareToDB(\OCP\Share::SHARE_TYPE_GROUP, 'group0', 'user1', 'user1', 'file',
  1795. 42, 'mytaret', 31, null, null);
  1796. $user0 = $this->createMock(IUser::class);
  1797. $user0->method('getUID')->willReturn('user0');
  1798. $user1 = $this->createMock(IUser::class);
  1799. $user1->method('getUID')->willReturn('user1');
  1800. $group0 = $this->createMock(IGroup::class);
  1801. $group0->method('getGID')->willReturn('group0');
  1802. $group0->method('inGroup')->with($user0)->willReturn(true);
  1803. $this->groupManager->method('get')->with('group0')->willReturn($group0);
  1804. $this->userManager->method('get')->will($this->returnValueMap([
  1805. ['user0', $user0],
  1806. ['user1', $user1],
  1807. ]));
  1808. $folder = $this->createMock(Folder::class);
  1809. $folder->method('getId')->willReturn(42);
  1810. $this->rootFolder->method('getUserFolder')->with('user1')->will($this->returnSelf());
  1811. $this->rootFolder->method('getById')->willReturn([$folder]);
  1812. $share = $this->provider->getShareById($id, 'user0');
  1813. $share->setTarget('/newTarget');
  1814. $this->provider->move($share, 'user0');
  1815. $share = $this->provider->getShareById($id, 'user0');
  1816. $this->assertSame('/newTarget', $share->getTarget());
  1817. $share->setTarget('/ultraNewTarget');
  1818. $this->provider->move($share, 'user0');
  1819. $share = $this->provider->getShareById($id, 'user0');
  1820. $this->assertSame('/ultraNewTarget', $share->getTarget());
  1821. }
  1822. public function dataDeleteUser() {
  1823. return [
  1824. [\OCP\Share::SHARE_TYPE_USER, 'a', 'b', 'c', 'a', true],
  1825. [\OCP\Share::SHARE_TYPE_USER, 'a', 'b', 'c', 'b', false],
  1826. [\OCP\Share::SHARE_TYPE_USER, 'a', 'b', 'c', 'c', true],
  1827. [\OCP\Share::SHARE_TYPE_USER, 'a', 'b', 'c', 'd', false],
  1828. [\OCP\Share::SHARE_TYPE_GROUP, 'a', 'b', 'c', 'a', true],
  1829. [\OCP\Share::SHARE_TYPE_GROUP, 'a', 'b', 'c', 'b', false],
  1830. // The group c is still valid but user c is deleted so group share stays
  1831. [\OCP\Share::SHARE_TYPE_GROUP, 'a', 'b', 'c', 'c', false],
  1832. [\OCP\Share::SHARE_TYPE_GROUP, 'a', 'b', 'c', 'd', false],
  1833. [\OCP\Share::SHARE_TYPE_LINK, 'a', 'b', 'c', 'a', true],
  1834. // To avoid invisible link shares delete initiated link shares as well (see #22327)
  1835. [\OCP\Share::SHARE_TYPE_LINK, 'a', 'b', 'c', 'b', true],
  1836. [\OCP\Share::SHARE_TYPE_LINK, 'a', 'b', 'c', 'c', false],
  1837. [\OCP\Share::SHARE_TYPE_LINK, 'a', 'b', 'c', 'd', false],
  1838. ];
  1839. }
  1840. /**
  1841. * @dataProvider dataDeleteUser
  1842. *
  1843. * @param int $type The shareType (user/group/link)
  1844. * @param string $owner The owner of the share (uid)
  1845. * @param string $initiator The initiator of the share (uid)
  1846. * @param string $recipient The recipient of the share (uid/gid/pass)
  1847. * @param string $deletedUser The user that is deleted
  1848. * @param bool $rowDeleted Is the row deleted in this setup
  1849. */
  1850. public function testDeleteUser($type, $owner, $initiator, $recipient, $deletedUser, $rowDeleted) {
  1851. $qb = $this->dbConn->getQueryBuilder();
  1852. $qb->insert('share')
  1853. ->setValue('share_type', $qb->createNamedParameter($type))
  1854. ->setValue('uid_owner', $qb->createNamedParameter($owner))
  1855. ->setValue('uid_initiator', $qb->createNamedParameter($initiator))
  1856. ->setValue('share_with', $qb->createNamedParameter($recipient))
  1857. ->setValue('item_type', $qb->createNamedParameter('file'))
  1858. ->setValue('item_source', $qb->createNamedParameter(42))
  1859. ->setValue('file_source', $qb->createNamedParameter(42))
  1860. ->execute();
  1861. $id = $qb->getLastInsertId();
  1862. $this->provider->userDeleted($deletedUser, $type);
  1863. $qb = $this->dbConn->getQueryBuilder();
  1864. $qb->select('*')
  1865. ->from('share')
  1866. ->where(
  1867. $qb->expr()->eq('id', $qb->createNamedParameter($id))
  1868. );
  1869. $cursor = $qb->execute();
  1870. $data = $cursor->fetchAll();
  1871. $cursor->closeCursor();
  1872. $this->assertCount($rowDeleted ? 0 : 1, $data);
  1873. }
  1874. public function dataDeleteUserGroup() {
  1875. return [
  1876. ['a', 'b', 'c', 'a', true, true],
  1877. ['a', 'b', 'c', 'b', false, false],
  1878. ['a', 'b', 'c', 'c', false, true],
  1879. ['a', 'b', 'c', 'd', false, false],
  1880. ];
  1881. }
  1882. /**
  1883. * @dataProvider dataDeleteUserGroup
  1884. *
  1885. * @param string $owner The owner of the share (uid)
  1886. * @param string $initiator The initiator of the share (uid)
  1887. * @param string $recipient The recipient of the usergroup share (uid)
  1888. * @param string $deletedUser The user that is deleted
  1889. * @param bool $groupShareDeleted
  1890. * @param bool $userGroupShareDeleted
  1891. */
  1892. public function testDeleteUserGroup($owner, $initiator, $recipient, $deletedUser, $groupShareDeleted, $userGroupShareDeleted) {
  1893. $qb = $this->dbConn->getQueryBuilder();
  1894. $qb->insert('share')
  1895. ->setValue('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_GROUP))
  1896. ->setValue('uid_owner', $qb->createNamedParameter($owner))
  1897. ->setValue('uid_initiator', $qb->createNamedParameter($initiator))
  1898. ->setValue('share_with', $qb->createNamedParameter('group'))
  1899. ->setValue('item_type', $qb->createNamedParameter('file'))
  1900. ->setValue('item_source', $qb->createNamedParameter(42))
  1901. ->setValue('file_source', $qb->createNamedParameter(42))
  1902. ->execute();
  1903. $groupId = $qb->getLastInsertId();
  1904. $qb = $this->dbConn->getQueryBuilder();
  1905. $qb->insert('share')
  1906. ->setValue('share_type', $qb->createNamedParameter(2))
  1907. ->setValue('uid_owner', $qb->createNamedParameter($owner))
  1908. ->setValue('uid_initiator', $qb->createNamedParameter($initiator))
  1909. ->setValue('share_with', $qb->createNamedParameter($recipient))
  1910. ->setValue('item_type', $qb->createNamedParameter('file'))
  1911. ->setValue('item_source', $qb->createNamedParameter(42))
  1912. ->setValue('file_source', $qb->createNamedParameter(42))
  1913. ->execute();
  1914. $userGroupId = $qb->getLastInsertId();
  1915. $this->provider->userDeleted($deletedUser, \OCP\Share::SHARE_TYPE_GROUP);
  1916. $qb = $this->dbConn->getQueryBuilder();
  1917. $qb->select('*')
  1918. ->from('share')
  1919. ->where(
  1920. $qb->expr()->eq('id', $qb->createNamedParameter($userGroupId))
  1921. );
  1922. $cursor = $qb->execute();
  1923. $data = $cursor->fetchAll();
  1924. $cursor->closeCursor();
  1925. $this->assertCount($userGroupShareDeleted ? 0 : 1, $data);
  1926. $qb = $this->dbConn->getQueryBuilder();
  1927. $qb->select('*')
  1928. ->from('share')
  1929. ->where(
  1930. $qb->expr()->eq('id', $qb->createNamedParameter($groupId))
  1931. );
  1932. $cursor = $qb->execute();
  1933. $data = $cursor->fetchAll();
  1934. $cursor->closeCursor();
  1935. $this->assertCount($groupShareDeleted ? 0 : 1, $data);
  1936. }
  1937. public function dataGroupDeleted() {
  1938. return [
  1939. [
  1940. [
  1941. 'type' => \OCP\Share::SHARE_TYPE_USER,
  1942. 'recipient' => 'user',
  1943. 'children' => []
  1944. ], 'group', false
  1945. ],
  1946. [
  1947. [
  1948. 'type' => \OCP\Share::SHARE_TYPE_USER,
  1949. 'recipient' => 'user',
  1950. 'children' => []
  1951. ], 'user', false
  1952. ],
  1953. [
  1954. [
  1955. 'type' => \OCP\Share::SHARE_TYPE_LINK,
  1956. 'recipient' => 'user',
  1957. 'children' => []
  1958. ], 'group', false
  1959. ],
  1960. [
  1961. [
  1962. 'type' => \OCP\Share::SHARE_TYPE_GROUP,
  1963. 'recipient' => 'group1',
  1964. 'children' => [
  1965. 'foo',
  1966. 'bar'
  1967. ]
  1968. ], 'group2', false
  1969. ],
  1970. [
  1971. [
  1972. 'type' => \OCP\Share::SHARE_TYPE_GROUP,
  1973. 'recipient' => 'group1',
  1974. 'children' => [
  1975. 'foo',
  1976. 'bar'
  1977. ]
  1978. ], 'group1', true
  1979. ],
  1980. ];
  1981. }
  1982. /**
  1983. * @dataProvider dataGroupDeleted
  1984. *
  1985. * @param $shares
  1986. * @param $groupToDelete
  1987. * @param $shouldBeDeleted
  1988. */
  1989. public function testGroupDeleted($shares, $groupToDelete, $shouldBeDeleted) {
  1990. $qb = $this->dbConn->getQueryBuilder();
  1991. $qb->insert('share')
  1992. ->setValue('share_type', $qb->createNamedParameter($shares['type']))
  1993. ->setValue('uid_owner', $qb->createNamedParameter('owner'))
  1994. ->setValue('uid_initiator', $qb->createNamedParameter('initiator'))
  1995. ->setValue('share_with', $qb->createNamedParameter($shares['recipient']))
  1996. ->setValue('item_type', $qb->createNamedParameter('file'))
  1997. ->setValue('item_source', $qb->createNamedParameter(42))
  1998. ->setValue('file_source', $qb->createNamedParameter(42))
  1999. ->execute();
  2000. $ids = [$qb->getLastInsertId()];
  2001. foreach ($shares['children'] as $child) {
  2002. $qb = $this->dbConn->getQueryBuilder();
  2003. $qb->insert('share')
  2004. ->setValue('share_type', $qb->createNamedParameter(2))
  2005. ->setValue('uid_owner', $qb->createNamedParameter('owner'))
  2006. ->setValue('uid_initiator', $qb->createNamedParameter('initiator'))
  2007. ->setValue('share_with', $qb->createNamedParameter($child))
  2008. ->setValue('item_type', $qb->createNamedParameter('file'))
  2009. ->setValue('item_source', $qb->createNamedParameter(42))
  2010. ->setValue('file_source', $qb->createNamedParameter(42))
  2011. ->setValue('parent', $qb->createNamedParameter($ids[0]))
  2012. ->execute();
  2013. $ids[] = $qb->getLastInsertId();
  2014. }
  2015. $this->provider->groupDeleted($groupToDelete);
  2016. $qb = $this->dbConn->getQueryBuilder();
  2017. $cursor = $qb->select('*')
  2018. ->from('share')
  2019. ->where($qb->expr()->in('id', $qb->createNamedParameter($ids, IQueryBuilder::PARAM_INT_ARRAY)))
  2020. ->execute();
  2021. $data = $cursor->fetchAll();
  2022. $cursor->closeCursor();
  2023. $this->assertCount($shouldBeDeleted ? 0 : count($ids), $data);
  2024. }
  2025. public function dataUserDeletedFromGroup() {
  2026. return [
  2027. ['group1', 'user1', true],
  2028. ['group1', 'user2', false],
  2029. ['group2', 'user1', false],
  2030. ];
  2031. }
  2032. /**
  2033. * Given a group share with 'group1'
  2034. * And a user specific group share with 'user1'.
  2035. * User $user is deleted from group $gid.
  2036. *
  2037. * @dataProvider dataUserDeletedFromGroup
  2038. *
  2039. * @param string $group
  2040. * @param string $user
  2041. * @param bool $toDelete
  2042. */
  2043. public function testUserDeletedFromGroup($group, $user, $toDelete) {
  2044. $qb = $this->dbConn->getQueryBuilder();
  2045. $qb->insert('share')
  2046. ->setValue('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_GROUP))
  2047. ->setValue('uid_owner', $qb->createNamedParameter('owner'))
  2048. ->setValue('uid_initiator', $qb->createNamedParameter('initiator'))
  2049. ->setValue('share_with', $qb->createNamedParameter('group1'))
  2050. ->setValue('item_type', $qb->createNamedParameter('file'))
  2051. ->setValue('item_source', $qb->createNamedParameter(42))
  2052. ->setValue('file_source', $qb->createNamedParameter(42));
  2053. $qb->execute();
  2054. $id1 = $qb->getLastInsertId();
  2055. $qb = $this->dbConn->getQueryBuilder();
  2056. $qb->insert('share')
  2057. ->setValue('share_type', $qb->createNamedParameter(2))
  2058. ->setValue('uid_owner', $qb->createNamedParameter('owner'))
  2059. ->setValue('uid_initiator', $qb->createNamedParameter('initiator'))
  2060. ->setValue('share_with', $qb->createNamedParameter('user1'))
  2061. ->setValue('item_type', $qb->createNamedParameter('file'))
  2062. ->setValue('item_source', $qb->createNamedParameter(42))
  2063. ->setValue('file_source', $qb->createNamedParameter(42))
  2064. ->setValue('parent', $qb->createNamedParameter($id1));
  2065. $qb->execute();
  2066. $id2 = $qb->getLastInsertId();
  2067. $this->provider->userDeletedFromGroup($user, $group);
  2068. $qb = $this->dbConn->getQueryBuilder();
  2069. $qb->select('*')
  2070. ->from('share')
  2071. ->where($qb->expr()->eq('id', $qb->createNamedParameter($id2)));
  2072. $cursor = $qb->execute();
  2073. $data = $cursor->fetchAll();
  2074. $cursor->closeCursor();
  2075. $this->assertCount($toDelete ? 0 : 1, $data);
  2076. }
  2077. public function testGetSharesInFolder() {
  2078. $userManager = \OC::$server->getUserManager();
  2079. $groupManager = \OC::$server->getGroupManager();
  2080. $rootFolder = \OC::$server->getRootFolder();
  2081. $provider = new DefaultShareProvider(
  2082. $this->dbConn,
  2083. $userManager,
  2084. $groupManager,
  2085. $rootFolder,
  2086. $this->mailer,
  2087. $this->defaults,
  2088. $this->l10n,
  2089. $this->urlGenerator
  2090. );
  2091. $password = md5(time());
  2092. $u1 = $userManager->createUser('testShare1', $password);
  2093. $u2 = $userManager->createUser('testShare2', $password);
  2094. $u3 = $userManager->createUser('testShare3', $password);
  2095. $g1 = $groupManager->createGroup('group1');
  2096. $u1Folder = $rootFolder->getUserFolder($u1->getUID());
  2097. $folder1 = $u1Folder->newFolder('foo');
  2098. $file1 = $folder1->newFile('bar');
  2099. $folder2 = $folder1->newFolder('baz');
  2100. $shareManager = \OC::$server->getShareManager();
  2101. $share1 = $shareManager->newShare();
  2102. $share1->setNode($folder1)
  2103. ->setSharedBy($u1->getUID())
  2104. ->setSharedWith($u2->getUID())
  2105. ->setShareOwner($u1->getUID())
  2106. ->setShareType(\OCP\Share::SHARE_TYPE_USER)
  2107. ->setPermissions(\OCP\Constants::PERMISSION_ALL);
  2108. $share1 = $this->provider->create($share1);
  2109. $share2 = $shareManager->newShare();
  2110. $share2->setNode($file1)
  2111. ->setSharedBy($u2->getUID())
  2112. ->setSharedWith($u3->getUID())
  2113. ->setShareOwner($u1->getUID())
  2114. ->setShareType(\OCP\Share::SHARE_TYPE_USER)
  2115. ->setPermissions(\OCP\Constants::PERMISSION_READ);
  2116. $share2 = $this->provider->create($share2);
  2117. $share3 = $shareManager->newShare();
  2118. $share3->setNode($folder2)
  2119. ->setSharedBy($u2->getUID())
  2120. ->setShareOwner($u1->getUID())
  2121. ->setShareType(\OCP\Share::SHARE_TYPE_LINK)
  2122. ->setPermissions(\OCP\Constants::PERMISSION_READ);
  2123. $share3 = $this->provider->create($share3);
  2124. $share4 = $shareManager->newShare();
  2125. $share4->setNode($folder2)
  2126. ->setSharedBy($u1->getUID())
  2127. ->setSharedWith($g1->getGID())
  2128. ->setShareOwner($u1->getUID())
  2129. ->setShareType(\OCP\Share::SHARE_TYPE_GROUP)
  2130. ->setPermissions(\OCP\Constants::PERMISSION_READ);
  2131. $share4 = $this->provider->create($share4);
  2132. $result = $provider->getSharesInFolder($u1->getUID(), $folder1, false);
  2133. $this->assertCount(1, $result);
  2134. $shares = array_pop($result);
  2135. $this->assertCount(1, $shares);
  2136. $this->assertSame($folder2->getId(), $shares[0]->getNodeId());
  2137. $result = $provider->getSharesInFolder($u1->getUID(), $folder1, true);
  2138. $this->assertCount(2, $result);
  2139. $file_shares = $result[$file1->getId()];
  2140. $this->assertCount(1, $file_shares);
  2141. $this->assertSame($file1->getId(), $file_shares[0]->getNodeId());
  2142. $this->assertSame(\OCP\Share::SHARE_TYPE_USER, $file_shares[0]->getShareType());
  2143. $folder_shares = $result[$folder2->getId()];
  2144. $this->assertCount(2, $folder_shares);
  2145. $this->assertSame($folder2->getId(), $folder_shares[0]->getNodeId());
  2146. $this->assertSame($folder2->getId(), $folder_shares[1]->getNodeId());
  2147. $this->assertSame(\OCP\Share::SHARE_TYPE_LINK, $folder_shares[0]->getShareType());
  2148. $this->assertSame(\OCP\Share::SHARE_TYPE_GROUP, $folder_shares[1]->getShareType());
  2149. $provider->delete($share1);
  2150. $provider->delete($share2);
  2151. $provider->delete($share3);
  2152. $provider->delete($share4);
  2153. $u1->delete();
  2154. $u2->delete();
  2155. $u3->delete();
  2156. $g1->delete();
  2157. }
  2158. public function testGetAccessListNoCurrentAccessRequired() {
  2159. $userManager = \OC::$server->getUserManager();
  2160. $groupManager = \OC::$server->getGroupManager();
  2161. $rootFolder = \OC::$server->getRootFolder();
  2162. $provider = new DefaultShareProvider(
  2163. $this->dbConn,
  2164. $userManager,
  2165. $groupManager,
  2166. $rootFolder,
  2167. $this->mailer,
  2168. $this->defaults,
  2169. $this->l10n,
  2170. $this->urlGenerator
  2171. );
  2172. $u1 = $userManager->createUser('testShare1', 'test');
  2173. $u2 = $userManager->createUser('testShare2', 'test');
  2174. $u3 = $userManager->createUser('testShare3', 'test');
  2175. $u4 = $userManager->createUser('testShare4', 'test');
  2176. $u5 = $userManager->createUser('testShare5', 'test');
  2177. $g1 = $groupManager->createGroup('group1');
  2178. $g1->addUser($u3);
  2179. $g1->addUser($u4);
  2180. $u1Folder = $rootFolder->getUserFolder($u1->getUID());
  2181. $folder1 = $u1Folder->newFolder('foo');
  2182. $folder2 = $folder1->newFolder('baz');
  2183. $file1 = $folder2->newFile('bar');
  2184. $result = $provider->getAccessList([$folder1, $folder2, $file1], false);
  2185. $this->assertCount(0, $result['users']);
  2186. $this->assertFalse($result['public']);
  2187. $shareManager = \OC::$server->getShareManager();
  2188. $share1 = $shareManager->newShare();
  2189. $share1->setNode($folder1)
  2190. ->setSharedBy($u1->getUID())
  2191. ->setSharedWith($u2->getUID())
  2192. ->setShareOwner($u1->getUID())
  2193. ->setShareType(\OCP\Share::SHARE_TYPE_USER)
  2194. ->setPermissions(\OCP\Constants::PERMISSION_ALL);
  2195. $share1 = $this->provider->create($share1);
  2196. $share2 = $shareManager->newShare();
  2197. $share2->setNode($folder2)
  2198. ->setSharedBy($u2->getUID())
  2199. ->setSharedWith($g1->getGID())
  2200. ->setShareOwner($u1->getUID())
  2201. ->setShareType(\OCP\Share::SHARE_TYPE_GROUP)
  2202. ->setPermissions(\OCP\Constants::PERMISSION_ALL);
  2203. $share2 = $this->provider->create($share2);
  2204. $shareManager->deleteFromSelf($share2, $u4->getUID());
  2205. $share3 = $shareManager->newShare();
  2206. $share3->setNode($file1)
  2207. ->setSharedBy($u3->getUID())
  2208. ->setShareOwner($u1->getUID())
  2209. ->setShareType(\OCP\Share::SHARE_TYPE_LINK)
  2210. ->setPermissions(\OCP\Constants::PERMISSION_READ);
  2211. $share3 = $this->provider->create($share3);
  2212. $share4 = $shareManager->newShare();
  2213. $share4->setNode($file1)
  2214. ->setSharedBy($u3->getUID())
  2215. ->setSharedWith($u5->getUID())
  2216. ->setShareOwner($u1->getUID())
  2217. ->setShareType(\OCP\Share::SHARE_TYPE_USER)
  2218. ->setPermissions(\OCP\Constants::PERMISSION_READ);
  2219. $share4 = $this->provider->create($share4);
  2220. $result = $provider->getAccessList([$folder1, $folder2, $file1], false);
  2221. $this->assertCount(4, $result['users']);
  2222. $this->assertContains('testShare2', $result['users']);
  2223. $this->assertContains('testShare3', $result['users']);
  2224. $this->assertContains('testShare4', $result['users']);
  2225. $this->assertContains('testShare5', $result['users']);
  2226. $this->assertTrue($result['public']);
  2227. $provider->delete($share1);
  2228. $provider->delete($share2);
  2229. $provider->delete($share3);
  2230. $provider->delete($share4);
  2231. $u1->delete();
  2232. $u2->delete();
  2233. $u3->delete();
  2234. $u4->delete();
  2235. $u5->delete();
  2236. $g1->delete();
  2237. }
  2238. public function testGetAccessListCurrentAccessRequired() {
  2239. $userManager = \OC::$server->getUserManager();
  2240. $groupManager = \OC::$server->getGroupManager();
  2241. $rootFolder = \OC::$server->getRootFolder();
  2242. $provider = new DefaultShareProvider(
  2243. $this->dbConn,
  2244. $userManager,
  2245. $groupManager,
  2246. $rootFolder,
  2247. $this->mailer,
  2248. $this->defaults,
  2249. $this->l10n,
  2250. $this->urlGenerator
  2251. );
  2252. $u1 = $userManager->createUser('testShare1', 'test');
  2253. $u2 = $userManager->createUser('testShare2', 'test');
  2254. $u3 = $userManager->createUser('testShare3', 'test');
  2255. $u4 = $userManager->createUser('testShare4', 'test');
  2256. $u5 = $userManager->createUser('testShare5', 'test');
  2257. $g1 = $groupManager->createGroup('group1');
  2258. $g1->addUser($u3);
  2259. $g1->addUser($u4);
  2260. $u1Folder = $rootFolder->getUserFolder($u1->getUID());
  2261. $folder1 = $u1Folder->newFolder('foo');
  2262. $folder2 = $folder1->newFolder('baz');
  2263. $file1 = $folder2->newFile('bar');
  2264. $result = $provider->getAccessList([$folder1, $folder2, $file1], false);
  2265. $this->assertCount(0, $result['users']);
  2266. $this->assertFalse($result['public']);
  2267. $shareManager = \OC::$server->getShareManager();
  2268. $share1 = $shareManager->newShare();
  2269. $share1->setNode($folder1)
  2270. ->setSharedBy($u1->getUID())
  2271. ->setSharedWith($u2->getUID())
  2272. ->setShareOwner($u1->getUID())
  2273. ->setShareType(\OCP\Share::SHARE_TYPE_USER)
  2274. ->setPermissions(\OCP\Constants::PERMISSION_ALL);
  2275. $share1 = $this->provider->create($share1);
  2276. $share2 = $shareManager->newShare();
  2277. $share2->setNode($folder2)
  2278. ->setSharedBy($u2->getUID())
  2279. ->setSharedWith($g1->getGID())
  2280. ->setShareOwner($u1->getUID())
  2281. ->setShareType(\OCP\Share::SHARE_TYPE_GROUP)
  2282. ->setPermissions(\OCP\Constants::PERMISSION_ALL);
  2283. $share2 = $this->provider->create($share2);
  2284. $shareManager->deleteFromSelf($share2, $u4->getUID());
  2285. $share3 = $shareManager->newShare();
  2286. $share3->setNode($file1)
  2287. ->setSharedBy($u3->getUID())
  2288. ->setShareOwner($u1->getUID())
  2289. ->setShareType(\OCP\Share::SHARE_TYPE_LINK)
  2290. ->setPermissions(\OCP\Constants::PERMISSION_READ);
  2291. $share3 = $this->provider->create($share3);
  2292. $share4 = $shareManager->newShare();
  2293. $share4->setNode($file1)
  2294. ->setSharedBy($u3->getUID())
  2295. ->setSharedWith($u5->getUID())
  2296. ->setShareOwner($u1->getUID())
  2297. ->setShareType(\OCP\Share::SHARE_TYPE_USER)
  2298. ->setPermissions(\OCP\Constants::PERMISSION_READ);
  2299. $share4 = $this->provider->create($share4);
  2300. $result = $provider->getAccessList([$folder1, $folder2, $file1], true);
  2301. $this->assertCount(3, $result['users']);
  2302. $this->assertArrayHasKey('testShare2', $result['users']);
  2303. $this->assertArrayHasKey('testShare3', $result['users']);
  2304. $this->assertArrayHasKey('testShare5', $result['users']);
  2305. $this->assertTrue($result['public']);
  2306. $provider->delete($share1);
  2307. $provider->delete($share2);
  2308. $provider->delete($share3);
  2309. $provider->delete($share4);
  2310. $u1->delete();
  2311. $u2->delete();
  2312. $u3->delete();
  2313. $u4->delete();
  2314. $u5->delete();
  2315. $g1->delete();
  2316. }
  2317. }