DefaultShareProviderTest.php 101 KB

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