You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ManagerTest.php 113KB

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