1
0

DefaultShareProviderTest.php 102 KB

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