DefaultShareProviderTest.php 91 KB

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