ManagerTest.php 112 KB

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