DefaultShareProviderTest.php 101 KB

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