DefaultShareProviderTest.php 106 KB

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