ManagerTest.php 113 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716
  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. $owner = $this->createMock(IUser::class);
  476. $owner->method('getUID')
  477. ->willReturn($user0);
  478. $file = $this->createMock(File::class);
  479. $node = $this->createMock(Node::class);
  480. $data = [
  481. [$this->createShare(null, \OCP\Share::SHARE_TYPE_USER, $file, null, $user0, $user0, 31, null, null), 'SharedWith is not a valid user', true],
  482. [$this->createShare(null, \OCP\Share::SHARE_TYPE_USER, $file, $group0, $user0, $user0, 31, null, null), 'SharedWith is not a valid user', true],
  483. [$this->createShare(null, \OCP\Share::SHARE_TYPE_USER, $file, 'foo@bar.com', $user0, $user0, 31, null, null), 'SharedWith is not a valid user', true],
  484. [$this->createShare(null, \OCP\Share::SHARE_TYPE_GROUP, $file, null, $user0, $user0, 31, null, null), 'SharedWith is not a valid group', true],
  485. [$this->createShare(null, \OCP\Share::SHARE_TYPE_GROUP, $file, $user2, $user0, $user0, 31, null, null), 'SharedWith is not a valid group', true],
  486. [$this->createShare(null, \OCP\Share::SHARE_TYPE_GROUP, $file, 'foo@bar.com', $user0, $user0, 31, null, null), 'SharedWith is not a valid group', true],
  487. [$this->createShare(null, \OCP\Share::SHARE_TYPE_LINK, $file, $user2, $user0, $user0, 31, null, null), 'SharedWith should be empty', true],
  488. [$this->createShare(null, \OCP\Share::SHARE_TYPE_LINK, $file, $group0, $user0, $user0, 31, null, null), 'SharedWith should be empty', true],
  489. [$this->createShare(null, \OCP\Share::SHARE_TYPE_LINK, $file, 'foo@bar.com', $user0, $user0, 31, null, null), 'SharedWith should be empty', true],
  490. [$this->createShare(null, -1, $file, null, $user0, $user0, 31, null, null), 'unknown share type', true],
  491. [$this->createShare(null, \OCP\Share::SHARE_TYPE_USER, $file, $user2, null, $user0, 31, null, null), 'SharedBy should be set', true],
  492. [$this->createShare(null, \OCP\Share::SHARE_TYPE_GROUP, $file, $group0, null, $user0, 31, null, null), 'SharedBy should be set', true],
  493. [$this->createShare(null, \OCP\Share::SHARE_TYPE_LINK, $file, null, null, $user0, 31, null, null), 'SharedBy should be set', true],
  494. [$this->createShare(null, \OCP\Share::SHARE_TYPE_USER, $file, $user0, $user0, $user0, 31, null, null), 'Can’t share with yourself', true],
  495. [$this->createShare(null, \OCP\Share::SHARE_TYPE_USER, null, $user2, $user0, $user0, 31, null, null), 'Path should be set', true],
  496. [$this->createShare(null, \OCP\Share::SHARE_TYPE_GROUP, null, $group0, $user0, $user0, 31, null, null), 'Path should be set', true],
  497. [$this->createShare(null, \OCP\Share::SHARE_TYPE_LINK, null, null, $user0, $user0, 31, null, null), 'Path should be set', true],
  498. [$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],
  499. [$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],
  500. [$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],
  501. ];
  502. $nonShareAble = $this->createMock(Folder::class);
  503. $nonShareAble->method('isShareable')->willReturn(false);
  504. $nonShareAble->method('getPath')->willReturn('path');
  505. $nonShareAble->method('getOwner')
  506. ->willReturn($owner);
  507. $data[] = [$this->createShare(null, \OCP\Share::SHARE_TYPE_USER, $nonShareAble, $user2, $user0, $user0, 31, null, null), 'You are not allowed to share path', true];
  508. $data[] = [$this->createShare(null, \OCP\Share::SHARE_TYPE_GROUP, $nonShareAble, $group0, $user0, $user0, 31, null, null), 'You are not allowed to share path', true];
  509. $data[] = [$this->createShare(null, \OCP\Share::SHARE_TYPE_LINK, $nonShareAble, null, $user0, $user0, 31, null, null), 'You are not allowed to share path', true];
  510. $limitedPermssions = $this->createMock(File::class);
  511. $limitedPermssions->method('isShareable')->willReturn(true);
  512. $limitedPermssions->method('getPermissions')->willReturn(\OCP\Constants::PERMISSION_READ);
  513. $limitedPermssions->method('getPath')->willReturn('path');
  514. $limitedPermssions->method('getOwner')
  515. ->willReturn($owner);
  516. $data[] = [$this->createShare(null, \OCP\Share::SHARE_TYPE_USER, $limitedPermssions, $user2, $user0, $user0, null, null, null), 'A share requires permissions', true];
  517. $data[] = [$this->createShare(null, \OCP\Share::SHARE_TYPE_GROUP, $limitedPermssions, $group0, $user0, $user0, null, null, null), 'A share requires permissions', true];
  518. $data[] = [$this->createShare(null, \OCP\Share::SHARE_TYPE_LINK, $limitedPermssions, null, $user0, $user0, null, null, null), 'A share requires permissions', true];
  519. $mount = $this->createMock(MoveableMount::class);
  520. $limitedPermssions->method('getMountPoint')->willReturn($mount);
  521. $data[] = [$this->createShare(null, \OCP\Share::SHARE_TYPE_USER, $limitedPermssions, $user2, $user0, $user0, 31, null, null), 'Can’t increase permissions of path', true];
  522. $data[] = [$this->createShare(null, \OCP\Share::SHARE_TYPE_GROUP, $limitedPermssions, $group0, $user0, $user0, 17, null, null), 'Can’t increase permissions of path', true];
  523. $data[] = [$this->createShare(null, \OCP\Share::SHARE_TYPE_LINK, $limitedPermssions, null, $user0, $user0, 3, null, null), 'Can’t increase permissions of path', true];
  524. $nonMoveableMountPermssions = $this->createMock(Folder::class);
  525. $nonMoveableMountPermssions->method('isShareable')->willReturn(true);
  526. $nonMoveableMountPermssions->method('getPermissions')->willReturn(\OCP\Constants::PERMISSION_READ);
  527. $nonMoveableMountPermssions->method('getPath')->willReturn('path');
  528. $nonMoveableMountPermssions->method('getOwner')
  529. ->willReturn($owner);
  530. $data[] = [$this->createShare(null, \OCP\Share::SHARE_TYPE_USER, $nonMoveableMountPermssions, $user2, $user0, $user0, 11, null, null), 'Can’t increase permissions of path', false];
  531. $data[] = [$this->createShare(null, \OCP\Share::SHARE_TYPE_GROUP, $nonMoveableMountPermssions, $group0, $user0, $user0, 11, null, null), 'Can’t increase permissions of path', false];
  532. $rootFolder = $this->createMock(Folder::class);
  533. $rootFolder->method('isShareable')->willReturn(true);
  534. $rootFolder->method('getPermissions')->willReturn(\OCP\Constants::PERMISSION_ALL);
  535. $rootFolder->method('getPath')->willReturn('myrootfolder');
  536. $data[] = [$this->createShare(null, \OCP\Share::SHARE_TYPE_USER, $rootFolder, $user2, $user0, $user0, 30, null, null), 'You can’t share your root folder', true];
  537. $data[] = [$this->createShare(null, \OCP\Share::SHARE_TYPE_GROUP, $rootFolder, $group0, $user0, $user0, 2, null, null), 'You can’t share your root folder', true];
  538. $data[] = [$this->createShare(null, \OCP\Share::SHARE_TYPE_LINK, $rootFolder, null, $user0, $user0, 16, null, null), 'You can’t share your root folder', true];
  539. $allPermssions = $this->createMock(Folder::class);
  540. $allPermssions->method('isShareable')->willReturn(true);
  541. $allPermssions->method('getPermissions')->willReturn(\OCP\Constants::PERMISSION_ALL);
  542. $allPermssions->method('getOwner')
  543. ->willReturn($owner);
  544. $data[] = [$this->createShare(null, \OCP\Share::SHARE_TYPE_USER, $allPermssions, $user2, $user0, $user0, 30, null, null), 'Shares need at least read permissions', true];
  545. $data[] = [$this->createShare(null, \OCP\Share::SHARE_TYPE_GROUP, $allPermssions, $group0, $user0, $user0, 2, null, null), 'Shares need at least read permissions', true];
  546. $data[] = [$this->createShare(null, \OCP\Share::SHARE_TYPE_USER, $allPermssions, $user2, $user0, $user0, 31, null, null), null, false];
  547. $data[] = [$this->createShare(null, \OCP\Share::SHARE_TYPE_GROUP, $allPermssions, $group0, $user0, $user0, 3, null, null), null, false];
  548. $data[] = [$this->createShare(null, \OCP\Share::SHARE_TYPE_LINK, $allPermssions, null, $user0, $user0, 17, null, null), null, false];
  549. return $data;
  550. }
  551. /**
  552. * @dataProvider dataGeneralChecks
  553. *
  554. * @param $share
  555. * @param $exceptionMessage
  556. * @param $exception
  557. */
  558. public function testGeneralChecks($share, $exceptionMessage, $exception) {
  559. $thrown = null;
  560. $this->userManager->method('userExists')->will($this->returnValueMap([
  561. ['user0', true],
  562. ['user1', true],
  563. ]));
  564. $this->groupManager->method('groupExists')->will($this->returnValueMap([
  565. ['group0', true],
  566. ]));
  567. $userFolder = $this->createMock(Folder::class);
  568. $userFolder->method('getPath')->willReturn('myrootfolder');
  569. $this->rootFolder->method('getUserFolder')->willReturn($userFolder);
  570. try {
  571. self::invokePrivate($this->manager, 'generalCreateChecks', [$share]);
  572. $thrown = false;
  573. } catch (\OCP\Share\Exceptions\GenericShareException $e) {
  574. $this->assertEquals($exceptionMessage, $e->getHint());
  575. $thrown = true;
  576. } catch(\InvalidArgumentException $e) {
  577. $this->assertEquals($exceptionMessage, $e->getMessage());
  578. $thrown = true;
  579. }
  580. $this->assertSame($exception, $thrown);
  581. }
  582. /**
  583. * @expectedException \InvalidArgumentException
  584. * @expectedExceptionMessage You can’t share your root folder
  585. */
  586. public function testGeneralCheckShareRoot() {
  587. $thrown = null;
  588. $this->userManager->method('userExists')->will($this->returnValueMap([
  589. ['user0', true],
  590. ['user1', true],
  591. ]));
  592. $userFolder = $this->createMock(Folder::class);
  593. $userFolder->method('isSubNode')->with($userFolder)->willReturn(false);
  594. $this->rootFolder->method('getUserFolder')->willReturn($userFolder);
  595. $share = $this->manager->newShare();
  596. $share->setShareType(\OCP\Share::SHARE_TYPE_USER)
  597. ->setSharedWith('user0')
  598. ->setSharedBy('user1')
  599. ->setNode($userFolder);
  600. self::invokePrivate($this->manager, 'generalCreateChecks', [$share]);
  601. }
  602. /**
  603. * @expectedException \OCP\Share\Exceptions\GenericShareException
  604. * @expectedExceptionMessage Expiration date is in the past
  605. */
  606. public function testvalidateExpirationDateInPast() {
  607. // Expire date in the past
  608. $past = new \DateTime();
  609. $past->sub(new \DateInterval('P1D'));
  610. $share = $this->manager->newShare();
  611. $share->setExpirationDate($past);
  612. self::invokePrivate($this->manager, 'validateExpirationDate', [$share]);
  613. }
  614. /**
  615. * @expectedException \InvalidArgumentException
  616. * @expectedExceptionMessage Expiration date is enforced
  617. */
  618. public function testvalidateExpirationDateEnforceButNotSet() {
  619. $share = $this->manager->newShare();
  620. $share->setProviderId('foo')->setId('bar');
  621. $this->config->method('getAppValue')
  622. ->will($this->returnValueMap([
  623. ['core', 'shareapi_default_expire_date', 'no', 'yes'],
  624. ['core', 'shareapi_enforce_expire_date', 'no', 'yes'],
  625. ]));
  626. self::invokePrivate($this->manager, 'validateExpirationDate', [$share]);
  627. }
  628. public function testvalidateExpirationDateEnforceButNotEnabledAndNotSet() {
  629. $share = $this->manager->newShare();
  630. $share->setProviderId('foo')->setId('bar');
  631. $this->config->method('getAppValue')
  632. ->will($this->returnValueMap([
  633. ['core', 'shareapi_enforce_expire_date', 'no', 'yes'],
  634. ]));
  635. self::invokePrivate($this->manager, 'validateExpirationDate', [$share]);
  636. $this->assertNull($share->getExpirationDate());
  637. }
  638. public function testvalidateExpirationDateEnforceButNotSetNewShare() {
  639. $share = $this->manager->newShare();
  640. $this->config->method('getAppValue')
  641. ->will($this->returnValueMap([
  642. ['core', 'shareapi_enforce_expire_date', 'no', 'yes'],
  643. ['core', 'shareapi_expire_after_n_days', '7', '3'],
  644. ['core', 'shareapi_default_expire_date', 'no', 'yes'],
  645. ]));
  646. $expected = new \DateTime();
  647. $expected->setTime(0,0,0);
  648. $expected->add(new \DateInterval('P3D'));
  649. self::invokePrivate($this->manager, 'validateExpirationDate', [$share]);
  650. $this->assertNotNull($share->getExpirationDate());
  651. $this->assertEquals($expected, $share->getExpirationDate());
  652. }
  653. public function testvalidateExpirationDateEnforceToFarIntoFuture() {
  654. // Expire date in the past
  655. $future = new \DateTime();
  656. $future->add(new \DateInterval('P7D'));
  657. $share = $this->manager->newShare();
  658. $share->setExpirationDate($future);
  659. $this->config->method('getAppValue')
  660. ->will($this->returnValueMap([
  661. ['core', 'shareapi_enforce_expire_date', 'no', 'yes'],
  662. ['core', 'shareapi_expire_after_n_days', '7', '3'],
  663. ]));
  664. try {
  665. self::invokePrivate($this->manager, 'validateExpirationDate', [$share]);
  666. $this->addToAssertionCount(1);
  667. } catch (\OCP\Share\Exceptions\GenericShareException $e) {
  668. $this->assertEquals('Cannot set expiration date more than 3 days in the future', $e->getMessage());
  669. $this->assertEquals('Cannot set expiration date more than 3 days in the future', $e->getHint());
  670. $this->assertEquals(404, $e->getCode());
  671. }
  672. }
  673. public function testvalidateExpirationDateEnforceValid() {
  674. // Expire date in the past
  675. $future = new \DateTime();
  676. $future->add(new \DateInterval('P2D'));
  677. $future->setTime(0,0,0);
  678. $expected = clone $future;
  679. $future->setTime(1,2,3);
  680. $share = $this->manager->newShare();
  681. $share->setExpirationDate($future);
  682. $this->config->method('getAppValue')
  683. ->will($this->returnValueMap([
  684. ['core', 'shareapi_enforce_expire_date', 'no', 'yes'],
  685. ['core', 'shareapi_expire_after_n_days', '7', '3'],
  686. ]));
  687. $hookListner = $this->getMockBuilder('Dummy')->setMethods(['listener'])->getMock();
  688. \OCP\Util::connectHook('\OC\Share', 'verifyExpirationDate', $hookListner, 'listener');
  689. $hookListner->expects($this->once())->method('listener')->with($this->callback(function ($data) use ($future) {
  690. return $data['expirationDate'] == $future;
  691. }));
  692. self::invokePrivate($this->manager, 'validateExpirationDate', [$share]);
  693. $this->assertEquals($expected, $share->getExpirationDate());
  694. }
  695. public function testvalidateExpirationDateNoDateNoDefaultNull() {
  696. $date = new \DateTime();
  697. $date->add(new \DateInterval('P5D'));
  698. $expected = clone $date;
  699. $expected->setTime(0,0,0);
  700. $share = $this->manager->newShare();
  701. $share->setExpirationDate($date);
  702. $hookListner = $this->getMockBuilder('Dummy')->setMethods(['listener'])->getMock();
  703. \OCP\Util::connectHook('\OC\Share', 'verifyExpirationDate', $hookListner, 'listener');
  704. $hookListner->expects($this->once())->method('listener')->with($this->callback(function ($data) use ($expected) {
  705. return $data['expirationDate'] == $expected && $data['passwordSet'] === false;
  706. }));
  707. self::invokePrivate($this->manager, 'validateExpirationDate', [$share]);
  708. $this->assertEquals($expected, $share->getExpirationDate());
  709. }
  710. public function testvalidateExpirationDateNoDateNoDefault() {
  711. $hookListner = $this->getMockBuilder('Dummy')->setMethods(['listener'])->getMock();
  712. \OCP\Util::connectHook('\OC\Share', 'verifyExpirationDate', $hookListner, 'listener');
  713. $hookListner->expects($this->once())->method('listener')->with($this->callback(function ($data) {
  714. return $data['expirationDate'] === null && $data['passwordSet'] === true;
  715. }));
  716. $share = $this->manager->newShare();
  717. $share->setPassword('password');
  718. self::invokePrivate($this->manager, 'validateExpirationDate', [$share]);
  719. $this->assertNull($share->getExpirationDate());
  720. }
  721. public function testvalidateExpirationDateNoDateDefault() {
  722. $future = new \DateTime();
  723. $future->add(new \DateInterval('P3D'));
  724. $future->setTime(0,0,0);
  725. $expected = clone $future;
  726. $share = $this->manager->newShare();
  727. $share->setExpirationDate($future);
  728. $this->config->method('getAppValue')
  729. ->will($this->returnValueMap([
  730. ['core', 'shareapi_default_expire_date', 'no', 'yes'],
  731. ['core', 'shareapi_expire_after_n_days', '7', '3'],
  732. ]));
  733. $hookListner = $this->getMockBuilder('Dummy')->setMethods(['listener'])->getMock();
  734. \OCP\Util::connectHook('\OC\Share', 'verifyExpirationDate', $hookListner, 'listener');
  735. $hookListner->expects($this->once())->method('listener')->with($this->callback(function ($data) use ($expected) {
  736. return $data['expirationDate'] == $expected;
  737. }));
  738. self::invokePrivate($this->manager, 'validateExpirationDate', [$share]);
  739. $this->assertEquals($expected, $share->getExpirationDate());
  740. }
  741. public function testValidateExpirationDateHookModification() {
  742. $nextWeek = new \DateTime();
  743. $nextWeek->add(new \DateInterval('P7D'));
  744. $nextWeek->setTime(0,0,0);
  745. $save = clone $nextWeek;
  746. $hookListner = $this->getMockBuilder('Dummy')->setMethods(['listener'])->getMock();
  747. \OCP\Util::connectHook('\OC\Share', 'verifyExpirationDate', $hookListner, 'listener');
  748. $hookListner->expects($this->once())->method('listener')->will($this->returnCallback(function ($data) {
  749. $data['expirationDate']->sub(new \DateInterval('P2D'));
  750. }));
  751. $share = $this->manager->newShare();
  752. $share->setExpirationDate($nextWeek);
  753. self::invokePrivate($this->manager, 'validateExpirationDate', [$share]);
  754. $save->sub(new \DateInterval('P2D'));
  755. $this->assertEquals($save, $share->getExpirationDate());
  756. }
  757. /**
  758. * @expectedException \Exception
  759. * @expectedExceptionMessage Invalid date!
  760. */
  761. public function testValidateExpirationDateHookException() {
  762. $nextWeek = new \DateTime();
  763. $nextWeek->add(new \DateInterval('P7D'));
  764. $nextWeek->setTime(0,0,0);
  765. $share = $this->manager->newShare();
  766. $share->setExpirationDate($nextWeek);
  767. $hookListner = $this->getMockBuilder('Dummy')->setMethods(['listener'])->getMock();
  768. \OCP\Util::connectHook('\OC\Share', 'verifyExpirationDate', $hookListner, 'listener');
  769. $hookListner->expects($this->once())->method('listener')->will($this->returnCallback(function ($data) {
  770. $data['accepted'] = false;
  771. $data['message'] = 'Invalid date!';
  772. }));
  773. self::invokePrivate($this->manager, 'validateExpirationDate', [$share]);
  774. }
  775. public function testValidateExpirationDateExistingShareNoDefault() {
  776. $share = $this->manager->newShare();
  777. $share->setId('42')->setProviderId('foo');
  778. $this->config->method('getAppValue')
  779. ->will($this->returnValueMap([
  780. ['core', 'shareapi_default_expire_date', 'no', 'yes'],
  781. ['core', 'shareapi_expire_after_n_days', '7', '6'],
  782. ]));
  783. self::invokePrivate($this->manager, 'validateExpirationDate', [$share]);
  784. $this->assertEquals(null, $share->getExpirationDate());
  785. }
  786. /**
  787. * @expectedException Exception
  788. * @expectedExceptionMessage Sharing is only allowed with group members
  789. */
  790. public function testUserCreateChecksShareWithGroupMembersOnlyDifferentGroups() {
  791. $share = $this->manager->newShare();
  792. $sharedBy = $this->createMock(IUser::class);
  793. $sharedWith = $this->createMock(IUser::class);
  794. $share->setSharedBy('sharedBy')->setSharedWith('sharedWith');
  795. $this->groupManager
  796. ->method('getUserGroupIds')
  797. ->will(
  798. $this->returnValueMap([
  799. [$sharedBy, ['group1']],
  800. [$sharedWith, ['group2']],
  801. ])
  802. );
  803. $this->userManager->method('get')->will($this->returnValueMap([
  804. ['sharedBy', $sharedBy],
  805. ['sharedWith', $sharedWith],
  806. ]));
  807. $this->config
  808. ->method('getAppValue')
  809. ->will($this->returnValueMap([
  810. ['core', 'shareapi_only_share_with_group_members', 'no', 'yes'],
  811. ]));
  812. self::invokePrivate($this->manager, 'userCreateChecks', [$share]);
  813. }
  814. public function testUserCreateChecksShareWithGroupMembersOnlySharedGroup() {
  815. $share = $this->manager->newShare();
  816. $sharedBy = $this->createMock(IUser::class);
  817. $sharedWith = $this->createMock(IUser::class);
  818. $share->setSharedBy('sharedBy')->setSharedWith('sharedWith');
  819. $path = $this->createMock(Node::class);
  820. $share->setNode($path);
  821. $this->groupManager
  822. ->method('getUserGroupIds')
  823. ->will(
  824. $this->returnValueMap([
  825. [$sharedBy, ['group1', 'group3']],
  826. [$sharedWith, ['group2', 'group3']],
  827. ])
  828. );
  829. $this->userManager->method('get')->will($this->returnValueMap([
  830. ['sharedBy', $sharedBy],
  831. ['sharedWith', $sharedWith],
  832. ]));
  833. $this->config
  834. ->method('getAppValue')
  835. ->will($this->returnValueMap([
  836. ['core', 'shareapi_only_share_with_group_members', 'no', 'yes'],
  837. ]));
  838. $this->defaultProvider
  839. ->method('getSharesByPath')
  840. ->with($path)
  841. ->willReturn([]);
  842. self::invokePrivate($this->manager, 'userCreateChecks', [$share]);
  843. $this->addToAssertionCount(1);
  844. }
  845. /**
  846. * @expectedException Exception
  847. * @expectedExceptionMessage Path is already shared with this user
  848. */
  849. public function testUserCreateChecksIdenticalShareExists() {
  850. $share = $this->manager->newShare();
  851. $share2 = $this->manager->newShare();
  852. $sharedWith = $this->createMock(IUser::class);
  853. $path = $this->createMock(Node::class);
  854. $share->setSharedWith('sharedWith')->setNode($path)
  855. ->setProviderId('foo')->setId('bar');
  856. $share2->setSharedWith('sharedWith')->setNode($path)
  857. ->setProviderId('foo')->setId('baz');
  858. $this->defaultProvider
  859. ->method('getSharesByPath')
  860. ->with($path)
  861. ->willReturn([$share2]);
  862. self::invokePrivate($this->manager, 'userCreateChecks', [$share]);
  863. }
  864. /**
  865. * @expectedException Exception
  866. * @expectedExceptionMessage Path is already shared with this user
  867. */
  868. public function testUserCreateChecksIdenticalPathSharedViaGroup() {
  869. $share = $this->manager->newShare();
  870. $sharedWith = $this->createMock(IUser::class);
  871. $sharedWith->method('getUID')->willReturn('sharedWith');
  872. $this->userManager->method('get')->with('sharedWith')->willReturn($sharedWith);
  873. $path = $this->createMock(Node::class);
  874. $share->setSharedWith('sharedWith')
  875. ->setNode($path)
  876. ->setShareOwner('shareOwner')
  877. ->setProviderId('foo')
  878. ->setId('bar');
  879. $share2 = $this->manager->newShare();
  880. $share2->setShareType(\OCP\Share::SHARE_TYPE_GROUP)
  881. ->setShareOwner('shareOwner2')
  882. ->setProviderId('foo')
  883. ->setId('baz')
  884. ->setSharedWith('group');
  885. $group = $this->createMock(IGroup::class);
  886. $group->method('inGroup')
  887. ->with($sharedWith)
  888. ->willReturn(true);
  889. $this->groupManager->method('get')->with('group')->willReturn($group);
  890. $this->defaultProvider
  891. ->method('getSharesByPath')
  892. ->with($path)
  893. ->willReturn([$share2]);
  894. self::invokePrivate($this->manager, 'userCreateChecks', [$share]);
  895. }
  896. public function testUserCreateChecksIdenticalPathSharedViaDeletedGroup() {
  897. $share = $this->manager->newShare();
  898. $sharedWith = $this->createMock(IUser::class);
  899. $sharedWith->method('getUID')->willReturn('sharedWith');
  900. $this->userManager->method('get')->with('sharedWith')->willReturn($sharedWith);
  901. $path = $this->createMock(Node::class);
  902. $share->setSharedWith('sharedWith')
  903. ->setNode($path)
  904. ->setShareOwner('shareOwner')
  905. ->setProviderId('foo')
  906. ->setId('bar');
  907. $share2 = $this->manager->newShare();
  908. $share2->setShareType(\OCP\Share::SHARE_TYPE_GROUP)
  909. ->setShareOwner('shareOwner2')
  910. ->setProviderId('foo')
  911. ->setId('baz')
  912. ->setSharedWith('group');
  913. $this->groupManager->method('get')->with('group')->willReturn(null);
  914. $this->defaultProvider
  915. ->method('getSharesByPath')
  916. ->with($path)
  917. ->willReturn([$share2]);
  918. $this->assertNull($this->invokePrivate($this->manager, 'userCreateChecks', [$share]));
  919. }
  920. public function testUserCreateChecksIdenticalPathNotSharedWithUser() {
  921. $share = $this->manager->newShare();
  922. $sharedWith = $this->createMock(IUser::class);
  923. $path = $this->createMock(Node::class);
  924. $share->setSharedWith('sharedWith')
  925. ->setNode($path)
  926. ->setShareOwner('shareOwner')
  927. ->setProviderId('foo')
  928. ->setId('bar');
  929. $this->userManager->method('get')->with('sharedWith')->willReturn($sharedWith);
  930. $share2 = $this->manager->newShare();
  931. $share2->setShareType(\OCP\Share::SHARE_TYPE_GROUP)
  932. ->setShareOwner('shareOwner2')
  933. ->setProviderId('foo')
  934. ->setId('baz');
  935. $group = $this->createMock(IGroup::class);
  936. $group->method('inGroup')
  937. ->with($sharedWith)
  938. ->willReturn(false);
  939. $this->groupManager->method('get')->with('group')->willReturn($group);
  940. $share2->setSharedWith('group');
  941. $this->defaultProvider
  942. ->method('getSharesByPath')
  943. ->with($path)
  944. ->willReturn([$share2]);
  945. self::invokePrivate($this->manager, 'userCreateChecks', [$share]);
  946. $this->addToAssertionCount(1);
  947. }
  948. /**
  949. * @expectedException Exception
  950. * @expectedExceptionMessage Group sharing is now allowed
  951. */
  952. public function testGroupCreateChecksShareWithGroupMembersGroupSharingNotAllowed() {
  953. $share = $this->manager->newShare();
  954. $this->config
  955. ->method('getAppValue')
  956. ->will($this->returnValueMap([
  957. ['core', 'shareapi_allow_group_sharing', 'yes', 'no'],
  958. ]));
  959. self::invokePrivate($this->manager, 'groupCreateChecks', [$share]);
  960. }
  961. /**
  962. * @expectedException Exception
  963. * @expectedExceptionMessage Sharing is only allowed within your own groups
  964. */
  965. public function testGroupCreateChecksShareWithGroupMembersOnlyNotInGroup() {
  966. $share = $this->manager->newShare();
  967. $user = $this->createMock(IUser::class);
  968. $group = $this->createMock(IGroup::class);
  969. $share->setSharedBy('user')->setSharedWith('group');
  970. $group->method('inGroup')->with($user)->willReturn(false);
  971. $this->groupManager->method('get')->with('group')->willReturn($group);
  972. $this->userManager->method('get')->with('user')->willReturn($user);
  973. $this->config
  974. ->method('getAppValue')
  975. ->will($this->returnValueMap([
  976. ['core', 'shareapi_only_share_with_group_members', 'no', 'yes'],
  977. ['core', 'shareapi_allow_group_sharing', 'yes', 'yes'],
  978. ]));
  979. self::invokePrivate($this->manager, 'groupCreateChecks', [$share]);
  980. }
  981. /**
  982. * @expectedException Exception
  983. * @expectedExceptionMessage Sharing is only allowed within your own groups
  984. */
  985. public function testGroupCreateChecksShareWithGroupMembersOnlyNullGroup() {
  986. $share = $this->manager->newShare();
  987. $user = $this->createMock(IUser::class);
  988. $share->setSharedBy('user')->setSharedWith('group');
  989. $this->groupManager->method('get')->with('group')->willReturn(null);
  990. $this->userManager->method('get')->with('user')->willReturn($user);
  991. $this->config
  992. ->method('getAppValue')
  993. ->will($this->returnValueMap([
  994. ['core', 'shareapi_only_share_with_group_members', 'no', 'yes'],
  995. ['core', 'shareapi_allow_group_sharing', 'yes', 'yes'],
  996. ]));
  997. $this->assertNull($this->invokePrivate($this->manager, 'groupCreateChecks', [$share]));
  998. }
  999. public function testGroupCreateChecksShareWithGroupMembersOnlyInGroup() {
  1000. $share = $this->manager->newShare();
  1001. $user = $this->createMock(IUser::class);
  1002. $group = $this->createMock(IGroup::class);
  1003. $share->setSharedBy('user')->setSharedWith('group');
  1004. $this->userManager->method('get')->with('user')->willReturn($user);
  1005. $this->groupManager->method('get')->with('group')->willReturn($group);
  1006. $group->method('inGroup')->with($user)->willReturn(true);
  1007. $path = $this->createMock(Node::class);
  1008. $share->setNode($path);
  1009. $this->defaultProvider->method('getSharesByPath')
  1010. ->with($path)
  1011. ->willReturn([]);
  1012. $this->config
  1013. ->method('getAppValue')
  1014. ->will($this->returnValueMap([
  1015. ['core', 'shareapi_only_share_with_group_members', 'no', 'yes'],
  1016. ['core', 'shareapi_allow_group_sharing', 'yes', 'yes'],
  1017. ]));
  1018. self::invokePrivate($this->manager, 'groupCreateChecks', [$share]);
  1019. $this->addToAssertionCount(1);
  1020. }
  1021. /**
  1022. * @expectedException Exception
  1023. * @expectedExceptionMessage Path is already shared with this group
  1024. */
  1025. public function testGroupCreateChecksPathAlreadySharedWithSameGroup() {
  1026. $share = $this->manager->newShare();
  1027. $path = $this->createMock(Node::class);
  1028. $share->setSharedWith('sharedWith')
  1029. ->setNode($path)
  1030. ->setProviderId('foo')
  1031. ->setId('bar');
  1032. $share2 = $this->manager->newShare();
  1033. $share2->setSharedWith('sharedWith')
  1034. ->setProviderId('foo')
  1035. ->setId('baz');
  1036. $this->defaultProvider->method('getSharesByPath')
  1037. ->with($path)
  1038. ->willReturn([$share2]);
  1039. $this->config
  1040. ->method('getAppValue')
  1041. ->will($this->returnValueMap([
  1042. ['core', 'shareapi_allow_group_sharing', 'yes', 'yes'],
  1043. ]));
  1044. self::invokePrivate($this->manager, 'groupCreateChecks', [$share]);
  1045. }
  1046. public function testGroupCreateChecksPathAlreadySharedWithDifferentGroup() {
  1047. $share = $this->manager->newShare();
  1048. $share->setSharedWith('sharedWith');
  1049. $path = $this->createMock(Node::class);
  1050. $share->setNode($path);
  1051. $share2 = $this->manager->newShare();
  1052. $share2->setSharedWith('sharedWith2');
  1053. $this->defaultProvider->method('getSharesByPath')
  1054. ->with($path)
  1055. ->willReturn([$share2]);
  1056. $this->config
  1057. ->method('getAppValue')
  1058. ->will($this->returnValueMap([
  1059. ['core', 'shareapi_allow_group_sharing', 'yes', 'yes'],
  1060. ]));
  1061. self::invokePrivate($this->manager, 'groupCreateChecks', [$share]);
  1062. $this->addToAssertionCount(1);
  1063. }
  1064. /**
  1065. * @expectedException Exception
  1066. * @expectedExceptionMessage Link sharing is not allowed
  1067. */
  1068. public function testLinkCreateChecksNoLinkSharesAllowed() {
  1069. $share = $this->manager->newShare();
  1070. $this->config
  1071. ->method('getAppValue')
  1072. ->will($this->returnValueMap([
  1073. ['core', 'shareapi_allow_links', 'yes', 'no'],
  1074. ]));
  1075. self::invokePrivate($this->manager, 'linkCreateChecks', [$share]);
  1076. }
  1077. /**
  1078. * @expectedException Exception
  1079. * @expectedExceptionMessage Link shares can’t have reshare permissions
  1080. */
  1081. public function testLinkCreateChecksSharePermissions() {
  1082. $share = $this->manager->newShare();
  1083. $share->setPermissions(\OCP\Constants::PERMISSION_SHARE);
  1084. $this->config
  1085. ->method('getAppValue')
  1086. ->will($this->returnValueMap([
  1087. ['core', 'shareapi_allow_links', 'yes', 'yes'],
  1088. ]));
  1089. self::invokePrivate($this->manager, 'linkCreateChecks', [$share]);
  1090. }
  1091. /**
  1092. * @expectedException Exception
  1093. * @expectedExceptionMessage Public upload is not allowed
  1094. */
  1095. public function testLinkCreateChecksNoPublicUpload() {
  1096. $share = $this->manager->newShare();
  1097. $share->setPermissions(\OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE);
  1098. $this->config
  1099. ->method('getAppValue')
  1100. ->will($this->returnValueMap([
  1101. ['core', 'shareapi_allow_links', 'yes', 'yes'],
  1102. ['core', 'shareapi_allow_public_upload', 'yes', 'no']
  1103. ]));
  1104. self::invokePrivate($this->manager, 'linkCreateChecks', [$share]);
  1105. }
  1106. public function testLinkCreateChecksPublicUpload() {
  1107. $share = $this->manager->newShare();
  1108. $share->setPermissions(\OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE);
  1109. $this->config
  1110. ->method('getAppValue')
  1111. ->will($this->returnValueMap([
  1112. ['core', 'shareapi_allow_links', 'yes', 'yes'],
  1113. ['core', 'shareapi_allow_public_upload', 'yes', 'yes']
  1114. ]));
  1115. self::invokePrivate($this->manager, 'linkCreateChecks', [$share]);
  1116. $this->addToAssertionCount(1);
  1117. }
  1118. public function testLinkCreateChecksReadOnly() {
  1119. $share = $this->manager->newShare();
  1120. $share->setPermissions(\OCP\Constants::PERMISSION_READ);
  1121. $this->config
  1122. ->method('getAppValue')
  1123. ->will($this->returnValueMap([
  1124. ['core', 'shareapi_allow_links', 'yes', 'yes'],
  1125. ['core', 'shareapi_allow_public_upload', 'yes', 'no']
  1126. ]));
  1127. self::invokePrivate($this->manager, 'linkCreateChecks', [$share]);
  1128. $this->addToAssertionCount(1);
  1129. }
  1130. /**
  1131. * @expectedException \InvalidArgumentException
  1132. * @expectedExceptionMessage Path contains files shared with you
  1133. */
  1134. public function testPathCreateChecksContainsSharedMount() {
  1135. $path = $this->createMock(Folder::class);
  1136. $path->method('getPath')->willReturn('path');
  1137. $mount = $this->createMock(IMountPoint::class);
  1138. $storage = $this->createMock(Storage::class);
  1139. $mount->method('getStorage')->willReturn($storage);
  1140. $storage->method('instanceOfStorage')->with('\OCA\Files_Sharing\ISharedStorage')->willReturn(true);
  1141. $this->mountManager->method('findIn')->with('path')->willReturn([$mount]);
  1142. self::invokePrivate($this->manager, 'pathCreateChecks', [$path]);
  1143. }
  1144. public function testPathCreateChecksContainsNoSharedMount() {
  1145. $path = $this->createMock(Folder::class);
  1146. $path->method('getPath')->willReturn('path');
  1147. $mount = $this->createMock(IMountPoint::class);
  1148. $storage = $this->createMock(Storage::class);
  1149. $mount->method('getStorage')->willReturn($storage);
  1150. $storage->method('instanceOfStorage')->with('\OCA\Files_Sharing\ISharedStorage')->willReturn(false);
  1151. $this->mountManager->method('findIn')->with('path')->willReturn([$mount]);
  1152. self::invokePrivate($this->manager, 'pathCreateChecks', [$path]);
  1153. $this->addToAssertionCount(1);
  1154. }
  1155. public function testPathCreateChecksContainsNoFolder() {
  1156. $path = $this->createMock(File::class);
  1157. self::invokePrivate($this->manager, 'pathCreateChecks', [$path]);
  1158. $this->addToAssertionCount(1);
  1159. }
  1160. public function dataIsSharingDisabledForUser() {
  1161. $data = [];
  1162. // No exclude groups
  1163. $data[] = ['no', null, null, null, false];
  1164. // empty exclude list, user no groups
  1165. $data[] = ['yes', '', json_encode(['']), [], false];
  1166. // empty exclude list, user groups
  1167. $data[] = ['yes', '', json_encode(['']), ['group1', 'group2'], false];
  1168. // Convert old list to json
  1169. $data[] = ['yes', 'group1,group2', json_encode(['group1', 'group2']), [], false];
  1170. // Old list partly groups in common
  1171. $data[] = ['yes', 'group1,group2', json_encode(['group1', 'group2']), ['group1', 'group3'], false];
  1172. // Old list only groups in common
  1173. $data[] = ['yes', 'group1,group2', json_encode(['group1', 'group2']), ['group1'], true];
  1174. // New list partly in common
  1175. $data[] = ['yes', json_encode(['group1', 'group2']), null, ['group1', 'group3'], false];
  1176. // New list only groups in common
  1177. $data[] = ['yes', json_encode(['group1', 'group2']), null, ['group2'], true];
  1178. return $data;
  1179. }
  1180. /**
  1181. * @dataProvider dataIsSharingDisabledForUser
  1182. *
  1183. * @param string $excludeGroups
  1184. * @param string $groupList
  1185. * @param string $setList
  1186. * @param string[] $groupIds
  1187. * @param bool $expected
  1188. */
  1189. public function testIsSharingDisabledForUser($excludeGroups, $groupList, $setList, $groupIds, $expected) {
  1190. $user = $this->createMock(IUser::class);
  1191. $this->config->method('getAppValue')
  1192. ->will($this->returnValueMap([
  1193. ['core', 'shareapi_exclude_groups', 'no', $excludeGroups],
  1194. ['core', 'shareapi_exclude_groups_list', '', $groupList],
  1195. ]));
  1196. if ($setList !== null) {
  1197. $this->config->expects($this->once())
  1198. ->method('setAppValue')
  1199. ->with('core', 'shareapi_exclude_groups_list', $setList);
  1200. } else {
  1201. $this->config->expects($this->never())
  1202. ->method('setAppValue');
  1203. }
  1204. $this->groupManager->method('getUserGroupIds')
  1205. ->with($user)
  1206. ->willReturn($groupIds);
  1207. $this->userManager->method('get')->with('user')->willReturn($user);
  1208. $res = $this->manager->sharingDisabledForUser('user');
  1209. $this->assertEquals($expected, $res);
  1210. }
  1211. public function dataCanShare() {
  1212. $data = [];
  1213. /*
  1214. * [expected, sharing enabled, disabled for user]
  1215. */
  1216. $data[] = [false, 'no', false];
  1217. $data[] = [false, 'no', true];
  1218. $data[] = [true, 'yes', false];
  1219. $data[] = [false, 'yes', true];
  1220. return $data;
  1221. }
  1222. /**
  1223. * @dataProvider dataCanShare
  1224. *
  1225. * @param bool $expected
  1226. * @param string $sharingEnabled
  1227. * @param bool $disabledForUser
  1228. */
  1229. public function testCanShare($expected, $sharingEnabled, $disabledForUser) {
  1230. $this->config->method('getAppValue')
  1231. ->will($this->returnValueMap([
  1232. ['core', 'shareapi_enabled', 'yes', $sharingEnabled],
  1233. ]));
  1234. $manager = $this->createManagerMock()
  1235. ->setMethods(['sharingDisabledForUser'])
  1236. ->getMock();
  1237. $manager->method('sharingDisabledForUser')
  1238. ->with('user')
  1239. ->willReturn($disabledForUser);
  1240. $share = $this->manager->newShare();
  1241. $share->setSharedBy('user');
  1242. $exception = false;
  1243. try {
  1244. $res = self::invokePrivate($manager, 'canShare', [$share]);
  1245. } catch (\Exception $e) {
  1246. $exception = true;
  1247. }
  1248. $this->assertEquals($expected, !$exception);
  1249. }
  1250. public function testCreateShareUser() {
  1251. $manager = $this->createManagerMock()
  1252. ->setMethods(['canShare', 'generalCreateChecks', 'userCreateChecks', 'pathCreateChecks'])
  1253. ->getMock();
  1254. $shareOwner = $this->createMock(IUser::class);
  1255. $shareOwner->method('getUID')->willReturn('shareOwner');
  1256. $storage = $this->createMock(Storage::class);
  1257. $path = $this->createMock(File::class);
  1258. $path->method('getOwner')->willReturn($shareOwner);
  1259. $path->method('getName')->willReturn('target');
  1260. $path->method('getStorage')->willReturn($storage);
  1261. $share = $this->createShare(
  1262. null,
  1263. \OCP\Share::SHARE_TYPE_USER,
  1264. $path,
  1265. 'sharedWith',
  1266. 'sharedBy',
  1267. null,
  1268. \OCP\Constants::PERMISSION_ALL);
  1269. $manager->expects($this->once())
  1270. ->method('canShare')
  1271. ->with($share)
  1272. ->willReturn(true);
  1273. $manager->expects($this->once())
  1274. ->method('generalCreateChecks')
  1275. ->with($share);;
  1276. $manager->expects($this->once())
  1277. ->method('userCreateChecks')
  1278. ->with($share);;
  1279. $manager->expects($this->once())
  1280. ->method('pathCreateChecks')
  1281. ->with($path);
  1282. $this->defaultProvider
  1283. ->expects($this->once())
  1284. ->method('create')
  1285. ->with($share)
  1286. ->will($this->returnArgument(0));
  1287. $share->expects($this->once())
  1288. ->method('setShareOwner')
  1289. ->with('shareOwner');
  1290. $share->expects($this->once())
  1291. ->method('setTarget')
  1292. ->with('/target');
  1293. $manager->createShare($share);
  1294. }
  1295. public function testCreateShareGroup() {
  1296. $manager = $this->createManagerMock()
  1297. ->setMethods(['canShare', 'generalCreateChecks', 'groupCreateChecks', 'pathCreateChecks'])
  1298. ->getMock();
  1299. $shareOwner = $this->createMock(IUser::class);
  1300. $shareOwner->method('getUID')->willReturn('shareOwner');
  1301. $storage = $this->createMock(Storage::class);
  1302. $path = $this->createMock(File::class);
  1303. $path->method('getOwner')->willReturn($shareOwner);
  1304. $path->method('getName')->willReturn('target');
  1305. $path->method('getStorage')->willReturn($storage);
  1306. $share = $this->createShare(
  1307. null,
  1308. \OCP\Share::SHARE_TYPE_GROUP,
  1309. $path,
  1310. 'sharedWith',
  1311. 'sharedBy',
  1312. null,
  1313. \OCP\Constants::PERMISSION_ALL);
  1314. $manager->expects($this->once())
  1315. ->method('canShare')
  1316. ->with($share)
  1317. ->willReturn(true);
  1318. $manager->expects($this->once())
  1319. ->method('generalCreateChecks')
  1320. ->with($share);;
  1321. $manager->expects($this->once())
  1322. ->method('groupCreateChecks')
  1323. ->with($share);;
  1324. $manager->expects($this->once())
  1325. ->method('pathCreateChecks')
  1326. ->with($path);
  1327. $this->defaultProvider
  1328. ->expects($this->once())
  1329. ->method('create')
  1330. ->with($share)
  1331. ->will($this->returnArgument(0));
  1332. $share->expects($this->once())
  1333. ->method('setShareOwner')
  1334. ->with('shareOwner');
  1335. $share->expects($this->once())
  1336. ->method('setTarget')
  1337. ->with('/target');
  1338. $manager->createShare($share);
  1339. }
  1340. public function testCreateShareLink() {
  1341. $manager = $this->createManagerMock()
  1342. ->setMethods([
  1343. 'canShare',
  1344. 'generalCreateChecks',
  1345. 'linkCreateChecks',
  1346. 'pathCreateChecks',
  1347. 'validateExpirationDate',
  1348. 'verifyPassword',
  1349. 'setLinkParent',
  1350. ])
  1351. ->getMock();
  1352. $shareOwner = $this->createMock(IUser::class);
  1353. $shareOwner->method('getUID')->willReturn('shareOwner');
  1354. $storage = $this->createMock(Storage::class);
  1355. $path = $this->createMock(File::class);
  1356. $path->method('getOwner')->willReturn($shareOwner);
  1357. $path->method('getName')->willReturn('target');
  1358. $path->method('getId')->willReturn(1);
  1359. $path->method('getStorage')->willReturn($storage);
  1360. $date = new \DateTime();
  1361. $share = $this->manager->newShare();
  1362. $share->setShareType(\OCP\Share::SHARE_TYPE_LINK)
  1363. ->setNode($path)
  1364. ->setSharedBy('sharedBy')
  1365. ->setPermissions(\OCP\Constants::PERMISSION_ALL)
  1366. ->setExpirationDate($date)
  1367. ->setPassword('password');
  1368. $manager->expects($this->once())
  1369. ->method('canShare')
  1370. ->with($share)
  1371. ->willReturn(true);
  1372. $manager->expects($this->once())
  1373. ->method('generalCreateChecks')
  1374. ->with($share);;
  1375. $manager->expects($this->once())
  1376. ->method('linkCreateChecks')
  1377. ->with($share);;
  1378. $manager->expects($this->once())
  1379. ->method('pathCreateChecks')
  1380. ->with($path);
  1381. $manager->expects($this->once())
  1382. ->method('validateExpirationDate')
  1383. ->with($share);
  1384. $manager->expects($this->once())
  1385. ->method('verifyPassword')
  1386. ->with('password');
  1387. $manager->expects($this->once())
  1388. ->method('setLinkParent')
  1389. ->with($share);
  1390. $this->hasher->expects($this->once())
  1391. ->method('hash')
  1392. ->with('password')
  1393. ->willReturn('hashed');
  1394. $this->secureRandom->method('getMediumStrengthGenerator')
  1395. ->will($this->returnSelf());
  1396. $this->secureRandom->method('generate')
  1397. ->willReturn('token');
  1398. $this->defaultProvider
  1399. ->expects($this->once())
  1400. ->method('create')
  1401. ->with($share)
  1402. ->will($this->returnCallback(function(Share $share) {
  1403. return $share->setId(42);
  1404. }));
  1405. // Pre share
  1406. $this->eventDispatcher->expects($this->at(0))
  1407. ->method('dispatch')
  1408. ->with(
  1409. $this->equalTo('OCP\Share::preShare'),
  1410. $this->callback(function(GenericEvent $e) use ($path, $date) {
  1411. /** @var IShare $share */
  1412. $share = $e->getSubject();
  1413. return $share->getShareType() === \OCP\Share::SHARE_TYPE_LINK &&
  1414. $share->getNode() === $path &&
  1415. $share->getSharedBy() === 'sharedBy' &&
  1416. $share->getPermissions() === \OCP\Constants::PERMISSION_ALL &&
  1417. $share->getExpirationDate() === $date &&
  1418. $share->getPassword() === 'hashed' &&
  1419. $share->getToken() === 'token';
  1420. })
  1421. );
  1422. // Post share
  1423. $this->eventDispatcher->expects($this->at(1))
  1424. ->method('dispatch')
  1425. ->with(
  1426. $this->equalTo('OCP\Share::postShare'),
  1427. $this->callback(function(GenericEvent $e) use ($path, $date) {
  1428. /** @var IShare $share */
  1429. $share = $e->getSubject();
  1430. return $share->getShareType() === \OCP\Share::SHARE_TYPE_LINK &&
  1431. $share->getNode() === $path &&
  1432. $share->getSharedBy() === 'sharedBy' &&
  1433. $share->getPermissions() === \OCP\Constants::PERMISSION_ALL &&
  1434. $share->getExpirationDate() === $date &&
  1435. $share->getPassword() === 'hashed' &&
  1436. $share->getToken() === 'token' &&
  1437. $share->getId() === '42' &&
  1438. $share->getTarget() === '/target';
  1439. })
  1440. );
  1441. /** @var IShare $share */
  1442. $share = $manager->createShare($share);
  1443. $this->assertSame('shareOwner', $share->getShareOwner());
  1444. $this->assertEquals('/target', $share->getTarget());
  1445. $this->assertSame($date, $share->getExpirationDate());
  1446. $this->assertEquals('token', $share->getToken());
  1447. $this->assertEquals('hashed', $share->getPassword());
  1448. }
  1449. public function testCreateShareMail() {
  1450. $manager = $this->createManagerMock()
  1451. ->setMethods([
  1452. 'canShare',
  1453. 'generalCreateChecks',
  1454. 'linkCreateChecks',
  1455. 'pathCreateChecks',
  1456. 'validateExpirationDate',
  1457. 'verifyPassword',
  1458. 'setLinkParent',
  1459. ])
  1460. ->getMock();
  1461. $shareOwner = $this->createMock(IUser::class);
  1462. $shareOwner->method('getUID')->willReturn('shareOwner');
  1463. $storage = $this->createMock(Storage::class);
  1464. $path = $this->createMock(File::class);
  1465. $path->method('getOwner')->willReturn($shareOwner);
  1466. $path->method('getName')->willReturn('target');
  1467. $path->method('getId')->willReturn(1);
  1468. $path->method('getStorage')->willReturn($storage);
  1469. $share = $this->manager->newShare();
  1470. $share->setShareType(\OCP\Share::SHARE_TYPE_EMAIL)
  1471. ->setNode($path)
  1472. ->setSharedBy('sharedBy')
  1473. ->setPermissions(\OCP\Constants::PERMISSION_ALL);
  1474. $manager->expects($this->once())
  1475. ->method('canShare')
  1476. ->with($share)
  1477. ->willReturn(true);
  1478. $manager->expects($this->once())
  1479. ->method('generalCreateChecks')
  1480. ->with($share);;
  1481. $manager->expects($this->never())
  1482. ->method('linkCreateChecks');
  1483. $manager->expects($this->once())
  1484. ->method('pathCreateChecks')
  1485. ->with($path);
  1486. $manager->expects($this->never())
  1487. ->method('validateExpirationDate');
  1488. $manager->expects($this->never())
  1489. ->method('verifyPassword');
  1490. $manager->expects($this->never())
  1491. ->method('setLinkParent');
  1492. $this->secureRandom->method('getMediumStrengthGenerator')
  1493. ->will($this->returnSelf());
  1494. $this->secureRandom->method('generate')
  1495. ->willReturn('token');
  1496. $this->defaultProvider
  1497. ->expects($this->once())
  1498. ->method('create')
  1499. ->with($share)
  1500. ->will($this->returnCallback(function(Share $share) {
  1501. return $share->setId(42);
  1502. }));
  1503. // Pre share
  1504. $this->eventDispatcher->expects($this->at(0))
  1505. ->method('dispatch')
  1506. ->with(
  1507. $this->equalTo('OCP\Share::preShare'),
  1508. $this->callback(function(GenericEvent $e) use ($path) {
  1509. /** @var IShare $share */
  1510. $share = $e->getSubject();
  1511. return $share->getShareType() === \OCP\Share::SHARE_TYPE_EMAIL &&
  1512. $share->getNode() === $path &&
  1513. $share->getSharedBy() === 'sharedBy' &&
  1514. $share->getPermissions() === \OCP\Constants::PERMISSION_ALL &&
  1515. $share->getExpirationDate() === null &&
  1516. $share->getPassword() === null &&
  1517. $share->getToken() === 'token';
  1518. })
  1519. );
  1520. // Post share
  1521. $this->eventDispatcher->expects($this->at(1))
  1522. ->method('dispatch')
  1523. ->with(
  1524. $this->equalTo('OCP\Share::postShare'),
  1525. $this->callback(function(GenericEvent $e) use ($path) {
  1526. /** @var IShare $share */
  1527. $share = $e->getSubject();
  1528. return $share->getShareType() === \OCP\Share::SHARE_TYPE_EMAIL &&
  1529. $share->getNode() === $path &&
  1530. $share->getSharedBy() === 'sharedBy' &&
  1531. $share->getPermissions() === \OCP\Constants::PERMISSION_ALL &&
  1532. $share->getExpirationDate() === null &&
  1533. $share->getPassword() === null &&
  1534. $share->getToken() === 'token' &&
  1535. $share->getId() === '42' &&
  1536. $share->getTarget() === '/target';
  1537. })
  1538. );
  1539. /** @var IShare $share */
  1540. $share = $manager->createShare($share);
  1541. $this->assertSame('shareOwner', $share->getShareOwner());
  1542. $this->assertEquals('/target', $share->getTarget());
  1543. $this->assertEquals('token', $share->getToken());
  1544. }
  1545. /**
  1546. * @expectedException Exception
  1547. * @expectedExceptionMessage I won't let you share
  1548. */
  1549. public function testCreateShareHookError() {
  1550. $manager = $this->createManagerMock()
  1551. ->setMethods([
  1552. 'canShare',
  1553. 'generalCreateChecks',
  1554. 'userCreateChecks',
  1555. 'pathCreateChecks',
  1556. ])
  1557. ->getMock();
  1558. $shareOwner = $this->createMock(IUser::class);
  1559. $shareOwner->method('getUID')->willReturn('shareOwner');
  1560. $storage = $this->createMock(Storage::class);
  1561. $path = $this->createMock(File::class);
  1562. $path->method('getOwner')->willReturn($shareOwner);
  1563. $path->method('getName')->willReturn('target');
  1564. $path->method('getStorage')->willReturn($storage);
  1565. $share = $this->createShare(
  1566. null,
  1567. \OCP\Share::SHARE_TYPE_USER,
  1568. $path,
  1569. 'sharedWith',
  1570. 'sharedBy',
  1571. null,
  1572. \OCP\Constants::PERMISSION_ALL);
  1573. $manager->expects($this->once())
  1574. ->method('canShare')
  1575. ->with($share)
  1576. ->willReturn(true);
  1577. $manager->expects($this->once())
  1578. ->method('generalCreateChecks')
  1579. ->with($share);;
  1580. $manager->expects($this->once())
  1581. ->method('userCreateChecks')
  1582. ->with($share);;
  1583. $manager->expects($this->once())
  1584. ->method('pathCreateChecks')
  1585. ->with($path);
  1586. $share->expects($this->once())
  1587. ->method('setShareOwner')
  1588. ->with('shareOwner');
  1589. $share->expects($this->once())
  1590. ->method('setTarget')
  1591. ->with('/target');
  1592. // Pre share
  1593. $this->eventDispatcher->expects($this->once())
  1594. ->method('dispatch')
  1595. ->with(
  1596. $this->equalTo('OCP\Share::preShare'),
  1597. $this->isInstanceOf(GenericEvent::class)
  1598. )->will($this->returnCallback(function($name, GenericEvent $e) {
  1599. $e->setArgument('error', 'I won\'t let you share!');
  1600. $e->stopPropagation();
  1601. })
  1602. );
  1603. $manager->createShare($share);
  1604. }
  1605. public function testCreateShareOfIncommingFederatedShare() {
  1606. $manager = $this->createManagerMock()
  1607. ->setMethods(['canShare', 'generalCreateChecks', 'userCreateChecks', 'pathCreateChecks'])
  1608. ->getMock();
  1609. $shareOwner = $this->createMock(IUser::class);
  1610. $shareOwner->method('getUID')->willReturn('shareOwner');
  1611. $storage = $this->createMock(Storage::class);
  1612. $storage->method('instanceOfStorage')
  1613. ->with('OCA\Files_Sharing\External\Storage')
  1614. ->willReturn(true);
  1615. $storage2 = $this->createMock(Storage::class);
  1616. $storage2->method('instanceOfStorage')
  1617. ->with('OCA\Files_Sharing\External\Storage')
  1618. ->willReturn(false);
  1619. $path = $this->createMock(File::class);
  1620. $path->expects($this->never())->method('getOwner');
  1621. $path->method('getName')->willReturn('target');
  1622. $path->method('getStorage')->willReturn($storage);
  1623. $parent = $this->createMock(Folder::class);
  1624. $parent->method('getStorage')->willReturn($storage);
  1625. $parentParent = $this->createMock(Folder::class);
  1626. $parentParent->method('getStorage')->willReturn($storage2);
  1627. $parentParent->method('getOwner')->willReturn($shareOwner);
  1628. $path->method('getParent')->willReturn($parent);
  1629. $parent->method('getParent')->willReturn($parentParent);
  1630. $share = $this->createShare(
  1631. null,
  1632. \OCP\Share::SHARE_TYPE_USER,
  1633. $path,
  1634. 'sharedWith',
  1635. 'sharedBy',
  1636. null,
  1637. \OCP\Constants::PERMISSION_ALL);
  1638. $manager->expects($this->once())
  1639. ->method('canShare')
  1640. ->with($share)
  1641. ->willReturn(true);
  1642. $manager->expects($this->once())
  1643. ->method('generalCreateChecks')
  1644. ->with($share);;
  1645. $manager->expects($this->once())
  1646. ->method('userCreateChecks')
  1647. ->with($share);;
  1648. $manager->expects($this->once())
  1649. ->method('pathCreateChecks')
  1650. ->with($path);
  1651. $this->defaultProvider
  1652. ->expects($this->once())
  1653. ->method('create')
  1654. ->with($share)
  1655. ->will($this->returnArgument(0));
  1656. $share->expects($this->once())
  1657. ->method('setShareOwner')
  1658. ->with('shareOwner');
  1659. $share->expects($this->once())
  1660. ->method('setTarget')
  1661. ->with('/target');
  1662. $manager->createShare($share);
  1663. }
  1664. public function testGetSharesBy() {
  1665. $share = $this->manager->newShare();
  1666. $node = $this->createMock(Folder::class);
  1667. $this->defaultProvider->expects($this->once())
  1668. ->method('getSharesBy')
  1669. ->with(
  1670. $this->equalTo('user'),
  1671. $this->equalTo(\OCP\Share::SHARE_TYPE_USER),
  1672. $this->equalTo($node),
  1673. $this->equalTo(true),
  1674. $this->equalTo(1),
  1675. $this->equalTo(1)
  1676. )->willReturn([$share]);
  1677. $shares = $this->manager->getSharesBy('user', \OCP\Share::SHARE_TYPE_USER, $node, true, 1, 1);
  1678. $this->assertCount(1, $shares);
  1679. $this->assertSame($share, $shares[0]);
  1680. }
  1681. /**
  1682. * Test to ensure we correctly remove expired link shares
  1683. *
  1684. * We have 8 Shares and we want the 3 first valid shares.
  1685. * share 3-6 and 8 are expired. Thus at the end of this test we should
  1686. * have received share 1,2 and 7. And from the manager. Share 3-6 should be
  1687. * deleted (as they are evaluated). but share 8 should still be there.
  1688. */
  1689. public function testGetSharesByExpiredLinkShares() {
  1690. $manager = $this->createManagerMock()
  1691. ->setMethods(['deleteShare'])
  1692. ->getMock();
  1693. /** @var \OCP\Share\IShare[] $shares */
  1694. $shares = [];
  1695. /*
  1696. * This results in an array of 8 IShare elements
  1697. */
  1698. for ($i = 0; $i < 8; $i++) {
  1699. $share = $this->manager->newShare();
  1700. $share->setId($i);
  1701. $shares[] = $share;
  1702. }
  1703. $today = new \DateTime();
  1704. $today->setTime(0,0,0);
  1705. /*
  1706. * Set the expiration date to today for some shares
  1707. */
  1708. $shares[2]->setExpirationDate($today);
  1709. $shares[3]->setExpirationDate($today);
  1710. $shares[4]->setExpirationDate($today);
  1711. $shares[5]->setExpirationDate($today);
  1712. /** @var \OCP\Share\IShare[] $i */
  1713. $shares2 = [];
  1714. for ($i = 0; $i < 8; $i++) {
  1715. $shares2[] = clone $shares[$i];
  1716. }
  1717. $node = $this->createMock(File::class);
  1718. /*
  1719. * Simulate the getSharesBy call.
  1720. */
  1721. $this->defaultProvider
  1722. ->method('getSharesBy')
  1723. ->will($this->returnCallback(function($uid, $type, $node, $reshares, $limit, $offset) use (&$shares2) {
  1724. return array_slice($shares2, $offset, $limit);
  1725. }));
  1726. /*
  1727. * Simulate the deleteShare call.
  1728. */
  1729. $manager->method('deleteShare')
  1730. ->will($this->returnCallback(function($share) use (&$shares2) {
  1731. for($i = 0; $i < count($shares2); $i++) {
  1732. if ($shares2[$i]->getId() === $share->getId()) {
  1733. array_splice($shares2, $i, 1);
  1734. break;
  1735. }
  1736. }
  1737. }));
  1738. $res = $manager->getSharesBy('user', \OCP\Share::SHARE_TYPE_LINK, $node, true, 3, 0);
  1739. $this->assertCount(3, $res);
  1740. $this->assertEquals($shares[0]->getId(), $res[0]->getId());
  1741. $this->assertEquals($shares[1]->getId(), $res[1]->getId());
  1742. $this->assertEquals($shares[6]->getId(), $res[2]->getId());
  1743. $this->assertCount(4, $shares2);
  1744. $this->assertEquals(0, $shares2[0]->getId());
  1745. $this->assertEquals(1, $shares2[1]->getId());
  1746. $this->assertEquals(6, $shares2[2]->getId());
  1747. $this->assertEquals(7, $shares2[3]->getId());
  1748. $this->assertSame($today, $shares[3]->getExpirationDate());
  1749. }
  1750. public function testGetShareByToken() {
  1751. $this->config
  1752. ->expects($this->once())
  1753. ->method('getAppValue')
  1754. ->with('core', 'shareapi_allow_links', 'yes')
  1755. ->willReturn('yes');
  1756. $factory = $this->createMock(IProviderFactory::class);
  1757. $manager = new Manager(
  1758. $this->logger,
  1759. $this->config,
  1760. $this->secureRandom,
  1761. $this->hasher,
  1762. $this->mountManager,
  1763. $this->groupManager,
  1764. $this->l,
  1765. $this->l10nFactory,
  1766. $factory,
  1767. $this->userManager,
  1768. $this->rootFolder,
  1769. $this->eventDispatcher,
  1770. $this->mailer,
  1771. $this->urlGenerator,
  1772. $this->defaults
  1773. );
  1774. $share = $this->createMock(IShare::class);
  1775. $factory->expects($this->once())
  1776. ->method('getProviderForType')
  1777. ->with(\OCP\Share::SHARE_TYPE_LINK)
  1778. ->willReturn($this->defaultProvider);
  1779. $this->defaultProvider->expects($this->once())
  1780. ->method('getShareByToken')
  1781. ->with('token')
  1782. ->willReturn($share);
  1783. $ret = $manager->getShareByToken('token');
  1784. $this->assertSame($share, $ret);
  1785. }
  1786. public function testGetShareByTokenRoom() {
  1787. $this->config
  1788. ->expects($this->once())
  1789. ->method('getAppValue')
  1790. ->with('core', 'shareapi_allow_links', 'yes')
  1791. ->willReturn('no');
  1792. $factory = $this->createMock(IProviderFactory::class);
  1793. $manager = new Manager(
  1794. $this->logger,
  1795. $this->config,
  1796. $this->secureRandom,
  1797. $this->hasher,
  1798. $this->mountManager,
  1799. $this->groupManager,
  1800. $this->l,
  1801. $this->l10nFactory,
  1802. $factory,
  1803. $this->userManager,
  1804. $this->rootFolder,
  1805. $this->eventDispatcher,
  1806. $this->mailer,
  1807. $this->urlGenerator,
  1808. $this->defaults
  1809. );
  1810. $share = $this->createMock(IShare::class);
  1811. $roomShareProvider = $this->createMock(IShareProvider::class);
  1812. $factory->expects($this->any())
  1813. ->method('getProviderForType')
  1814. ->will($this->returnCallback(function($shareType) use ($roomShareProvider) {
  1815. if ($shareType !== \OCP\Share::SHARE_TYPE_ROOM) {
  1816. throw new Exception\ProviderException();
  1817. }
  1818. return $roomShareProvider;
  1819. }));
  1820. $roomShareProvider->expects($this->once())
  1821. ->method('getShareByToken')
  1822. ->with('token')
  1823. ->willReturn($share);
  1824. $ret = $manager->getShareByToken('token');
  1825. $this->assertSame($share, $ret);
  1826. }
  1827. public function testGetShareByTokenWithException() {
  1828. $this->config
  1829. ->expects($this->once())
  1830. ->method('getAppValue')
  1831. ->with('core', 'shareapi_allow_links', 'yes')
  1832. ->willReturn('yes');
  1833. $factory = $this->createMock(IProviderFactory::class);
  1834. $manager = new Manager(
  1835. $this->logger,
  1836. $this->config,
  1837. $this->secureRandom,
  1838. $this->hasher,
  1839. $this->mountManager,
  1840. $this->groupManager,
  1841. $this->l,
  1842. $this->l10nFactory,
  1843. $factory,
  1844. $this->userManager,
  1845. $this->rootFolder,
  1846. $this->eventDispatcher,
  1847. $this->mailer,
  1848. $this->urlGenerator,
  1849. $this->defaults
  1850. );
  1851. $share = $this->createMock(IShare::class);
  1852. $factory->expects($this->at(0))
  1853. ->method('getProviderForType')
  1854. ->with(\OCP\Share::SHARE_TYPE_LINK)
  1855. ->willReturn($this->defaultProvider);
  1856. $factory->expects($this->at(1))
  1857. ->method('getProviderForType')
  1858. ->with(\OCP\Share::SHARE_TYPE_REMOTE)
  1859. ->willReturn($this->defaultProvider);
  1860. $this->defaultProvider->expects($this->at(0))
  1861. ->method('getShareByToken')
  1862. ->with('token')
  1863. ->will($this->throwException(new ShareNotFound()));
  1864. $this->defaultProvider->expects($this->at(1))
  1865. ->method('getShareByToken')
  1866. ->with('token')
  1867. ->willReturn($share);
  1868. $ret = $manager->getShareByToken('token');
  1869. $this->assertSame($share, $ret);
  1870. }
  1871. /**
  1872. * @expectedException \OCP\Share\Exceptions\ShareNotFound
  1873. * @expectedExceptionMessage The requested share does not exist anymore
  1874. */
  1875. public function testGetShareByTokenExpired() {
  1876. $this->config
  1877. ->expects($this->once())
  1878. ->method('getAppValue')
  1879. ->with('core', 'shareapi_allow_links', 'yes')
  1880. ->willReturn('yes');
  1881. $this->l->expects($this->once())
  1882. ->method('t')
  1883. ->willReturnArgument(0);
  1884. $manager = $this->createManagerMock()
  1885. ->setMethods(['deleteShare'])
  1886. ->getMock();
  1887. $date = new \DateTime();
  1888. $date->setTime(0,0,0);
  1889. $share = $this->manager->newShare();
  1890. $share->setExpirationDate($date);
  1891. $this->defaultProvider->expects($this->once())
  1892. ->method('getShareByToken')
  1893. ->with('expiredToken')
  1894. ->willReturn($share);
  1895. $manager->expects($this->once())
  1896. ->method('deleteShare')
  1897. ->with($this->equalTo($share));
  1898. $manager->getShareByToken('expiredToken');
  1899. }
  1900. public function testGetShareByTokenNotExpired() {
  1901. $this->config
  1902. ->expects($this->once())
  1903. ->method('getAppValue')
  1904. ->with('core', 'shareapi_allow_links', 'yes')
  1905. ->willReturn('yes');
  1906. $date = new \DateTime();
  1907. $date->setTime(0,0,0);
  1908. $date->add(new \DateInterval('P2D'));
  1909. $share = $this->manager->newShare();
  1910. $share->setExpirationDate($date);
  1911. $this->defaultProvider->expects($this->once())
  1912. ->method('getShareByToken')
  1913. ->with('expiredToken')
  1914. ->willReturn($share);
  1915. $res = $this->manager->getShareByToken('expiredToken');
  1916. $this->assertSame($share, $res);
  1917. }
  1918. /**
  1919. * @expectedException \OCP\Share\Exceptions\ShareNotFound
  1920. */
  1921. public function testGetShareByTokenWithPublicLinksDisabled() {
  1922. $this->config
  1923. ->expects($this->once())
  1924. ->method('getAppValue')
  1925. ->with('core', 'shareapi_allow_links', 'yes')
  1926. ->willReturn('no');
  1927. $this->manager->getShareByToken('validToken');
  1928. }
  1929. public function testGetShareByTokenPublicUploadDisabled() {
  1930. $this->config
  1931. ->expects($this->at(0))
  1932. ->method('getAppValue')
  1933. ->with('core', 'shareapi_allow_links', 'yes')
  1934. ->willReturn('yes');
  1935. $share = $this->manager->newShare();
  1936. $share->setShareType(\OCP\Share::SHARE_TYPE_LINK)
  1937. ->setPermissions(\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE);
  1938. $this->config
  1939. ->expects($this->at(1))
  1940. ->method('getAppValue')
  1941. ->will($this->returnValueMap([
  1942. ['core', 'shareapi_allow_public_upload', 'yes', 'no'],
  1943. ]));
  1944. $this->defaultProvider->expects($this->once())
  1945. ->method('getShareByToken')
  1946. ->willReturn('validToken')
  1947. ->willReturn($share);
  1948. $res = $this->manager->getShareByToken('validToken');
  1949. $this->assertSame(\OCP\Constants::PERMISSION_READ, $res->getPermissions());
  1950. }
  1951. public function testCheckPasswordNoLinkShare() {
  1952. $share = $this->createMock(IShare::class);
  1953. $share->method('getShareType')->willReturn(\OCP\Share::SHARE_TYPE_USER);
  1954. $this->assertFalse($this->manager->checkPassword($share, 'password'));
  1955. }
  1956. public function testCheckPasswordNoPassword() {
  1957. $share = $this->createMock(IShare::class);
  1958. $share->method('getShareType')->willReturn(\OCP\Share::SHARE_TYPE_LINK);
  1959. $this->assertFalse($this->manager->checkPassword($share, 'password'));
  1960. $share->method('getPassword')->willReturn('password');
  1961. $this->assertFalse($this->manager->checkPassword($share, null));
  1962. }
  1963. public function testCheckPasswordInvalidPassword() {
  1964. $share = $this->createMock(IShare::class);
  1965. $share->method('getShareType')->willReturn(\OCP\Share::SHARE_TYPE_LINK);
  1966. $share->method('getPassword')->willReturn('password');
  1967. $this->hasher->method('verify')->with('invalidpassword', 'password', '')->willReturn(false);
  1968. $this->assertFalse($this->manager->checkPassword($share, 'invalidpassword'));
  1969. }
  1970. public function testCheckPasswordValidPassword() {
  1971. $share = $this->createMock(IShare::class);
  1972. $share->method('getShareType')->willReturn(\OCP\Share::SHARE_TYPE_LINK);
  1973. $share->method('getPassword')->willReturn('passwordHash');
  1974. $this->hasher->method('verify')->with('password', 'passwordHash', '')->willReturn(true);
  1975. $this->assertTrue($this->manager->checkPassword($share, 'password'));
  1976. }
  1977. public function testCheckPasswordUpdateShare() {
  1978. $share = $this->manager->newShare();
  1979. $share->setShareType(\OCP\Share::SHARE_TYPE_LINK)
  1980. ->setPassword('passwordHash');
  1981. $this->hasher->method('verify')->with('password', 'passwordHash', '')
  1982. ->will($this->returnCallback(function($pass, $hash, &$newHash) {
  1983. $newHash = 'newHash';
  1984. return true;
  1985. }));
  1986. $this->defaultProvider->expects($this->once())
  1987. ->method('update')
  1988. ->with($this->callback(function (\OCP\Share\IShare $share) {
  1989. return $share->getPassword() === 'newHash';
  1990. }));
  1991. $this->assertTrue($this->manager->checkPassword($share, 'password'));
  1992. }
  1993. /**
  1994. * @expectedException Exception
  1995. * @expectedExceptionMessage Can’t change share type
  1996. */
  1997. public function testUpdateShareCantChangeShareType() {
  1998. $manager = $this->createManagerMock()
  1999. ->setMethods([
  2000. 'canShare',
  2001. 'getShareById'
  2002. ])
  2003. ->getMock();
  2004. $originalShare = $this->manager->newShare();
  2005. $originalShare->setShareType(\OCP\Share::SHARE_TYPE_GROUP);
  2006. $manager->expects($this->once())->method('canShare')->willReturn(true);
  2007. $manager->expects($this->once())->method('getShareById')->with('foo:42')->willReturn($originalShare);
  2008. $share = $this->manager->newShare();
  2009. $share->setProviderId('foo')
  2010. ->setId('42')
  2011. ->setShareType(\OCP\Share::SHARE_TYPE_USER);
  2012. $manager->updateShare($share);
  2013. }
  2014. /**
  2015. * @expectedException Exception
  2016. * @expectedExceptionMessage Can only update recipient on user shares
  2017. */
  2018. public function testUpdateShareCantChangeRecipientForGroupShare() {
  2019. $manager = $this->createManagerMock()
  2020. ->setMethods([
  2021. 'canShare',
  2022. 'getShareById'
  2023. ])
  2024. ->getMock();
  2025. $originalShare = $this->manager->newShare();
  2026. $originalShare->setShareType(\OCP\Share::SHARE_TYPE_GROUP)
  2027. ->setSharedWith('origGroup');
  2028. $manager->expects($this->once())->method('canShare')->willReturn(true);
  2029. $manager->expects($this->once())->method('getShareById')->with('foo:42')->willReturn($originalShare);
  2030. $share = $this->manager->newShare();
  2031. $share->setProviderId('foo')
  2032. ->setId('42')
  2033. ->setShareType(\OCP\Share::SHARE_TYPE_GROUP)
  2034. ->setSharedWith('newGroup');
  2035. $manager->updateShare($share);
  2036. }
  2037. /**
  2038. * @expectedException Exception
  2039. * @expectedExceptionMessage Can’t share with the share owner
  2040. */
  2041. public function testUpdateShareCantShareWithOwner() {
  2042. $manager = $this->createManagerMock()
  2043. ->setMethods([
  2044. 'canShare',
  2045. 'getShareById'
  2046. ])
  2047. ->getMock();
  2048. $originalShare = $this->manager->newShare();
  2049. $originalShare->setShareType(\OCP\Share::SHARE_TYPE_USER)
  2050. ->setSharedWith('sharedWith');
  2051. $manager->expects($this->once())->method('canShare')->willReturn(true);
  2052. $manager->expects($this->once())->method('getShareById')->with('foo:42')->willReturn($originalShare);
  2053. $share = $this->manager->newShare();
  2054. $share->setProviderId('foo')
  2055. ->setId('42')
  2056. ->setShareType(\OCP\Share::SHARE_TYPE_USER)
  2057. ->setSharedWith('newUser')
  2058. ->setShareOwner('newUser');
  2059. $manager->updateShare($share);
  2060. }
  2061. public function testUpdateShareUser() {
  2062. $this->userManager->expects($this->any())->method('userExists')->willReturn(true);
  2063. $manager = $this->createManagerMock()
  2064. ->setMethods([
  2065. 'canShare',
  2066. 'getShareById',
  2067. 'generalCreateChecks',
  2068. 'userCreateChecks',
  2069. 'pathCreateChecks',
  2070. ])
  2071. ->getMock();
  2072. $originalShare = $this->manager->newShare();
  2073. $originalShare->setShareType(\OCP\Share::SHARE_TYPE_USER)
  2074. ->setSharedWith('origUser')
  2075. ->setPermissions(1);
  2076. $node = $this->createMock(File::class);
  2077. $node->method('getId')->willReturn(100);
  2078. $node->method('getPath')->willReturn('/newUser/files/myPath');
  2079. $manager->expects($this->once())->method('canShare')->willReturn(true);
  2080. $manager->expects($this->once())->method('getShareById')->with('foo:42')->willReturn($originalShare);
  2081. $share = $this->manager->newShare();
  2082. $share->setProviderId('foo')
  2083. ->setId('42')
  2084. ->setShareType(\OCP\Share::SHARE_TYPE_USER)
  2085. ->setSharedWith('origUser')
  2086. ->setShareOwner('newUser')
  2087. ->setSharedBy('sharer')
  2088. ->setPermissions(31)
  2089. ->setNode($node);
  2090. $this->defaultProvider->expects($this->once())
  2091. ->method('update')
  2092. ->with($share)
  2093. ->willReturn($share);
  2094. $hookListner = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  2095. \OCP\Util::connectHook('OCP\Share', 'post_set_expiration_date', $hookListner, 'post');
  2096. $hookListner->expects($this->never())->method('post');
  2097. $this->rootFolder->method('getUserFolder')->with('newUser')->will($this->returnSelf());
  2098. $this->rootFolder->method('getRelativePath')->with('/newUser/files/myPath')->willReturn('/myPath');
  2099. $hookListner2 = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  2100. \OCP\Util::connectHook('OCP\Share', 'post_update_permissions', $hookListner2, 'post');
  2101. $hookListner2->expects($this->once())->method('post')->with([
  2102. 'itemType' => 'file',
  2103. 'itemSource' => 100,
  2104. 'shareType' => \OCP\Share::SHARE_TYPE_USER,
  2105. 'shareWith' => 'origUser',
  2106. 'uidOwner' => 'sharer',
  2107. 'permissions' => 31,
  2108. 'path' => '/myPath',
  2109. ]);
  2110. $manager->updateShare($share);
  2111. }
  2112. public function testUpdateShareGroup() {
  2113. $manager = $this->createManagerMock()
  2114. ->setMethods([
  2115. 'canShare',
  2116. 'getShareById',
  2117. 'generalCreateChecks',
  2118. 'groupCreateChecks',
  2119. 'pathCreateChecks',
  2120. ])
  2121. ->getMock();
  2122. $originalShare = $this->manager->newShare();
  2123. $originalShare->setShareType(\OCP\Share::SHARE_TYPE_GROUP)
  2124. ->setSharedWith('origUser')
  2125. ->setPermissions(31);
  2126. $manager->expects($this->once())->method('canShare')->willReturn(true);
  2127. $manager->expects($this->once())->method('getShareById')->with('foo:42')->willReturn($originalShare);
  2128. $node = $this->createMock(File::class);
  2129. $share = $this->manager->newShare();
  2130. $share->setProviderId('foo')
  2131. ->setId('42')
  2132. ->setShareType(\OCP\Share::SHARE_TYPE_GROUP)
  2133. ->setSharedWith('origUser')
  2134. ->setShareOwner('owner')
  2135. ->setNode($node)
  2136. ->setPermissions(31);
  2137. $this->defaultProvider->expects($this->once())
  2138. ->method('update')
  2139. ->with($share)
  2140. ->willReturn($share);
  2141. $hookListner = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  2142. \OCP\Util::connectHook('OCP\Share', 'post_set_expiration_date', $hookListner, 'post');
  2143. $hookListner->expects($this->never())->method('post');
  2144. $hookListner2 = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  2145. \OCP\Util::connectHook('OCP\Share', 'post_update_permissions', $hookListner2, 'post');
  2146. $hookListner2->expects($this->never())->method('post');
  2147. $manager->updateShare($share);
  2148. }
  2149. public function testUpdateShareLink() {
  2150. $manager = $this->createManagerMock()
  2151. ->setMethods([
  2152. 'canShare',
  2153. 'getShareById',
  2154. 'generalCreateChecks',
  2155. 'linkCreateChecks',
  2156. 'pathCreateChecks',
  2157. 'verifyPassword',
  2158. 'validateExpirationDate',
  2159. ])
  2160. ->getMock();
  2161. $originalShare = $this->manager->newShare();
  2162. $originalShare->setShareType(\OCP\Share::SHARE_TYPE_LINK)
  2163. ->setPermissions(15);
  2164. $tomorrow = new \DateTime();
  2165. $tomorrow->setTime(0,0,0);
  2166. $tomorrow->add(new \DateInterval('P1D'));
  2167. $file = $this->createMock(File::class);
  2168. $file->method('getId')->willReturn(100);
  2169. $share = $this->manager->newShare();
  2170. $share->setProviderId('foo')
  2171. ->setId('42')
  2172. ->setShareType(\OCP\Share::SHARE_TYPE_LINK)
  2173. ->setToken('token')
  2174. ->setSharedBy('owner')
  2175. ->setShareOwner('owner')
  2176. ->setPassword('password')
  2177. ->setExpirationDate($tomorrow)
  2178. ->setNode($file)
  2179. ->setPermissions(15);
  2180. $manager->expects($this->once())->method('canShare')->willReturn(true);
  2181. $manager->expects($this->once())->method('getShareById')->with('foo:42')->willReturn($originalShare);
  2182. $manager->expects($this->once())->method('validateExpirationDate')->with($share);
  2183. $manager->expects($this->once())->method('verifyPassword')->with('password');
  2184. $this->hasher->expects($this->once())
  2185. ->method('hash')
  2186. ->with('password')
  2187. ->willReturn('hashed');
  2188. $this->defaultProvider->expects($this->once())
  2189. ->method('update')
  2190. ->with($share)
  2191. ->willReturn($share);
  2192. $hookListner = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  2193. \OCP\Util::connectHook('OCP\Share', 'post_set_expiration_date', $hookListner, 'post');
  2194. $hookListner->expects($this->once())->method('post')->with([
  2195. 'itemType' => 'file',
  2196. 'itemSource' => 100,
  2197. 'date' => $tomorrow,
  2198. 'uidOwner' => 'owner',
  2199. ]);
  2200. $hookListner2 = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  2201. \OCP\Util::connectHook('OCP\Share', 'post_update_password', $hookListner2, 'post');
  2202. $hookListner2->expects($this->once())->method('post')->with([
  2203. 'itemType' => 'file',
  2204. 'itemSource' => 100,
  2205. 'uidOwner' => 'owner',
  2206. 'token' => 'token',
  2207. 'disabled' => false,
  2208. ]);
  2209. $hookListner3 = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  2210. \OCP\Util::connectHook('OCP\Share', 'post_update_permissions', $hookListner3, 'post');
  2211. $hookListner3->expects($this->never())->method('post');
  2212. $manager->updateShare($share);
  2213. }
  2214. public function testUpdateShareMail() {
  2215. $manager = $this->createManagerMock()
  2216. ->setMethods([
  2217. 'canShare',
  2218. 'getShareById',
  2219. 'generalCreateChecks',
  2220. 'verifyPassword',
  2221. 'pathCreateChecks',
  2222. 'linkCreateChecks',
  2223. 'validateExpirationDate',
  2224. ])
  2225. ->getMock();
  2226. $originalShare = $this->manager->newShare();
  2227. $originalShare->setShareType(\OCP\Share::SHARE_TYPE_EMAIL)
  2228. ->setPermissions(\OCP\Constants::PERMISSION_ALL);
  2229. $tomorrow = new \DateTime();
  2230. $tomorrow->setTime(0,0,0);
  2231. $tomorrow->add(new \DateInterval('P1D'));
  2232. $file = $this->createMock(File::class);
  2233. $file->method('getId')->willReturn(100);
  2234. $share = $this->manager->newShare();
  2235. $share->setProviderId('foo')
  2236. ->setId('42')
  2237. ->setShareType(\OCP\Share::SHARE_TYPE_EMAIL)
  2238. ->setToken('token')
  2239. ->setSharedBy('owner')
  2240. ->setShareOwner('owner')
  2241. ->setPassword('password')
  2242. ->setExpirationDate($tomorrow)
  2243. ->setNode($file)
  2244. ->setPermissions(\OCP\Constants::PERMISSION_ALL);
  2245. $manager->expects($this->once())->method('canShare')->willReturn(true);
  2246. $manager->expects($this->once())->method('getShareById')->with('foo:42')->willReturn($originalShare);
  2247. $manager->expects($this->once())->method('generalCreateChecks')->with($share);
  2248. $manager->expects($this->once())->method('verifyPassword')->with('password');
  2249. $manager->expects($this->once())->method('pathCreateChecks')->with($file);
  2250. $manager->expects($this->never())->method('linkCreateChecks');
  2251. $manager->expects($this->never())->method('validateExpirationDate');
  2252. $this->hasher->expects($this->once())
  2253. ->method('hash')
  2254. ->with('password')
  2255. ->willReturn('hashed');
  2256. $this->defaultProvider->expects($this->once())
  2257. ->method('update')
  2258. ->with($share, 'password')
  2259. ->willReturn($share);
  2260. $hookListner = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  2261. \OCP\Util::connectHook('OCP\Share', 'post_set_expiration_date', $hookListner, 'post');
  2262. $hookListner->expects($this->never())->method('post');
  2263. $hookListner2 = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  2264. \OCP\Util::connectHook('OCP\Share', 'post_update_password', $hookListner2, 'post');
  2265. $hookListner2->expects($this->once())->method('post')->with([
  2266. 'itemType' => 'file',
  2267. 'itemSource' => 100,
  2268. 'uidOwner' => 'owner',
  2269. 'token' => 'token',
  2270. 'disabled' => false,
  2271. ]);
  2272. $hookListner3 = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  2273. \OCP\Util::connectHook('OCP\Share', 'post_update_permissions', $hookListner3, 'post');
  2274. $hookListner3->expects($this->never())->method('post');
  2275. $manager->updateShare($share);
  2276. }
  2277. public function testUpdateShareMailEnableSendPasswordByTalk() {
  2278. $manager = $this->createManagerMock()
  2279. ->setMethods([
  2280. 'canShare',
  2281. 'getShareById',
  2282. 'generalCreateChecks',
  2283. 'verifyPassword',
  2284. 'pathCreateChecks',
  2285. 'linkCreateChecks',
  2286. 'validateExpirationDate',
  2287. ])
  2288. ->getMock();
  2289. $originalShare = $this->manager->newShare();
  2290. $originalShare->setShareType(\OCP\Share::SHARE_TYPE_EMAIL)
  2291. ->setPermissions(\OCP\Constants::PERMISSION_ALL)
  2292. ->setPassword(null)
  2293. ->setSendPasswordByTalk(false);
  2294. $tomorrow = new \DateTime();
  2295. $tomorrow->setTime(0,0,0);
  2296. $tomorrow->add(new \DateInterval('P1D'));
  2297. $file = $this->createMock(File::class);
  2298. $file->method('getId')->willReturn(100);
  2299. $share = $this->manager->newShare();
  2300. $share->setProviderId('foo')
  2301. ->setId('42')
  2302. ->setShareType(\OCP\Share::SHARE_TYPE_EMAIL)
  2303. ->setToken('token')
  2304. ->setSharedBy('owner')
  2305. ->setShareOwner('owner')
  2306. ->setPassword('password')
  2307. ->setSendPasswordByTalk(true)
  2308. ->setExpirationDate($tomorrow)
  2309. ->setNode($file)
  2310. ->setPermissions(\OCP\Constants::PERMISSION_ALL);
  2311. $manager->expects($this->once())->method('canShare')->willReturn(true);
  2312. $manager->expects($this->once())->method('getShareById')->with('foo:42')->willReturn($originalShare);
  2313. $manager->expects($this->once())->method('generalCreateChecks')->with($share);
  2314. $manager->expects($this->once())->method('verifyPassword')->with('password');
  2315. $manager->expects($this->once())->method('pathCreateChecks')->with($file);
  2316. $manager->expects($this->never())->method('linkCreateChecks');
  2317. $manager->expects($this->never())->method('validateExpirationDate');
  2318. $this->hasher->expects($this->once())
  2319. ->method('hash')
  2320. ->with('password')
  2321. ->willReturn('hashed');
  2322. $this->defaultProvider->expects($this->once())
  2323. ->method('update')
  2324. ->with($share, 'password')
  2325. ->willReturn($share);
  2326. $hookListner = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  2327. \OCP\Util::connectHook('OCP\Share', 'post_set_expiration_date', $hookListner, 'post');
  2328. $hookListner->expects($this->never())->method('post');
  2329. $hookListner2 = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  2330. \OCP\Util::connectHook('OCP\Share', 'post_update_password', $hookListner2, 'post');
  2331. $hookListner2->expects($this->once())->method('post')->with([
  2332. 'itemType' => 'file',
  2333. 'itemSource' => 100,
  2334. 'uidOwner' => 'owner',
  2335. 'token' => 'token',
  2336. 'disabled' => false,
  2337. ]);
  2338. $hookListner3 = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  2339. \OCP\Util::connectHook('OCP\Share', 'post_update_permissions', $hookListner3, 'post');
  2340. $hookListner3->expects($this->never())->method('post');
  2341. $manager->updateShare($share);
  2342. }
  2343. /**
  2344. * @expectedException \InvalidArgumentException
  2345. * @expectedExceptionMessage Can’t enable sending the password by Talk without setting a new password
  2346. */
  2347. public function testUpdateShareMailEnableSendPasswordByTalkWithNoPassword() {
  2348. $manager = $this->createManagerMock()
  2349. ->setMethods([
  2350. 'canShare',
  2351. 'getShareById',
  2352. 'generalCreateChecks',
  2353. 'verifyPassword',
  2354. 'pathCreateChecks',
  2355. 'linkCreateChecks',
  2356. 'validateExpirationDate',
  2357. ])
  2358. ->getMock();
  2359. $originalShare = $this->manager->newShare();
  2360. $originalShare->setShareType(\OCP\Share::SHARE_TYPE_EMAIL)
  2361. ->setPermissions(\OCP\Constants::PERMISSION_ALL)
  2362. ->setPassword(null)
  2363. ->setSendPasswordByTalk(false);
  2364. $tomorrow = new \DateTime();
  2365. $tomorrow->setTime(0,0,0);
  2366. $tomorrow->add(new \DateInterval('P1D'));
  2367. $file = $this->createMock(File::class);
  2368. $file->method('getId')->willReturn(100);
  2369. $share = $this->manager->newShare();
  2370. $share->setProviderId('foo')
  2371. ->setId('42')
  2372. ->setShareType(\OCP\Share::SHARE_TYPE_EMAIL)
  2373. ->setToken('token')
  2374. ->setSharedBy('owner')
  2375. ->setShareOwner('owner')
  2376. ->setPassword(null)
  2377. ->setSendPasswordByTalk(true)
  2378. ->setExpirationDate($tomorrow)
  2379. ->setNode($file)
  2380. ->setPermissions(\OCP\Constants::PERMISSION_ALL);
  2381. $manager->expects($this->once())->method('canShare')->willReturn(true);
  2382. $manager->expects($this->once())->method('getShareById')->with('foo:42')->willReturn($originalShare);
  2383. $manager->expects($this->once())->method('generalCreateChecks')->with($share);
  2384. $manager->expects($this->never())->method('verifyPassword');
  2385. $manager->expects($this->never())->method('pathCreateChecks');
  2386. $manager->expects($this->never())->method('linkCreateChecks');
  2387. $manager->expects($this->never())->method('validateExpirationDate');
  2388. $this->hasher->expects($this->never())
  2389. ->method('hash');
  2390. $this->defaultProvider->expects($this->never())
  2391. ->method('update');
  2392. $hookListner = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  2393. \OCP\Util::connectHook('OCP\Share', 'post_set_expiration_date', $hookListner, 'post');
  2394. $hookListner->expects($this->never())->method('post');
  2395. $hookListner2 = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  2396. \OCP\Util::connectHook('OCP\Share', 'post_update_password', $hookListner2, 'post');
  2397. $hookListner2->expects($this->never())->method('post');
  2398. $hookListner3 = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  2399. \OCP\Util::connectHook('OCP\Share', 'post_update_permissions', $hookListner3, 'post');
  2400. $hookListner3->expects($this->never())->method('post');
  2401. $manager->updateShare($share);
  2402. }
  2403. /**
  2404. * @expectedException \InvalidArgumentException
  2405. * @expectedExceptionMessage Can’t enable sending the password by Talk without setting a new password
  2406. */
  2407. public function testUpdateShareMailEnableSendPasswordByTalkRemovingPassword() {
  2408. $manager = $this->createManagerMock()
  2409. ->setMethods([
  2410. 'canShare',
  2411. 'getShareById',
  2412. 'generalCreateChecks',
  2413. 'verifyPassword',
  2414. 'pathCreateChecks',
  2415. 'linkCreateChecks',
  2416. 'validateExpirationDate',
  2417. ])
  2418. ->getMock();
  2419. $originalShare = $this->manager->newShare();
  2420. $originalShare->setShareType(\OCP\Share::SHARE_TYPE_EMAIL)
  2421. ->setPermissions(\OCP\Constants::PERMISSION_ALL)
  2422. ->setPassword('password')
  2423. ->setSendPasswordByTalk(false);
  2424. $tomorrow = new \DateTime();
  2425. $tomorrow->setTime(0,0,0);
  2426. $tomorrow->add(new \DateInterval('P1D'));
  2427. $file = $this->createMock(File::class);
  2428. $file->method('getId')->willReturn(100);
  2429. $share = $this->manager->newShare();
  2430. $share->setProviderId('foo')
  2431. ->setId('42')
  2432. ->setShareType(\OCP\Share::SHARE_TYPE_EMAIL)
  2433. ->setToken('token')
  2434. ->setSharedBy('owner')
  2435. ->setShareOwner('owner')
  2436. ->setPassword(null)
  2437. ->setSendPasswordByTalk(true)
  2438. ->setExpirationDate($tomorrow)
  2439. ->setNode($file)
  2440. ->setPermissions(\OCP\Constants::PERMISSION_ALL);
  2441. $manager->expects($this->once())->method('canShare')->willReturn(true);
  2442. $manager->expects($this->once())->method('getShareById')->with('foo:42')->willReturn($originalShare);
  2443. $manager->expects($this->once())->method('generalCreateChecks')->with($share);
  2444. $manager->expects($this->never())->method('verifyPassword');
  2445. $manager->expects($this->never())->method('pathCreateChecks');
  2446. $manager->expects($this->never())->method('linkCreateChecks');
  2447. $manager->expects($this->never())->method('validateExpirationDate');
  2448. $this->hasher->expects($this->never())
  2449. ->method('hash');
  2450. $this->defaultProvider->expects($this->never())
  2451. ->method('update');
  2452. $hookListner = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  2453. \OCP\Util::connectHook('OCP\Share', 'post_set_expiration_date', $hookListner, 'post');
  2454. $hookListner->expects($this->never())->method('post');
  2455. $hookListner2 = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  2456. \OCP\Util::connectHook('OCP\Share', 'post_update_password', $hookListner2, 'post');
  2457. $hookListner2->expects($this->never())->method('post');
  2458. $hookListner3 = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  2459. \OCP\Util::connectHook('OCP\Share', 'post_update_permissions', $hookListner3, 'post');
  2460. $hookListner3->expects($this->never())->method('post');
  2461. $manager->updateShare($share);
  2462. }
  2463. /**
  2464. * @expectedException \InvalidArgumentException
  2465. * @expectedExceptionMessage Can’t enable sending the password by Talk without setting a new password
  2466. */
  2467. public function testUpdateShareMailEnableSendPasswordByTalkRemovingPasswordWithEmptyString() {
  2468. $manager = $this->createManagerMock()
  2469. ->setMethods([
  2470. 'canShare',
  2471. 'getShareById',
  2472. 'generalCreateChecks',
  2473. 'verifyPassword',
  2474. 'pathCreateChecks',
  2475. 'linkCreateChecks',
  2476. 'validateExpirationDate',
  2477. ])
  2478. ->getMock();
  2479. $originalShare = $this->manager->newShare();
  2480. $originalShare->setShareType(\OCP\Share::SHARE_TYPE_EMAIL)
  2481. ->setPermissions(\OCP\Constants::PERMISSION_ALL)
  2482. ->setPassword('password')
  2483. ->setSendPasswordByTalk(false);
  2484. $tomorrow = new \DateTime();
  2485. $tomorrow->setTime(0,0,0);
  2486. $tomorrow->add(new \DateInterval('P1D'));
  2487. $file = $this->createMock(File::class);
  2488. $file->method('getId')->willReturn(100);
  2489. $share = $this->manager->newShare();
  2490. $share->setProviderId('foo')
  2491. ->setId('42')
  2492. ->setShareType(\OCP\Share::SHARE_TYPE_EMAIL)
  2493. ->setToken('token')
  2494. ->setSharedBy('owner')
  2495. ->setShareOwner('owner')
  2496. ->setPassword('')
  2497. ->setSendPasswordByTalk(true)
  2498. ->setExpirationDate($tomorrow)
  2499. ->setNode($file)
  2500. ->setPermissions(\OCP\Constants::PERMISSION_ALL);
  2501. $manager->expects($this->once())->method('canShare')->willReturn(true);
  2502. $manager->expects($this->once())->method('getShareById')->with('foo:42')->willReturn($originalShare);
  2503. $manager->expects($this->once())->method('generalCreateChecks')->with($share);
  2504. $manager->expects($this->never())->method('verifyPassword');
  2505. $manager->expects($this->never())->method('pathCreateChecks');
  2506. $manager->expects($this->never())->method('linkCreateChecks');
  2507. $manager->expects($this->never())->method('validateExpirationDate');
  2508. $this->hasher->expects($this->never())
  2509. ->method('hash');
  2510. $this->defaultProvider->expects($this->never())
  2511. ->method('update');
  2512. $hookListner = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  2513. \OCP\Util::connectHook('OCP\Share', 'post_set_expiration_date', $hookListner, 'post');
  2514. $hookListner->expects($this->never())->method('post');
  2515. $hookListner2 = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  2516. \OCP\Util::connectHook('OCP\Share', 'post_update_password', $hookListner2, 'post');
  2517. $hookListner2->expects($this->never())->method('post');
  2518. $hookListner3 = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  2519. \OCP\Util::connectHook('OCP\Share', 'post_update_permissions', $hookListner3, 'post');
  2520. $hookListner3->expects($this->never())->method('post');
  2521. $manager->updateShare($share);
  2522. }
  2523. /**
  2524. * @expectedException \InvalidArgumentException
  2525. * @expectedExceptionMessage Can’t enable sending the password by Talk without setting a new password
  2526. */
  2527. public function testUpdateShareMailEnableSendPasswordByTalkWithPreviousPassword() {
  2528. $manager = $this->createManagerMock()
  2529. ->setMethods([
  2530. 'canShare',
  2531. 'getShareById',
  2532. 'generalCreateChecks',
  2533. 'verifyPassword',
  2534. 'pathCreateChecks',
  2535. 'linkCreateChecks',
  2536. 'validateExpirationDate',
  2537. ])
  2538. ->getMock();
  2539. $originalShare = $this->manager->newShare();
  2540. $originalShare->setShareType(\OCP\Share::SHARE_TYPE_EMAIL)
  2541. ->setPermissions(\OCP\Constants::PERMISSION_ALL)
  2542. ->setPassword('password')
  2543. ->setSendPasswordByTalk(false);
  2544. $tomorrow = new \DateTime();
  2545. $tomorrow->setTime(0,0,0);
  2546. $tomorrow->add(new \DateInterval('P1D'));
  2547. $file = $this->createMock(File::class);
  2548. $file->method('getId')->willReturn(100);
  2549. $share = $this->manager->newShare();
  2550. $share->setProviderId('foo')
  2551. ->setId('42')
  2552. ->setShareType(\OCP\Share::SHARE_TYPE_EMAIL)
  2553. ->setToken('token')
  2554. ->setSharedBy('owner')
  2555. ->setShareOwner('owner')
  2556. ->setPassword('password')
  2557. ->setSendPasswordByTalk(true)
  2558. ->setExpirationDate($tomorrow)
  2559. ->setNode($file)
  2560. ->setPermissions(\OCP\Constants::PERMISSION_ALL);
  2561. $manager->expects($this->once())->method('canShare')->willReturn(true);
  2562. $manager->expects($this->once())->method('getShareById')->with('foo:42')->willReturn($originalShare);
  2563. $manager->expects($this->once())->method('generalCreateChecks')->with($share);
  2564. $manager->expects($this->never())->method('verifyPassword');
  2565. $manager->expects($this->never())->method('pathCreateChecks');
  2566. $manager->expects($this->never())->method('linkCreateChecks');
  2567. $manager->expects($this->never())->method('validateExpirationDate');
  2568. $this->hasher->expects($this->never())
  2569. ->method('hash');
  2570. $this->defaultProvider->expects($this->never())
  2571. ->method('update');
  2572. $hookListner = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  2573. \OCP\Util::connectHook('OCP\Share', 'post_set_expiration_date', $hookListner, 'post');
  2574. $hookListner->expects($this->never())->method('post');
  2575. $hookListner2 = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  2576. \OCP\Util::connectHook('OCP\Share', 'post_update_password', $hookListner2, 'post');
  2577. $hookListner2->expects($this->never())->method('post');
  2578. $hookListner3 = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  2579. \OCP\Util::connectHook('OCP\Share', 'post_update_permissions', $hookListner3, 'post');
  2580. $hookListner3->expects($this->never())->method('post');
  2581. $manager->updateShare($share);
  2582. }
  2583. public function testUpdateShareMailDisableSendPasswordByTalkWithPreviousPassword() {
  2584. $manager = $this->createManagerMock()
  2585. ->setMethods([
  2586. 'canShare',
  2587. 'getShareById',
  2588. 'generalCreateChecks',
  2589. 'verifyPassword',
  2590. 'pathCreateChecks',
  2591. 'linkCreateChecks',
  2592. 'validateExpirationDate',
  2593. ])
  2594. ->getMock();
  2595. $originalShare = $this->manager->newShare();
  2596. $originalShare->setShareType(\OCP\Share::SHARE_TYPE_EMAIL)
  2597. ->setPermissions(\OCP\Constants::PERMISSION_ALL)
  2598. ->setPassword('password')
  2599. ->setSendPasswordByTalk(true);
  2600. $tomorrow = new \DateTime();
  2601. $tomorrow->setTime(0,0,0);
  2602. $tomorrow->add(new \DateInterval('P1D'));
  2603. $file = $this->createMock(File::class);
  2604. $file->method('getId')->willReturn(100);
  2605. $share = $this->manager->newShare();
  2606. $share->setProviderId('foo')
  2607. ->setId('42')
  2608. ->setShareType(\OCP\Share::SHARE_TYPE_EMAIL)
  2609. ->setToken('token')
  2610. ->setSharedBy('owner')
  2611. ->setShareOwner('owner')
  2612. ->setPassword('password')
  2613. ->setSendPasswordByTalk(false)
  2614. ->setExpirationDate($tomorrow)
  2615. ->setNode($file)
  2616. ->setPermissions(\OCP\Constants::PERMISSION_ALL);
  2617. $manager->expects($this->once())->method('canShare')->willReturn(true);
  2618. $manager->expects($this->once())->method('getShareById')->with('foo:42')->willReturn($originalShare);
  2619. $manager->expects($this->once())->method('generalCreateChecks')->with($share);
  2620. $manager->expects($this->once())->method('pathCreateChecks')->with($file);
  2621. $manager->expects($this->never())->method('verifyPassword');
  2622. $manager->expects($this->never())->method('linkCreateChecks');
  2623. $manager->expects($this->never())->method('validateExpirationDate');
  2624. $this->hasher->expects($this->never())
  2625. ->method('hash');
  2626. $this->defaultProvider->expects($this->once())
  2627. ->method('update')
  2628. ->with($share, 'password')
  2629. ->willReturn($share);
  2630. $hookListner = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  2631. \OCP\Util::connectHook('OCP\Share', 'post_set_expiration_date', $hookListner, 'post');
  2632. $hookListner->expects($this->never())->method('post');
  2633. $hookListner2 = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  2634. \OCP\Util::connectHook('OCP\Share', 'post_update_password', $hookListner2, 'post');
  2635. $hookListner2->expects($this->never())->method('post');
  2636. $hookListner3 = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  2637. \OCP\Util::connectHook('OCP\Share', 'post_update_permissions', $hookListner3, 'post');
  2638. $hookListner3->expects($this->never())->method('post');
  2639. $manager->updateShare($share);
  2640. }
  2641. /**
  2642. * @expectedException \InvalidArgumentException
  2643. * @expectedExceptionMessage Can’t change target of link share
  2644. */
  2645. public function testMoveShareLink() {
  2646. $share = $this->manager->newShare();
  2647. $share->setShareType(\OCP\Share::SHARE_TYPE_LINK);
  2648. $recipient = $this->createMock(IUser::class);
  2649. $this->manager->moveShare($share, $recipient);
  2650. }
  2651. /**
  2652. * @expectedException \InvalidArgumentException
  2653. * @expectedExceptionMessage Invalid recipient
  2654. */
  2655. public function testMoveShareUserNotRecipient() {
  2656. $share = $this->manager->newShare();
  2657. $share->setShareType(\OCP\Share::SHARE_TYPE_USER);
  2658. $share->setSharedWith('sharedWith');
  2659. $this->manager->moveShare($share, 'recipient');
  2660. }
  2661. public function testMoveShareUser() {
  2662. $share = $this->manager->newShare();
  2663. $share->setShareType(\OCP\Share::SHARE_TYPE_USER)
  2664. ->setId('42')
  2665. ->setProviderId('foo');
  2666. $share->setSharedWith('recipient');
  2667. $this->defaultProvider->method('move')->with($share, 'recipient')->will($this->returnArgument(0));
  2668. $this->manager->moveShare($share, 'recipient');
  2669. $this->addToAssertionCount(1);
  2670. }
  2671. /**
  2672. * @expectedException \InvalidArgumentException
  2673. * @expectedExceptionMessage Invalid recipient
  2674. */
  2675. public function testMoveShareGroupNotRecipient() {
  2676. $share = $this->manager->newShare();
  2677. $share->setShareType(\OCP\Share::SHARE_TYPE_GROUP);
  2678. $sharedWith = $this->createMock(IGroup::class);
  2679. $share->setSharedWith('shareWith');
  2680. $recipient = $this->createMock(IUser::class);
  2681. $sharedWith->method('inGroup')->with($recipient)->willReturn(false);
  2682. $this->groupManager->method('get')->with('shareWith')->willReturn($sharedWith);
  2683. $this->userManager->method('get')->with('recipient')->willReturn($recipient);
  2684. $this->manager->moveShare($share, 'recipient');
  2685. }
  2686. /**
  2687. * @expectedException \InvalidArgumentException
  2688. * @expectedExceptionMessage Group "shareWith" does not exist
  2689. */
  2690. public function testMoveShareGroupNull() {
  2691. $share = $this->manager->newShare();
  2692. $share->setShareType(\OCP\Share::SHARE_TYPE_GROUP);
  2693. $share->setSharedWith('shareWith');
  2694. $recipient = $this->createMock(IUser::class);
  2695. $this->groupManager->method('get')->with('shareWith')->willReturn(null);
  2696. $this->userManager->method('get')->with('recipient')->willReturn($recipient);
  2697. $this->manager->moveShare($share, 'recipient');
  2698. }
  2699. public function testMoveShareGroup() {
  2700. $share = $this->manager->newShare();
  2701. $share->setShareType(\OCP\Share::SHARE_TYPE_GROUP)
  2702. ->setId('42')
  2703. ->setProviderId('foo');
  2704. $group = $this->createMock(IGroup::class);
  2705. $share->setSharedWith('group');
  2706. $recipient = $this->createMock(IUser::class);
  2707. $group->method('inGroup')->with($recipient)->willReturn(true);
  2708. $this->groupManager->method('get')->with('group')->willReturn($group);
  2709. $this->userManager->method('get')->with('recipient')->willReturn($recipient);
  2710. $this->defaultProvider->method('move')->with($share, 'recipient')->will($this->returnArgument(0));
  2711. $this->manager->moveShare($share, 'recipient');
  2712. $this->addToAssertionCount(1);
  2713. }
  2714. /**
  2715. * @dataProvider dataTestShareProviderExists
  2716. */
  2717. public function testShareProviderExists($shareType, $expected) {
  2718. $factory = $this->getMockBuilder('OCP\Share\IProviderFactory')->getMock();
  2719. $factory->expects($this->any())->method('getProviderForType')
  2720. ->willReturnCallback(function ($id) {
  2721. if ($id === \OCP\Share::SHARE_TYPE_USER) {
  2722. return true;
  2723. }
  2724. throw new Exception\ProviderException();
  2725. });
  2726. $manager = new Manager(
  2727. $this->logger,
  2728. $this->config,
  2729. $this->secureRandom,
  2730. $this->hasher,
  2731. $this->mountManager,
  2732. $this->groupManager,
  2733. $this->l,
  2734. $this->l10nFactory,
  2735. $factory,
  2736. $this->userManager,
  2737. $this->rootFolder,
  2738. $this->eventDispatcher,
  2739. $this->mailer,
  2740. $this->urlGenerator,
  2741. $this->defaults
  2742. );
  2743. $this->assertSame($expected,
  2744. $manager->shareProviderExists($shareType)
  2745. );
  2746. }
  2747. public function dataTestShareProviderExists() {
  2748. return [
  2749. [\OCP\Share::SHARE_TYPE_USER, true],
  2750. [42, false],
  2751. ];
  2752. }
  2753. public function testGetSharesInFolder() {
  2754. $factory = new DummyFactory2($this->createMock(IServerContainer::class));
  2755. $manager = new Manager(
  2756. $this->logger,
  2757. $this->config,
  2758. $this->secureRandom,
  2759. $this->hasher,
  2760. $this->mountManager,
  2761. $this->groupManager,
  2762. $this->l,
  2763. $this->l10nFactory,
  2764. $factory,
  2765. $this->userManager,
  2766. $this->rootFolder,
  2767. $this->eventDispatcher,
  2768. $this->mailer,
  2769. $this->urlGenerator,
  2770. $this->defaults
  2771. );
  2772. $factory->setProvider($this->defaultProvider);
  2773. $extraProvider = $this->createMock(IShareProvider::class);
  2774. $factory->setSecondProvider($extraProvider);
  2775. $share1 = $this->createMock(IShare::class);
  2776. $share2 = $this->createMock(IShare::class);
  2777. $share3 = $this->createMock(IShare::class);
  2778. $share4 = $this->createMock(IShare::class);
  2779. $folder = $this->createMock(Folder::class);
  2780. $this->defaultProvider->method('getSharesInFolder')
  2781. ->with(
  2782. $this->equalTo('user'),
  2783. $this->equalTo($folder),
  2784. $this->equalTo(false)
  2785. )->willReturn([
  2786. 1 => [$share1],
  2787. 2 => [$share2],
  2788. ]);
  2789. $extraProvider->method('getSharesInFolder')
  2790. ->with(
  2791. $this->equalTo('user'),
  2792. $this->equalTo($folder),
  2793. $this->equalTo(false)
  2794. )->willReturn([
  2795. 2 => [$share3],
  2796. 3 => [$share4],
  2797. ]);
  2798. $result = $manager->getSharesInFolder('user', $folder, false);
  2799. $expects = [
  2800. 1 => [$share1],
  2801. 2 => [$share2, $share3],
  2802. 3 => [$share4],
  2803. ];
  2804. $this->assertSame($expects, $result);
  2805. }
  2806. public function testGetAccessList() {
  2807. $factory = new DummyFactory2($this->createMock(IServerContainer::class));
  2808. $manager = new Manager(
  2809. $this->logger,
  2810. $this->config,
  2811. $this->secureRandom,
  2812. $this->hasher,
  2813. $this->mountManager,
  2814. $this->groupManager,
  2815. $this->l,
  2816. $this->l10nFactory,
  2817. $factory,
  2818. $this->userManager,
  2819. $this->rootFolder,
  2820. $this->eventDispatcher,
  2821. $this->mailer,
  2822. $this->urlGenerator,
  2823. $this->defaults
  2824. );
  2825. $factory->setProvider($this->defaultProvider);
  2826. $extraProvider = $this->createMock(IShareProvider::class);
  2827. $factory->setSecondProvider($extraProvider);
  2828. $nodeOwner = $this->createMock(IUser::class);
  2829. $nodeOwner->expects($this->once())
  2830. ->method('getUID')
  2831. ->willReturn('user1');
  2832. $node = $this->createMock(Node::class);
  2833. $node->expects($this->once())
  2834. ->method('getOwner')
  2835. ->willReturn($nodeOwner);
  2836. $node->method('getId')
  2837. ->willReturn(42);
  2838. $userFolder = $this->createMock(Folder::class);
  2839. $file = $this->createMock(File::class);
  2840. $folder = $this->createMock(Folder::class);
  2841. $owner = $this->createMock(IUser::class);
  2842. $owner->expects($this->once())
  2843. ->method('getUID')
  2844. ->willReturn('owner');
  2845. $file->method('getParent')
  2846. ->willReturn($folder);
  2847. $file->method('getPath')
  2848. ->willReturn('/owner/files/folder/file');
  2849. $file->method('getOwner')
  2850. ->willReturn($owner);
  2851. $file->method('getId')
  2852. ->willReturn(23);
  2853. $folder->method('getParent')
  2854. ->willReturn($userFolder);
  2855. $folder->method('getPath')
  2856. ->willReturn('/owner/files/folder');
  2857. $userFolder->method('getById')
  2858. ->with($this->equalTo(42))
  2859. ->willReturn([12 => $file]);
  2860. $userFolder->method('getPath')
  2861. ->willReturn('/user1/files');
  2862. $this->userManager->method('userExists')
  2863. ->with($this->equalTo('user1'))
  2864. ->willReturn(true);
  2865. $this->defaultProvider->method('getAccessList')
  2866. ->with(
  2867. $this->equalTo([$file, $folder]),
  2868. false
  2869. )
  2870. ->willReturn([
  2871. 'users' => [
  2872. 'user1',
  2873. 'user2',
  2874. 'user3',
  2875. '123456',
  2876. ],
  2877. 'public' => true,
  2878. ]);
  2879. $extraProvider->method('getAccessList')
  2880. ->with(
  2881. $this->equalTo([$file, $folder]),
  2882. false
  2883. )
  2884. ->willReturn([
  2885. 'users' => [
  2886. 'user3',
  2887. 'user4',
  2888. 'user5',
  2889. '234567',
  2890. ],
  2891. 'remote' => true,
  2892. ]);
  2893. $this->rootFolder->method('getUserFolder')
  2894. ->with($this->equalTo('user1'))
  2895. ->willReturn($userFolder);
  2896. $expected = [
  2897. 'users' => ['owner', 'user1', 'user2', 'user3', '123456','user4', 'user5', '234567'],
  2898. 'remote' => true,
  2899. 'public' => true,
  2900. ];
  2901. $result = $manager->getAccessList($node, true, false);
  2902. $this->assertSame($expected['public'], $result['public']);
  2903. $this->assertSame($expected['remote'], $result['remote']);
  2904. $this->assertSame($expected['users'], $result['users']);
  2905. }
  2906. public function testGetAccessListWithCurrentAccess() {
  2907. $factory = new DummyFactory2($this->createMock(IServerContainer::class));
  2908. $manager = new Manager(
  2909. $this->logger,
  2910. $this->config,
  2911. $this->secureRandom,
  2912. $this->hasher,
  2913. $this->mountManager,
  2914. $this->groupManager,
  2915. $this->l,
  2916. $this->l10nFactory,
  2917. $factory,
  2918. $this->userManager,
  2919. $this->rootFolder,
  2920. $this->eventDispatcher,
  2921. $this->mailer,
  2922. $this->urlGenerator,
  2923. $this->defaults
  2924. );
  2925. $factory->setProvider($this->defaultProvider);
  2926. $extraProvider = $this->createMock(IShareProvider::class);
  2927. $factory->setSecondProvider($extraProvider);
  2928. $nodeOwner = $this->createMock(IUser::class);
  2929. $nodeOwner->expects($this->once())
  2930. ->method('getUID')
  2931. ->willReturn('user1');
  2932. $node = $this->createMock(Node::class);
  2933. $node->expects($this->once())
  2934. ->method('getOwner')
  2935. ->willReturn($nodeOwner);
  2936. $node->method('getId')
  2937. ->willReturn(42);
  2938. $userFolder = $this->createMock(Folder::class);
  2939. $file = $this->createMock(File::class);
  2940. $owner = $this->createMock(IUser::class);
  2941. $owner->expects($this->once())
  2942. ->method('getUID')
  2943. ->willReturn('owner');
  2944. $folder = $this->createMock(Folder::class);
  2945. $file->method('getParent')
  2946. ->willReturn($folder);
  2947. $file->method('getPath')
  2948. ->willReturn('/owner/files/folder/file');
  2949. $file->method('getOwner')
  2950. ->willReturn($owner);
  2951. $file->method('getId')
  2952. ->willReturn(23);
  2953. $folder->method('getParent')
  2954. ->willReturn($userFolder);
  2955. $folder->method('getPath')
  2956. ->willReturn('/owner/files/folder');
  2957. $userFolder->method('getById')
  2958. ->with($this->equalTo(42))
  2959. ->willReturn([42 => $file]);
  2960. $userFolder->method('getPath')
  2961. ->willReturn('/user1/files');
  2962. $this->userManager->method('userExists')
  2963. ->with($this->equalTo('user1'))
  2964. ->willReturn(true);
  2965. $this->defaultProvider->method('getAccessList')
  2966. ->with(
  2967. $this->equalTo([$file, $folder]),
  2968. true
  2969. )
  2970. ->willReturn([
  2971. 'users' => [
  2972. 'user1' => [],
  2973. 'user2' => [],
  2974. 'user3' => [],
  2975. '123456' => [],
  2976. ],
  2977. 'public' => true,
  2978. ]);
  2979. $extraProvider->method('getAccessList')
  2980. ->with(
  2981. $this->equalTo([$file, $folder]),
  2982. true
  2983. )
  2984. ->willReturn([
  2985. 'users' => [
  2986. 'user3' => [],
  2987. 'user4' => [],
  2988. 'user5' => [],
  2989. '234567' => [],
  2990. ],
  2991. 'remote' => [
  2992. 'remote1',
  2993. ],
  2994. ]);
  2995. $this->rootFolder->method('getUserFolder')
  2996. ->with($this->equalTo('user1'))
  2997. ->willReturn($userFolder);
  2998. $expected = [
  2999. 'users' => [
  3000. 'owner' => [
  3001. 'node_id' => 23,
  3002. 'node_path' => '/folder/file'
  3003. ]
  3004. , 'user1' => [], 'user2' => [], 'user3' => [], '123456' => [], 'user4' => [], 'user5' => [], '234567' => []],
  3005. 'remote' => [
  3006. 'remote1',
  3007. ],
  3008. 'public' => true,
  3009. ];
  3010. $result = $manager->getAccessList($node, true, true);
  3011. $this->assertSame($expected['public'], $result['public']);
  3012. $this->assertSame($expected['remote'], $result['remote']);
  3013. $this->assertSame($expected['users'], $result['users']);
  3014. }
  3015. }
  3016. class DummyFactory implements IProviderFactory {
  3017. /** @var IShareProvider */
  3018. protected $provider;
  3019. public function __construct(\OCP\IServerContainer $serverContainer) {
  3020. }
  3021. /**
  3022. * @param IShareProvider $provider
  3023. */
  3024. public function setProvider($provider) {
  3025. $this->provider = $provider;
  3026. }
  3027. /**
  3028. * @param string $id
  3029. * @return IShareProvider
  3030. */
  3031. public function getProvider($id) {
  3032. return $this->provider;
  3033. }
  3034. /**
  3035. * @param int $shareType
  3036. * @return IShareProvider
  3037. */
  3038. public function getProviderForType($shareType) {
  3039. return $this->provider;
  3040. }
  3041. /**
  3042. * @return IShareProvider[]
  3043. */
  3044. public function getAllProviders() {
  3045. return [$this->provider];
  3046. }
  3047. }
  3048. class DummyFactory2 extends DummyFactory {
  3049. /** @var IShareProvider */
  3050. private $provider2;
  3051. /**
  3052. * @param IShareProvider $provider
  3053. */
  3054. public function setSecondProvider($provider) {
  3055. $this->provider2 = $provider;
  3056. }
  3057. public function getAllProviders() {
  3058. return [$this->provider, $this->provider2];
  3059. }
  3060. }