DefaultShareProviderTest.php 100 KB

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