DefaultShareProviderTest.php 103 KB

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