ManagerTest.php 95 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194
  1. <?php
  2. /**
  3. * @author Roeland Jago Douma <rullzer@owncloud.com>
  4. *
  5. * @copyright Copyright (c) 2015, ownCloud, Inc.
  6. * @license AGPL-3.0
  7. *
  8. * This code is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License, version 3,
  10. * as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License, version 3,
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>
  19. *
  20. */
  21. namespace Test\Share20;
  22. use OC\Files\Mount\MoveableMount;
  23. use OC\HintException;
  24. use OC\Share20\DefaultShareProvider;
  25. use OCP\Defaults;
  26. use OCP\Files\File;
  27. use OCP\Files\Folder;
  28. use OCP\Files\IRootFolder;
  29. use OCP\Files\Mount\IMountPoint;
  30. use OCP\Files\Node;
  31. use OCP\Files\Storage;
  32. use OCP\IGroup;
  33. use OCP\IServerContainer;
  34. use OCP\IURLGenerator;
  35. use OCP\IUser;
  36. use OCP\IUserManager;
  37. use OCP\L10N\IFactory;
  38. use OCP\Mail\IMailer;
  39. use OCP\Share\Exceptions\ShareNotFound;
  40. use OCP\Share\IProviderFactory;
  41. use OCP\Share\IShare;
  42. use OC\Share20\Manager;
  43. use OC\Share20\Exception;
  44. use OC\Share20\Share;
  45. use OCP\IL10N;
  46. use OCP\ILogger;
  47. use OCP\IConfig;
  48. use OCP\Share\IShareProvider;
  49. use OCP\Security\ISecureRandom;
  50. use OCP\Security\IHasher;
  51. use OCP\Files\Mount\IMountManager;
  52. use OCP\IGroupManager;
  53. use Symfony\Component\EventDispatcher\EventDispatcher;
  54. use Symfony\Component\EventDispatcher\GenericEvent;
  55. /**
  56. * Class ManagerTest
  57. *
  58. * @package Test\Share20
  59. * @group DB
  60. */
  61. class ManagerTest extends \Test\TestCase {
  62. /** @var Manager */
  63. protected $manager;
  64. /** @var ILogger|\PHPUnit_Framework_MockObject_MockObject */
  65. protected $logger;
  66. /** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */
  67. protected $config;
  68. /** @var ISecureRandom|\PHPUnit_Framework_MockObject_MockObject */
  69. protected $secureRandom;
  70. /** @var IHasher|\PHPUnit_Framework_MockObject_MockObject */
  71. protected $hasher;
  72. /** @var IShareProvider|\PHPUnit_Framework_MockObject_MockObject */
  73. protected $defaultProvider;
  74. /** @var IMountManager|\PHPUnit_Framework_MockObject_MockObject */
  75. protected $mountManager;
  76. /** @var IGroupManager|\PHPUnit_Framework_MockObject_MockObject */
  77. protected $groupManager;
  78. /** @var IL10N|\PHPUnit_Framework_MockObject_MockObject */
  79. protected $l;
  80. /** @var IFactory|\PHPUnit_Framework_MockObject_MockObject */
  81. protected $l10nFactory;
  82. /** @var DummyFactory */
  83. protected $factory;
  84. /** @var IUserManager|\PHPUnit_Framework_MockObject_MockObject */
  85. protected $userManager;
  86. /** @var IRootFolder | \PHPUnit_Framework_MockObject_MockObject */
  87. protected $rootFolder;
  88. /** @var EventDispatcher | \PHPUnit_Framework_MockObject_MockObject */
  89. protected $eventDispatcher;
  90. /** @var IMailer|\PHPUnit_Framework_MockObject_MockObject */
  91. protected $mailer;
  92. /** @var IURLGenerator|\PHPUnit_Framework_MockObject_MockObject */
  93. protected $urlGenerator;
  94. /** @var \OC_Defaults|\PHPUnit_Framework_MockObject_MockObject */
  95. protected $defaults;
  96. public function setUp() {
  97. $this->logger = $this->createMock(ILogger::class);
  98. $this->config = $this->createMock(IConfig::class);
  99. $this->secureRandom = $this->createMock(ISecureRandom::class);
  100. $this->hasher = $this->createMock(IHasher::class);
  101. $this->mountManager = $this->createMock(IMountManager::class);
  102. $this->groupManager = $this->createMock(IGroupManager::class);
  103. $this->userManager = $this->createMock(IUserManager::class);
  104. $this->rootFolder = $this->createMock(IRootFolder::class);
  105. $this->eventDispatcher = $this->createMock(EventDispatcher::class);
  106. $this->mailer = $this->createMock(IMailer::class);
  107. $this->urlGenerator = $this->createMock(IURLGenerator::class);
  108. $this->defaults = $this->createMock(\OC_Defaults::class);
  109. $this->l10nFactory = $this->createMock(IFactory::class);
  110. $this->l = $this->createMock(IL10N::class);
  111. $this->l->method('t')
  112. ->will($this->returnCallback(function($text, $parameters = []) {
  113. return vsprintf($text, $parameters);
  114. }));
  115. $this->factory = new DummyFactory(\OC::$server);
  116. $this->manager = new Manager(
  117. $this->logger,
  118. $this->config,
  119. $this->secureRandom,
  120. $this->hasher,
  121. $this->mountManager,
  122. $this->groupManager,
  123. $this->l,
  124. $this->l10nFactory,
  125. $this->factory,
  126. $this->userManager,
  127. $this->rootFolder,
  128. $this->eventDispatcher,
  129. $this->mailer,
  130. $this->urlGenerator,
  131. $this->defaults
  132. );
  133. $this->defaultProvider = $this->createMock(DefaultShareProvider::class);
  134. $this->defaultProvider->method('identifier')->willReturn('default');
  135. $this->factory->setProvider($this->defaultProvider);
  136. }
  137. /**
  138. * @return \PHPUnit_Framework_MockObject_MockBuilder
  139. */
  140. private function createManagerMock() {
  141. return $this->getMockBuilder('\OC\Share20\Manager')
  142. ->setConstructorArgs([
  143. $this->logger,
  144. $this->config,
  145. $this->secureRandom,
  146. $this->hasher,
  147. $this->mountManager,
  148. $this->groupManager,
  149. $this->l,
  150. $this->l10nFactory,
  151. $this->factory,
  152. $this->userManager,
  153. $this->rootFolder,
  154. $this->eventDispatcher,
  155. $this->mailer,
  156. $this->urlGenerator,
  157. $this->defaults
  158. ]);
  159. }
  160. /**
  161. * @expectedException \InvalidArgumentException
  162. */
  163. public function testDeleteNoShareId() {
  164. $share = $this->manager->newShare();
  165. $this->manager->deleteShare($share);
  166. }
  167. public function dataTestDelete() {
  168. $user = $this->createMock(IUser::class);
  169. $user->method('getUID')->willReturn('sharedWithUser');
  170. $group = $this->createMock(IGroup::class);
  171. $group->method('getGID')->willReturn('sharedWithGroup');
  172. return [
  173. [\OCP\Share::SHARE_TYPE_USER, 'sharedWithUser'],
  174. [\OCP\Share::SHARE_TYPE_GROUP, 'sharedWithGroup'],
  175. [\OCP\Share::SHARE_TYPE_LINK, ''],
  176. [\OCP\Share::SHARE_TYPE_REMOTE, 'foo@bar.com'],
  177. ];
  178. }
  179. /**
  180. * @dataProvider dataTestDelete
  181. */
  182. public function testDelete($shareType, $sharedWith) {
  183. $manager = $this->createManagerMock()
  184. ->setMethods(['getShareById', 'deleteChildren'])
  185. ->getMock();
  186. $path = $this->createMock(File::class);
  187. $path->method('getId')->willReturn(1);
  188. $share = $this->manager->newShare();
  189. $share->setId(42)
  190. ->setProviderId('prov')
  191. ->setShareType($shareType)
  192. ->setSharedWith($sharedWith)
  193. ->setSharedBy('sharedBy')
  194. ->setNode($path)
  195. ->setTarget('myTarget');
  196. $manager->expects($this->once())->method('deleteChildren')->with($share);
  197. $this->defaultProvider
  198. ->expects($this->once())
  199. ->method('delete')
  200. ->with($share);
  201. $this->eventDispatcher->expects($this->at(0))
  202. ->method('dispatch')
  203. ->with(
  204. 'OCP\Share::preUnshare',
  205. $this->callBack(function(GenericEvent $e) use ($share) {
  206. return $e->getSubject() === $share;
  207. })
  208. );
  209. $this->eventDispatcher->expects($this->at(1))
  210. ->method('dispatch')
  211. ->with(
  212. 'OCP\Share::postUnshare',
  213. $this->callBack(function(GenericEvent $e) use ($share) {
  214. return $e->getSubject() === $share &&
  215. $e->getArgument('deletedShares') === [$share];
  216. })
  217. );
  218. $manager->deleteShare($share);
  219. }
  220. public function testDeleteLazyShare() {
  221. $manager = $this->createManagerMock()
  222. ->setMethods(['getShareById', 'deleteChildren'])
  223. ->getMock();
  224. $share = $this->manager->newShare();
  225. $share->setId(42)
  226. ->setProviderId('prov')
  227. ->setShareType(\OCP\Share::SHARE_TYPE_USER)
  228. ->setSharedWith('sharedWith')
  229. ->setSharedBy('sharedBy')
  230. ->setShareOwner('shareOwner')
  231. ->setTarget('myTarget')
  232. ->setNodeId(1)
  233. ->setNodeType('file');
  234. $this->rootFolder->expects($this->never())->method($this->anything());
  235. $manager->expects($this->once())->method('deleteChildren')->with($share);
  236. $this->defaultProvider
  237. ->expects($this->once())
  238. ->method('delete')
  239. ->with($share);
  240. $this->eventDispatcher->expects($this->at(0))
  241. ->method('dispatch')
  242. ->with(
  243. 'OCP\Share::preUnshare',
  244. $this->callBack(function(GenericEvent $e) use ($share) {
  245. return $e->getSubject() === $share;
  246. })
  247. );
  248. $this->eventDispatcher->expects($this->at(1))
  249. ->method('dispatch')
  250. ->with(
  251. 'OCP\Share::postUnshare',
  252. $this->callBack(function(GenericEvent $e) use ($share) {
  253. return $e->getSubject() === $share &&
  254. $e->getArgument('deletedShares') === [$share];
  255. })
  256. );
  257. $manager->deleteShare($share);
  258. }
  259. public function testDeleteNested() {
  260. $manager = $this->createManagerMock()
  261. ->setMethods(['getShareById'])
  262. ->getMock();
  263. $path = $this->createMock(File::class);
  264. $path->method('getId')->willReturn(1);
  265. $share1 = $this->manager->newShare();
  266. $share1->setId(42)
  267. ->setProviderId('prov')
  268. ->setShareType(\OCP\Share::SHARE_TYPE_USER)
  269. ->setSharedWith('sharedWith1')
  270. ->setSharedBy('sharedBy1')
  271. ->setNode($path)
  272. ->setTarget('myTarget1');
  273. $share2 = $this->manager->newShare();
  274. $share2->setId(43)
  275. ->setProviderId('prov')
  276. ->setShareType(\OCP\Share::SHARE_TYPE_GROUP)
  277. ->setSharedWith('sharedWith2')
  278. ->setSharedBy('sharedBy2')
  279. ->setNode($path)
  280. ->setTarget('myTarget2')
  281. ->setParent(42);
  282. $share3 = $this->manager->newShare();
  283. $share3->setId(44)
  284. ->setProviderId('prov')
  285. ->setShareType(\OCP\Share::SHARE_TYPE_LINK)
  286. ->setSharedBy('sharedBy3')
  287. ->setNode($path)
  288. ->setTarget('myTarget3')
  289. ->setParent(43);
  290. $this->defaultProvider
  291. ->method('getChildren')
  292. ->will($this->returnValueMap([
  293. [$share1, [$share2]],
  294. [$share2, [$share3]],
  295. [$share3, []],
  296. ]));
  297. $this->defaultProvider
  298. ->method('delete')
  299. ->withConsecutive($share3, $share2, $share1);
  300. $this->eventDispatcher->expects($this->at(0))
  301. ->method('dispatch')
  302. ->with(
  303. 'OCP\Share::preUnshare',
  304. $this->callBack(function(GenericEvent $e) use ($share1) {
  305. return $e->getSubject() === $share1;
  306. })
  307. );
  308. $this->eventDispatcher->expects($this->at(1))
  309. ->method('dispatch')
  310. ->with(
  311. 'OCP\Share::postUnshare',
  312. $this->callBack(function(GenericEvent $e) use ($share1, $share2, $share3) {
  313. return $e->getSubject() === $share1 &&
  314. $e->getArgument('deletedShares') === [$share3, $share2, $share1];
  315. })
  316. );
  317. $manager->deleteShare($share1);
  318. }
  319. public function testDeleteFromSelf() {
  320. $manager = $this->createManagerMock()
  321. ->setMethods(['getShareById'])
  322. ->getMock();
  323. $recipientId = 'unshareFrom';
  324. $share = $this->manager->newShare();
  325. $share->setId(42)
  326. ->setProviderId('prov')
  327. ->setShareType(\OCP\Share::SHARE_TYPE_USER)
  328. ->setSharedWith('sharedWith')
  329. ->setSharedBy('sharedBy')
  330. ->setShareOwner('shareOwner')
  331. ->setTarget('myTarget')
  332. ->setNodeId(1)
  333. ->setNodeType('file');
  334. $this->defaultProvider
  335. ->expects($this->once())
  336. ->method('deleteFromSelf')
  337. ->with($share, $recipientId);
  338. $this->eventDispatcher->expects($this->at(0))
  339. ->method('dispatch')
  340. ->with(
  341. 'OCP\Share::postUnshareFromSelf',
  342. $this->callBack(function(GenericEvent $e) use ($share) {
  343. return $e->getSubject() === $share;
  344. })
  345. );
  346. $manager->deleteFromSelf($share, $recipientId);
  347. }
  348. public function testDeleteChildren() {
  349. $manager = $this->createManagerMock()
  350. ->setMethods(['deleteShare'])
  351. ->getMock();
  352. $share = $this->createMock(IShare::class);
  353. $share->method('getShareType')->willReturn(\OCP\Share::SHARE_TYPE_USER);
  354. $child1 = $this->createMock(IShare::class);
  355. $child1->method('getShareType')->willReturn(\OCP\Share::SHARE_TYPE_USER);
  356. $child2 = $this->createMock(IShare::class);
  357. $child2->method('getShareType')->willReturn(\OCP\Share::SHARE_TYPE_USER);
  358. $child3 = $this->createMock(IShare::class);
  359. $child3->method('getShareType')->willReturn(\OCP\Share::SHARE_TYPE_USER);
  360. $shares = [
  361. $child1,
  362. $child2,
  363. $child3,
  364. ];
  365. $this->defaultProvider
  366. ->expects($this->exactly(4))
  367. ->method('getChildren')
  368. ->will($this->returnCallback(function($_share) use ($share, $shares) {
  369. if ($_share === $share) {
  370. return $shares;
  371. }
  372. return [];
  373. }));
  374. $this->defaultProvider
  375. ->expects($this->exactly(3))
  376. ->method('delete')
  377. ->withConsecutive($child1, $child2, $child3);
  378. $result = self::invokePrivate($manager, 'deleteChildren', [$share]);
  379. $this->assertSame($shares, $result);
  380. }
  381. public function testGetShareById() {
  382. $share = $this->createMock(IShare::class);
  383. $this->defaultProvider
  384. ->expects($this->once())
  385. ->method('getShareById')
  386. ->with(42)
  387. ->willReturn($share);
  388. $this->assertEquals($share, $this->manager->getShareById('default:42'));
  389. }
  390. /**
  391. * @expectedException \OCP\Share\Exceptions\ShareNotFound
  392. */
  393. public function testGetExpiredShareById() {
  394. $manager = $this->createManagerMock()
  395. ->setMethods(['deleteShare'])
  396. ->getMock();
  397. $date = new \DateTime();
  398. $date->setTime(0,0,0);
  399. $share = $this->manager->newShare();
  400. $share->setExpirationDate($date)
  401. ->setShareType(\OCP\Share::SHARE_TYPE_LINK);
  402. $this->defaultProvider->expects($this->once())
  403. ->method('getShareById')
  404. ->with('42')
  405. ->willReturn($share);
  406. $manager->expects($this->once())
  407. ->method('deleteShare')
  408. ->with($share);
  409. $manager->getShareById('default:42');
  410. }
  411. /**
  412. * @expectedException \InvalidArgumentException
  413. * @expectedExceptionMessage Passwords are enforced for link shares
  414. */
  415. public function testVerifyPasswordNullButEnforced() {
  416. $this->config->method('getAppValue')->will($this->returnValueMap([
  417. ['core', 'shareapi_enforce_links_password', 'no', 'yes'],
  418. ]));
  419. self::invokePrivate($this->manager, 'verifyPassword', [null]);
  420. }
  421. public function testVerifyPasswordNull() {
  422. $this->config->method('getAppValue')->will($this->returnValueMap([
  423. ['core', 'shareapi_enforce_links_password', 'no', 'no'],
  424. ]));
  425. $result = self::invokePrivate($this->manager, 'verifyPassword', [null]);
  426. $this->assertNull($result);
  427. }
  428. public function testVerifyPasswordHook() {
  429. $this->config->method('getAppValue')->will($this->returnValueMap([
  430. ['core', 'shareapi_enforce_links_password', 'no', 'no'],
  431. ]));
  432. $this->eventDispatcher->expects($this->once())->method('dispatch')
  433. ->willReturnCallback(function($eventName, GenericEvent $event) {
  434. $this->assertSame('OCP\PasswordPolicy::validate', $eventName);
  435. $this->assertSame('password', $event->getSubject());
  436. }
  437. );
  438. $result = self::invokePrivate($this->manager, 'verifyPassword', ['password']);
  439. $this->assertNull($result);
  440. }
  441. /**
  442. * @expectedException \Exception
  443. * @expectedExceptionMessage password not accepted
  444. */
  445. public function testVerifyPasswordHookFails() {
  446. $this->config->method('getAppValue')->will($this->returnValueMap([
  447. ['core', 'shareapi_enforce_links_password', 'no', 'no'],
  448. ]));
  449. $this->eventDispatcher->expects($this->once())->method('dispatch')
  450. ->willReturnCallback(function($eventName, GenericEvent $event) {
  451. $this->assertSame('OCP\PasswordPolicy::validate', $eventName);
  452. $this->assertSame('password', $event->getSubject());
  453. throw new HintException('message', 'password not accepted');
  454. }
  455. );
  456. self::invokePrivate($this->manager, 'verifyPassword', ['password']);
  457. }
  458. public function createShare($id, $type, $path, $sharedWith, $sharedBy, $shareOwner,
  459. $permissions, $expireDate = null, $password = null) {
  460. $share = $this->createMock(IShare::class);
  461. $share->method('getShareType')->willReturn($type);
  462. $share->method('getSharedWith')->willReturn($sharedWith);
  463. $share->method('getSharedBy')->willReturn($sharedBy);
  464. $share->method('getShareOwner')->willReturn($shareOwner);
  465. $share->method('getNode')->willReturn($path);
  466. $share->method('getPermissions')->willReturn($permissions);
  467. $share->method('getExpirationDate')->willReturn($expireDate);
  468. $share->method('getPassword')->willReturn($password);
  469. return $share;
  470. }
  471. public function dataGeneralChecks() {
  472. $user0 = 'user0';
  473. $user2 = 'user1';
  474. $group0 = 'group0';
  475. $file = $this->createMock(File::class);
  476. $node = $this->createMock(Node::class);
  477. $data = [
  478. [$this->createShare(null, \OCP\Share::SHARE_TYPE_USER, $file, null, $user0, $user0, 31, null, null), 'SharedWith is not a valid user', true],
  479. [$this->createShare(null, \OCP\Share::SHARE_TYPE_USER, $file, $group0, $user0, $user0, 31, null, null), 'SharedWith is not a valid user', true],
  480. [$this->createShare(null, \OCP\Share::SHARE_TYPE_USER, $file, 'foo@bar.com', $user0, $user0, 31, null, null), 'SharedWith is not a valid user', true],
  481. [$this->createShare(null, \OCP\Share::SHARE_TYPE_GROUP, $file, null, $user0, $user0, 31, null, null), 'SharedWith is not a valid group', true],
  482. [$this->createShare(null, \OCP\Share::SHARE_TYPE_GROUP, $file, $user2, $user0, $user0, 31, null, null), 'SharedWith is not a valid group', true],
  483. [$this->createShare(null, \OCP\Share::SHARE_TYPE_GROUP, $file, 'foo@bar.com', $user0, $user0, 31, null, null), 'SharedWith is not a valid group', true],
  484. [$this->createShare(null, \OCP\Share::SHARE_TYPE_LINK, $file, $user2, $user0, $user0, 31, null, null), 'SharedWith should be empty', true],
  485. [$this->createShare(null, \OCP\Share::SHARE_TYPE_LINK, $file, $group0, $user0, $user0, 31, null, null), 'SharedWith should be empty', true],
  486. [$this->createShare(null, \OCP\Share::SHARE_TYPE_LINK, $file, 'foo@bar.com', $user0, $user0, 31, null, null), 'SharedWith should be empty', true],
  487. [$this->createShare(null, -1, $file, null, $user0, $user0, 31, null, null), 'unknown share type', true],
  488. [$this->createShare(null, \OCP\Share::SHARE_TYPE_USER, $file, $user2, null, $user0, 31, null, null), 'SharedBy should be set', true],
  489. [$this->createShare(null, \OCP\Share::SHARE_TYPE_GROUP, $file, $group0, null, $user0, 31, null, null), 'SharedBy should be set', true],
  490. [$this->createShare(null, \OCP\Share::SHARE_TYPE_LINK, $file, null, null, $user0, 31, null, null), 'SharedBy should be set', true],
  491. [$this->createShare(null, \OCP\Share::SHARE_TYPE_USER, $file, $user0, $user0, $user0, 31, null, null), 'Can’t share with yourself', true],
  492. [$this->createShare(null, \OCP\Share::SHARE_TYPE_USER, null, $user2, $user0, $user0, 31, null, null), 'Path should be set', true],
  493. [$this->createShare(null, \OCP\Share::SHARE_TYPE_GROUP, null, $group0, $user0, $user0, 31, null, null), 'Path should be set', true],
  494. [$this->createShare(null, \OCP\Share::SHARE_TYPE_LINK, null, null, $user0, $user0, 31, null, null), 'Path should be set', true],
  495. [$this->createShare(null, \OCP\Share::SHARE_TYPE_USER, $node, $user2, $user0, $user0, 31, null, null), 'Path should be either a file or a folder', true],
  496. [$this->createShare(null, \OCP\Share::SHARE_TYPE_GROUP, $node, $group0, $user0, $user0, 31, null, null), 'Path should be either a file or a folder', true],
  497. [$this->createShare(null, \OCP\Share::SHARE_TYPE_LINK, $node, null, $user0, $user0, 31, null, null), 'Path should be either a file or a folder', true],
  498. ];
  499. $nonShareAble = $this->createMock(Folder::class);
  500. $nonShareAble->method('isShareable')->willReturn(false);
  501. $nonShareAble->method('getPath')->willReturn('path');
  502. $data[] = [$this->createShare(null, \OCP\Share::SHARE_TYPE_USER, $nonShareAble, $user2, $user0, $user0, 31, null, null), 'You are not allowed to share path', true];
  503. $data[] = [$this->createShare(null, \OCP\Share::SHARE_TYPE_GROUP, $nonShareAble, $group0, $user0, $user0, 31, null, null), 'You are not allowed to share path', true];
  504. $data[] = [$this->createShare(null, \OCP\Share::SHARE_TYPE_LINK, $nonShareAble, null, $user0, $user0, 31, null, null), 'You are not allowed to share path', true];
  505. $limitedPermssions = $this->createMock(File::class);
  506. $limitedPermssions->method('isShareable')->willReturn(true);
  507. $limitedPermssions->method('getPermissions')->willReturn(\OCP\Constants::PERMISSION_READ);
  508. $limitedPermssions->method('getPath')->willReturn('path');
  509. $data[] = [$this->createShare(null, \OCP\Share::SHARE_TYPE_USER, $limitedPermssions, $user2, $user0, $user0, null, null, null), 'A share requires permissions', true];
  510. $data[] = [$this->createShare(null, \OCP\Share::SHARE_TYPE_GROUP, $limitedPermssions, $group0, $user0, $user0, null, null, null), 'A share requires permissions', true];
  511. $data[] = [$this->createShare(null, \OCP\Share::SHARE_TYPE_LINK, $limitedPermssions, null, $user0, $user0, null, null, null), 'A share requires permissions', true];
  512. $mount = $this->createMock(MoveableMount::class);
  513. $limitedPermssions->method('getMountPoint')->willReturn($mount);
  514. $data[] = [$this->createShare(null, \OCP\Share::SHARE_TYPE_USER, $limitedPermssions, $user2, $user0, $user0, 31, null, null), 'Can’t increase permissions of path', true];
  515. $data[] = [$this->createShare(null, \OCP\Share::SHARE_TYPE_GROUP, $limitedPermssions, $group0, $user0, $user0, 17, null, null), 'Can’t increase permissions of path', true];
  516. $data[] = [$this->createShare(null, \OCP\Share::SHARE_TYPE_LINK, $limitedPermssions, null, $user0, $user0, 3, null, null), 'Can’t increase permissions of path', true];
  517. $nonMoveableMountPermssions = $this->createMock(Folder::class);
  518. $nonMoveableMountPermssions->method('isShareable')->willReturn(true);
  519. $nonMoveableMountPermssions->method('getPermissions')->willReturn(\OCP\Constants::PERMISSION_READ);
  520. $nonMoveableMountPermssions->method('getPath')->willReturn('path');
  521. $data[] = [$this->createShare(null, \OCP\Share::SHARE_TYPE_USER, $nonMoveableMountPermssions, $user2, $user0, $user0, 11, null, null), 'Can’t increase permissions of path', false];
  522. $data[] = [$this->createShare(null, \OCP\Share::SHARE_TYPE_GROUP, $nonMoveableMountPermssions, $group0, $user0, $user0, 11, null, null), 'Can’t increase permissions of path', false];
  523. $rootFolder = $this->createMock(Folder::class);
  524. $rootFolder->method('isShareable')->willReturn(true);
  525. $rootFolder->method('getPermissions')->willReturn(\OCP\Constants::PERMISSION_ALL);
  526. $rootFolder->method('getPath')->willReturn('myrootfolder');
  527. $data[] = [$this->createShare(null, \OCP\Share::SHARE_TYPE_USER, $rootFolder, $user2, $user0, $user0, 30, null, null), 'You can’t share your root folder', true];
  528. $data[] = [$this->createShare(null, \OCP\Share::SHARE_TYPE_GROUP, $rootFolder, $group0, $user0, $user0, 2, null, null), 'You can’t share your root folder', true];
  529. $data[] = [$this->createShare(null, \OCP\Share::SHARE_TYPE_LINK, $rootFolder, null, $user0, $user0, 16, null, null), 'You can’t share your root folder', true];
  530. $allPermssions = $this->createMock(Folder::class);
  531. $allPermssions->method('isShareable')->willReturn(true);
  532. $allPermssions->method('getPermissions')->willReturn(\OCP\Constants::PERMISSION_ALL);
  533. $data[] = [$this->createShare(null, \OCP\Share::SHARE_TYPE_USER, $allPermssions, $user2, $user0, $user0, 30, null, null), 'Shares need at least read permissions', true];
  534. $data[] = [$this->createShare(null, \OCP\Share::SHARE_TYPE_GROUP, $allPermssions, $group0, $user0, $user0, 2, null, null), 'Shares need at least read permissions', true];
  535. $data[] = [$this->createShare(null, \OCP\Share::SHARE_TYPE_USER, $allPermssions, $user2, $user0, $user0, 31, null, null), null, false];
  536. $data[] = [$this->createShare(null, \OCP\Share::SHARE_TYPE_GROUP, $allPermssions, $group0, $user0, $user0, 3, null, null), null, false];
  537. $data[] = [$this->createShare(null, \OCP\Share::SHARE_TYPE_LINK, $allPermssions, null, $user0, $user0, 17, null, null), null, false];
  538. return $data;
  539. }
  540. /**
  541. * @dataProvider dataGeneralChecks
  542. *
  543. * @param $share
  544. * @param $exceptionMessage
  545. * @param $exception
  546. */
  547. public function testGeneralChecks($share, $exceptionMessage, $exception) {
  548. $thrown = null;
  549. $this->userManager->method('userExists')->will($this->returnValueMap([
  550. ['user0', true],
  551. ['user1', true],
  552. ]));
  553. $this->groupManager->method('groupExists')->will($this->returnValueMap([
  554. ['group0', true],
  555. ]));
  556. $userFolder = $this->createMock(Folder::class);
  557. $userFolder->method('getPath')->willReturn('myrootfolder');
  558. $this->rootFolder->method('getUserFolder')->willReturn($userFolder);
  559. try {
  560. self::invokePrivate($this->manager, 'generalCreateChecks', [$share]);
  561. $thrown = false;
  562. } catch (\OCP\Share\Exceptions\GenericShareException $e) {
  563. $this->assertEquals($exceptionMessage, $e->getHint());
  564. $thrown = true;
  565. } catch(\InvalidArgumentException $e) {
  566. $this->assertEquals($exceptionMessage, $e->getMessage());
  567. $thrown = true;
  568. }
  569. $this->assertSame($exception, $thrown);
  570. }
  571. /**
  572. * @expectedException \InvalidArgumentException
  573. * @expectedExceptionMessage You can’t share your root folder
  574. */
  575. public function testGeneralCheckShareRoot() {
  576. $thrown = null;
  577. $this->userManager->method('userExists')->will($this->returnValueMap([
  578. ['user0', true],
  579. ['user1', true],
  580. ]));
  581. $userFolder = $this->createMock(Folder::class);
  582. $userFolder->method('isSubNode')->with($userFolder)->willReturn(false);
  583. $this->rootFolder->method('getUserFolder')->willReturn($userFolder);
  584. $share = $this->manager->newShare();
  585. $share->setShareType(\OCP\Share::SHARE_TYPE_USER)
  586. ->setSharedWith('user0')
  587. ->setSharedBy('user1')
  588. ->setNode($userFolder);
  589. self::invokePrivate($this->manager, 'generalCreateChecks', [$share]);
  590. }
  591. /**
  592. * @expectedException \OCP\Share\Exceptions\GenericShareException
  593. * @expectedExceptionMessage Expiration date is in the past
  594. */
  595. public function testvalidateExpirationDateInPast() {
  596. // Expire date in the past
  597. $past = new \DateTime();
  598. $past->sub(new \DateInterval('P1D'));
  599. $share = $this->manager->newShare();
  600. $share->setExpirationDate($past);
  601. self::invokePrivate($this->manager, 'validateExpirationDate', [$share]);
  602. }
  603. /**
  604. * @expectedException \InvalidArgumentException
  605. * @expectedExceptionMessage Expiration date is enforced
  606. */
  607. public function testvalidateExpirationDateEnforceButNotSet() {
  608. $share = $this->manager->newShare();
  609. $share->setProviderId('foo')->setId('bar');
  610. $this->config->method('getAppValue')
  611. ->will($this->returnValueMap([
  612. ['core', 'shareapi_default_expire_date', 'no', 'yes'],
  613. ['core', 'shareapi_enforce_expire_date', 'no', 'yes'],
  614. ]));
  615. self::invokePrivate($this->manager, 'validateExpirationDate', [$share]);
  616. }
  617. public function testvalidateExpirationDateEnforceButNotEnabledAndNotSet() {
  618. $share = $this->manager->newShare();
  619. $share->setProviderId('foo')->setId('bar');
  620. $this->config->method('getAppValue')
  621. ->will($this->returnValueMap([
  622. ['core', 'shareapi_enforce_expire_date', 'no', 'yes'],
  623. ]));
  624. self::invokePrivate($this->manager, 'validateExpirationDate', [$share]);
  625. $this->assertNull($share->getExpirationDate());
  626. }
  627. public function testvalidateExpirationDateEnforceButNotSetNewShare() {
  628. $share = $this->manager->newShare();
  629. $this->config->method('getAppValue')
  630. ->will($this->returnValueMap([
  631. ['core', 'shareapi_enforce_expire_date', 'no', 'yes'],
  632. ['core', 'shareapi_expire_after_n_days', '7', '3'],
  633. ['core', 'shareapi_default_expire_date', 'no', 'yes'],
  634. ]));
  635. $expected = new \DateTime();
  636. $expected->setTime(0,0,0);
  637. $expected->add(new \DateInterval('P3D'));
  638. self::invokePrivate($this->manager, 'validateExpirationDate', [$share]);
  639. $this->assertNotNull($share->getExpirationDate());
  640. $this->assertEquals($expected, $share->getExpirationDate());
  641. }
  642. public function testvalidateExpirationDateEnforceToFarIntoFuture() {
  643. // Expire date in the past
  644. $future = new \DateTime();
  645. $future->add(new \DateInterval('P7D'));
  646. $share = $this->manager->newShare();
  647. $share->setExpirationDate($future);
  648. $this->config->method('getAppValue')
  649. ->will($this->returnValueMap([
  650. ['core', 'shareapi_enforce_expire_date', 'no', 'yes'],
  651. ['core', 'shareapi_expire_after_n_days', '7', '3'],
  652. ]));
  653. try {
  654. self::invokePrivate($this->manager, 'validateExpirationDate', [$share]);
  655. } catch (\OCP\Share\Exceptions\GenericShareException $e) {
  656. $this->assertEquals('Cannot set expiration date more than 3 days in the future', $e->getMessage());
  657. $this->assertEquals('Cannot set expiration date more than 3 days in the future', $e->getHint());
  658. $this->assertEquals(404, $e->getCode());
  659. }
  660. }
  661. public function testvalidateExpirationDateEnforceValid() {
  662. // Expire date in the past
  663. $future = new \DateTime();
  664. $future->add(new \DateInterval('P2D'));
  665. $future->setTime(0,0,0);
  666. $expected = clone $future;
  667. $future->setTime(1,2,3);
  668. $share = $this->manager->newShare();
  669. $share->setExpirationDate($future);
  670. $this->config->method('getAppValue')
  671. ->will($this->returnValueMap([
  672. ['core', 'shareapi_enforce_expire_date', 'no', 'yes'],
  673. ['core', 'shareapi_expire_after_n_days', '7', '3'],
  674. ]));
  675. $hookListner = $this->getMockBuilder('Dummy')->setMethods(['listener'])->getMock();
  676. \OCP\Util::connectHook('\OC\Share', 'verifyExpirationDate', $hookListner, 'listener');
  677. $hookListner->expects($this->once())->method('listener')->with($this->callback(function ($data) use ($future) {
  678. return $data['expirationDate'] == $future;
  679. }));
  680. self::invokePrivate($this->manager, 'validateExpirationDate', [$share]);
  681. $this->assertEquals($expected, $share->getExpirationDate());
  682. }
  683. public function testvalidateExpirationDateNoDateNoDefaultNull() {
  684. $date = new \DateTime();
  685. $date->add(new \DateInterval('P5D'));
  686. $expected = clone $date;
  687. $expected->setTime(0,0,0);
  688. $share = $this->manager->newShare();
  689. $share->setExpirationDate($date);
  690. $hookListner = $this->getMockBuilder('Dummy')->setMethods(['listener'])->getMock();
  691. \OCP\Util::connectHook('\OC\Share', 'verifyExpirationDate', $hookListner, 'listener');
  692. $hookListner->expects($this->once())->method('listener')->with($this->callback(function ($data) use ($expected) {
  693. return $data['expirationDate'] == $expected && $data['passwordSet'] === false;
  694. }));
  695. self::invokePrivate($this->manager, 'validateExpirationDate', [$share]);
  696. $this->assertEquals($expected, $share->getExpirationDate());
  697. }
  698. public function testvalidateExpirationDateNoDateNoDefault() {
  699. $hookListner = $this->getMockBuilder('Dummy')->setMethods(['listener'])->getMock();
  700. \OCP\Util::connectHook('\OC\Share', 'verifyExpirationDate', $hookListner, 'listener');
  701. $hookListner->expects($this->once())->method('listener')->with($this->callback(function ($data) {
  702. return $data['expirationDate'] === null && $data['passwordSet'] === true;
  703. }));
  704. $share = $this->manager->newShare();
  705. $share->setPassword('password');
  706. self::invokePrivate($this->manager, 'validateExpirationDate', [$share]);
  707. $this->assertNull($share->getExpirationDate());
  708. }
  709. public function testvalidateExpirationDateNoDateDefault() {
  710. $future = new \DateTime();
  711. $future->add(new \DateInterval('P3D'));
  712. $future->setTime(0,0,0);
  713. $expected = clone $future;
  714. $share = $this->manager->newShare();
  715. $share->setExpirationDate($future);
  716. $this->config->method('getAppValue')
  717. ->will($this->returnValueMap([
  718. ['core', 'shareapi_default_expire_date', 'no', 'yes'],
  719. ['core', 'shareapi_expire_after_n_days', '7', '3'],
  720. ]));
  721. $hookListner = $this->getMockBuilder('Dummy')->setMethods(['listener'])->getMock();
  722. \OCP\Util::connectHook('\OC\Share', 'verifyExpirationDate', $hookListner, 'listener');
  723. $hookListner->expects($this->once())->method('listener')->with($this->callback(function ($data) use ($expected) {
  724. return $data['expirationDate'] == $expected;
  725. }));
  726. self::invokePrivate($this->manager, 'validateExpirationDate', [$share]);
  727. $this->assertEquals($expected, $share->getExpirationDate());
  728. }
  729. public function testValidateExpirationDateHookModification() {
  730. $nextWeek = new \DateTime();
  731. $nextWeek->add(new \DateInterval('P7D'));
  732. $nextWeek->setTime(0,0,0);
  733. $save = clone $nextWeek;
  734. $hookListner = $this->getMockBuilder('Dummy')->setMethods(['listener'])->getMock();
  735. \OCP\Util::connectHook('\OC\Share', 'verifyExpirationDate', $hookListner, 'listener');
  736. $hookListner->expects($this->once())->method('listener')->will($this->returnCallback(function ($data) {
  737. $data['expirationDate']->sub(new \DateInterval('P2D'));
  738. }));
  739. $share = $this->manager->newShare();
  740. $share->setExpirationDate($nextWeek);
  741. self::invokePrivate($this->manager, 'validateExpirationDate', [$share]);
  742. $save->sub(new \DateInterval('P2D'));
  743. $this->assertEquals($save, $share->getExpirationDate());
  744. }
  745. /**
  746. * @expectedException \Exception
  747. * @expectedExceptionMessage Invalid date!
  748. */
  749. public function testValidateExpirationDateHookException() {
  750. $nextWeek = new \DateTime();
  751. $nextWeek->add(new \DateInterval('P7D'));
  752. $nextWeek->setTime(0,0,0);
  753. $share = $this->manager->newShare();
  754. $share->setExpirationDate($nextWeek);
  755. $hookListner = $this->getMockBuilder('Dummy')->setMethods(['listener'])->getMock();
  756. \OCP\Util::connectHook('\OC\Share', 'verifyExpirationDate', $hookListner, 'listener');
  757. $hookListner->expects($this->once())->method('listener')->will($this->returnCallback(function ($data) {
  758. $data['accepted'] = false;
  759. $data['message'] = 'Invalid date!';
  760. }));
  761. self::invokePrivate($this->manager, 'validateExpirationDate', [$share]);
  762. }
  763. public function testValidateExpirationDateExistingShareNoDefault() {
  764. $share = $this->manager->newShare();
  765. $share->setId('42')->setProviderId('foo');
  766. $this->config->method('getAppValue')
  767. ->will($this->returnValueMap([
  768. ['core', 'shareapi_default_expire_date', 'no', 'yes'],
  769. ['core', 'shareapi_expire_after_n_days', '7', '6'],
  770. ]));
  771. self::invokePrivate($this->manager, 'validateExpirationDate', [$share]);
  772. $this->assertEquals(null, $share->getExpirationDate());
  773. }
  774. /**
  775. * @expectedException Exception
  776. * @expectedExceptionMessage Sharing is only allowed with group members
  777. */
  778. public function testUserCreateChecksShareWithGroupMembersOnlyDifferentGroups() {
  779. $share = $this->manager->newShare();
  780. $sharedBy = $this->createMock(IUser::class);
  781. $sharedWith = $this->createMock(IUser::class);
  782. $share->setSharedBy('sharedBy')->setSharedWith('sharedWith');
  783. $this->groupManager
  784. ->method('getUserGroupIds')
  785. ->will(
  786. $this->returnValueMap([
  787. [$sharedBy, ['group1']],
  788. [$sharedWith, ['group2']],
  789. ])
  790. );
  791. $this->userManager->method('get')->will($this->returnValueMap([
  792. ['sharedBy', $sharedBy],
  793. ['sharedWith', $sharedWith],
  794. ]));
  795. $this->config
  796. ->method('getAppValue')
  797. ->will($this->returnValueMap([
  798. ['core', 'shareapi_only_share_with_group_members', 'no', 'yes'],
  799. ]));
  800. self::invokePrivate($this->manager, 'userCreateChecks', [$share]);
  801. }
  802. public function testUserCreateChecksShareWithGroupMembersOnlySharedGroup() {
  803. $share = $this->manager->newShare();
  804. $sharedBy = $this->createMock(IUser::class);
  805. $sharedWith = $this->createMock(IUser::class);
  806. $share->setSharedBy('sharedBy')->setSharedWith('sharedWith');
  807. $path = $this->createMock(Node::class);
  808. $share->setNode($path);
  809. $this->groupManager
  810. ->method('getUserGroupIds')
  811. ->will(
  812. $this->returnValueMap([
  813. [$sharedBy, ['group1', 'group3']],
  814. [$sharedWith, ['group2', 'group3']],
  815. ])
  816. );
  817. $this->userManager->method('get')->will($this->returnValueMap([
  818. ['sharedBy', $sharedBy],
  819. ['sharedWith', $sharedWith],
  820. ]));
  821. $this->config
  822. ->method('getAppValue')
  823. ->will($this->returnValueMap([
  824. ['core', 'shareapi_only_share_with_group_members', 'no', 'yes'],
  825. ]));
  826. $this->defaultProvider
  827. ->method('getSharesByPath')
  828. ->with($path)
  829. ->willReturn([]);
  830. self::invokePrivate($this->manager, 'userCreateChecks', [$share]);
  831. }
  832. /**
  833. * @expectedException Exception
  834. * @expectedExceptionMessage Path is already shared with this user
  835. */
  836. public function testUserCreateChecksIdenticalShareExists() {
  837. $share = $this->manager->newShare();
  838. $share2 = $this->manager->newShare();
  839. $sharedWith = $this->createMock(IUser::class);
  840. $path = $this->createMock(Node::class);
  841. $share->setSharedWith('sharedWith')->setNode($path)
  842. ->setProviderId('foo')->setId('bar');
  843. $share2->setSharedWith('sharedWith')->setNode($path)
  844. ->setProviderId('foo')->setId('baz');
  845. $this->defaultProvider
  846. ->method('getSharesByPath')
  847. ->with($path)
  848. ->willReturn([$share2]);
  849. self::invokePrivate($this->manager, 'userCreateChecks', [$share]);
  850. }
  851. /**
  852. * @expectedException Exception
  853. * @expectedExceptionMessage Path is already shared with this user
  854. */
  855. public function testUserCreateChecksIdenticalPathSharedViaGroup() {
  856. $share = $this->manager->newShare();
  857. $sharedWith = $this->createMock(IUser::class);
  858. $sharedWith->method('getUID')->willReturn('sharedWith');
  859. $this->userManager->method('get')->with('sharedWith')->willReturn($sharedWith);
  860. $path = $this->createMock(Node::class);
  861. $share->setSharedWith('sharedWith')
  862. ->setNode($path)
  863. ->setShareOwner('shareOwner')
  864. ->setProviderId('foo')
  865. ->setId('bar');
  866. $share2 = $this->manager->newShare();
  867. $share2->setShareType(\OCP\Share::SHARE_TYPE_GROUP)
  868. ->setShareOwner('shareOwner2')
  869. ->setProviderId('foo')
  870. ->setId('baz')
  871. ->setSharedWith('group');
  872. $group = $this->createMock(IGroup::class);
  873. $group->method('inGroup')
  874. ->with($sharedWith)
  875. ->willReturn(true);
  876. $this->groupManager->method('get')->with('group')->willReturn($group);
  877. $this->defaultProvider
  878. ->method('getSharesByPath')
  879. ->with($path)
  880. ->willReturn([$share2]);
  881. self::invokePrivate($this->manager, 'userCreateChecks', [$share]);
  882. }
  883. public function testUserCreateChecksIdenticalPathSharedViaDeletedGroup() {
  884. $share = $this->manager->newShare();
  885. $sharedWith = $this->createMock(IUser::class);
  886. $sharedWith->method('getUID')->willReturn('sharedWith');
  887. $this->userManager->method('get')->with('sharedWith')->willReturn($sharedWith);
  888. $path = $this->createMock(Node::class);
  889. $share->setSharedWith('sharedWith')
  890. ->setNode($path)
  891. ->setShareOwner('shareOwner')
  892. ->setProviderId('foo')
  893. ->setId('bar');
  894. $share2 = $this->manager->newShare();
  895. $share2->setShareType(\OCP\Share::SHARE_TYPE_GROUP)
  896. ->setShareOwner('shareOwner2')
  897. ->setProviderId('foo')
  898. ->setId('baz')
  899. ->setSharedWith('group');
  900. $this->groupManager->method('get')->with('group')->willReturn(null);
  901. $this->defaultProvider
  902. ->method('getSharesByPath')
  903. ->with($path)
  904. ->willReturn([$share2]);
  905. $this->assertNull($this->invokePrivate($this->manager, 'userCreateChecks', [$share]));
  906. }
  907. public function testUserCreateChecksIdenticalPathNotSharedWithUser() {
  908. $share = $this->manager->newShare();
  909. $sharedWith = $this->createMock(IUser::class);
  910. $path = $this->createMock(Node::class);
  911. $share->setSharedWith('sharedWith')
  912. ->setNode($path)
  913. ->setShareOwner('shareOwner')
  914. ->setProviderId('foo')
  915. ->setId('bar');
  916. $this->userManager->method('get')->with('sharedWith')->willReturn($sharedWith);
  917. $share2 = $this->manager->newShare();
  918. $share2->setShareType(\OCP\Share::SHARE_TYPE_GROUP)
  919. ->setShareOwner('shareOwner2')
  920. ->setProviderId('foo')
  921. ->setId('baz');
  922. $group = $this->createMock(IGroup::class);
  923. $group->method('inGroup')
  924. ->with($sharedWith)
  925. ->willReturn(false);
  926. $this->groupManager->method('get')->with('group')->willReturn($group);
  927. $share2->setSharedWith('group');
  928. $this->defaultProvider
  929. ->method('getSharesByPath')
  930. ->with($path)
  931. ->willReturn([$share2]);
  932. self::invokePrivate($this->manager, 'userCreateChecks', [$share]);
  933. }
  934. /**
  935. * @expectedException Exception
  936. * @expectedExceptionMessage Group sharing is now allowed
  937. */
  938. public function testGroupCreateChecksShareWithGroupMembersGroupSharingNotAllowed() {
  939. $share = $this->manager->newShare();
  940. $this->config
  941. ->method('getAppValue')
  942. ->will($this->returnValueMap([
  943. ['core', 'shareapi_allow_group_sharing', 'yes', 'no'],
  944. ]));
  945. self::invokePrivate($this->manager, 'groupCreateChecks', [$share]);
  946. }
  947. /**
  948. * @expectedException Exception
  949. * @expectedExceptionMessage Sharing is only allowed within your own groups
  950. */
  951. public function testGroupCreateChecksShareWithGroupMembersOnlyNotInGroup() {
  952. $share = $this->manager->newShare();
  953. $user = $this->createMock(IUser::class);
  954. $group = $this->createMock(IGroup::class);
  955. $share->setSharedBy('user')->setSharedWith('group');
  956. $group->method('inGroup')->with($user)->willReturn(false);
  957. $this->groupManager->method('get')->with('group')->willReturn($group);
  958. $this->userManager->method('get')->with('user')->willReturn($user);
  959. $this->config
  960. ->method('getAppValue')
  961. ->will($this->returnValueMap([
  962. ['core', 'shareapi_only_share_with_group_members', 'no', 'yes'],
  963. ['core', 'shareapi_allow_group_sharing', 'yes', 'yes'],
  964. ]));
  965. self::invokePrivate($this->manager, 'groupCreateChecks', [$share]);
  966. }
  967. /**
  968. * @expectedException Exception
  969. * @expectedExceptionMessage Sharing is only allowed within your own groups
  970. */
  971. public function testGroupCreateChecksShareWithGroupMembersOnlyNullGroup() {
  972. $share = $this->manager->newShare();
  973. $user = $this->createMock(IUser::class);
  974. $share->setSharedBy('user')->setSharedWith('group');
  975. $this->groupManager->method('get')->with('group')->willReturn(null);
  976. $this->userManager->method('get')->with('user')->willReturn($user);
  977. $this->config
  978. ->method('getAppValue')
  979. ->will($this->returnValueMap([
  980. ['core', 'shareapi_only_share_with_group_members', 'no', 'yes'],
  981. ['core', 'shareapi_allow_group_sharing', 'yes', 'yes'],
  982. ]));
  983. $this->assertNull($this->invokePrivate($this->manager, 'groupCreateChecks', [$share]));
  984. }
  985. public function testGroupCreateChecksShareWithGroupMembersOnlyInGroup() {
  986. $share = $this->manager->newShare();
  987. $user = $this->createMock(IUser::class);
  988. $group = $this->createMock(IGroup::class);
  989. $share->setSharedBy('user')->setSharedWith('group');
  990. $this->userManager->method('get')->with('user')->willReturn($user);
  991. $this->groupManager->method('get')->with('group')->willReturn($group);
  992. $group->method('inGroup')->with($user)->willReturn(true);
  993. $path = $this->createMock(Node::class);
  994. $share->setNode($path);
  995. $this->defaultProvider->method('getSharesByPath')
  996. ->with($path)
  997. ->willReturn([]);
  998. $this->config
  999. ->method('getAppValue')
  1000. ->will($this->returnValueMap([
  1001. ['core', 'shareapi_only_share_with_group_members', 'no', 'yes'],
  1002. ['core', 'shareapi_allow_group_sharing', 'yes', 'yes'],
  1003. ]));
  1004. self::invokePrivate($this->manager, 'groupCreateChecks', [$share]);
  1005. }
  1006. /**
  1007. * @expectedException Exception
  1008. * @expectedExceptionMessage Path is already shared with this group
  1009. */
  1010. public function testGroupCreateChecksPathAlreadySharedWithSameGroup() {
  1011. $share = $this->manager->newShare();
  1012. $path = $this->createMock(Node::class);
  1013. $share->setSharedWith('sharedWith')
  1014. ->setNode($path)
  1015. ->setProviderId('foo')
  1016. ->setId('bar');
  1017. $share2 = $this->manager->newShare();
  1018. $share2->setSharedWith('sharedWith')
  1019. ->setProviderId('foo')
  1020. ->setId('baz');
  1021. $this->defaultProvider->method('getSharesByPath')
  1022. ->with($path)
  1023. ->willReturn([$share2]);
  1024. $this->config
  1025. ->method('getAppValue')
  1026. ->will($this->returnValueMap([
  1027. ['core', 'shareapi_allow_group_sharing', 'yes', 'yes'],
  1028. ]));
  1029. self::invokePrivate($this->manager, 'groupCreateChecks', [$share]);
  1030. }
  1031. public function testGroupCreateChecksPathAlreadySharedWithDifferentGroup() {
  1032. $share = $this->manager->newShare();
  1033. $share->setSharedWith('sharedWith');
  1034. $path = $this->createMock(Node::class);
  1035. $share->setNode($path);
  1036. $share2 = $this->manager->newShare();
  1037. $share2->setSharedWith('sharedWith2');
  1038. $this->defaultProvider->method('getSharesByPath')
  1039. ->with($path)
  1040. ->willReturn([$share2]);
  1041. $this->config
  1042. ->method('getAppValue')
  1043. ->will($this->returnValueMap([
  1044. ['core', 'shareapi_allow_group_sharing', 'yes', 'yes'],
  1045. ]));
  1046. self::invokePrivate($this->manager, 'groupCreateChecks', [$share]);
  1047. }
  1048. /**
  1049. * @expectedException Exception
  1050. * @expectedExceptionMessage Link sharing is not allowed
  1051. */
  1052. public function testLinkCreateChecksNoLinkSharesAllowed() {
  1053. $share = $this->manager->newShare();
  1054. $this->config
  1055. ->method('getAppValue')
  1056. ->will($this->returnValueMap([
  1057. ['core', 'shareapi_allow_links', 'yes', 'no'],
  1058. ]));
  1059. self::invokePrivate($this->manager, 'linkCreateChecks', [$share]);
  1060. }
  1061. /**
  1062. * @expectedException Exception
  1063. * @expectedExceptionMessage Link shares can’t have reshare permissions
  1064. */
  1065. public function testLinkCreateChecksSharePermissions() {
  1066. $share = $this->manager->newShare();
  1067. $share->setPermissions(\OCP\Constants::PERMISSION_SHARE);
  1068. $this->config
  1069. ->method('getAppValue')
  1070. ->will($this->returnValueMap([
  1071. ['core', 'shareapi_allow_links', 'yes', 'yes'],
  1072. ]));
  1073. self::invokePrivate($this->manager, 'linkCreateChecks', [$share]);
  1074. }
  1075. /**
  1076. * @expectedException Exception
  1077. * @expectedExceptionMessage Public upload is not allowed
  1078. */
  1079. public function testLinkCreateChecksNoPublicUpload() {
  1080. $share = $this->manager->newShare();
  1081. $share->setPermissions(\OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE);
  1082. $this->config
  1083. ->method('getAppValue')
  1084. ->will($this->returnValueMap([
  1085. ['core', 'shareapi_allow_links', 'yes', 'yes'],
  1086. ['core', 'shareapi_allow_public_upload', 'yes', 'no']
  1087. ]));
  1088. self::invokePrivate($this->manager, 'linkCreateChecks', [$share]);
  1089. }
  1090. public function testLinkCreateChecksPublicUpload() {
  1091. $share = $this->manager->newShare();
  1092. $share->setPermissions(\OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE);
  1093. $this->config
  1094. ->method('getAppValue')
  1095. ->will($this->returnValueMap([
  1096. ['core', 'shareapi_allow_links', 'yes', 'yes'],
  1097. ['core', 'shareapi_allow_public_upload', 'yes', 'yes']
  1098. ]));
  1099. self::invokePrivate($this->manager, 'linkCreateChecks', [$share]);
  1100. }
  1101. public function testLinkCreateChecksReadOnly() {
  1102. $share = $this->manager->newShare();
  1103. $share->setPermissions(\OCP\Constants::PERMISSION_READ);
  1104. $this->config
  1105. ->method('getAppValue')
  1106. ->will($this->returnValueMap([
  1107. ['core', 'shareapi_allow_links', 'yes', 'yes'],
  1108. ['core', 'shareapi_allow_public_upload', 'yes', 'no']
  1109. ]));
  1110. self::invokePrivate($this->manager, 'linkCreateChecks', [$share]);
  1111. }
  1112. /**
  1113. * @expectedException \InvalidArgumentException
  1114. * @expectedExceptionMessage Path contains files shared with you
  1115. */
  1116. public function testPathCreateChecksContainsSharedMount() {
  1117. $path = $this->createMock(Folder::class);
  1118. $path->method('getPath')->willReturn('path');
  1119. $mount = $this->createMock(IMountPoint::class);
  1120. $storage = $this->createMock(Storage::class);
  1121. $mount->method('getStorage')->willReturn($storage);
  1122. $storage->method('instanceOfStorage')->with('\OCA\Files_Sharing\ISharedStorage')->willReturn(true);
  1123. $this->mountManager->method('findIn')->with('path')->willReturn([$mount]);
  1124. self::invokePrivate($this->manager, 'pathCreateChecks', [$path]);
  1125. }
  1126. public function testPathCreateChecksContainsNoSharedMount() {
  1127. $path = $this->createMock(Folder::class);
  1128. $path->method('getPath')->willReturn('path');
  1129. $mount = $this->createMock(IMountPoint::class);
  1130. $storage = $this->createMock(Storage::class);
  1131. $mount->method('getStorage')->willReturn($storage);
  1132. $storage->method('instanceOfStorage')->with('\OCA\Files_Sharing\ISharedStorage')->willReturn(false);
  1133. $this->mountManager->method('findIn')->with('path')->willReturn([$mount]);
  1134. self::invokePrivate($this->manager, 'pathCreateChecks', [$path]);
  1135. }
  1136. public function testPathCreateChecksContainsNoFolder() {
  1137. $path = $this->createMock(File::class);
  1138. self::invokePrivate($this->manager, 'pathCreateChecks', [$path]);
  1139. }
  1140. public function dataIsSharingDisabledForUser() {
  1141. $data = [];
  1142. // No exclude groups
  1143. $data[] = ['no', null, null, null, false];
  1144. // empty exclude list, user no groups
  1145. $data[] = ['yes', '', json_encode(['']), [], false];
  1146. // empty exclude list, user groups
  1147. $data[] = ['yes', '', json_encode(['']), ['group1', 'group2'], false];
  1148. // Convert old list to json
  1149. $data[] = ['yes', 'group1,group2', json_encode(['group1', 'group2']), [], false];
  1150. // Old list partly groups in common
  1151. $data[] = ['yes', 'group1,group2', json_encode(['group1', 'group2']), ['group1', 'group3'], false];
  1152. // Old list only groups in common
  1153. $data[] = ['yes', 'group1,group2', json_encode(['group1', 'group2']), ['group1'], true];
  1154. // New list partly in common
  1155. $data[] = ['yes', json_encode(['group1', 'group2']), null, ['group1', 'group3'], false];
  1156. // New list only groups in common
  1157. $data[] = ['yes', json_encode(['group1', 'group2']), null, ['group2'], true];
  1158. return $data;
  1159. }
  1160. /**
  1161. * @dataProvider dataIsSharingDisabledForUser
  1162. *
  1163. * @param string $excludeGroups
  1164. * @param string $groupList
  1165. * @param string $setList
  1166. * @param string[] $groupIds
  1167. * @param bool $expected
  1168. */
  1169. public function testIsSharingDisabledForUser($excludeGroups, $groupList, $setList, $groupIds, $expected) {
  1170. $user = $this->createMock(IUser::class);
  1171. $this->config->method('getAppValue')
  1172. ->will($this->returnValueMap([
  1173. ['core', 'shareapi_exclude_groups', 'no', $excludeGroups],
  1174. ['core', 'shareapi_exclude_groups_list', '', $groupList],
  1175. ]));
  1176. if ($setList !== null) {
  1177. $this->config->expects($this->once())
  1178. ->method('setAppValue')
  1179. ->with('core', 'shareapi_exclude_groups_list', $setList);
  1180. } else {
  1181. $this->config->expects($this->never())
  1182. ->method('setAppValue');
  1183. }
  1184. $this->groupManager->method('getUserGroupIds')
  1185. ->with($user)
  1186. ->willReturn($groupIds);
  1187. $this->userManager->method('get')->with('user')->willReturn($user);
  1188. $res = $this->manager->sharingDisabledForUser('user');
  1189. $this->assertEquals($expected, $res);
  1190. }
  1191. public function dataCanShare() {
  1192. $data = [];
  1193. /*
  1194. * [expected, sharing enabled, disabled for user]
  1195. */
  1196. $data[] = [false, 'no', false];
  1197. $data[] = [false, 'no', true];
  1198. $data[] = [true, 'yes', false];
  1199. $data[] = [false, 'yes', true];
  1200. return $data;
  1201. }
  1202. /**
  1203. * @dataProvider dataCanShare
  1204. *
  1205. * @param bool $expected
  1206. * @param string $sharingEnabled
  1207. * @param bool $disabledForUser
  1208. */
  1209. public function testCanShare($expected, $sharingEnabled, $disabledForUser) {
  1210. $this->config->method('getAppValue')
  1211. ->will($this->returnValueMap([
  1212. ['core', 'shareapi_enabled', 'yes', $sharingEnabled],
  1213. ]));
  1214. $manager = $this->createManagerMock()
  1215. ->setMethods(['sharingDisabledForUser'])
  1216. ->getMock();
  1217. $manager->method('sharingDisabledForUser')
  1218. ->with('user')
  1219. ->willReturn($disabledForUser);
  1220. $share = $this->manager->newShare();
  1221. $share->setSharedBy('user');
  1222. $exception = false;
  1223. try {
  1224. $res = self::invokePrivate($manager, 'canShare', [$share]);
  1225. } catch (\Exception $e) {
  1226. $exception = true;
  1227. }
  1228. $this->assertEquals($expected, !$exception);
  1229. }
  1230. public function testCreateShareUser() {
  1231. $manager = $this->createManagerMock()
  1232. ->setMethods(['canShare', 'generalCreateChecks', 'userCreateChecks', 'pathCreateChecks'])
  1233. ->getMock();
  1234. $shareOwner = $this->createMock(IUser::class);
  1235. $shareOwner->method('getUID')->willReturn('shareOwner');
  1236. $storage = $this->createMock(Storage::class);
  1237. $path = $this->createMock(File::class);
  1238. $path->method('getOwner')->willReturn($shareOwner);
  1239. $path->method('getName')->willReturn('target');
  1240. $path->method('getStorage')->willReturn($storage);
  1241. $share = $this->createShare(
  1242. null,
  1243. \OCP\Share::SHARE_TYPE_USER,
  1244. $path,
  1245. 'sharedWith',
  1246. 'sharedBy',
  1247. null,
  1248. \OCP\Constants::PERMISSION_ALL);
  1249. $manager->expects($this->once())
  1250. ->method('canShare')
  1251. ->with($share)
  1252. ->willReturn(true);
  1253. $manager->expects($this->once())
  1254. ->method('generalCreateChecks')
  1255. ->with($share);;
  1256. $manager->expects($this->once())
  1257. ->method('userCreateChecks')
  1258. ->with($share);;
  1259. $manager->expects($this->once())
  1260. ->method('pathCreateChecks')
  1261. ->with($path);
  1262. $this->defaultProvider
  1263. ->expects($this->once())
  1264. ->method('create')
  1265. ->with($share)
  1266. ->will($this->returnArgument(0));
  1267. $share->expects($this->once())
  1268. ->method('setShareOwner')
  1269. ->with('shareOwner');
  1270. $share->expects($this->once())
  1271. ->method('setTarget')
  1272. ->with('/target');
  1273. $manager->createShare($share);
  1274. }
  1275. public function testCreateShareGroup() {
  1276. $manager = $this->createManagerMock()
  1277. ->setMethods(['canShare', 'generalCreateChecks', 'groupCreateChecks', 'pathCreateChecks'])
  1278. ->getMock();
  1279. $shareOwner = $this->createMock(IUser::class);
  1280. $shareOwner->method('getUID')->willReturn('shareOwner');
  1281. $storage = $this->createMock(Storage::class);
  1282. $path = $this->createMock(File::class);
  1283. $path->method('getOwner')->willReturn($shareOwner);
  1284. $path->method('getName')->willReturn('target');
  1285. $path->method('getStorage')->willReturn($storage);
  1286. $share = $this->createShare(
  1287. null,
  1288. \OCP\Share::SHARE_TYPE_GROUP,
  1289. $path,
  1290. 'sharedWith',
  1291. 'sharedBy',
  1292. null,
  1293. \OCP\Constants::PERMISSION_ALL);
  1294. $manager->expects($this->once())
  1295. ->method('canShare')
  1296. ->with($share)
  1297. ->willReturn(true);
  1298. $manager->expects($this->once())
  1299. ->method('generalCreateChecks')
  1300. ->with($share);;
  1301. $manager->expects($this->once())
  1302. ->method('groupCreateChecks')
  1303. ->with($share);;
  1304. $manager->expects($this->once())
  1305. ->method('pathCreateChecks')
  1306. ->with($path);
  1307. $this->defaultProvider
  1308. ->expects($this->once())
  1309. ->method('create')
  1310. ->with($share)
  1311. ->will($this->returnArgument(0));
  1312. $share->expects($this->once())
  1313. ->method('setShareOwner')
  1314. ->with('shareOwner');
  1315. $share->expects($this->once())
  1316. ->method('setTarget')
  1317. ->with('/target');
  1318. $manager->createShare($share);
  1319. }
  1320. public function testCreateShareLink() {
  1321. $manager = $this->createManagerMock()
  1322. ->setMethods([
  1323. 'canShare',
  1324. 'generalCreateChecks',
  1325. 'linkCreateChecks',
  1326. 'pathCreateChecks',
  1327. 'validateExpirationDate',
  1328. 'verifyPassword',
  1329. 'setLinkParent',
  1330. ])
  1331. ->getMock();
  1332. $shareOwner = $this->createMock(IUser::class);
  1333. $shareOwner->method('getUID')->willReturn('shareOwner');
  1334. $storage = $this->createMock(Storage::class);
  1335. $path = $this->createMock(File::class);
  1336. $path->method('getOwner')->willReturn($shareOwner);
  1337. $path->method('getName')->willReturn('target');
  1338. $path->method('getId')->willReturn(1);
  1339. $path->method('getStorage')->willReturn($storage);
  1340. $date = new \DateTime();
  1341. $share = $this->manager->newShare();
  1342. $share->setShareType(\OCP\Share::SHARE_TYPE_LINK)
  1343. ->setNode($path)
  1344. ->setSharedBy('sharedBy')
  1345. ->setPermissions(\OCP\Constants::PERMISSION_ALL)
  1346. ->setExpirationDate($date)
  1347. ->setPassword('password');
  1348. $manager->expects($this->once())
  1349. ->method('canShare')
  1350. ->with($share)
  1351. ->willReturn(true);
  1352. $manager->expects($this->once())
  1353. ->method('generalCreateChecks')
  1354. ->with($share);;
  1355. $manager->expects($this->once())
  1356. ->method('linkCreateChecks')
  1357. ->with($share);;
  1358. $manager->expects($this->once())
  1359. ->method('pathCreateChecks')
  1360. ->with($path);
  1361. $manager->expects($this->once())
  1362. ->method('validateExpirationDate')
  1363. ->with($share);
  1364. $manager->expects($this->once())
  1365. ->method('verifyPassword')
  1366. ->with('password');
  1367. $manager->expects($this->once())
  1368. ->method('setLinkParent')
  1369. ->with($share);
  1370. $this->hasher->expects($this->once())
  1371. ->method('hash')
  1372. ->with('password')
  1373. ->willReturn('hashed');
  1374. $this->secureRandom->method('getMediumStrengthGenerator')
  1375. ->will($this->returnSelf());
  1376. $this->secureRandom->method('generate')
  1377. ->willReturn('token');
  1378. $this->defaultProvider
  1379. ->expects($this->once())
  1380. ->method('create')
  1381. ->with($share)
  1382. ->will($this->returnCallback(function(Share $share) {
  1383. return $share->setId(42);
  1384. }));
  1385. // Pre share
  1386. $this->eventDispatcher->expects($this->at(0))
  1387. ->method('dispatch')
  1388. ->with(
  1389. $this->equalTo('OCP\Share::preShare'),
  1390. $this->callback(function(GenericEvent $e) use ($path, $date) {
  1391. /** @var IShare $share */
  1392. $share = $e->getSubject();
  1393. return $share->getShareType() === \OCP\Share::SHARE_TYPE_LINK &&
  1394. $share->getNode() === $path &&
  1395. $share->getSharedBy() === 'sharedBy' &&
  1396. $share->getPermissions() === \OCP\Constants::PERMISSION_ALL &&
  1397. $share->getExpirationDate() === $date &&
  1398. $share->getPassword() === 'hashed' &&
  1399. $share->getToken() === 'token';
  1400. })
  1401. );
  1402. // Post share
  1403. $this->eventDispatcher->expects($this->at(1))
  1404. ->method('dispatch')
  1405. ->with(
  1406. $this->equalTo('OCP\Share::postShare'),
  1407. $this->callback(function(GenericEvent $e) use ($path, $date) {
  1408. /** @var IShare $share */
  1409. $share = $e->getSubject();
  1410. return $share->getShareType() === \OCP\Share::SHARE_TYPE_LINK &&
  1411. $share->getNode() === $path &&
  1412. $share->getSharedBy() === 'sharedBy' &&
  1413. $share->getPermissions() === \OCP\Constants::PERMISSION_ALL &&
  1414. $share->getExpirationDate() === $date &&
  1415. $share->getPassword() === 'hashed' &&
  1416. $share->getToken() === 'token' &&
  1417. $share->getId() === '42' &&
  1418. $share->getTarget() === '/target';
  1419. })
  1420. );
  1421. /** @var IShare $share */
  1422. $share = $manager->createShare($share);
  1423. $this->assertSame('shareOwner', $share->getShareOwner());
  1424. $this->assertEquals('/target', $share->getTarget());
  1425. $this->assertSame($date, $share->getExpirationDate());
  1426. $this->assertEquals('token', $share->getToken());
  1427. $this->assertEquals('hashed', $share->getPassword());
  1428. }
  1429. public function testCreateShareMail() {
  1430. $manager = $this->createManagerMock()
  1431. ->setMethods([
  1432. 'canShare',
  1433. 'generalCreateChecks',
  1434. 'linkCreateChecks',
  1435. 'pathCreateChecks',
  1436. 'validateExpirationDate',
  1437. 'verifyPassword',
  1438. 'setLinkParent',
  1439. ])
  1440. ->getMock();
  1441. $shareOwner = $this->createMock(IUser::class);
  1442. $shareOwner->method('getUID')->willReturn('shareOwner');
  1443. $storage = $this->createMock(Storage::class);
  1444. $path = $this->createMock(File::class);
  1445. $path->method('getOwner')->willReturn($shareOwner);
  1446. $path->method('getName')->willReturn('target');
  1447. $path->method('getId')->willReturn(1);
  1448. $path->method('getStorage')->willReturn($storage);
  1449. $share = $this->manager->newShare();
  1450. $share->setShareType(\OCP\Share::SHARE_TYPE_EMAIL)
  1451. ->setNode($path)
  1452. ->setSharedBy('sharedBy')
  1453. ->setPermissions(\OCP\Constants::PERMISSION_ALL);
  1454. $manager->expects($this->once())
  1455. ->method('canShare')
  1456. ->with($share)
  1457. ->willReturn(true);
  1458. $manager->expects($this->once())
  1459. ->method('generalCreateChecks')
  1460. ->with($share);;
  1461. $manager->expects($this->never())
  1462. ->method('linkCreateChecks');
  1463. $manager->expects($this->once())
  1464. ->method('pathCreateChecks')
  1465. ->with($path);
  1466. $manager->expects($this->never())
  1467. ->method('validateExpirationDate');
  1468. $manager->expects($this->never())
  1469. ->method('verifyPassword');
  1470. $manager->expects($this->never())
  1471. ->method('setLinkParent');
  1472. $this->secureRandom->method('getMediumStrengthGenerator')
  1473. ->will($this->returnSelf());
  1474. $this->secureRandom->method('generate')
  1475. ->willReturn('token');
  1476. $this->defaultProvider
  1477. ->expects($this->once())
  1478. ->method('create')
  1479. ->with($share)
  1480. ->will($this->returnCallback(function(Share $share) {
  1481. return $share->setId(42);
  1482. }));
  1483. // Pre share
  1484. $this->eventDispatcher->expects($this->at(0))
  1485. ->method('dispatch')
  1486. ->with(
  1487. $this->equalTo('OCP\Share::preShare'),
  1488. $this->callback(function(GenericEvent $e) use ($path) {
  1489. /** @var IShare $share */
  1490. $share = $e->getSubject();
  1491. return $share->getShareType() === \OCP\Share::SHARE_TYPE_EMAIL &&
  1492. $share->getNode() === $path &&
  1493. $share->getSharedBy() === 'sharedBy' &&
  1494. $share->getPermissions() === \OCP\Constants::PERMISSION_ALL &&
  1495. $share->getExpirationDate() === null &&
  1496. $share->getPassword() === null &&
  1497. $share->getToken() === 'token';
  1498. })
  1499. );
  1500. // Post share
  1501. $this->eventDispatcher->expects($this->at(1))
  1502. ->method('dispatch')
  1503. ->with(
  1504. $this->equalTo('OCP\Share::postShare'),
  1505. $this->callback(function(GenericEvent $e) use ($path) {
  1506. /** @var IShare $share */
  1507. $share = $e->getSubject();
  1508. return $share->getShareType() === \OCP\Share::SHARE_TYPE_EMAIL &&
  1509. $share->getNode() === $path &&
  1510. $share->getSharedBy() === 'sharedBy' &&
  1511. $share->getPermissions() === \OCP\Constants::PERMISSION_ALL &&
  1512. $share->getExpirationDate() === null &&
  1513. $share->getPassword() === null &&
  1514. $share->getToken() === 'token' &&
  1515. $share->getId() === '42' &&
  1516. $share->getTarget() === '/target';
  1517. })
  1518. );
  1519. /** @var IShare $share */
  1520. $share = $manager->createShare($share);
  1521. $this->assertSame('shareOwner', $share->getShareOwner());
  1522. $this->assertEquals('/target', $share->getTarget());
  1523. $this->assertEquals('token', $share->getToken());
  1524. }
  1525. /**
  1526. * @expectedException Exception
  1527. * @expectedExceptionMessage I won't let you share
  1528. */
  1529. public function testCreateShareHookError() {
  1530. $manager = $this->createManagerMock()
  1531. ->setMethods([
  1532. 'canShare',
  1533. 'generalCreateChecks',
  1534. 'userCreateChecks',
  1535. 'pathCreateChecks',
  1536. ])
  1537. ->getMock();
  1538. $shareOwner = $this->createMock(IUser::class);
  1539. $shareOwner->method('getUID')->willReturn('shareOwner');
  1540. $storage = $this->createMock(Storage::class);
  1541. $path = $this->createMock(File::class);
  1542. $path->method('getOwner')->willReturn($shareOwner);
  1543. $path->method('getName')->willReturn('target');
  1544. $path->method('getStorage')->willReturn($storage);
  1545. $share = $this->createShare(
  1546. null,
  1547. \OCP\Share::SHARE_TYPE_USER,
  1548. $path,
  1549. 'sharedWith',
  1550. 'sharedBy',
  1551. null,
  1552. \OCP\Constants::PERMISSION_ALL);
  1553. $manager->expects($this->once())
  1554. ->method('canShare')
  1555. ->with($share)
  1556. ->willReturn(true);
  1557. $manager->expects($this->once())
  1558. ->method('generalCreateChecks')
  1559. ->with($share);;
  1560. $manager->expects($this->once())
  1561. ->method('userCreateChecks')
  1562. ->with($share);;
  1563. $manager->expects($this->once())
  1564. ->method('pathCreateChecks')
  1565. ->with($path);
  1566. $share->expects($this->once())
  1567. ->method('setShareOwner')
  1568. ->with('shareOwner');
  1569. $share->expects($this->once())
  1570. ->method('setTarget')
  1571. ->with('/target');
  1572. // Pre share
  1573. $this->eventDispatcher->expects($this->once())
  1574. ->method('dispatch')
  1575. ->with(
  1576. $this->equalTo('OCP\Share::preShare'),
  1577. $this->isInstanceOf(GenericEvent::class)
  1578. )->will($this->returnCallback(function($name, GenericEvent $e) {
  1579. $e->setArgument('error', 'I won\'t let you share!');
  1580. $e->stopPropagation();
  1581. })
  1582. );
  1583. $manager->createShare($share);
  1584. }
  1585. public function testCreateShareOfIncommingFederatedShare() {
  1586. $manager = $this->createManagerMock()
  1587. ->setMethods(['canShare', 'generalCreateChecks', 'userCreateChecks', 'pathCreateChecks'])
  1588. ->getMock();
  1589. $shareOwner = $this->createMock(IUser::class);
  1590. $shareOwner->method('getUID')->willReturn('shareOwner');
  1591. $storage = $this->createMock(Storage::class);
  1592. $storage->method('instanceOfStorage')
  1593. ->with('OCA\Files_Sharing\External\Storage')
  1594. ->willReturn(true);
  1595. $storage2 = $this->createMock(Storage::class);
  1596. $storage2->method('instanceOfStorage')
  1597. ->with('OCA\Files_Sharing\External\Storage')
  1598. ->willReturn(false);
  1599. $path = $this->createMock(File::class);
  1600. $path->expects($this->never())->method('getOwner');
  1601. $path->method('getName')->willReturn('target');
  1602. $path->method('getStorage')->willReturn($storage);
  1603. $parent = $this->createMock(Folder::class);
  1604. $parent->method('getStorage')->willReturn($storage);
  1605. $parentParent = $this->createMock(Folder::class);
  1606. $parentParent->method('getStorage')->willReturn($storage2);
  1607. $parentParent->method('getOwner')->willReturn($shareOwner);
  1608. $path->method('getParent')->willReturn($parent);
  1609. $parent->method('getParent')->willReturn($parentParent);
  1610. $share = $this->createShare(
  1611. null,
  1612. \OCP\Share::SHARE_TYPE_USER,
  1613. $path,
  1614. 'sharedWith',
  1615. 'sharedBy',
  1616. null,
  1617. \OCP\Constants::PERMISSION_ALL);
  1618. $manager->expects($this->once())
  1619. ->method('canShare')
  1620. ->with($share)
  1621. ->willReturn(true);
  1622. $manager->expects($this->once())
  1623. ->method('generalCreateChecks')
  1624. ->with($share);;
  1625. $manager->expects($this->once())
  1626. ->method('userCreateChecks')
  1627. ->with($share);;
  1628. $manager->expects($this->once())
  1629. ->method('pathCreateChecks')
  1630. ->with($path);
  1631. $this->defaultProvider
  1632. ->expects($this->once())
  1633. ->method('create')
  1634. ->with($share)
  1635. ->will($this->returnArgument(0));
  1636. $share->expects($this->once())
  1637. ->method('setShareOwner')
  1638. ->with('shareOwner');
  1639. $share->expects($this->once())
  1640. ->method('setTarget')
  1641. ->with('/target');
  1642. $manager->createShare($share);
  1643. }
  1644. public function testGetSharesBy() {
  1645. $share = $this->manager->newShare();
  1646. $node = $this->createMock(Folder::class);
  1647. $this->defaultProvider->expects($this->once())
  1648. ->method('getSharesBy')
  1649. ->with(
  1650. $this->equalTo('user'),
  1651. $this->equalTo(\OCP\Share::SHARE_TYPE_USER),
  1652. $this->equalTo($node),
  1653. $this->equalTo(true),
  1654. $this->equalTo(1),
  1655. $this->equalTo(1)
  1656. )->willReturn([$share]);
  1657. $shares = $this->manager->getSharesBy('user', \OCP\Share::SHARE_TYPE_USER, $node, true, 1, 1);
  1658. $this->assertCount(1, $shares);
  1659. $this->assertSame($share, $shares[0]);
  1660. }
  1661. /**
  1662. * Test to ensure we correctly remove expired link shares
  1663. *
  1664. * We have 8 Shares and we want the 3 first valid shares.
  1665. * share 3-6 and 8 are expired. Thus at the end of this test we should
  1666. * have received share 1,2 and 7. And from the manager. Share 3-6 should be
  1667. * deleted (as they are evaluated). but share 8 should still be there.
  1668. */
  1669. public function testGetSharesByExpiredLinkShares() {
  1670. $manager = $this->createManagerMock()
  1671. ->setMethods(['deleteShare'])
  1672. ->getMock();
  1673. /** @var \OCP\Share\IShare[] $shares */
  1674. $shares = [];
  1675. /*
  1676. * This results in an array of 8 IShare elements
  1677. */
  1678. for ($i = 0; $i < 8; $i++) {
  1679. $share = $this->manager->newShare();
  1680. $share->setId($i);
  1681. $shares[] = $share;
  1682. }
  1683. $today = new \DateTime();
  1684. $today->setTime(0,0,0);
  1685. /*
  1686. * Set the expiration date to today for some shares
  1687. */
  1688. $shares[2]->setExpirationDate($today);
  1689. $shares[3]->setExpirationDate($today);
  1690. $shares[4]->setExpirationDate($today);
  1691. $shares[5]->setExpirationDate($today);
  1692. /** @var \OCP\Share\IShare[] $i */
  1693. $shares2 = [];
  1694. for ($i = 0; $i < 8; $i++) {
  1695. $shares2[] = clone $shares[$i];
  1696. }
  1697. $node = $this->createMock(File::class);
  1698. /*
  1699. * Simulate the getSharesBy call.
  1700. */
  1701. $this->defaultProvider
  1702. ->method('getSharesBy')
  1703. ->will($this->returnCallback(function($uid, $type, $node, $reshares, $limit, $offset) use (&$shares2) {
  1704. return array_slice($shares2, $offset, $limit);
  1705. }));
  1706. /*
  1707. * Simulate the deleteShare call.
  1708. */
  1709. $manager->method('deleteShare')
  1710. ->will($this->returnCallback(function($share) use (&$shares2) {
  1711. for($i = 0; $i < count($shares2); $i++) {
  1712. if ($shares2[$i]->getId() === $share->getId()) {
  1713. array_splice($shares2, $i, 1);
  1714. break;
  1715. }
  1716. }
  1717. }));
  1718. $res = $manager->getSharesBy('user', \OCP\Share::SHARE_TYPE_LINK, $node, true, 3, 0);
  1719. $this->assertCount(3, $res);
  1720. $this->assertEquals($shares[0]->getId(), $res[0]->getId());
  1721. $this->assertEquals($shares[1]->getId(), $res[1]->getId());
  1722. $this->assertEquals($shares[6]->getId(), $res[2]->getId());
  1723. $this->assertCount(4, $shares2);
  1724. $this->assertEquals(0, $shares2[0]->getId());
  1725. $this->assertEquals(1, $shares2[1]->getId());
  1726. $this->assertEquals(6, $shares2[2]->getId());
  1727. $this->assertEquals(7, $shares2[3]->getId());
  1728. $this->assertSame($today, $shares[3]->getExpirationDate());
  1729. }
  1730. public function testGetShareByToken() {
  1731. $this->config
  1732. ->expects($this->once())
  1733. ->method('getAppValue')
  1734. ->with('core', 'shareapi_allow_links', 'yes')
  1735. ->willReturn('yes');
  1736. $factory = $this->createMock(IProviderFactory::class);
  1737. $manager = new Manager(
  1738. $this->logger,
  1739. $this->config,
  1740. $this->secureRandom,
  1741. $this->hasher,
  1742. $this->mountManager,
  1743. $this->groupManager,
  1744. $this->l,
  1745. $this->l10nFactory,
  1746. $factory,
  1747. $this->userManager,
  1748. $this->rootFolder,
  1749. $this->eventDispatcher,
  1750. $this->mailer,
  1751. $this->urlGenerator,
  1752. $this->defaults
  1753. );
  1754. $share = $this->createMock(IShare::class);
  1755. $factory->expects($this->once())
  1756. ->method('getProviderForType')
  1757. ->with(\OCP\Share::SHARE_TYPE_LINK)
  1758. ->willReturn($this->defaultProvider);
  1759. $this->defaultProvider->expects($this->once())
  1760. ->method('getShareByToken')
  1761. ->with('token')
  1762. ->willReturn($share);
  1763. $ret = $manager->getShareByToken('token');
  1764. $this->assertSame($share, $ret);
  1765. }
  1766. public function testGetShareByTokenWithException() {
  1767. $this->config
  1768. ->expects($this->once())
  1769. ->method('getAppValue')
  1770. ->with('core', 'shareapi_allow_links', 'yes')
  1771. ->willReturn('yes');
  1772. $factory = $this->createMock(IProviderFactory::class);
  1773. $manager = new Manager(
  1774. $this->logger,
  1775. $this->config,
  1776. $this->secureRandom,
  1777. $this->hasher,
  1778. $this->mountManager,
  1779. $this->groupManager,
  1780. $this->l,
  1781. $this->l10nFactory,
  1782. $factory,
  1783. $this->userManager,
  1784. $this->rootFolder,
  1785. $this->eventDispatcher,
  1786. $this->mailer,
  1787. $this->urlGenerator,
  1788. $this->defaults
  1789. );
  1790. $share = $this->createMock(IShare::class);
  1791. $factory->expects($this->at(0))
  1792. ->method('getProviderForType')
  1793. ->with(\OCP\Share::SHARE_TYPE_LINK)
  1794. ->willReturn($this->defaultProvider);
  1795. $factory->expects($this->at(1))
  1796. ->method('getProviderForType')
  1797. ->with(\OCP\Share::SHARE_TYPE_REMOTE)
  1798. ->willReturn($this->defaultProvider);
  1799. $this->defaultProvider->expects($this->at(0))
  1800. ->method('getShareByToken')
  1801. ->with('token')
  1802. ->will($this->throwException(new ShareNotFound()));
  1803. $this->defaultProvider->expects($this->at(1))
  1804. ->method('getShareByToken')
  1805. ->with('token')
  1806. ->willReturn($share);
  1807. $ret = $manager->getShareByToken('token');
  1808. $this->assertSame($share, $ret);
  1809. }
  1810. /**
  1811. * @expectedException \OCP\Share\Exceptions\ShareNotFound
  1812. * @expectedExceptionMessage The requested share does not exist anymore
  1813. */
  1814. public function testGetShareByTokenExpired() {
  1815. $this->config
  1816. ->expects($this->once())
  1817. ->method('getAppValue')
  1818. ->with('core', 'shareapi_allow_links', 'yes')
  1819. ->willReturn('yes');
  1820. $this->l->expects($this->once())
  1821. ->method('t')
  1822. ->willReturnArgument(0);
  1823. $manager = $this->createManagerMock()
  1824. ->setMethods(['deleteShare'])
  1825. ->getMock();
  1826. $date = new \DateTime();
  1827. $date->setTime(0,0,0);
  1828. $share = $this->manager->newShare();
  1829. $share->setExpirationDate($date);
  1830. $this->defaultProvider->expects($this->once())
  1831. ->method('getShareByToken')
  1832. ->with('expiredToken')
  1833. ->willReturn($share);
  1834. $manager->expects($this->once())
  1835. ->method('deleteShare')
  1836. ->with($this->equalTo($share));
  1837. $manager->getShareByToken('expiredToken');
  1838. }
  1839. public function testGetShareByTokenNotExpired() {
  1840. $this->config
  1841. ->expects($this->once())
  1842. ->method('getAppValue')
  1843. ->with('core', 'shareapi_allow_links', 'yes')
  1844. ->willReturn('yes');
  1845. $date = new \DateTime();
  1846. $date->setTime(0,0,0);
  1847. $date->add(new \DateInterval('P2D'));
  1848. $share = $this->manager->newShare();
  1849. $share->setExpirationDate($date);
  1850. $this->defaultProvider->expects($this->once())
  1851. ->method('getShareByToken')
  1852. ->with('expiredToken')
  1853. ->willReturn($share);
  1854. $res = $this->manager->getShareByToken('expiredToken');
  1855. $this->assertSame($share, $res);
  1856. }
  1857. /**
  1858. * @expectedException \OCP\Share\Exceptions\ShareNotFound
  1859. */
  1860. public function testGetShareByTokenWithPublicLinksDisabled() {
  1861. $this->config
  1862. ->expects($this->once())
  1863. ->method('getAppValue')
  1864. ->with('core', 'shareapi_allow_links', 'yes')
  1865. ->willReturn('no');
  1866. $this->manager->getShareByToken('validToken');
  1867. }
  1868. public function testGetShareByTokenPublicUploadDisabled() {
  1869. $this->config
  1870. ->expects($this->at(0))
  1871. ->method('getAppValue')
  1872. ->with('core', 'shareapi_allow_links', 'yes')
  1873. ->willReturn('yes');
  1874. $share = $this->manager->newShare();
  1875. $share->setShareType(\OCP\Share::SHARE_TYPE_LINK)
  1876. ->setPermissions(\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE);
  1877. $this->config
  1878. ->expects($this->at(1))
  1879. ->method('getAppValue')
  1880. ->will($this->returnValueMap([
  1881. ['core', 'shareapi_allow_public_upload', 'yes', 'no'],
  1882. ]));
  1883. $this->defaultProvider->expects($this->once())
  1884. ->method('getShareByToken')
  1885. ->willReturn('validToken')
  1886. ->willReturn($share);
  1887. $res = $this->manager->getShareByToken('validToken');
  1888. $this->assertSame(\OCP\Constants::PERMISSION_READ, $res->getPermissions());
  1889. }
  1890. public function testCheckPasswordNoLinkShare() {
  1891. $share = $this->createMock(IShare::class);
  1892. $share->method('getShareType')->willReturn(\OCP\Share::SHARE_TYPE_USER);
  1893. $this->assertFalse($this->manager->checkPassword($share, 'password'));
  1894. }
  1895. public function testCheckPasswordNoPassword() {
  1896. $share = $this->createMock(IShare::class);
  1897. $share->method('getShareType')->willReturn(\OCP\Share::SHARE_TYPE_LINK);
  1898. $this->assertFalse($this->manager->checkPassword($share, 'password'));
  1899. $share->method('getPassword')->willReturn('password');
  1900. $this->assertFalse($this->manager->checkPassword($share, null));
  1901. }
  1902. public function testCheckPasswordInvalidPassword() {
  1903. $share = $this->createMock(IShare::class);
  1904. $share->method('getShareType')->willReturn(\OCP\Share::SHARE_TYPE_LINK);
  1905. $share->method('getPassword')->willReturn('password');
  1906. $this->hasher->method('verify')->with('invalidpassword', 'password', '')->willReturn(false);
  1907. $this->assertFalse($this->manager->checkPassword($share, 'invalidpassword'));
  1908. }
  1909. public function testCheckPasswordValidPassword() {
  1910. $share = $this->createMock(IShare::class);
  1911. $share->method('getShareType')->willReturn(\OCP\Share::SHARE_TYPE_LINK);
  1912. $share->method('getPassword')->willReturn('passwordHash');
  1913. $this->hasher->method('verify')->with('password', 'passwordHash', '')->willReturn(true);
  1914. $this->assertTrue($this->manager->checkPassword($share, 'password'));
  1915. }
  1916. public function testCheckPasswordUpdateShare() {
  1917. $share = $this->manager->newShare();
  1918. $share->setShareType(\OCP\Share::SHARE_TYPE_LINK)
  1919. ->setPassword('passwordHash');
  1920. $this->hasher->method('verify')->with('password', 'passwordHash', '')
  1921. ->will($this->returnCallback(function($pass, $hash, &$newHash) {
  1922. $newHash = 'newHash';
  1923. return true;
  1924. }));
  1925. $this->defaultProvider->expects($this->once())
  1926. ->method('update')
  1927. ->with($this->callback(function (\OCP\Share\IShare $share) {
  1928. return $share->getPassword() === 'newHash';
  1929. }));
  1930. $this->assertTrue($this->manager->checkPassword($share, 'password'));
  1931. }
  1932. /**
  1933. * @expectedException Exception
  1934. * @expectedExceptionMessage Can’t change share type
  1935. */
  1936. public function testUpdateShareCantChangeShareType() {
  1937. $manager = $this->createManagerMock()
  1938. ->setMethods([
  1939. 'canShare',
  1940. 'getShareById'
  1941. ])
  1942. ->getMock();
  1943. $originalShare = $this->manager->newShare();
  1944. $originalShare->setShareType(\OCP\Share::SHARE_TYPE_GROUP);
  1945. $manager->expects($this->once())->method('canShare')->willReturn(true);
  1946. $manager->expects($this->once())->method('getShareById')->with('foo:42')->willReturn($originalShare);
  1947. $share = $this->manager->newShare();
  1948. $share->setProviderId('foo')
  1949. ->setId('42')
  1950. ->setShareType(\OCP\Share::SHARE_TYPE_USER);
  1951. $manager->updateShare($share);
  1952. }
  1953. /**
  1954. * @expectedException Exception
  1955. * @expectedExceptionMessage Can only update recipient on user shares
  1956. */
  1957. public function testUpdateShareCantChangeRecipientForGroupShare() {
  1958. $manager = $this->createManagerMock()
  1959. ->setMethods([
  1960. 'canShare',
  1961. 'getShareById'
  1962. ])
  1963. ->getMock();
  1964. $originalShare = $this->manager->newShare();
  1965. $originalShare->setShareType(\OCP\Share::SHARE_TYPE_GROUP)
  1966. ->setSharedWith('origGroup');
  1967. $manager->expects($this->once())->method('canShare')->willReturn(true);
  1968. $manager->expects($this->once())->method('getShareById')->with('foo:42')->willReturn($originalShare);
  1969. $share = $this->manager->newShare();
  1970. $share->setProviderId('foo')
  1971. ->setId('42')
  1972. ->setShareType(\OCP\Share::SHARE_TYPE_GROUP)
  1973. ->setSharedWith('newGroup');
  1974. $manager->updateShare($share);
  1975. }
  1976. /**
  1977. * @expectedException Exception
  1978. * @expectedExceptionMessage Can’t share with the share owner
  1979. */
  1980. public function testUpdateShareCantShareWithOwner() {
  1981. $manager = $this->createManagerMock()
  1982. ->setMethods([
  1983. 'canShare',
  1984. 'getShareById'
  1985. ])
  1986. ->getMock();
  1987. $originalShare = $this->manager->newShare();
  1988. $originalShare->setShareType(\OCP\Share::SHARE_TYPE_USER)
  1989. ->setSharedWith('sharedWith');
  1990. $manager->expects($this->once())->method('canShare')->willReturn(true);
  1991. $manager->expects($this->once())->method('getShareById')->with('foo:42')->willReturn($originalShare);
  1992. $share = $this->manager->newShare();
  1993. $share->setProviderId('foo')
  1994. ->setId('42')
  1995. ->setShareType(\OCP\Share::SHARE_TYPE_USER)
  1996. ->setSharedWith('newUser')
  1997. ->setShareOwner('newUser');
  1998. $manager->updateShare($share);
  1999. }
  2000. public function testUpdateShareUser() {
  2001. $this->userManager->expects($this->any())->method('userExists')->willReturn(true);
  2002. $manager = $this->createManagerMock()
  2003. ->setMethods([
  2004. 'canShare',
  2005. 'getShareById',
  2006. 'generalCreateChecks',
  2007. 'userCreateChecks',
  2008. 'pathCreateChecks',
  2009. ])
  2010. ->getMock();
  2011. $originalShare = $this->manager->newShare();
  2012. $originalShare->setShareType(\OCP\Share::SHARE_TYPE_USER)
  2013. ->setSharedWith('origUser')
  2014. ->setPermissions(1);
  2015. $node = $this->createMock(File::class);
  2016. $node->method('getId')->willReturn(100);
  2017. $node->method('getPath')->willReturn('/newUser/files/myPath');
  2018. $manager->expects($this->once())->method('canShare')->willReturn(true);
  2019. $manager->expects($this->once())->method('getShareById')->with('foo:42')->willReturn($originalShare);
  2020. $share = $this->manager->newShare();
  2021. $share->setProviderId('foo')
  2022. ->setId('42')
  2023. ->setShareType(\OCP\Share::SHARE_TYPE_USER)
  2024. ->setSharedWith('origUser')
  2025. ->setShareOwner('newUser')
  2026. ->setSharedBy('sharer')
  2027. ->setPermissions(31)
  2028. ->setNode($node);
  2029. $this->defaultProvider->expects($this->once())
  2030. ->method('update')
  2031. ->with($share)
  2032. ->willReturn($share);
  2033. $hookListner = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  2034. \OCP\Util::connectHook('OCP\Share', 'post_set_expiration_date', $hookListner, 'post');
  2035. $hookListner->expects($this->never())->method('post');
  2036. $this->rootFolder->method('getUserFolder')->with('newUser')->will($this->returnSelf());
  2037. $this->rootFolder->method('getRelativePath')->with('/newUser/files/myPath')->willReturn('/myPath');
  2038. $hookListner2 = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  2039. \OCP\Util::connectHook('OCP\Share', 'post_update_permissions', $hookListner2, 'post');
  2040. $hookListner2->expects($this->once())->method('post')->with([
  2041. 'itemType' => 'file',
  2042. 'itemSource' => 100,
  2043. 'shareType' => \OCP\Share::SHARE_TYPE_USER,
  2044. 'shareWith' => 'origUser',
  2045. 'uidOwner' => 'sharer',
  2046. 'permissions' => 31,
  2047. 'path' => '/myPath',
  2048. ]);
  2049. $manager->updateShare($share);
  2050. }
  2051. public function testUpdateShareGroup() {
  2052. $manager = $this->createManagerMock()
  2053. ->setMethods([
  2054. 'canShare',
  2055. 'getShareById',
  2056. 'generalCreateChecks',
  2057. 'groupCreateChecks',
  2058. 'pathCreateChecks',
  2059. ])
  2060. ->getMock();
  2061. $originalShare = $this->manager->newShare();
  2062. $originalShare->setShareType(\OCP\Share::SHARE_TYPE_GROUP)
  2063. ->setSharedWith('origUser')
  2064. ->setPermissions(31);
  2065. $manager->expects($this->once())->method('canShare')->willReturn(true);
  2066. $manager->expects($this->once())->method('getShareById')->with('foo:42')->willReturn($originalShare);
  2067. $node = $this->createMock(File::class);
  2068. $share = $this->manager->newShare();
  2069. $share->setProviderId('foo')
  2070. ->setId('42')
  2071. ->setShareType(\OCP\Share::SHARE_TYPE_GROUP)
  2072. ->setSharedWith('origUser')
  2073. ->setShareOwner('owner')
  2074. ->setNode($node)
  2075. ->setPermissions(31);
  2076. $this->defaultProvider->expects($this->once())
  2077. ->method('update')
  2078. ->with($share)
  2079. ->willReturn($share);
  2080. $hookListner = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  2081. \OCP\Util::connectHook('OCP\Share', 'post_set_expiration_date', $hookListner, 'post');
  2082. $hookListner->expects($this->never())->method('post');
  2083. $hookListner2 = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  2084. \OCP\Util::connectHook('OCP\Share', 'post_update_permissions', $hookListner2, 'post');
  2085. $hookListner2->expects($this->never())->method('post');
  2086. $manager->updateShare($share);
  2087. }
  2088. public function testUpdateShareLink() {
  2089. $manager = $this->createManagerMock()
  2090. ->setMethods([
  2091. 'canShare',
  2092. 'getShareById',
  2093. 'generalCreateChecks',
  2094. 'linkCreateChecks',
  2095. 'pathCreateChecks',
  2096. 'verifyPassword',
  2097. 'validateExpirationDate',
  2098. ])
  2099. ->getMock();
  2100. $originalShare = $this->manager->newShare();
  2101. $originalShare->setShareType(\OCP\Share::SHARE_TYPE_LINK)
  2102. ->setPermissions(15);
  2103. $tomorrow = new \DateTime();
  2104. $tomorrow->setTime(0,0,0);
  2105. $tomorrow->add(new \DateInterval('P1D'));
  2106. $file = $this->createMock(File::class);
  2107. $file->method('getId')->willReturn(100);
  2108. $share = $this->manager->newShare();
  2109. $share->setProviderId('foo')
  2110. ->setId('42')
  2111. ->setShareType(\OCP\Share::SHARE_TYPE_LINK)
  2112. ->setToken('token')
  2113. ->setSharedBy('owner')
  2114. ->setShareOwner('owner')
  2115. ->setPassword('password')
  2116. ->setExpirationDate($tomorrow)
  2117. ->setNode($file)
  2118. ->setPermissions(15);
  2119. $manager->expects($this->once())->method('canShare')->willReturn(true);
  2120. $manager->expects($this->once())->method('getShareById')->with('foo:42')->willReturn($originalShare);
  2121. $manager->expects($this->once())->method('validateExpirationDate')->with($share);
  2122. $manager->expects($this->once())->method('verifyPassword')->with('password');
  2123. $this->hasher->expects($this->once())
  2124. ->method('hash')
  2125. ->with('password')
  2126. ->willReturn('hashed');
  2127. $this->defaultProvider->expects($this->once())
  2128. ->method('update')
  2129. ->with($share)
  2130. ->willReturn($share);
  2131. $hookListner = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  2132. \OCP\Util::connectHook('OCP\Share', 'post_set_expiration_date', $hookListner, 'post');
  2133. $hookListner->expects($this->once())->method('post')->with([
  2134. 'itemType' => 'file',
  2135. 'itemSource' => 100,
  2136. 'date' => $tomorrow,
  2137. 'uidOwner' => 'owner',
  2138. ]);
  2139. $hookListner2 = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  2140. \OCP\Util::connectHook('OCP\Share', 'post_update_password', $hookListner2, 'post');
  2141. $hookListner2->expects($this->once())->method('post')->with([
  2142. 'itemType' => 'file',
  2143. 'itemSource' => 100,
  2144. 'uidOwner' => 'owner',
  2145. 'token' => 'token',
  2146. 'disabled' => false,
  2147. ]);
  2148. $hookListner3 = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  2149. \OCP\Util::connectHook('OCP\Share', 'post_update_permissions', $hookListner3, 'post');
  2150. $hookListner3->expects($this->never())->method('post');
  2151. $manager->updateShare($share);
  2152. }
  2153. public function testUpdateShareMail() {
  2154. $manager = $this->createManagerMock()
  2155. ->setMethods([
  2156. 'canShare',
  2157. 'getShareById',
  2158. 'generalCreateChecks',
  2159. 'verifyPassword',
  2160. 'pathCreateChecks',
  2161. 'linkCreateChecks',
  2162. 'validateExpirationDate',
  2163. ])
  2164. ->getMock();
  2165. $originalShare = $this->manager->newShare();
  2166. $originalShare->setShareType(\OCP\Share::SHARE_TYPE_EMAIL)
  2167. ->setPermissions(\OCP\Constants::PERMISSION_ALL);
  2168. $tomorrow = new \DateTime();
  2169. $tomorrow->setTime(0,0,0);
  2170. $tomorrow->add(new \DateInterval('P1D'));
  2171. $file = $this->createMock(File::class);
  2172. $file->method('getId')->willReturn(100);
  2173. $share = $this->manager->newShare();
  2174. $share->setProviderId('foo')
  2175. ->setId('42')
  2176. ->setShareType(\OCP\Share::SHARE_TYPE_EMAIL)
  2177. ->setToken('token')
  2178. ->setSharedBy('owner')
  2179. ->setShareOwner('owner')
  2180. ->setPassword('password')
  2181. ->setExpirationDate($tomorrow)
  2182. ->setNode($file)
  2183. ->setPermissions(\OCP\Constants::PERMISSION_ALL);
  2184. $manager->expects($this->once())->method('canShare')->willReturn(true);
  2185. $manager->expects($this->once())->method('getShareById')->with('foo:42')->willReturn($originalShare);
  2186. $manager->expects($this->once())->method('generalCreateChecks')->with($share);
  2187. $manager->expects($this->once())->method('verifyPassword')->with('password');
  2188. $manager->expects($this->once())->method('pathCreateChecks')->with($file);
  2189. $manager->expects($this->never())->method('linkCreateChecks');
  2190. $manager->expects($this->never())->method('validateExpirationDate');
  2191. $this->hasher->expects($this->once())
  2192. ->method('hash')
  2193. ->with('password')
  2194. ->willReturn('hashed');
  2195. $this->defaultProvider->expects($this->once())
  2196. ->method('update')
  2197. ->with($share, 'password')
  2198. ->willReturn($share);
  2199. $hookListner = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  2200. \OCP\Util::connectHook('OCP\Share', 'post_set_expiration_date', $hookListner, 'post');
  2201. $hookListner->expects($this->never())->method('post');
  2202. $hookListner2 = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  2203. \OCP\Util::connectHook('OCP\Share', 'post_update_password', $hookListner2, 'post');
  2204. $hookListner2->expects($this->once())->method('post')->with([
  2205. 'itemType' => 'file',
  2206. 'itemSource' => 100,
  2207. 'uidOwner' => 'owner',
  2208. 'token' => 'token',
  2209. 'disabled' => false,
  2210. ]);
  2211. $hookListner3 = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  2212. \OCP\Util::connectHook('OCP\Share', 'post_update_permissions', $hookListner3, 'post');
  2213. $hookListner3->expects($this->never())->method('post');
  2214. $manager->updateShare($share);
  2215. }
  2216. /**
  2217. * @expectedException \InvalidArgumentException
  2218. * @expectedExceptionMessage Can’t change target of link share
  2219. */
  2220. public function testMoveShareLink() {
  2221. $share = $this->manager->newShare();
  2222. $share->setShareType(\OCP\Share::SHARE_TYPE_LINK);
  2223. $recipient = $this->createMock(IUser::class);
  2224. $this->manager->moveShare($share, $recipient);
  2225. }
  2226. /**
  2227. * @expectedException \InvalidArgumentException
  2228. * @expectedExceptionMessage Invalid recipient
  2229. */
  2230. public function testMoveShareUserNotRecipient() {
  2231. $share = $this->manager->newShare();
  2232. $share->setShareType(\OCP\Share::SHARE_TYPE_USER);
  2233. $share->setSharedWith('sharedWith');
  2234. $this->manager->moveShare($share, 'recipient');
  2235. }
  2236. public function testMoveShareUser() {
  2237. $share = $this->manager->newShare();
  2238. $share->setShareType(\OCP\Share::SHARE_TYPE_USER)
  2239. ->setId('42')
  2240. ->setProviderId('foo');
  2241. $share->setSharedWith('recipient');
  2242. $this->defaultProvider->method('move')->with($share, 'recipient')->will($this->returnArgument(0));
  2243. $this->manager->moveShare($share, 'recipient');
  2244. }
  2245. /**
  2246. * @expectedException \InvalidArgumentException
  2247. * @expectedExceptionMessage Invalid recipient
  2248. */
  2249. public function testMoveShareGroupNotRecipient() {
  2250. $share = $this->manager->newShare();
  2251. $share->setShareType(\OCP\Share::SHARE_TYPE_GROUP);
  2252. $sharedWith = $this->createMock(IGroup::class);
  2253. $share->setSharedWith('shareWith');
  2254. $recipient = $this->createMock(IUser::class);
  2255. $sharedWith->method('inGroup')->with($recipient)->willReturn(false);
  2256. $this->groupManager->method('get')->with('shareWith')->willReturn($sharedWith);
  2257. $this->userManager->method('get')->with('recipient')->willReturn($recipient);
  2258. $this->manager->moveShare($share, 'recipient');
  2259. }
  2260. /**
  2261. * @expectedException \InvalidArgumentException
  2262. * @expectedExceptionMessage Group "shareWith" does not exist
  2263. */
  2264. public function testMoveShareGroupNull() {
  2265. $share = $this->manager->newShare();
  2266. $share->setShareType(\OCP\Share::SHARE_TYPE_GROUP);
  2267. $share->setSharedWith('shareWith');
  2268. $recipient = $this->createMock(IUser::class);
  2269. $this->groupManager->method('get')->with('shareWith')->willReturn(null);
  2270. $this->userManager->method('get')->with('recipient')->willReturn($recipient);
  2271. $this->manager->moveShare($share, 'recipient');
  2272. }
  2273. public function testMoveShareGroup() {
  2274. $share = $this->manager->newShare();
  2275. $share->setShareType(\OCP\Share::SHARE_TYPE_GROUP)
  2276. ->setId('42')
  2277. ->setProviderId('foo');
  2278. $group = $this->createMock(IGroup::class);
  2279. $share->setSharedWith('group');
  2280. $recipient = $this->createMock(IUser::class);
  2281. $group->method('inGroup')->with($recipient)->willReturn(true);
  2282. $this->groupManager->method('get')->with('group')->willReturn($group);
  2283. $this->userManager->method('get')->with('recipient')->willReturn($recipient);
  2284. $this->defaultProvider->method('move')->with($share, 'recipient')->will($this->returnArgument(0));
  2285. $this->manager->moveShare($share, 'recipient');
  2286. }
  2287. /**
  2288. * @dataProvider dataTestShareProviderExists
  2289. */
  2290. public function testShareProviderExists($shareType, $expected) {
  2291. $factory = $this->getMockBuilder('OCP\Share\IProviderFactory')->getMock();
  2292. $factory->expects($this->any())->method('getProviderForType')
  2293. ->willReturnCallback(function ($id) {
  2294. if ($id === \OCP\Share::SHARE_TYPE_USER) {
  2295. return true;
  2296. }
  2297. throw new Exception\ProviderException();
  2298. });
  2299. $manager = new Manager(
  2300. $this->logger,
  2301. $this->config,
  2302. $this->secureRandom,
  2303. $this->hasher,
  2304. $this->mountManager,
  2305. $this->groupManager,
  2306. $this->l,
  2307. $this->l10nFactory,
  2308. $factory,
  2309. $this->userManager,
  2310. $this->rootFolder,
  2311. $this->eventDispatcher,
  2312. $this->mailer,
  2313. $this->urlGenerator,
  2314. $this->defaults
  2315. );
  2316. $this->assertSame($expected,
  2317. $manager->shareProviderExists($shareType)
  2318. );
  2319. }
  2320. public function dataTestShareProviderExists() {
  2321. return [
  2322. [\OCP\Share::SHARE_TYPE_USER, true],
  2323. [42, false],
  2324. ];
  2325. }
  2326. public function testGetSharesInFolder() {
  2327. $factory = new DummyFactory2($this->createMock(IServerContainer::class));
  2328. $manager = new Manager(
  2329. $this->logger,
  2330. $this->config,
  2331. $this->secureRandom,
  2332. $this->hasher,
  2333. $this->mountManager,
  2334. $this->groupManager,
  2335. $this->l,
  2336. $this->l10nFactory,
  2337. $factory,
  2338. $this->userManager,
  2339. $this->rootFolder,
  2340. $this->eventDispatcher,
  2341. $this->mailer,
  2342. $this->urlGenerator,
  2343. $this->defaults
  2344. );
  2345. $factory->setProvider($this->defaultProvider);
  2346. $extraProvider = $this->createMock(IShareProvider::class);
  2347. $factory->setSecondProvider($extraProvider);
  2348. $share1 = $this->createMock(IShare::class);
  2349. $share2 = $this->createMock(IShare::class);
  2350. $share3 = $this->createMock(IShare::class);
  2351. $share4 = $this->createMock(IShare::class);
  2352. $folder = $this->createMock(Folder::class);
  2353. $this->defaultProvider->method('getSharesInFolder')
  2354. ->with(
  2355. $this->equalTo('user'),
  2356. $this->equalTo($folder),
  2357. $this->equalTo(false)
  2358. )->willReturn([
  2359. 1 => [$share1],
  2360. 2 => [$share2],
  2361. ]);
  2362. $extraProvider->method('getSharesInFolder')
  2363. ->with(
  2364. $this->equalTo('user'),
  2365. $this->equalTo($folder),
  2366. $this->equalTo(false)
  2367. )->willReturn([
  2368. 2 => [$share3],
  2369. 3 => [$share4],
  2370. ]);
  2371. $result = $manager->getSharesInFolder('user', $folder, false);
  2372. $expects = [
  2373. 1 => [$share1],
  2374. 2 => [$share2, $share3],
  2375. 3 => [$share4],
  2376. ];
  2377. $this->assertSame($expects, $result);
  2378. }
  2379. public function testGetAccessList() {
  2380. $factory = new DummyFactory2($this->createMock(IServerContainer::class));
  2381. $manager = new Manager(
  2382. $this->logger,
  2383. $this->config,
  2384. $this->secureRandom,
  2385. $this->hasher,
  2386. $this->mountManager,
  2387. $this->groupManager,
  2388. $this->l,
  2389. $this->l10nFactory,
  2390. $factory,
  2391. $this->userManager,
  2392. $this->rootFolder,
  2393. $this->eventDispatcher,
  2394. $this->mailer,
  2395. $this->urlGenerator,
  2396. $this->defaults
  2397. );
  2398. $factory->setProvider($this->defaultProvider);
  2399. $extraProvider = $this->createMock(IShareProvider::class);
  2400. $factory->setSecondProvider($extraProvider);
  2401. $owner = $this->createMock(IUser::class);
  2402. $owner->expects($this->once())
  2403. ->method('getUID')
  2404. ->willReturn('owner');
  2405. $node = $this->createMock(Node::class);
  2406. $node->expects($this->once())
  2407. ->method('getOwner')
  2408. ->willReturn($owner);
  2409. $node->method('getId')
  2410. ->willReturn(42);
  2411. $userFolder = $this->createMock(Folder::class);
  2412. $file = $this->createMock(File::class);
  2413. $folder = $this->createMock(Folder::class);
  2414. $file->method('getParent')
  2415. ->willReturn($folder);
  2416. $file->method('getPath')
  2417. ->willReturn('/owner/files/folder/file');
  2418. $file->method('getId')
  2419. ->willReturn(23);
  2420. $folder->method('getParent')
  2421. ->willReturn($userFolder);
  2422. $folder->method('getPath')
  2423. ->willReturn('/owner/files/folder');
  2424. $userFolder->method('getById')
  2425. ->with($this->equalTo(42))
  2426. ->willReturn([$file]);
  2427. $userFolder->method('getPath')
  2428. ->willReturn('/owner/files');
  2429. $this->userManager->method('userExists')
  2430. ->with($this->equalTo('owner'))
  2431. ->willReturn(true);
  2432. $this->defaultProvider->method('getAccessList')
  2433. ->with(
  2434. $this->equalTo([$file, $folder]),
  2435. false
  2436. )
  2437. ->willReturn([
  2438. 'users' => [
  2439. 'user1',
  2440. 'user2',
  2441. 'user3',
  2442. '123456',
  2443. ],
  2444. 'public' => true,
  2445. ]);
  2446. $extraProvider->method('getAccessList')
  2447. ->with(
  2448. $this->equalTo([$file, $folder]),
  2449. false
  2450. )
  2451. ->willReturn([
  2452. 'users' => [
  2453. 'user3',
  2454. 'user4',
  2455. 'user5',
  2456. '234567',
  2457. ],
  2458. 'remote' => true,
  2459. ]);
  2460. $this->rootFolder->method('getUserFolder')
  2461. ->with($this->equalTo('owner'))
  2462. ->willReturn($userFolder);
  2463. $expected = [
  2464. 'users' => ['owner', 'user1', 'user2', 'user3', '123456','user4', 'user5', '234567'],
  2465. 'remote' => true,
  2466. 'public' => true,
  2467. ];
  2468. $result = $manager->getAccessList($node, true, false);
  2469. $this->assertSame($expected['public'], $result['public']);
  2470. $this->assertSame($expected['remote'], $result['remote']);
  2471. $this->assertSame($expected['users'], $result['users']);
  2472. }
  2473. public function testGetAccessListWithCurrentAccess() {
  2474. $factory = new DummyFactory2($this->createMock(IServerContainer::class));
  2475. $manager = new Manager(
  2476. $this->logger,
  2477. $this->config,
  2478. $this->secureRandom,
  2479. $this->hasher,
  2480. $this->mountManager,
  2481. $this->groupManager,
  2482. $this->l,
  2483. $this->l10nFactory,
  2484. $factory,
  2485. $this->userManager,
  2486. $this->rootFolder,
  2487. $this->eventDispatcher,
  2488. $this->mailer,
  2489. $this->urlGenerator,
  2490. $this->defaults
  2491. );
  2492. $factory->setProvider($this->defaultProvider);
  2493. $extraProvider = $this->createMock(IShareProvider::class);
  2494. $factory->setSecondProvider($extraProvider);
  2495. $owner = $this->createMock(IUser::class);
  2496. $owner->expects($this->once())
  2497. ->method('getUID')
  2498. ->willReturn('owner');
  2499. $node = $this->createMock(Node::class);
  2500. $node->expects($this->once())
  2501. ->method('getOwner')
  2502. ->willReturn($owner);
  2503. $node->method('getId')
  2504. ->willReturn(42);
  2505. $userFolder = $this->createMock(Folder::class);
  2506. $file = $this->createMock(File::class);
  2507. $folder = $this->createMock(Folder::class);
  2508. $file->method('getParent')
  2509. ->willReturn($folder);
  2510. $file->method('getPath')
  2511. ->willReturn('/owner/files/folder/file');
  2512. $file->method('getId')
  2513. ->willReturn(23);
  2514. $folder->method('getParent')
  2515. ->willReturn($userFolder);
  2516. $folder->method('getPath')
  2517. ->willReturn('/owner/files/folder');
  2518. $userFolder->method('getById')
  2519. ->with($this->equalTo(42))
  2520. ->willReturn([$file]);
  2521. $userFolder->method('getPath')
  2522. ->willReturn('/owner/files');
  2523. $this->userManager->method('userExists')
  2524. ->with($this->equalTo('owner'))
  2525. ->willReturn(true);
  2526. $this->defaultProvider->method('getAccessList')
  2527. ->with(
  2528. $this->equalTo([$file, $folder]),
  2529. true
  2530. )
  2531. ->willReturn([
  2532. 'users' => [
  2533. 'user1' => [],
  2534. 'user2' => [],
  2535. 'user3' => [],
  2536. '123456' => [],
  2537. ],
  2538. 'public' => true,
  2539. ]);
  2540. $extraProvider->method('getAccessList')
  2541. ->with(
  2542. $this->equalTo([$file, $folder]),
  2543. true
  2544. )
  2545. ->willReturn([
  2546. 'users' => [
  2547. 'user3' => [],
  2548. 'user4' => [],
  2549. 'user5' => [],
  2550. '234567' => [],
  2551. ],
  2552. 'remote' => [
  2553. 'remote1',
  2554. ],
  2555. ]);
  2556. $this->rootFolder->method('getUserFolder')
  2557. ->with($this->equalTo('owner'))
  2558. ->willReturn($userFolder);
  2559. $expected = [
  2560. 'users' => [
  2561. 'owner' => [
  2562. 'node_id' => 23,
  2563. 'node_path' => '/folder/file'
  2564. ]
  2565. , 'user1' => [], 'user2' => [], 'user3' => [], '123456' => [], 'user4' => [], 'user5' => [], '234567' => []],
  2566. 'remote' => [
  2567. 'remote1',
  2568. ],
  2569. 'public' => true,
  2570. ];
  2571. $result = $manager->getAccessList($node, true, true);
  2572. $this->assertSame($expected['public'], $result['public']);
  2573. $this->assertSame($expected['remote'], $result['remote']);
  2574. $this->assertSame($expected['users'], $result['users']);
  2575. }
  2576. }
  2577. class DummyFactory implements IProviderFactory {
  2578. /** @var IShareProvider */
  2579. protected $provider;
  2580. public function __construct(\OCP\IServerContainer $serverContainer) {
  2581. }
  2582. /**
  2583. * @param IShareProvider $provider
  2584. */
  2585. public function setProvider($provider) {
  2586. $this->provider = $provider;
  2587. }
  2588. /**
  2589. * @param string $id
  2590. * @return IShareProvider
  2591. */
  2592. public function getProvider($id) {
  2593. return $this->provider;
  2594. }
  2595. /**
  2596. * @param int $shareType
  2597. * @return IShareProvider
  2598. */
  2599. public function getProviderForType($shareType) {
  2600. return $this->provider;
  2601. }
  2602. /**
  2603. * @return IShareProvider[]
  2604. */
  2605. public function getAllProviders() {
  2606. return [$this->provider];
  2607. }
  2608. }
  2609. class DummyFactory2 extends DummyFactory {
  2610. /** @var IShareProvider */
  2611. private $provider2;
  2612. /**
  2613. * @param IShareProvider $provider
  2614. */
  2615. public function setSecondProvider($provider) {
  2616. $this->provider2 = $provider;
  2617. }
  2618. public function getAllProviders() {
  2619. return [$this->provider, $this->provider2];
  2620. }
  2621. }