DefaultShareProviderTest.php 101 KB

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