ManagerTest.php 148 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731
  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\KnownUser\KnownUserService;
  24. use OC\Share20\DefaultShareProvider;
  25. use OC\Share20\Exception;
  26. use OC\Share20\Manager;
  27. use OC\Share20\Share;
  28. use OCP\EventDispatcher\Event;
  29. use OCP\EventDispatcher\IEventDispatcher;
  30. use OCP\Files\File;
  31. use OCP\Files\Folder;
  32. use OCP\Files\IRootFolder;
  33. use OCP\Files\Mount\IMountManager;
  34. use OCP\Files\Mount\IMountPoint;
  35. use OCP\Files\Node;
  36. use OCP\Files\Storage;
  37. use OCP\HintException;
  38. use OCP\IConfig;
  39. use OCP\IGroup;
  40. use OCP\IGroupManager;
  41. use OCP\IL10N;
  42. use OCP\IServerContainer;
  43. use OCP\IURLGenerator;
  44. use OCP\IUser;
  45. use OCP\IUserManager;
  46. use OCP\IUserSession;
  47. use OCP\L10N\IFactory;
  48. use OCP\Mail\IMailer;
  49. use OCP\Security\Events\ValidatePasswordPolicyEvent;
  50. use OCP\Security\IHasher;
  51. use OCP\Security\ISecureRandom;
  52. use OCP\Share\Exceptions\AlreadySharedException;
  53. use OCP\Share\Exceptions\ShareNotFound;
  54. use OCP\Share\IManager;
  55. use OCP\Share\IProviderFactory;
  56. use OCP\Share\IShare;
  57. use OCP\Share\IShareProvider;
  58. use PHPUnit\Framework\MockObject\MockBuilder;
  59. use PHPUnit\Framework\MockObject\MockObject;
  60. use Psr\Log\LoggerInterface;
  61. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  62. use Symfony\Component\EventDispatcher\GenericEvent;
  63. /**
  64. * Class ManagerTest
  65. *
  66. * @package Test\Share20
  67. * @group DB
  68. */
  69. class ManagerTest extends \Test\TestCase {
  70. /** @var Manager */
  71. protected $manager;
  72. /** @var LoggerInterface|MockObject */
  73. protected $logger;
  74. /** @var IConfig|MockObject */
  75. protected $config;
  76. /** @var ISecureRandom|MockObject */
  77. protected $secureRandom;
  78. /** @var IHasher|MockObject */
  79. protected $hasher;
  80. /** @var IShareProvider|MockObject */
  81. protected $defaultProvider;
  82. /** @var IMountManager|MockObject */
  83. protected $mountManager;
  84. /** @var IGroupManager|MockObject */
  85. protected $groupManager;
  86. /** @var IL10N|MockObject */
  87. protected $l;
  88. /** @var IFactory|MockObject */
  89. protected $l10nFactory;
  90. /** @var DummyFactory */
  91. protected $factory;
  92. /** @var IUserManager|MockObject */
  93. protected $userManager;
  94. /** @var IRootFolder | MockObject */
  95. protected $rootFolder;
  96. /** @var EventDispatcherInterface | MockObject */
  97. protected $eventDispatcher;
  98. /** @var IEventDispatcher|MockObject */
  99. protected $dispatcher;
  100. /** @var IMailer|MockObject */
  101. protected $mailer;
  102. /** @var IURLGenerator|MockObject */
  103. protected $urlGenerator;
  104. /** @var \OC_Defaults|MockObject */
  105. protected $defaults;
  106. /** @var IUserSession|MockObject */
  107. protected $userSession;
  108. /** @var KnownUserService|MockObject */
  109. protected $knownUserService;
  110. protected function setUp(): void {
  111. $this->logger = $this->createMock(LoggerInterface::class);
  112. $this->config = $this->createMock(IConfig::class);
  113. $this->secureRandom = $this->createMock(ISecureRandom::class);
  114. $this->hasher = $this->createMock(IHasher::class);
  115. $this->mountManager = $this->createMock(IMountManager::class);
  116. $this->groupManager = $this->createMock(IGroupManager::class);
  117. $this->userManager = $this->createMock(IUserManager::class);
  118. $this->rootFolder = $this->createMock(IRootFolder::class);
  119. $this->eventDispatcher = $this->createMock(EventDispatcherInterface::class);
  120. $this->mailer = $this->createMock(IMailer::class);
  121. $this->urlGenerator = $this->createMock(IURLGenerator::class);
  122. $this->defaults = $this->createMock(\OC_Defaults::class);
  123. $this->dispatcher = $this->createMock(IEventDispatcher::class);
  124. $this->userSession = $this->createMock(IUserSession::class);
  125. $this->knownUserService = $this->createMock(KnownUserService::class);
  126. $this->l10nFactory = $this->createMock(IFactory::class);
  127. $this->l = $this->createMock(IL10N::class);
  128. $this->l->method('t')
  129. ->willReturnCallback(function ($text, $parameters = []) {
  130. return vsprintf($text, $parameters);
  131. });
  132. $this->l->method('n')
  133. ->willReturnCallback(function ($singular, $plural, $count, $parameters = []) {
  134. return vsprintf(str_replace('%n', $count, ($count === 1) ? $singular : $plural), $parameters);
  135. });
  136. $this->factory = new DummyFactory(\OC::$server);
  137. $this->manager = new Manager(
  138. $this->logger,
  139. $this->config,
  140. $this->secureRandom,
  141. $this->hasher,
  142. $this->mountManager,
  143. $this->groupManager,
  144. $this->l,
  145. $this->l10nFactory,
  146. $this->factory,
  147. $this->userManager,
  148. $this->rootFolder,
  149. $this->eventDispatcher,
  150. $this->mailer,
  151. $this->urlGenerator,
  152. $this->defaults,
  153. $this->dispatcher,
  154. $this->userSession,
  155. $this->knownUserService
  156. );
  157. $this->defaultProvider = $this->createMock(DefaultShareProvider::class);
  158. $this->defaultProvider->method('identifier')->willReturn('default');
  159. $this->factory->setProvider($this->defaultProvider);
  160. }
  161. /**
  162. * @return MockBuilder
  163. */
  164. private function createManagerMock() {
  165. return $this->getMockBuilder(Manager::class)
  166. ->setConstructorArgs([
  167. $this->logger,
  168. $this->config,
  169. $this->secureRandom,
  170. $this->hasher,
  171. $this->mountManager,
  172. $this->groupManager,
  173. $this->l,
  174. $this->l10nFactory,
  175. $this->factory,
  176. $this->userManager,
  177. $this->rootFolder,
  178. $this->eventDispatcher,
  179. $this->mailer,
  180. $this->urlGenerator,
  181. $this->defaults,
  182. $this->dispatcher,
  183. $this->userSession,
  184. $this->knownUserService
  185. ]);
  186. }
  187. public function testDeleteNoShareId() {
  188. $this->expectException(\InvalidArgumentException::class);
  189. $share = $this->manager->newShare();
  190. $this->manager->deleteShare($share);
  191. }
  192. public function dataTestDelete() {
  193. $user = $this->createMock(IUser::class);
  194. $user->method('getUID')->willReturn('sharedWithUser');
  195. $group = $this->createMock(IGroup::class);
  196. $group->method('getGID')->willReturn('sharedWithGroup');
  197. return [
  198. [IShare::TYPE_USER, 'sharedWithUser'],
  199. [IShare::TYPE_GROUP, 'sharedWithGroup'],
  200. [IShare::TYPE_LINK, ''],
  201. [IShare::TYPE_REMOTE, 'foo@bar.com'],
  202. ];
  203. }
  204. /**
  205. * @dataProvider dataTestDelete
  206. */
  207. public function testDelete($shareType, $sharedWith) {
  208. $manager = $this->createManagerMock()
  209. ->setMethods(['getShareById', 'deleteChildren'])
  210. ->getMock();
  211. $manager->method('deleteChildren')->willReturn([]);
  212. $path = $this->createMock(File::class);
  213. $path->method('getId')->willReturn(1);
  214. $share = $this->manager->newShare();
  215. $share->setId(42)
  216. ->setProviderId('prov')
  217. ->setShareType($shareType)
  218. ->setSharedWith($sharedWith)
  219. ->setSharedBy('sharedBy')
  220. ->setNode($path)
  221. ->setTarget('myTarget');
  222. $manager->expects($this->once())->method('deleteChildren')->with($share);
  223. $this->defaultProvider
  224. ->expects($this->once())
  225. ->method('delete')
  226. ->with($share);
  227. $this->eventDispatcher->expects($this->at(0))
  228. ->method('dispatch')
  229. ->with(
  230. 'OCP\Share::preUnshare',
  231. $this->callBack(function (GenericEvent $e) use ($share) {
  232. return $e->getSubject() === $share;
  233. })
  234. );
  235. $this->eventDispatcher->expects($this->at(1))
  236. ->method('dispatch')
  237. ->with(
  238. 'OCP\Share::postUnshare',
  239. $this->callBack(function (GenericEvent $e) use ($share) {
  240. return $e->getSubject() === $share &&
  241. $e->getArgument('deletedShares') === [$share];
  242. })
  243. );
  244. $manager->deleteShare($share);
  245. }
  246. public function testDeleteLazyShare() {
  247. $manager = $this->createManagerMock()
  248. ->setMethods(['getShareById', 'deleteChildren'])
  249. ->getMock();
  250. $manager->method('deleteChildren')->willReturn([]);
  251. $share = $this->manager->newShare();
  252. $share->setId(42)
  253. ->setProviderId('prov')
  254. ->setShareType(IShare::TYPE_USER)
  255. ->setSharedWith('sharedWith')
  256. ->setSharedBy('sharedBy')
  257. ->setShareOwner('shareOwner')
  258. ->setTarget('myTarget')
  259. ->setNodeId(1)
  260. ->setNodeType('file');
  261. $this->rootFolder->expects($this->never())->method($this->anything());
  262. $manager->expects($this->once())->method('deleteChildren')->with($share);
  263. $this->defaultProvider
  264. ->expects($this->once())
  265. ->method('delete')
  266. ->with($share);
  267. $this->eventDispatcher->expects($this->at(0))
  268. ->method('dispatch')
  269. ->with(
  270. 'OCP\Share::preUnshare',
  271. $this->callBack(function (GenericEvent $e) use ($share) {
  272. return $e->getSubject() === $share;
  273. })
  274. );
  275. $this->eventDispatcher->expects($this->at(1))
  276. ->method('dispatch')
  277. ->with(
  278. 'OCP\Share::postUnshare',
  279. $this->callBack(function (GenericEvent $e) use ($share) {
  280. return $e->getSubject() === $share &&
  281. $e->getArgument('deletedShares') === [$share];
  282. })
  283. );
  284. $manager->deleteShare($share);
  285. }
  286. public function testDeleteNested() {
  287. $manager = $this->createManagerMock()
  288. ->setMethods(['getShareById'])
  289. ->getMock();
  290. $path = $this->createMock(File::class);
  291. $path->method('getId')->willReturn(1);
  292. $share1 = $this->manager->newShare();
  293. $share1->setId(42)
  294. ->setProviderId('prov')
  295. ->setShareType(IShare::TYPE_USER)
  296. ->setSharedWith('sharedWith1')
  297. ->setSharedBy('sharedBy1')
  298. ->setNode($path)
  299. ->setTarget('myTarget1');
  300. $share2 = $this->manager->newShare();
  301. $share2->setId(43)
  302. ->setProviderId('prov')
  303. ->setShareType(IShare::TYPE_GROUP)
  304. ->setSharedWith('sharedWith2')
  305. ->setSharedBy('sharedBy2')
  306. ->setNode($path)
  307. ->setTarget('myTarget2')
  308. ->setParent(42);
  309. $share3 = $this->manager->newShare();
  310. $share3->setId(44)
  311. ->setProviderId('prov')
  312. ->setShareType(IShare::TYPE_LINK)
  313. ->setSharedBy('sharedBy3')
  314. ->setNode($path)
  315. ->setTarget('myTarget3')
  316. ->setParent(43);
  317. $this->defaultProvider
  318. ->method('getChildren')
  319. ->willReturnMap([
  320. [$share1, [$share2]],
  321. [$share2, [$share3]],
  322. [$share3, []],
  323. ]);
  324. $this->defaultProvider
  325. ->method('delete')
  326. ->withConsecutive([$share3], [$share2], [$share1]);
  327. $this->eventDispatcher->expects($this->at(0))
  328. ->method('dispatch')
  329. ->with(
  330. 'OCP\Share::preUnshare',
  331. $this->callBack(function (GenericEvent $e) use ($share1) {
  332. return $e->getSubject() === $share1;
  333. })
  334. );
  335. $this->eventDispatcher->expects($this->at(1))
  336. ->method('dispatch')
  337. ->with(
  338. 'OCP\Share::postUnshare',
  339. $this->callBack(function (GenericEvent $e) use ($share1, $share2, $share3) {
  340. return $e->getSubject() === $share1 &&
  341. $e->getArgument('deletedShares') === [$share3, $share2, $share1];
  342. })
  343. );
  344. $manager->deleteShare($share1);
  345. }
  346. public function testDeleteFromSelf() {
  347. $manager = $this->createManagerMock()
  348. ->setMethods(['getShareById'])
  349. ->getMock();
  350. $recipientId = 'unshareFrom';
  351. $share = $this->manager->newShare();
  352. $share->setId(42)
  353. ->setProviderId('prov')
  354. ->setShareType(IShare::TYPE_USER)
  355. ->setSharedWith('sharedWith')
  356. ->setSharedBy('sharedBy')
  357. ->setShareOwner('shareOwner')
  358. ->setTarget('myTarget')
  359. ->setNodeId(1)
  360. ->setNodeType('file');
  361. $this->defaultProvider
  362. ->expects($this->once())
  363. ->method('deleteFromSelf')
  364. ->with($share, $recipientId);
  365. $this->eventDispatcher->expects($this->at(0))
  366. ->method('dispatch')
  367. ->with(
  368. 'OCP\Share::postUnshareFromSelf',
  369. $this->callBack(function (GenericEvent $e) use ($share) {
  370. return $e->getSubject() === $share;
  371. })
  372. );
  373. $manager->deleteFromSelf($share, $recipientId);
  374. }
  375. public function testDeleteChildren() {
  376. $manager = $this->createManagerMock()
  377. ->setMethods(['deleteShare'])
  378. ->getMock();
  379. $share = $this->createMock(IShare::class);
  380. $share->method('getShareType')->willReturn(IShare::TYPE_USER);
  381. $child1 = $this->createMock(IShare::class);
  382. $child1->method('getShareType')->willReturn(IShare::TYPE_USER);
  383. $child2 = $this->createMock(IShare::class);
  384. $child2->method('getShareType')->willReturn(IShare::TYPE_USER);
  385. $child3 = $this->createMock(IShare::class);
  386. $child3->method('getShareType')->willReturn(IShare::TYPE_USER);
  387. $shares = [
  388. $child1,
  389. $child2,
  390. $child3,
  391. ];
  392. $this->defaultProvider
  393. ->expects($this->exactly(4))
  394. ->method('getChildren')
  395. ->willReturnCallback(function ($_share) use ($share, $shares) {
  396. if ($_share === $share) {
  397. return $shares;
  398. }
  399. return [];
  400. });
  401. $this->defaultProvider
  402. ->expects($this->exactly(3))
  403. ->method('delete')
  404. ->withConsecutive([$child1], [$child2], [$child3]);
  405. $result = self::invokePrivate($manager, 'deleteChildren', [$share]);
  406. $this->assertSame($shares, $result);
  407. }
  408. public function testGetShareById() {
  409. $share = $this->createMock(IShare::class);
  410. $this->defaultProvider
  411. ->expects($this->once())
  412. ->method('getShareById')
  413. ->with(42)
  414. ->willReturn($share);
  415. $this->assertEquals($share, $this->manager->getShareById('default:42'));
  416. }
  417. public function testGetExpiredShareById() {
  418. $this->expectException(\OCP\Share\Exceptions\ShareNotFound::class);
  419. $manager = $this->createManagerMock()
  420. ->setMethods(['deleteShare'])
  421. ->getMock();
  422. $date = new \DateTime();
  423. $date->setTime(0, 0, 0);
  424. $share = $this->manager->newShare();
  425. $share->setExpirationDate($date)
  426. ->setShareType(IShare::TYPE_LINK);
  427. $this->defaultProvider->expects($this->once())
  428. ->method('getShareById')
  429. ->with('42')
  430. ->willReturn($share);
  431. $manager->expects($this->once())
  432. ->method('deleteShare')
  433. ->with($share);
  434. $manager->getShareById('default:42');
  435. }
  436. public function testVerifyPasswordNullButEnforced() {
  437. $this->expectException(\InvalidArgumentException::class);
  438. $this->expectExceptionMessage('Passwords are enforced for link and mail shares');
  439. $this->config->method('getAppValue')->willReturnMap([
  440. ['core', 'shareapi_enforce_links_password_excluded_groups', '', ''],
  441. ['core', 'shareapi_enforce_links_password', 'no', 'yes'],
  442. ]);
  443. self::invokePrivate($this->manager, 'verifyPassword', [null]);
  444. }
  445. public function testVerifyPasswordNotEnforcedGroup() {
  446. $this->config->method('getAppValue')->willReturnMap([
  447. ['core', 'shareapi_enforce_links_password_excluded_groups', '', '["admin"]'],
  448. ['core', 'shareapi_enforce_links_password', 'no', 'yes'],
  449. ]);
  450. // Create admin user
  451. $user = $this->createMock(IUser::class);
  452. $this->userSession->method('getUser')->willReturn($user);
  453. $this->groupManager->method('getUserGroupIds')->with($user)->willReturn(['admin']);
  454. $result = self::invokePrivate($this->manager, 'verifyPassword', [null]);
  455. $this->assertNull($result);
  456. }
  457. public function testVerifyPasswordNotEnforcedMultipleGroups() {
  458. $this->config->method('getAppValue')->willReturnMap([
  459. ['core', 'shareapi_enforce_links_password_excluded_groups', '', '["admin", "special"]'],
  460. ['core', 'shareapi_enforce_links_password', 'no', 'yes'],
  461. ]);
  462. // Create admin user
  463. $user = $this->createMock(IUser::class);
  464. $this->userSession->method('getUser')->willReturn($user);
  465. $this->groupManager->method('getUserGroupIds')->with($user)->willReturn(['special']);
  466. $result = self::invokePrivate($this->manager, 'verifyPassword', [null]);
  467. $this->assertNull($result);
  468. }
  469. public function testVerifyPasswordNull() {
  470. $this->config->method('getAppValue')->willReturnMap([
  471. ['core', 'shareapi_enforce_links_password_excluded_groups', '', ''],
  472. ['core', 'shareapi_enforce_links_password', 'no', 'no'],
  473. ]);
  474. $result = self::invokePrivate($this->manager, 'verifyPassword', [null]);
  475. $this->assertNull($result);
  476. }
  477. public function testVerifyPasswordHook() {
  478. $this->config->method('getAppValue')->willReturnMap([
  479. ['core', 'shareapi_enforce_links_password_excluded_groups', '', ''],
  480. ['core', 'shareapi_enforce_links_password', 'no', 'no'],
  481. ]);
  482. $this->eventDispatcher->expects($this->once())->method('dispatch')
  483. ->willReturnCallback(function (Event $event) {
  484. $this->assertInstanceOf(ValidatePasswordPolicyEvent::class, $event);
  485. /** @var ValidatePasswordPolicyEvent $event */
  486. $this->assertSame('password', $event->getPassword());
  487. }
  488. );
  489. $result = self::invokePrivate($this->manager, 'verifyPassword', ['password']);
  490. $this->assertNull($result);
  491. }
  492. public function testVerifyPasswordHookFails() {
  493. $this->expectException(\Exception::class);
  494. $this->expectExceptionMessage('password not accepted');
  495. $this->config->method('getAppValue')->willReturnMap([
  496. ['core', 'shareapi_enforce_links_password_excluded_groups', '', ''],
  497. ['core', 'shareapi_enforce_links_password', 'no', 'no'],
  498. ]);
  499. $this->eventDispatcher->expects($this->once())->method('dispatch')
  500. ->willReturnCallback(function (Event $event) {
  501. $this->assertInstanceOf(ValidatePasswordPolicyEvent::class, $event);
  502. /** @var ValidatePasswordPolicyEvent $event */
  503. $this->assertSame('password', $event->getPassword());
  504. throw new HintException('message', 'password not accepted');
  505. }
  506. );
  507. self::invokePrivate($this->manager, 'verifyPassword', ['password']);
  508. }
  509. public function createShare($id, $type, $path, $sharedWith, $sharedBy, $shareOwner,
  510. $permissions, $expireDate = null, $password = null, $attributes = null) {
  511. $share = $this->createMock(IShare::class);
  512. $share->method('getShareType')->willReturn($type);
  513. $share->method('getSharedWith')->willReturn($sharedWith);
  514. $share->method('getSharedBy')->willReturn($sharedBy);
  515. $share->method('getShareOwner')->willReturn($shareOwner);
  516. $share->method('getNode')->willReturn($path);
  517. $share->method('getPermissions')->willReturn($permissions);
  518. $share->method('getAttributes')->willReturn($attributes);
  519. $share->method('getExpirationDate')->willReturn($expireDate);
  520. $share->method('getPassword')->willReturn($password);
  521. return $share;
  522. }
  523. public function dataGeneralChecks() {
  524. $user0 = 'user0';
  525. $user2 = 'user1';
  526. $group0 = 'group0';
  527. $owner = $this->createMock(IUser::class);
  528. $owner->method('getUID')
  529. ->willReturn($user0);
  530. $file = $this->createMock(File::class);
  531. $node = $this->createMock(Node::class);
  532. $storage = $this->createMock(Storage\IStorage::class);
  533. $storage->method('instanceOfStorage')
  534. ->with('\OCA\Files_Sharing\External\Storage')
  535. ->willReturn(false);
  536. $file->method('getStorage')
  537. ->willReturn($storage);
  538. $node->method('getStorage')
  539. ->willReturn($storage);
  540. $data = [
  541. [$this->createShare(null, IShare::TYPE_USER, $file, null, $user0, $user0, 31, null, null), 'SharedWith is not a valid user', true],
  542. [$this->createShare(null, IShare::TYPE_USER, $file, $group0, $user0, $user0, 31, null, null), 'SharedWith is not a valid user', true],
  543. [$this->createShare(null, IShare::TYPE_USER, $file, 'foo@bar.com', $user0, $user0, 31, null, null), 'SharedWith is not a valid user', true],
  544. [$this->createShare(null, IShare::TYPE_GROUP, $file, null, $user0, $user0, 31, null, null), 'SharedWith is not a valid group', true],
  545. [$this->createShare(null, IShare::TYPE_GROUP, $file, $user2, $user0, $user0, 31, null, null), 'SharedWith is not a valid group', true],
  546. [$this->createShare(null, IShare::TYPE_GROUP, $file, 'foo@bar.com', $user0, $user0, 31, null, null), 'SharedWith is not a valid group', true],
  547. [$this->createShare(null, IShare::TYPE_LINK, $file, $user2, $user0, $user0, 31, null, null), 'SharedWith should be empty', true],
  548. [$this->createShare(null, IShare::TYPE_LINK, $file, $group0, $user0, $user0, 31, null, null), 'SharedWith should be empty', true],
  549. [$this->createShare(null, IShare::TYPE_LINK, $file, 'foo@bar.com', $user0, $user0, 31, null, null), 'SharedWith should be empty', true],
  550. [$this->createShare(null, -1, $file, null, $user0, $user0, 31, null, null), 'unknown share type', true],
  551. [$this->createShare(null, IShare::TYPE_USER, $file, $user2, null, $user0, 31, null, null), 'SharedBy should be set', true],
  552. [$this->createShare(null, IShare::TYPE_GROUP, $file, $group0, null, $user0, 31, null, null), 'SharedBy should be set', true],
  553. [$this->createShare(null, IShare::TYPE_LINK, $file, null, null, $user0, 31, null, null), 'SharedBy should be set', true],
  554. [$this->createShare(null, IShare::TYPE_USER, $file, $user0, $user0, $user0, 31, null, null), 'Cannot share with yourself', true],
  555. [$this->createShare(null, IShare::TYPE_USER, null, $user2, $user0, $user0, 31, null, null), 'Path should be set', true],
  556. [$this->createShare(null, IShare::TYPE_GROUP, null, $group0, $user0, $user0, 31, null, null), 'Path should be set', true],
  557. [$this->createShare(null, IShare::TYPE_LINK, null, null, $user0, $user0, 31, null, null), 'Path should be set', true],
  558. [$this->createShare(null, IShare::TYPE_USER, $node, $user2, $user0, $user0, 31, null, null), 'Path should be either a file or a folder', true],
  559. [$this->createShare(null, IShare::TYPE_GROUP, $node, $group0, $user0, $user0, 31, null, null), 'Path should be either a file or a folder', true],
  560. [$this->createShare(null, IShare::TYPE_LINK, $node, null, $user0, $user0, 31, null, null), 'Path should be either a file or a folder', true],
  561. ];
  562. $nonShareAble = $this->createMock(Folder::class);
  563. $nonShareAble->method('isShareable')->willReturn(false);
  564. $nonShareAble->method('getPath')->willReturn('path');
  565. $nonShareAble->method('getName')->willReturn('name');
  566. $nonShareAble->method('getOwner')
  567. ->willReturn($owner);
  568. $nonShareAble->method('getStorage')
  569. ->willReturn($storage);
  570. $data[] = [$this->createShare(null, IShare::TYPE_USER, $nonShareAble, $user2, $user0, $user0, 31, null, null), 'You are not allowed to share name', true];
  571. $data[] = [$this->createShare(null, IShare::TYPE_GROUP, $nonShareAble, $group0, $user0, $user0, 31, null, null), 'You are not allowed to share name', true];
  572. $data[] = [$this->createShare(null, IShare::TYPE_LINK, $nonShareAble, null, $user0, $user0, 31, null, null), 'You are not allowed to share name', true];
  573. $limitedPermssions = $this->createMock(File::class);
  574. $limitedPermssions->method('isShareable')->willReturn(true);
  575. $limitedPermssions->method('getPermissions')->willReturn(\OCP\Constants::PERMISSION_READ);
  576. $limitedPermssions->method('getId')->willReturn(108);
  577. $limitedPermssions->method('getPath')->willReturn('path');
  578. $limitedPermssions->method('getName')->willReturn('name');
  579. $limitedPermssions->method('getOwner')
  580. ->willReturn($owner);
  581. $limitedPermssions->method('getStorage')
  582. ->willReturn($storage);
  583. $data[] = [$this->createShare(null, IShare::TYPE_USER, $limitedPermssions, $user2, $user0, $user0, null, null, null), 'A share requires permissions', true];
  584. $data[] = [$this->createShare(null, IShare::TYPE_GROUP, $limitedPermssions, $group0, $user0, $user0, null, null, null), 'A share requires permissions', true];
  585. $data[] = [$this->createShare(null, IShare::TYPE_LINK, $limitedPermssions, null, $user0, $user0, null, null, null), 'A share requires permissions', true];
  586. $mount = $this->createMock(MoveableMount::class);
  587. $limitedPermssions->method('getMountPoint')->willReturn($mount);
  588. $data[] = [$this->createShare(null, IShare::TYPE_USER, $limitedPermssions, $user2, $user0, $user0, 31, null, null), 'Cannot increase permissions of path', true];
  589. $data[] = [$this->createShare(null, IShare::TYPE_GROUP, $limitedPermssions, $group0, $user0, $user0, 17, null, null), 'Cannot increase permissions of path', true];
  590. $data[] = [$this->createShare(null, IShare::TYPE_LINK, $limitedPermssions, null, $user0, $user0, 3, null, null), 'Cannot increase permissions of path', true];
  591. $nonMoveableMountPermssions = $this->createMock(Folder::class);
  592. $nonMoveableMountPermssions->method('isShareable')->willReturn(true);
  593. $nonMoveableMountPermssions->method('getPermissions')->willReturn(\OCP\Constants::PERMISSION_READ);
  594. $nonMoveableMountPermssions->method('getId')->willReturn(108);
  595. $nonMoveableMountPermssions->method('getPath')->willReturn('path');
  596. $nonMoveableMountPermssions->method('getName')->willReturn('name');
  597. $nonMoveableMountPermssions->method('getOwner')
  598. ->willReturn($owner);
  599. $nonMoveableMountPermssions->method('getStorage')
  600. ->willReturn($storage);
  601. $data[] = [$this->createShare(null, IShare::TYPE_USER, $nonMoveableMountPermssions, $user2, $user0, $user0, 11, null, null), 'Cannot increase permissions of path', false];
  602. $data[] = [$this->createShare(null, IShare::TYPE_GROUP, $nonMoveableMountPermssions, $group0, $user0, $user0, 11, null, null), 'Cannot increase permissions of path', false];
  603. $rootFolder = $this->createMock(Folder::class);
  604. $rootFolder->method('isShareable')->willReturn(true);
  605. $rootFolder->method('getPermissions')->willReturn(\OCP\Constants::PERMISSION_ALL);
  606. $rootFolder->method('getId')->willReturn(42);
  607. $data[] = [$this->createShare(null, IShare::TYPE_USER, $rootFolder, $user2, $user0, $user0, 30, null, null), 'You cannot share your root folder', true];
  608. $data[] = [$this->createShare(null, IShare::TYPE_GROUP, $rootFolder, $group0, $user0, $user0, 2, null, null), 'You cannot share your root folder', true];
  609. $data[] = [$this->createShare(null, IShare::TYPE_LINK, $rootFolder, null, $user0, $user0, 16, null, null), 'You cannot share your root folder', true];
  610. $allPermssions = $this->createMock(Folder::class);
  611. $allPermssions->method('isShareable')->willReturn(true);
  612. $allPermssions->method('getPermissions')->willReturn(\OCP\Constants::PERMISSION_ALL);
  613. $allPermssions->method('getId')->willReturn(108);
  614. $allPermssions->method('getOwner')
  615. ->willReturn($owner);
  616. $allPermssions->method('getStorage')
  617. ->willReturn($storage);
  618. $data[] = [$this->createShare(null, IShare::TYPE_USER, $allPermssions, $user2, $user0, $user0, 30, null, null), 'Shares need at least read permissions', true];
  619. $data[] = [$this->createShare(null, IShare::TYPE_GROUP, $allPermssions, $group0, $user0, $user0, 2, null, null), 'Shares need at least read permissions', true];
  620. $data[] = [$this->createShare(null, IShare::TYPE_USER, $allPermssions, $user2, $user0, $user0, 31, null, null), null, false];
  621. $data[] = [$this->createShare(null, IShare::TYPE_GROUP, $allPermssions, $group0, $user0, $user0, 3, null, null), null, false];
  622. $data[] = [$this->createShare(null, IShare::TYPE_LINK, $allPermssions, null, $user0, $user0, 17, null, null), null, false];
  623. $remoteStorage = $this->createMock(Storage\IStorage::class);
  624. $remoteStorage->method('instanceOfStorage')
  625. ->with('\OCA\Files_Sharing\External\Storage')
  626. ->willReturn(true);
  627. $remoteFile = $this->createMock(Folder::class);
  628. $remoteFile->method('isShareable')->willReturn(true);
  629. $remoteFile->method('getPermissions')->willReturn(\OCP\Constants::PERMISSION_READ ^ \OCP\Constants::PERMISSION_UPDATE);
  630. $remoteFile->method('getId')->willReturn(108);
  631. $remoteFile->method('getOwner')
  632. ->willReturn($owner);
  633. $remoteFile->method('getStorage')
  634. ->willReturn($storage);
  635. $data[] = [$this->createShare(null, IShare::TYPE_REMOTE, $remoteFile, $user2, $user0, $user0, 1, null, null), null, false];
  636. $data[] = [$this->createShare(null, IShare::TYPE_REMOTE, $remoteFile, $user2, $user0, $user0, 3, null, null), null, false];
  637. $data[] = [$this->createShare(null, IShare::TYPE_REMOTE, $remoteFile, $user2, $user0, $user0, 31, null, null), 'Cannot increase permissions of ', true];
  638. return $data;
  639. }
  640. /**
  641. * @dataProvider dataGeneralChecks
  642. *
  643. * @param $share
  644. * @param $exceptionMessage
  645. * @param $exception
  646. */
  647. public function testGeneralChecks($share, $exceptionMessage, $exception) {
  648. $thrown = null;
  649. $this->userManager->method('userExists')->willReturnMap([
  650. ['user0', true],
  651. ['user1', true],
  652. ]);
  653. $this->groupManager->method('groupExists')->willReturnMap([
  654. ['group0', true],
  655. ]);
  656. $userFolder = $this->createMock(Folder::class);
  657. $userFolder->expects($this->any())
  658. ->method('getId')
  659. ->willReturn(42);
  660. // Id 108 is used in the data to refer to the node of the share.
  661. $userFolder->expects($this->any())
  662. ->method('getById')
  663. ->with(108)
  664. ->willReturn([$share->getNode()]);
  665. $userFolder->expects($this->any())
  666. ->method('getRelativePath')
  667. ->willReturnArgument(0);
  668. $this->rootFolder->method('getUserFolder')->willReturn($userFolder);
  669. try {
  670. self::invokePrivate($this->manager, 'generalCreateChecks', [$share]);
  671. $thrown = false;
  672. } catch (\OCP\Share\Exceptions\GenericShareException $e) {
  673. $this->assertEquals($exceptionMessage, $e->getHint());
  674. $thrown = true;
  675. } catch (\InvalidArgumentException $e) {
  676. $this->assertEquals($exceptionMessage, $e->getMessage());
  677. $thrown = true;
  678. }
  679. $this->assertSame($exception, $thrown);
  680. }
  681. public function testGeneralCheckShareRoot() {
  682. $this->expectException(\InvalidArgumentException::class);
  683. $this->expectExceptionMessage('You cannot share your root folder');
  684. $thrown = null;
  685. $this->userManager->method('userExists')->willReturnMap([
  686. ['user0', true],
  687. ['user1', true],
  688. ]);
  689. $userFolder = $this->createMock(Folder::class);
  690. $userFolder->method('isSubNode')->with($userFolder)->willReturn(false);
  691. $this->rootFolder->method('getUserFolder')->willReturn($userFolder);
  692. $share = $this->manager->newShare();
  693. $share->setShareType(IShare::TYPE_USER)
  694. ->setSharedWith('user0')
  695. ->setSharedBy('user1')
  696. ->setNode($userFolder);
  697. self::invokePrivate($this->manager, 'generalCreateChecks', [$share]);
  698. }
  699. public function validateExpirationDateInternalProvider() {
  700. return [[IShare::TYPE_USER], [IShare::TYPE_REMOTE], [IShare::TYPE_REMOTE_GROUP]];
  701. }
  702. /**
  703. * @dataProvider validateExpirationDateInternalProvider
  704. */
  705. public function testValidateExpirationDateInternalInPast($shareType) {
  706. $this->expectException(\OCP\Share\Exceptions\GenericShareException::class);
  707. $this->expectExceptionMessage('Expiration date is in the past');
  708. // Expire date in the past
  709. $past = new \DateTime();
  710. $past->sub(new \DateInterval('P1D'));
  711. $share = $this->manager->newShare();
  712. $share->setShareType($shareType);
  713. $share->setExpirationDate($past);
  714. self::invokePrivate($this->manager, 'validateExpirationDateInternal', [$share]);
  715. }
  716. /**
  717. * @dataProvider validateExpirationDateInternalProvider
  718. */
  719. public function testValidateExpirationDateInternalEnforceButNotSet($shareType) {
  720. $this->expectException(\InvalidArgumentException::class);
  721. $this->expectExceptionMessage('Expiration date is enforced');
  722. $share = $this->manager->newShare();
  723. $share->setProviderId('foo')->setId('bar');
  724. $share->setShareType($shareType);
  725. if ($shareType === IShare::TYPE_USER) {
  726. $this->config->method('getAppValue')
  727. ->willReturnMap([
  728. ['core', 'shareapi_default_internal_expire_date', 'no', 'yes'],
  729. ['core', 'shareapi_enforce_internal_expire_date', 'no', 'yes'],
  730. ]);
  731. } else {
  732. $this->config->method('getAppValue')
  733. ->willReturnMap([
  734. ['core', 'shareapi_default_remote_expire_date', 'no', 'yes'],
  735. ['core', 'shareapi_enforce_remote_expire_date', 'no', 'yes'],
  736. ]);
  737. }
  738. self::invokePrivate($this->manager, 'validateExpirationDateInternal', [$share]);
  739. }
  740. /**
  741. * @dataProvider validateExpirationDateInternalProvider
  742. */
  743. public function testValidateExpirationDateInternalEnforceButNotEnabledAndNotSet($shareType) {
  744. $share = $this->manager->newShare();
  745. $share->setProviderId('foo')->setId('bar');
  746. $share->setShareType($shareType);
  747. if ($shareType === IShare::TYPE_USER) {
  748. $this->config->method('getAppValue')
  749. ->willReturnMap([
  750. ['core', 'shareapi_enforce_internal_expire_date', 'no', 'yes'],
  751. ]);
  752. } else {
  753. $this->config->method('getAppValue')
  754. ->willReturnMap([
  755. ['core', 'shareapi_enforce_remote_expire_date', 'no', 'yes'],
  756. ]);
  757. }
  758. self::invokePrivate($this->manager, 'validateExpirationDateInternal', [$share]);
  759. $this->assertNull($share->getExpirationDate());
  760. }
  761. /**
  762. * @dataProvider validateExpirationDateInternalProvider
  763. */
  764. public function testValidateExpirationDateInternalEnforceButNotSetNewShare($shareType) {
  765. $share = $this->manager->newShare();
  766. $share->setShareType($shareType);
  767. if ($shareType === IShare::TYPE_USER) {
  768. $this->config->method('getAppValue')
  769. ->willReturnMap([
  770. ['core', 'shareapi_enforce_internal_expire_date', 'no', 'yes'],
  771. ['core', 'shareapi_internal_expire_after_n_days', '7', '3'],
  772. ['core', 'shareapi_default_internal_expire_date', 'no', 'yes'],
  773. ['core', 'internal_defaultExpDays', '3', '3'],
  774. ]);
  775. } else {
  776. $this->config->method('getAppValue')
  777. ->willReturnMap([
  778. ['core', 'shareapi_enforce_remote_expire_date', 'no', 'yes'],
  779. ['core', 'shareapi_remote_expire_after_n_days', '7', '3'],
  780. ['core', 'shareapi_default_remote_expire_date', 'no', 'yes'],
  781. ['core', 'remote_defaultExpDays', '3', '3'],
  782. ]);
  783. }
  784. $expected = new \DateTime();
  785. $expected->setTime(0, 0, 0);
  786. $expected->add(new \DateInterval('P3D'));
  787. self::invokePrivate($this->manager, 'validateExpirationDateInternal', [$share]);
  788. $this->assertNotNull($share->getExpirationDate());
  789. $this->assertEquals($expected, $share->getExpirationDate());
  790. }
  791. /**
  792. * @dataProvider validateExpirationDateInternalProvider
  793. */
  794. public function testValidateExpirationDateInternalEnforceRelaxedDefaultButNotSetNewShare($shareType) {
  795. $share = $this->manager->newShare();
  796. $share->setShareType($shareType);
  797. if ($shareType === IShare::TYPE_USER) {
  798. $this->config->method('getAppValue')
  799. ->willReturnMap([
  800. ['core', 'shareapi_enforce_internal_expire_date', 'no', 'yes'],
  801. ['core', 'shareapi_internal_expire_after_n_days', '7', '3'],
  802. ['core', 'shareapi_default_internal_expire_date', 'no', 'yes'],
  803. ['core', 'internal_defaultExpDays', '3', '1'],
  804. ]);
  805. } else {
  806. $this->config->method('getAppValue')
  807. ->willReturnMap([
  808. ['core', 'shareapi_enforce_remote_expire_date', 'no', 'yes'],
  809. ['core', 'shareapi_remote_expire_after_n_days', '7', '3'],
  810. ['core', 'shareapi_default_remote_expire_date', 'no', 'yes'],
  811. ['core', 'remote_defaultExpDays', '3', '1'],
  812. ]);
  813. }
  814. $expected = new \DateTime();
  815. $expected->setTime(0, 0, 0);
  816. $expected->add(new \DateInterval('P1D'));
  817. self::invokePrivate($this->manager, 'validateExpirationDateInternal', [$share]);
  818. $this->assertNotNull($share->getExpirationDate());
  819. $this->assertEquals($expected, $share->getExpirationDate());
  820. }
  821. /**
  822. * @dataProvider validateExpirationDateInternalProvider
  823. */
  824. public function testValidateExpirationDateInternalEnforceTooFarIntoFuture($shareType) {
  825. $this->expectException(\OCP\Share\Exceptions\GenericShareException::class);
  826. $this->expectExceptionMessage('Cannot set expiration date more than 3 days in the future');
  827. $future = new \DateTime();
  828. $future->add(new \DateInterval('P7D'));
  829. $share = $this->manager->newShare();
  830. $share->setShareType($shareType);
  831. $share->setExpirationDate($future);
  832. if ($shareType === IShare::TYPE_USER) {
  833. $this->config->method('getAppValue')
  834. ->willReturnMap([
  835. ['core', 'shareapi_enforce_internal_expire_date', 'no', 'yes'],
  836. ['core', 'shareapi_internal_expire_after_n_days', '7', '3'],
  837. ['core', 'shareapi_default_internal_expire_date', 'no', 'yes'],
  838. ]);
  839. } else {
  840. $this->config->method('getAppValue')
  841. ->willReturnMap([
  842. ['core', 'shareapi_enforce_remote_expire_date', 'no', 'yes'],
  843. ['core', 'shareapi_remote_expire_after_n_days', '7', '3'],
  844. ['core', 'shareapi_default_remote_expire_date', 'no', 'yes'],
  845. ]);
  846. }
  847. self::invokePrivate($this->manager, 'validateExpirationDateInternal', [$share]);
  848. }
  849. /**
  850. * @dataProvider validateExpirationDateInternalProvider
  851. */
  852. public function testValidateExpirationDateInternalEnforceValid($shareType) {
  853. $future = new \DateTime();
  854. $future->add(new \DateInterval('P2D'));
  855. $future->setTime(1, 2, 3);
  856. $expected = clone $future;
  857. $expected->setTime(0, 0, 0);
  858. $share = $this->manager->newShare();
  859. $share->setShareType($shareType);
  860. $share->setExpirationDate($future);
  861. if ($shareType === IShare::TYPE_USER) {
  862. $this->config->method('getAppValue')
  863. ->willReturnMap([
  864. ['core', 'shareapi_enforce_internal_expire_date', 'no', 'yes'],
  865. ['core', 'shareapi_internal_expire_after_n_days', '7', '3'],
  866. ['core', 'shareapi_default_internal_expire_date', 'no', 'yes'],
  867. ]);
  868. } else {
  869. $this->config->method('getAppValue')
  870. ->willReturnMap([
  871. ['core', 'shareapi_enforce_remote_expire_date', 'no', 'yes'],
  872. ['core', 'shareapi_remote_expire_after_n_days', '7', '3'],
  873. ['core', 'shareapi_default_remote_expire_date', 'no', 'yes'],
  874. ]);
  875. }
  876. $hookListener = $this->getMockBuilder('Dummy')->setMethods(['listener'])->getMock();
  877. \OCP\Util::connectHook('\OC\Share', 'verifyExpirationDate', $hookListener, 'listener');
  878. $hookListener->expects($this->once())->method('listener')->with($this->callback(function ($data) use ($future) {
  879. return $data['expirationDate'] == $future;
  880. }));
  881. self::invokePrivate($this->manager, 'validateExpirationDateInternal', [$share]);
  882. $this->assertEquals($expected, $share->getExpirationDate());
  883. }
  884. /**
  885. * @dataProvider validateExpirationDateInternalProvider
  886. */
  887. public function testValidateExpirationDateInternalNoDefault($shareType) {
  888. $date = new \DateTime();
  889. $date->add(new \DateInterval('P5D'));
  890. $date->setTime(1, 2, 3);
  891. $expected = clone $date;
  892. $expected->setTime(0, 0, 0);
  893. $share = $this->manager->newShare();
  894. $share->setShareType($shareType);
  895. $share->setExpirationDate($date);
  896. $hookListener = $this->getMockBuilder('Dummy')->setMethods(['listener'])->getMock();
  897. \OCP\Util::connectHook('\OC\Share', 'verifyExpirationDate', $hookListener, 'listener');
  898. $hookListener->expects($this->once())->method('listener')->with($this->callback(function ($data) use ($expected) {
  899. return $data['expirationDate'] == $expected && $data['passwordSet'] === false;
  900. }));
  901. self::invokePrivate($this->manager, 'validateExpirationDateInternal', [$share]);
  902. $this->assertEquals($expected, $share->getExpirationDate());
  903. }
  904. /**
  905. * @dataProvider validateExpirationDateInternalProvider
  906. */
  907. public function testValidateExpirationDateInternalNoDateNoDefault($shareType) {
  908. $hookListener = $this->getMockBuilder('Dummy')->setMethods(['listener'])->getMock();
  909. \OCP\Util::connectHook('\OC\Share', 'verifyExpirationDate', $hookListener, 'listener');
  910. $hookListener->expects($this->once())->method('listener')->with($this->callback(function ($data) {
  911. return $data['expirationDate'] === null && $data['passwordSet'] === true;
  912. }));
  913. $share = $this->manager->newShare();
  914. $share->setShareType($shareType);
  915. $share->setPassword('password');
  916. self::invokePrivate($this->manager, 'validateExpirationDateInternal', [$share]);
  917. $this->assertNull($share->getExpirationDate());
  918. }
  919. /**
  920. * @dataProvider validateExpirationDateInternalProvider
  921. */
  922. public function testValidateExpirationDateInternalNoDateDefault($shareType) {
  923. $share = $this->manager->newShare();
  924. $share->setShareType($shareType);
  925. $expected = new \DateTime();
  926. $expected->add(new \DateInterval('P3D'));
  927. $expected->setTime(0, 0, 0);
  928. if ($shareType === IShare::TYPE_USER) {
  929. $this->config->method('getAppValue')
  930. ->willReturnMap([
  931. ['core', 'shareapi_default_internal_expire_date', 'no', 'yes'],
  932. ['core', 'shareapi_internal_expire_after_n_days', '7', '3'],
  933. ['core', 'internal_defaultExpDays', '3', '3'],
  934. ]);
  935. } else {
  936. $this->config->method('getAppValue')
  937. ->willReturnMap([
  938. ['core', 'shareapi_default_remote_expire_date', 'no', 'yes'],
  939. ['core', 'shareapi_remote_expire_after_n_days', '7', '3'],
  940. ['core', 'remote_defaultExpDays', '3', '3'],
  941. ]);
  942. }
  943. $hookListener = $this->getMockBuilder('Dummy')->setMethods(['listener'])->getMock();
  944. \OCP\Util::connectHook('\OC\Share', 'verifyExpirationDate', $hookListener, 'listener');
  945. $hookListener->expects($this->once())->method('listener')->with($this->callback(function ($data) use ($expected) {
  946. return $data['expirationDate'] == $expected;
  947. }));
  948. self::invokePrivate($this->manager, 'validateExpirationDateInternal', [$share]);
  949. $this->assertEquals($expected, $share->getExpirationDate());
  950. }
  951. /**
  952. * @dataProvider validateExpirationDateInternalProvider
  953. */
  954. public function testValidateExpirationDateInternalDefault($shareType) {
  955. $future = new \DateTime();
  956. $future->add(new \DateInterval('P5D'));
  957. $future->setTime(1, 2, 3);
  958. $expected = clone $future;
  959. $expected->setTime(0, 0, 0);
  960. $share = $this->manager->newShare();
  961. $share->setShareType($shareType);
  962. $share->setExpirationDate($future);
  963. if ($shareType === IShare::TYPE_USER) {
  964. $this->config->method('getAppValue')
  965. ->willReturnMap([
  966. ['core', 'shareapi_default_internal_expire_date', 'no', 'yes'],
  967. ['core', 'shareapi_internal_expire_after_n_days', '7', '3'],
  968. ['core', 'internal_defaultExpDays', '3', '1'],
  969. ]);
  970. } else {
  971. $this->config->method('getAppValue')
  972. ->willReturnMap([
  973. ['core', 'shareapi_default_remote_expire_date', 'no', 'yes'],
  974. ['core', 'shareapi_remote_expire_after_n_days', '7', '3'],
  975. ['core', 'remote_defaultExpDays', '3', '1'],
  976. ]);
  977. }
  978. $hookListener = $this->getMockBuilder('Dummy')->setMethods(['listener'])->getMock();
  979. \OCP\Util::connectHook('\OC\Share', 'verifyExpirationDate', $hookListener, 'listener');
  980. $hookListener->expects($this->once())->method('listener')->with($this->callback(function ($data) use ($expected) {
  981. return $data['expirationDate'] == $expected;
  982. }));
  983. self::invokePrivate($this->manager, 'validateExpirationDateInternal', [$share]);
  984. $this->assertEquals($expected, $share->getExpirationDate());
  985. }
  986. /**
  987. * @dataProvider validateExpirationDateInternalProvider
  988. */
  989. public function testValidateExpirationDateInternalHookModification($shareType) {
  990. $nextWeek = new \DateTime();
  991. $nextWeek->add(new \DateInterval('P7D'));
  992. $nextWeek->setTime(0, 0, 0);
  993. $save = clone $nextWeek;
  994. $hookListener = $this->getMockBuilder('Dummy')->setMethods(['listener'])->getMock();
  995. \OCP\Util::connectHook('\OC\Share', 'verifyExpirationDate', $hookListener, 'listener');
  996. $hookListener->expects($this->once())->method('listener')->willReturnCallback(function ($data) {
  997. $data['expirationDate']->sub(new \DateInterval('P2D'));
  998. });
  999. $share = $this->manager->newShare();
  1000. $share->setShareType($shareType);
  1001. $share->setExpirationDate($nextWeek);
  1002. self::invokePrivate($this->manager, 'validateExpirationDateInternal', [$share]);
  1003. $save->sub(new \DateInterval('P2D'));
  1004. $this->assertEquals($save, $share->getExpirationDate());
  1005. }
  1006. /**
  1007. * @dataProvider validateExpirationDateInternalProvider
  1008. */
  1009. public function testValidateExpirationDateInternalHookException($shareType) {
  1010. $this->expectException(\Exception::class);
  1011. $this->expectExceptionMessage('Invalid date!');
  1012. $nextWeek = new \DateTime();
  1013. $nextWeek->add(new \DateInterval('P7D'));
  1014. $nextWeek->setTime(0, 0, 0);
  1015. $share = $this->manager->newShare();
  1016. $share->setShareType($shareType);
  1017. $share->setExpirationDate($nextWeek);
  1018. $hookListener = $this->getMockBuilder('Dummy')->setMethods(['listener'])->getMock();
  1019. \OCP\Util::connectHook('\OC\Share', 'verifyExpirationDate', $hookListener, 'listener');
  1020. $hookListener->expects($this->once())->method('listener')->willReturnCallback(function ($data) {
  1021. $data['accepted'] = false;
  1022. $data['message'] = 'Invalid date!';
  1023. });
  1024. self::invokePrivate($this->manager, 'validateExpirationDateInternal', [$share]);
  1025. }
  1026. /**
  1027. * @dataProvider validateExpirationDateInternalProvider
  1028. */
  1029. public function testValidateExpirationDateInternalExistingShareNoDefault($shareType) {
  1030. $share = $this->manager->newShare();
  1031. $share->setShareType($shareType);
  1032. $share->setId('42')->setProviderId('foo');
  1033. if ($shareType === IShare::TYPE_USER) {
  1034. $this->config->method('getAppValue')
  1035. ->willReturnMap([
  1036. ['core', 'shareapi_default_internal_expire_date', 'no', 'yes'],
  1037. ['core', 'shareapi_internal_expire_after_n_days', '7', '6'],
  1038. ]);
  1039. } else {
  1040. $this->config->method('getAppValue')
  1041. ->willReturnMap([
  1042. ['core', 'shareapi_default_remote_expire_date', 'no', 'yes'],
  1043. ['core', 'shareapi_remote_expire_after_n_days', '7', '6'],
  1044. ]);
  1045. }
  1046. self::invokePrivate($this->manager, 'validateExpirationDateInternal', [$share]);
  1047. $this->assertEquals(null, $share->getExpirationDate());
  1048. }
  1049. public function testValidateExpirationDateInPast() {
  1050. $this->expectException(\OCP\Share\Exceptions\GenericShareException::class);
  1051. $this->expectExceptionMessage('Expiration date is in the past');
  1052. // Expire date in the past
  1053. $past = new \DateTime();
  1054. $past->sub(new \DateInterval('P1D'));
  1055. $share = $this->manager->newShare();
  1056. $share->setExpirationDate($past);
  1057. self::invokePrivate($this->manager, 'validateExpirationDateLink', [$share]);
  1058. }
  1059. public function testValidateExpirationDateEnforceButNotSet() {
  1060. $this->expectException(\InvalidArgumentException::class);
  1061. $this->expectExceptionMessage('Expiration date is enforced');
  1062. $share = $this->manager->newShare();
  1063. $share->setProviderId('foo')->setId('bar');
  1064. $this->config->method('getAppValue')
  1065. ->willReturnMap([
  1066. ['core', 'shareapi_default_expire_date', 'no', 'yes'],
  1067. ['core', 'shareapi_enforce_expire_date', 'no', 'yes'],
  1068. ]);
  1069. self::invokePrivate($this->manager, 'validateExpirationDateLink', [$share]);
  1070. }
  1071. public function testValidateExpirationDateEnforceButNotEnabledAndNotSet() {
  1072. $share = $this->manager->newShare();
  1073. $share->setProviderId('foo')->setId('bar');
  1074. $this->config->method('getAppValue')
  1075. ->willReturnMap([
  1076. ['core', 'shareapi_enforce_expire_date', 'no', 'yes'],
  1077. ]);
  1078. self::invokePrivate($this->manager, 'validateExpirationDateLink', [$share]);
  1079. $this->assertNull($share->getExpirationDate());
  1080. }
  1081. public function testValidateExpirationDateEnforceButNotSetNewShare() {
  1082. $share = $this->manager->newShare();
  1083. $this->config->method('getAppValue')
  1084. ->willReturnMap([
  1085. ['core', 'shareapi_enforce_expire_date', 'no', 'yes'],
  1086. ['core', 'shareapi_expire_after_n_days', '7', '3'],
  1087. ['core', 'shareapi_default_expire_date', 'no', 'yes'],
  1088. ['core', 'link_defaultExpDays', 3, '3'],
  1089. ]);
  1090. $expected = new \DateTime();
  1091. $expected->setTime(0, 0, 0);
  1092. $expected->add(new \DateInterval('P3D'));
  1093. self::invokePrivate($this->manager, 'validateExpirationDateLink', [$share]);
  1094. $this->assertNotNull($share->getExpirationDate());
  1095. $this->assertEquals($expected, $share->getExpirationDate());
  1096. }
  1097. public function testValidateExpirationDateEnforceRelaxedDefaultButNotSetNewShare() {
  1098. $share = $this->manager->newShare();
  1099. $this->config->method('getAppValue')
  1100. ->willReturnMap([
  1101. ['core', 'shareapi_enforce_expire_date', 'no', 'yes'],
  1102. ['core', 'shareapi_expire_after_n_days', '7', '3'],
  1103. ['core', 'shareapi_default_expire_date', 'no', 'yes'],
  1104. ['core', 'link_defaultExpDays', 3, '1'],
  1105. ]);
  1106. $expected = new \DateTime();
  1107. $expected->setTime(0, 0, 0);
  1108. $expected->add(new \DateInterval('P1D'));
  1109. self::invokePrivate($this->manager, 'validateExpirationDateLink', [$share]);
  1110. $this->assertNotNull($share->getExpirationDate());
  1111. $this->assertEquals($expected, $share->getExpirationDate());
  1112. }
  1113. public function testValidateExpirationDateEnforceTooFarIntoFuture() {
  1114. $this->expectException(\OCP\Share\Exceptions\GenericShareException::class);
  1115. $this->expectExceptionMessage('Cannot set expiration date more than 3 days in the future');
  1116. $future = new \DateTime();
  1117. $future->add(new \DateInterval('P7D'));
  1118. $share = $this->manager->newShare();
  1119. $share->setExpirationDate($future);
  1120. $this->config->method('getAppValue')
  1121. ->willReturnMap([
  1122. ['core', 'shareapi_enforce_expire_date', 'no', 'yes'],
  1123. ['core', 'shareapi_expire_after_n_days', '7', '3'],
  1124. ['core', 'shareapi_default_expire_date', 'no', 'yes'],
  1125. ]);
  1126. self::invokePrivate($this->manager, 'validateExpirationDateLink', [$share]);
  1127. }
  1128. public function testValidateExpirationDateEnforceValid() {
  1129. $future = new \DateTime();
  1130. $future->add(new \DateInterval('P2D'));
  1131. $future->setTime(1, 2, 3);
  1132. $expected = clone $future;
  1133. $expected->setTime(0, 0, 0);
  1134. $share = $this->manager->newShare();
  1135. $share->setExpirationDate($future);
  1136. $this->config->method('getAppValue')
  1137. ->willReturnMap([
  1138. ['core', 'shareapi_enforce_expire_date', 'no', 'yes'],
  1139. ['core', 'shareapi_expire_after_n_days', '7', '3'],
  1140. ['core', 'shareapi_default_expire_date', 'no', 'yes'],
  1141. ]);
  1142. $hookListener = $this->getMockBuilder('Dummy')->setMethods(['listener'])->getMock();
  1143. \OCP\Util::connectHook('\OC\Share', 'verifyExpirationDate', $hookListener, 'listener');
  1144. $hookListener->expects($this->once())->method('listener')->with($this->callback(function ($data) use ($future) {
  1145. return $data['expirationDate'] == $future;
  1146. }));
  1147. self::invokePrivate($this->manager, 'validateExpirationDateLink', [$share]);
  1148. $this->assertEquals($expected, $share->getExpirationDate());
  1149. }
  1150. public function testValidateExpirationDateNoDefault() {
  1151. $date = new \DateTime();
  1152. $date->add(new \DateInterval('P5D'));
  1153. $date->setTime(1, 2, 3);
  1154. $expected = clone $date;
  1155. $expected->setTime(0, 0, 0);
  1156. $share = $this->manager->newShare();
  1157. $share->setExpirationDate($date);
  1158. $hookListener = $this->getMockBuilder('Dummy')->setMethods(['listener'])->getMock();
  1159. \OCP\Util::connectHook('\OC\Share', 'verifyExpirationDate', $hookListener, 'listener');
  1160. $hookListener->expects($this->once())->method('listener')->with($this->callback(function ($data) use ($expected) {
  1161. return $data['expirationDate'] == $expected && $data['passwordSet'] === false;
  1162. }));
  1163. self::invokePrivate($this->manager, 'validateExpirationDateLink', [$share]);
  1164. $this->assertEquals($expected, $share->getExpirationDate());
  1165. }
  1166. public function testValidateExpirationDateNoDateNoDefault() {
  1167. $hookListener = $this->getMockBuilder('Dummy')->setMethods(['listener'])->getMock();
  1168. \OCP\Util::connectHook('\OC\Share', 'verifyExpirationDate', $hookListener, 'listener');
  1169. $hookListener->expects($this->once())->method('listener')->with($this->callback(function ($data) {
  1170. return $data['expirationDate'] === null && $data['passwordSet'] === true;
  1171. }));
  1172. $share = $this->manager->newShare();
  1173. $share->setPassword('password');
  1174. self::invokePrivate($this->manager, 'validateExpirationDateLink', [$share]);
  1175. $this->assertNull($share->getExpirationDate());
  1176. }
  1177. public function testValidateExpirationDateNoDateDefault() {
  1178. $share = $this->manager->newShare();
  1179. $expected = new \DateTime();
  1180. $expected->add(new \DateInterval('P3D'));
  1181. $expected->setTime(0, 0, 0);
  1182. $this->config->method('getAppValue')
  1183. ->willReturnMap([
  1184. ['core', 'shareapi_default_expire_date', 'no', 'yes'],
  1185. ['core', 'shareapi_expire_after_n_days', '7', '3'],
  1186. ['core', 'link_defaultExpDays', 3, '3'],
  1187. ]);
  1188. $hookListener = $this->getMockBuilder('Dummy')->setMethods(['listener'])->getMock();
  1189. \OCP\Util::connectHook('\OC\Share', 'verifyExpirationDate', $hookListener, 'listener');
  1190. $hookListener->expects($this->once())->method('listener')->with($this->callback(function ($data) use ($expected) {
  1191. return $data['expirationDate'] == $expected;
  1192. }));
  1193. self::invokePrivate($this->manager, 'validateExpirationDateLink', [$share]);
  1194. $this->assertEquals($expected, $share->getExpirationDate());
  1195. }
  1196. public function testValidateExpirationDateDefault() {
  1197. $future = new \DateTime();
  1198. $future->add(new \DateInterval('P5D'));
  1199. $future->setTime(1, 2, 3);
  1200. $expected = clone $future;
  1201. $expected->setTime(0, 0, 0);
  1202. $share = $this->manager->newShare();
  1203. $share->setExpirationDate($future);
  1204. $this->config->method('getAppValue')
  1205. ->willReturnMap([
  1206. ['core', 'shareapi_default_expire_date', 'no', 'yes'],
  1207. ['core', 'shareapi_expire_after_n_days', '7', '3'],
  1208. ['core', 'link_defaultExpDays', 3, '1'],
  1209. ]);
  1210. $hookListener = $this->getMockBuilder('Dummy')->setMethods(['listener'])->getMock();
  1211. \OCP\Util::connectHook('\OC\Share', 'verifyExpirationDate', $hookListener, 'listener');
  1212. $hookListener->expects($this->once())->method('listener')->with($this->callback(function ($data) use ($expected) {
  1213. return $data['expirationDate'] == $expected;
  1214. }));
  1215. self::invokePrivate($this->manager, 'validateExpirationDateLink', [$share]);
  1216. $this->assertEquals($expected, $share->getExpirationDate());
  1217. }
  1218. public function testValidateExpirationDateHookModification() {
  1219. $nextWeek = new \DateTime();
  1220. $nextWeek->add(new \DateInterval('P7D'));
  1221. $nextWeek->setTime(0, 0, 0);
  1222. $save = clone $nextWeek;
  1223. $hookListener = $this->getMockBuilder('Dummy')->setMethods(['listener'])->getMock();
  1224. \OCP\Util::connectHook('\OC\Share', 'verifyExpirationDate', $hookListener, 'listener');
  1225. $hookListener->expects($this->once())->method('listener')->willReturnCallback(function ($data) {
  1226. $data['expirationDate']->sub(new \DateInterval('P2D'));
  1227. });
  1228. $share = $this->manager->newShare();
  1229. $share->setExpirationDate($nextWeek);
  1230. self::invokePrivate($this->manager, 'validateExpirationDateLink', [$share]);
  1231. $save->sub(new \DateInterval('P2D'));
  1232. $this->assertEquals($save, $share->getExpirationDate());
  1233. }
  1234. public function testValidateExpirationDateHookException() {
  1235. $this->expectException(\Exception::class);
  1236. $this->expectExceptionMessage('Invalid date!');
  1237. $nextWeek = new \DateTime();
  1238. $nextWeek->add(new \DateInterval('P7D'));
  1239. $nextWeek->setTime(0, 0, 0);
  1240. $share = $this->manager->newShare();
  1241. $share->setExpirationDate($nextWeek);
  1242. $hookListener = $this->getMockBuilder('Dummy')->setMethods(['listener'])->getMock();
  1243. \OCP\Util::connectHook('\OC\Share', 'verifyExpirationDate', $hookListener, 'listener');
  1244. $hookListener->expects($this->once())->method('listener')->willReturnCallback(function ($data) {
  1245. $data['accepted'] = false;
  1246. $data['message'] = 'Invalid date!';
  1247. });
  1248. self::invokePrivate($this->manager, 'validateExpirationDateLink', [$share]);
  1249. }
  1250. public function testValidateExpirationDateExistingShareNoDefault() {
  1251. $share = $this->manager->newShare();
  1252. $share->setId('42')->setProviderId('foo');
  1253. $this->config->method('getAppValue')
  1254. ->willReturnMap([
  1255. ['core', 'shareapi_default_expire_date', 'no', 'yes'],
  1256. ['core', 'shareapi_expire_after_n_days', '7', '6'],
  1257. ]);
  1258. self::invokePrivate($this->manager, 'validateExpirationDateLink', [$share]);
  1259. $this->assertEquals(null, $share->getExpirationDate());
  1260. }
  1261. public function testUserCreateChecksShareWithGroupMembersOnlyDifferentGroups() {
  1262. $this->expectException(\Exception::class);
  1263. $this->expectExceptionMessage('Sharing is only allowed with group members');
  1264. $share = $this->manager->newShare();
  1265. $sharedBy = $this->createMock(IUser::class);
  1266. $sharedWith = $this->createMock(IUser::class);
  1267. $share->setSharedBy('sharedBy')->setSharedWith('sharedWith');
  1268. $this->groupManager
  1269. ->method('getUserGroupIds')
  1270. ->willReturnMap(
  1271. [
  1272. [$sharedBy, ['group1']],
  1273. [$sharedWith, ['group2']],
  1274. ]
  1275. );
  1276. $this->userManager->method('get')->willReturnMap([
  1277. ['sharedBy', $sharedBy],
  1278. ['sharedWith', $sharedWith],
  1279. ]);
  1280. $this->config
  1281. ->method('getAppValue')
  1282. ->willReturnMap([
  1283. ['core', 'shareapi_only_share_with_group_members', 'no', 'yes'],
  1284. ]);
  1285. self::invokePrivate($this->manager, 'userCreateChecks', [$share]);
  1286. }
  1287. public function testUserCreateChecksShareWithGroupMembersOnlySharedGroup() {
  1288. $share = $this->manager->newShare();
  1289. $sharedBy = $this->createMock(IUser::class);
  1290. $sharedWith = $this->createMock(IUser::class);
  1291. $share->setSharedBy('sharedBy')->setSharedWith('sharedWith');
  1292. $path = $this->createMock(Node::class);
  1293. $share->setNode($path);
  1294. $this->groupManager
  1295. ->method('getUserGroupIds')
  1296. ->willReturnMap(
  1297. [
  1298. [$sharedBy, ['group1', 'group3']],
  1299. [$sharedWith, ['group2', 'group3']],
  1300. ]
  1301. );
  1302. $this->userManager->method('get')->willReturnMap([
  1303. ['sharedBy', $sharedBy],
  1304. ['sharedWith', $sharedWith],
  1305. ]);
  1306. $this->config
  1307. ->method('getAppValue')
  1308. ->willReturnMap([
  1309. ['core', 'shareapi_only_share_with_group_members', 'no', 'yes'],
  1310. ]);
  1311. $this->defaultProvider
  1312. ->method('getSharesByPath')
  1313. ->with($path)
  1314. ->willReturn([]);
  1315. self::invokePrivate($this->manager, 'userCreateChecks', [$share]);
  1316. $this->addToAssertionCount(1);
  1317. }
  1318. public function testUserCreateChecksIdenticalShareExists() {
  1319. $this->expectException(AlreadySharedException::class);
  1320. $this->expectExceptionMessage('Sharing name.txt failed, because this item is already shared with user user');
  1321. $share = $this->manager->newShare();
  1322. $share->setSharedWithDisplayName('user');
  1323. $share2 = $this->manager->newShare();
  1324. $sharedWith = $this->createMock(IUser::class);
  1325. $path = $this->createMock(Node::class);
  1326. $share->setSharedWith('sharedWith')->setNode($path)
  1327. ->setProviderId('foo')->setId('bar');
  1328. $share2->setSharedWith('sharedWith')->setNode($path)
  1329. ->setProviderId('foo')->setId('baz');
  1330. $this->defaultProvider
  1331. ->method('getSharesByPath')
  1332. ->with($path)
  1333. ->willReturn([$share2]);
  1334. $path->method('getName')
  1335. ->willReturn('name.txt');
  1336. self::invokePrivate($this->manager, 'userCreateChecks', [$share]);
  1337. }
  1338. public function testUserCreateChecksIdenticalPathSharedViaGroup() {
  1339. $this->expectException(AlreadySharedException::class);
  1340. $this->expectExceptionMessage('Sharing name2.txt failed, because this item is already shared with user userName');
  1341. $share = $this->manager->newShare();
  1342. $sharedWith = $this->createMock(IUser::class);
  1343. $sharedWith->method('getUID')->willReturn('sharedWith');
  1344. $this->userManager->method('get')->with('sharedWith')->willReturn($sharedWith);
  1345. $path = $this->createMock(Node::class);
  1346. $share->setSharedWith('sharedWith')
  1347. ->setNode($path)
  1348. ->setShareOwner('shareOwner')
  1349. ->setSharedWithDisplayName('userName')
  1350. ->setProviderId('foo')
  1351. ->setId('bar');
  1352. $share2 = $this->manager->newShare();
  1353. $share2->setShareType(IShare::TYPE_GROUP)
  1354. ->setShareOwner('shareOwner2')
  1355. ->setProviderId('foo')
  1356. ->setId('baz')
  1357. ->setSharedWith('group');
  1358. $group = $this->createMock(IGroup::class);
  1359. $group->method('inGroup')
  1360. ->with($sharedWith)
  1361. ->willReturn(true);
  1362. $this->groupManager->method('get')->with('group')->willReturn($group);
  1363. $this->defaultProvider
  1364. ->method('getSharesByPath')
  1365. ->with($path)
  1366. ->willReturn([$share2]);
  1367. $path->method('getName')
  1368. ->willReturn('name2.txt');
  1369. self::invokePrivate($this->manager, 'userCreateChecks', [$share]);
  1370. }
  1371. public function testUserCreateChecksIdenticalPathSharedViaDeletedGroup() {
  1372. $share = $this->manager->newShare();
  1373. $sharedWith = $this->createMock(IUser::class);
  1374. $sharedWith->method('getUID')->willReturn('sharedWith');
  1375. $this->userManager->method('get')->with('sharedWith')->willReturn($sharedWith);
  1376. $path = $this->createMock(Node::class);
  1377. $share->setSharedWith('sharedWith')
  1378. ->setNode($path)
  1379. ->setShareOwner('shareOwner')
  1380. ->setProviderId('foo')
  1381. ->setId('bar');
  1382. $share2 = $this->manager->newShare();
  1383. $share2->setShareType(IShare::TYPE_GROUP)
  1384. ->setShareOwner('shareOwner2')
  1385. ->setProviderId('foo')
  1386. ->setId('baz')
  1387. ->setSharedWith('group');
  1388. $this->groupManager->method('get')->with('group')->willReturn(null);
  1389. $this->defaultProvider
  1390. ->method('getSharesByPath')
  1391. ->with($path)
  1392. ->willReturn([$share2]);
  1393. $this->assertNull($this->invokePrivate($this->manager, 'userCreateChecks', [$share]));
  1394. }
  1395. public function testUserCreateChecksIdenticalPathNotSharedWithUser() {
  1396. $share = $this->manager->newShare();
  1397. $sharedWith = $this->createMock(IUser::class);
  1398. $path = $this->createMock(Node::class);
  1399. $share->setSharedWith('sharedWith')
  1400. ->setNode($path)
  1401. ->setShareOwner('shareOwner')
  1402. ->setProviderId('foo')
  1403. ->setId('bar');
  1404. $this->userManager->method('get')->with('sharedWith')->willReturn($sharedWith);
  1405. $share2 = $this->manager->newShare();
  1406. $share2->setShareType(IShare::TYPE_GROUP)
  1407. ->setShareOwner('shareOwner2')
  1408. ->setProviderId('foo')
  1409. ->setId('baz');
  1410. $group = $this->createMock(IGroup::class);
  1411. $group->method('inGroup')
  1412. ->with($sharedWith)
  1413. ->willReturn(false);
  1414. $this->groupManager->method('get')->with('group')->willReturn($group);
  1415. $share2->setSharedWith('group');
  1416. $this->defaultProvider
  1417. ->method('getSharesByPath')
  1418. ->with($path)
  1419. ->willReturn([$share2]);
  1420. self::invokePrivate($this->manager, 'userCreateChecks', [$share]);
  1421. $this->addToAssertionCount(1);
  1422. }
  1423. public function testGroupCreateChecksShareWithGroupMembersGroupSharingNotAllowed() {
  1424. $this->expectException(\Exception::class);
  1425. $this->expectExceptionMessage('Group sharing is now allowed');
  1426. $share = $this->manager->newShare();
  1427. $this->config
  1428. ->method('getAppValue')
  1429. ->willReturnMap([
  1430. ['core', 'shareapi_allow_group_sharing', 'yes', 'no'],
  1431. ]);
  1432. self::invokePrivate($this->manager, 'groupCreateChecks', [$share]);
  1433. }
  1434. public function testGroupCreateChecksShareWithGroupMembersOnlyNotInGroup() {
  1435. $this->expectException(\Exception::class);
  1436. $this->expectExceptionMessage('Sharing is only allowed within your own groups');
  1437. $share = $this->manager->newShare();
  1438. $user = $this->createMock(IUser::class);
  1439. $group = $this->createMock(IGroup::class);
  1440. $share->setSharedBy('user')->setSharedWith('group');
  1441. $group->method('inGroup')->with($user)->willReturn(false);
  1442. $this->groupManager->method('get')->with('group')->willReturn($group);
  1443. $this->userManager->method('get')->with('user')->willReturn($user);
  1444. $this->config
  1445. ->method('getAppValue')
  1446. ->willReturnMap([
  1447. ['core', 'shareapi_only_share_with_group_members', 'no', 'yes'],
  1448. ['core', 'shareapi_allow_group_sharing', 'yes', 'yes'],
  1449. ]);
  1450. self::invokePrivate($this->manager, 'groupCreateChecks', [$share]);
  1451. }
  1452. public function testGroupCreateChecksShareWithGroupMembersOnlyNullGroup() {
  1453. $this->expectException(\Exception::class);
  1454. $this->expectExceptionMessage('Sharing is only allowed within your own groups');
  1455. $share = $this->manager->newShare();
  1456. $user = $this->createMock(IUser::class);
  1457. $share->setSharedBy('user')->setSharedWith('group');
  1458. $this->groupManager->method('get')->with('group')->willReturn(null);
  1459. $this->userManager->method('get')->with('user')->willReturn($user);
  1460. $this->config
  1461. ->method('getAppValue')
  1462. ->willReturnMap([
  1463. ['core', 'shareapi_only_share_with_group_members', 'no', 'yes'],
  1464. ['core', 'shareapi_allow_group_sharing', 'yes', 'yes'],
  1465. ]);
  1466. $this->assertNull($this->invokePrivate($this->manager, 'groupCreateChecks', [$share]));
  1467. }
  1468. public function testGroupCreateChecksShareWithGroupMembersOnlyInGroup() {
  1469. $share = $this->manager->newShare();
  1470. $user = $this->createMock(IUser::class);
  1471. $group = $this->createMock(IGroup::class);
  1472. $share->setSharedBy('user')->setSharedWith('group');
  1473. $this->userManager->method('get')->with('user')->willReturn($user);
  1474. $this->groupManager->method('get')->with('group')->willReturn($group);
  1475. $group->method('inGroup')->with($user)->willReturn(true);
  1476. $path = $this->createMock(Node::class);
  1477. $share->setNode($path);
  1478. $this->defaultProvider->method('getSharesByPath')
  1479. ->with($path)
  1480. ->willReturn([]);
  1481. $this->config
  1482. ->method('getAppValue')
  1483. ->willReturnMap([
  1484. ['core', 'shareapi_only_share_with_group_members', 'no', 'yes'],
  1485. ['core', 'shareapi_allow_group_sharing', 'yes', 'yes'],
  1486. ]);
  1487. self::invokePrivate($this->manager, 'groupCreateChecks', [$share]);
  1488. $this->addToAssertionCount(1);
  1489. }
  1490. public function testGroupCreateChecksPathAlreadySharedWithSameGroup() {
  1491. $this->expectException(\Exception::class);
  1492. $this->expectExceptionMessage('Path is already shared with this group');
  1493. $share = $this->manager->newShare();
  1494. $path = $this->createMock(Node::class);
  1495. $share->setSharedWith('sharedWith')
  1496. ->setNode($path)
  1497. ->setProviderId('foo')
  1498. ->setId('bar');
  1499. $share2 = $this->manager->newShare();
  1500. $share2->setSharedWith('sharedWith')
  1501. ->setProviderId('foo')
  1502. ->setId('baz');
  1503. $this->defaultProvider->method('getSharesByPath')
  1504. ->with($path)
  1505. ->willReturn([$share2]);
  1506. $this->config
  1507. ->method('getAppValue')
  1508. ->willReturnMap([
  1509. ['core', 'shareapi_allow_group_sharing', 'yes', 'yes'],
  1510. ]);
  1511. self::invokePrivate($this->manager, 'groupCreateChecks', [$share]);
  1512. }
  1513. public function testGroupCreateChecksPathAlreadySharedWithDifferentGroup() {
  1514. $share = $this->manager->newShare();
  1515. $share->setSharedWith('sharedWith');
  1516. $path = $this->createMock(Node::class);
  1517. $share->setNode($path);
  1518. $share2 = $this->manager->newShare();
  1519. $share2->setSharedWith('sharedWith2');
  1520. $this->defaultProvider->method('getSharesByPath')
  1521. ->with($path)
  1522. ->willReturn([$share2]);
  1523. $this->config
  1524. ->method('getAppValue')
  1525. ->willReturnMap([
  1526. ['core', 'shareapi_allow_group_sharing', 'yes', 'yes'],
  1527. ]);
  1528. self::invokePrivate($this->manager, 'groupCreateChecks', [$share]);
  1529. $this->addToAssertionCount(1);
  1530. }
  1531. public function testLinkCreateChecksNoLinkSharesAllowed() {
  1532. $this->expectException(\Exception::class);
  1533. $this->expectExceptionMessage('Link sharing is not allowed');
  1534. $share = $this->manager->newShare();
  1535. $this->config
  1536. ->method('getAppValue')
  1537. ->willReturnMap([
  1538. ['core', 'shareapi_allow_links', 'yes', 'no'],
  1539. ]);
  1540. self::invokePrivate($this->manager, 'linkCreateChecks', [$share]);
  1541. }
  1542. public function testFileLinkCreateChecksNoPublicUpload() {
  1543. $share = $this->manager->newShare();
  1544. $share->setPermissions(\OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE);
  1545. $share->setNodeType('file');
  1546. $this->config
  1547. ->method('getAppValue')
  1548. ->willReturnMap([
  1549. ['core', 'shareapi_allow_links', 'yes', 'yes'],
  1550. ['core', 'shareapi_allow_public_upload', 'yes', 'no']
  1551. ]);
  1552. self::invokePrivate($this->manager, 'linkCreateChecks', [$share]);
  1553. $this->addToAssertionCount(1);
  1554. }
  1555. public function testFolderLinkCreateChecksNoPublicUpload() {
  1556. $this->expectException(\Exception::class);
  1557. $this->expectExceptionMessage('Public upload is not allowed');
  1558. $share = $this->manager->newShare();
  1559. $share->setPermissions(\OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE);
  1560. $share->setNodeType('folder');
  1561. $this->config
  1562. ->method('getAppValue')
  1563. ->willReturnMap([
  1564. ['core', 'shareapi_allow_links', 'yes', 'yes'],
  1565. ['core', 'shareapi_allow_public_upload', 'yes', 'no']
  1566. ]);
  1567. self::invokePrivate($this->manager, 'linkCreateChecks', [$share]);
  1568. }
  1569. public function testLinkCreateChecksPublicUpload() {
  1570. $share = $this->manager->newShare();
  1571. $share->setPermissions(\OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE);
  1572. $share->setSharedWith('sharedWith');
  1573. $folder = $this->createMock(\OC\Files\Node\Folder::class);
  1574. $share->setNode($folder);
  1575. $this->config
  1576. ->method('getAppValue')
  1577. ->willReturnMap([
  1578. ['core', 'shareapi_allow_links', 'yes', 'yes'],
  1579. ['core', 'shareapi_allow_public_upload', 'yes', 'yes']
  1580. ]);
  1581. self::invokePrivate($this->manager, 'linkCreateChecks', [$share]);
  1582. $this->addToAssertionCount(1);
  1583. }
  1584. public function testLinkCreateChecksReadOnly() {
  1585. $share = $this->manager->newShare();
  1586. $share->setPermissions(\OCP\Constants::PERMISSION_READ);
  1587. $share->setSharedWith('sharedWith');
  1588. $folder = $this->createMock(\OC\Files\Node\Folder::class);
  1589. $share->setNode($folder);
  1590. $this->config
  1591. ->method('getAppValue')
  1592. ->willReturnMap([
  1593. ['core', 'shareapi_allow_links', 'yes', 'yes'],
  1594. ['core', 'shareapi_allow_public_upload', 'yes', 'no']
  1595. ]);
  1596. self::invokePrivate($this->manager, 'linkCreateChecks', [$share]);
  1597. $this->addToAssertionCount(1);
  1598. }
  1599. public function testPathCreateChecksContainsSharedMount() {
  1600. $this->expectException(\InvalidArgumentException::class);
  1601. $this->expectExceptionMessage('Path contains files shared with you');
  1602. $path = $this->createMock(Folder::class);
  1603. $path->method('getPath')->willReturn('path');
  1604. $mount = $this->createMock(IMountPoint::class);
  1605. $storage = $this->createMock(Storage::class);
  1606. $mount->method('getStorage')->willReturn($storage);
  1607. $storage->method('instanceOfStorage')->with('\OCA\Files_Sharing\ISharedStorage')->willReturn(true);
  1608. $this->mountManager->method('findIn')->with('path')->willReturn([$mount]);
  1609. self::invokePrivate($this->manager, 'pathCreateChecks', [$path]);
  1610. }
  1611. public function testPathCreateChecksContainsNoSharedMount() {
  1612. $path = $this->createMock(Folder::class);
  1613. $path->method('getPath')->willReturn('path');
  1614. $mount = $this->createMock(IMountPoint::class);
  1615. $storage = $this->createMock(Storage::class);
  1616. $mount->method('getStorage')->willReturn($storage);
  1617. $storage->method('instanceOfStorage')->with('\OCA\Files_Sharing\ISharedStorage')->willReturn(false);
  1618. $this->mountManager->method('findIn')->with('path')->willReturn([$mount]);
  1619. self::invokePrivate($this->manager, 'pathCreateChecks', [$path]);
  1620. $this->addToAssertionCount(1);
  1621. }
  1622. public function testPathCreateChecksContainsNoFolder() {
  1623. $path = $this->createMock(File::class);
  1624. self::invokePrivate($this->manager, 'pathCreateChecks', [$path]);
  1625. $this->addToAssertionCount(1);
  1626. }
  1627. public function dataIsSharingDisabledForUser() {
  1628. $data = [];
  1629. // No exclude groups
  1630. $data[] = ['no', null, null, [], false];
  1631. // empty exclude list, user no groups
  1632. $data[] = ['yes', '', json_encode(['']), [], false];
  1633. // empty exclude list, user groups
  1634. $data[] = ['yes', '', json_encode(['']), ['group1', 'group2'], false];
  1635. // Convert old list to json
  1636. $data[] = ['yes', 'group1,group2', json_encode(['group1', 'group2']), [], false];
  1637. // Old list partly groups in common
  1638. $data[] = ['yes', 'group1,group2', json_encode(['group1', 'group2']), ['group1', 'group3'], false];
  1639. // Old list only groups in common
  1640. $data[] = ['yes', 'group1,group2', json_encode(['group1', 'group2']), ['group1'], true];
  1641. // New list partly in common
  1642. $data[] = ['yes', json_encode(['group1', 'group2']), null, ['group1', 'group3'], false];
  1643. // New list only groups in common
  1644. $data[] = ['yes', json_encode(['group1', 'group2']), null, ['group2'], true];
  1645. return $data;
  1646. }
  1647. /**
  1648. * @dataProvider dataIsSharingDisabledForUser
  1649. *
  1650. * @param string $excludeGroups
  1651. * @param string $groupList
  1652. * @param string $setList
  1653. * @param string[] $groupIds
  1654. * @param bool $expected
  1655. */
  1656. public function testIsSharingDisabledForUser($excludeGroups, $groupList, $setList, $groupIds, $expected) {
  1657. $user = $this->createMock(IUser::class);
  1658. $this->config->method('getAppValue')
  1659. ->willReturnMap([
  1660. ['core', 'shareapi_exclude_groups', 'no', $excludeGroups],
  1661. ['core', 'shareapi_exclude_groups_list', '', $groupList],
  1662. ]);
  1663. if ($setList !== null) {
  1664. $this->config->expects($this->once())
  1665. ->method('setAppValue')
  1666. ->with('core', 'shareapi_exclude_groups_list', $setList);
  1667. } else {
  1668. $this->config->expects($this->never())
  1669. ->method('setAppValue');
  1670. }
  1671. $this->groupManager->method('getUserGroupIds')
  1672. ->with($user)
  1673. ->willReturn($groupIds);
  1674. $this->userManager->method('get')->with('user')->willReturn($user);
  1675. $res = $this->manager->sharingDisabledForUser('user');
  1676. $this->assertEquals($expected, $res);
  1677. }
  1678. public function dataCanShare() {
  1679. $data = [];
  1680. /*
  1681. * [expected, sharing enabled, disabled for user]
  1682. */
  1683. $data[] = [false, 'no', false];
  1684. $data[] = [false, 'no', true];
  1685. $data[] = [true, 'yes', false];
  1686. $data[] = [false, 'yes', true];
  1687. return $data;
  1688. }
  1689. /**
  1690. * @dataProvider dataCanShare
  1691. *
  1692. * @param bool $expected
  1693. * @param string $sharingEnabled
  1694. * @param bool $disabledForUser
  1695. */
  1696. public function testCanShare($expected, $sharingEnabled, $disabledForUser) {
  1697. $this->config->method('getAppValue')
  1698. ->willReturnMap([
  1699. ['core', 'shareapi_enabled', 'yes', $sharingEnabled],
  1700. ]);
  1701. $manager = $this->createManagerMock()
  1702. ->setMethods(['sharingDisabledForUser'])
  1703. ->getMock();
  1704. $manager->method('sharingDisabledForUser')
  1705. ->with('user')
  1706. ->willReturn($disabledForUser);
  1707. $share = $this->manager->newShare();
  1708. $share->setSharedBy('user');
  1709. $exception = false;
  1710. try {
  1711. $res = self::invokePrivate($manager, 'canShare', [$share]);
  1712. } catch (\Exception $e) {
  1713. $exception = true;
  1714. }
  1715. $this->assertEquals($expected, !$exception);
  1716. }
  1717. public function testCreateShareUser() {
  1718. $manager = $this->createManagerMock()
  1719. ->setMethods(['canShare', 'generalCreateChecks', 'userCreateChecks', 'pathCreateChecks'])
  1720. ->getMock();
  1721. $shareOwner = $this->createMock(IUser::class);
  1722. $shareOwner->method('getUID')->willReturn('shareOwner');
  1723. $storage = $this->createMock(Storage::class);
  1724. $path = $this->createMock(File::class);
  1725. $path->method('getOwner')->willReturn($shareOwner);
  1726. $path->method('getName')->willReturn('target');
  1727. $path->method('getStorage')->willReturn($storage);
  1728. $share = $this->createShare(
  1729. null,
  1730. IShare::TYPE_USER,
  1731. $path,
  1732. 'sharedWith',
  1733. 'sharedBy',
  1734. null,
  1735. \OCP\Constants::PERMISSION_ALL);
  1736. $manager->expects($this->once())
  1737. ->method('canShare')
  1738. ->with($share)
  1739. ->willReturn(true);
  1740. $manager->expects($this->once())
  1741. ->method('generalCreateChecks')
  1742. ->with($share);
  1743. ;
  1744. $manager->expects($this->once())
  1745. ->method('userCreateChecks')
  1746. ->with($share);
  1747. ;
  1748. $manager->expects($this->once())
  1749. ->method('pathCreateChecks')
  1750. ->with($path);
  1751. $this->defaultProvider
  1752. ->expects($this->once())
  1753. ->method('create')
  1754. ->with($share)
  1755. ->willReturnArgument(0);
  1756. $share->expects($this->once())
  1757. ->method('setShareOwner')
  1758. ->with('shareOwner');
  1759. $share->expects($this->once())
  1760. ->method('setTarget')
  1761. ->with('/target');
  1762. $manager->createShare($share);
  1763. }
  1764. public function testCreateShareGroup() {
  1765. $manager = $this->createManagerMock()
  1766. ->setMethods(['canShare', 'generalCreateChecks', 'groupCreateChecks', 'pathCreateChecks'])
  1767. ->getMock();
  1768. $shareOwner = $this->createMock(IUser::class);
  1769. $shareOwner->method('getUID')->willReturn('shareOwner');
  1770. $storage = $this->createMock(Storage::class);
  1771. $path = $this->createMock(File::class);
  1772. $path->method('getOwner')->willReturn($shareOwner);
  1773. $path->method('getName')->willReturn('target');
  1774. $path->method('getStorage')->willReturn($storage);
  1775. $share = $this->createShare(
  1776. null,
  1777. IShare::TYPE_GROUP,
  1778. $path,
  1779. 'sharedWith',
  1780. 'sharedBy',
  1781. null,
  1782. \OCP\Constants::PERMISSION_ALL);
  1783. $manager->expects($this->once())
  1784. ->method('canShare')
  1785. ->with($share)
  1786. ->willReturn(true);
  1787. $manager->expects($this->once())
  1788. ->method('generalCreateChecks')
  1789. ->with($share);
  1790. ;
  1791. $manager->expects($this->once())
  1792. ->method('groupCreateChecks')
  1793. ->with($share);
  1794. ;
  1795. $manager->expects($this->once())
  1796. ->method('pathCreateChecks')
  1797. ->with($path);
  1798. $this->defaultProvider
  1799. ->expects($this->once())
  1800. ->method('create')
  1801. ->with($share)
  1802. ->willReturnArgument(0);
  1803. $share->expects($this->once())
  1804. ->method('setShareOwner')
  1805. ->with('shareOwner');
  1806. $share->expects($this->once())
  1807. ->method('setTarget')
  1808. ->with('/target');
  1809. $manager->createShare($share);
  1810. }
  1811. public function testCreateShareLink() {
  1812. $manager = $this->createManagerMock()
  1813. ->setMethods([
  1814. 'canShare',
  1815. 'generalCreateChecks',
  1816. 'linkCreateChecks',
  1817. 'pathCreateChecks',
  1818. 'validateExpirationDateLink',
  1819. 'verifyPassword',
  1820. 'setLinkParent',
  1821. ])
  1822. ->getMock();
  1823. $shareOwner = $this->createMock(IUser::class);
  1824. $shareOwner->method('getUID')->willReturn('shareOwner');
  1825. $storage = $this->createMock(Storage::class);
  1826. $path = $this->createMock(File::class);
  1827. $path->method('getOwner')->willReturn($shareOwner);
  1828. $path->method('getName')->willReturn('target');
  1829. $path->method('getId')->willReturn(1);
  1830. $path->method('getStorage')->willReturn($storage);
  1831. $date = new \DateTime();
  1832. $share = $this->manager->newShare();
  1833. $share->setShareType(IShare::TYPE_LINK)
  1834. ->setNode($path)
  1835. ->setSharedBy('sharedBy')
  1836. ->setPermissions(\OCP\Constants::PERMISSION_ALL)
  1837. ->setExpirationDate($date)
  1838. ->setPassword('password');
  1839. $manager->expects($this->once())
  1840. ->method('canShare')
  1841. ->with($share)
  1842. ->willReturn(true);
  1843. $manager->expects($this->once())
  1844. ->method('generalCreateChecks')
  1845. ->with($share);
  1846. ;
  1847. $manager->expects($this->once())
  1848. ->method('linkCreateChecks')
  1849. ->with($share);
  1850. ;
  1851. $manager->expects($this->once())
  1852. ->method('pathCreateChecks')
  1853. ->with($path);
  1854. $manager->expects($this->once())
  1855. ->method('validateExpirationDateLink')
  1856. ->with($share)
  1857. ->willReturn($share);
  1858. $manager->expects($this->once())
  1859. ->method('verifyPassword')
  1860. ->with('password');
  1861. $manager->expects($this->once())
  1862. ->method('setLinkParent')
  1863. ->with($share);
  1864. $this->hasher->expects($this->once())
  1865. ->method('hash')
  1866. ->with('password')
  1867. ->willReturn('hashed');
  1868. $this->secureRandom->method('generate')
  1869. ->willReturn('token');
  1870. $this->defaultProvider
  1871. ->expects($this->once())
  1872. ->method('create')
  1873. ->with($share)
  1874. ->willReturnCallback(function (Share $share) {
  1875. return $share->setId(42);
  1876. });
  1877. // Pre share
  1878. $this->eventDispatcher->expects($this->at(0))
  1879. ->method('dispatch')
  1880. ->with(
  1881. $this->equalTo('OCP\Share::preShare'),
  1882. $this->callback(function (GenericEvent $e) use ($path, $date) {
  1883. /** @var IShare $share */
  1884. $share = $e->getSubject();
  1885. return $share->getShareType() === IShare::TYPE_LINK &&
  1886. $share->getNode() === $path &&
  1887. $share->getSharedBy() === 'sharedBy' &&
  1888. $share->getPermissions() === \OCP\Constants::PERMISSION_ALL &&
  1889. $share->getExpirationDate() === $date &&
  1890. $share->getPassword() === 'hashed' &&
  1891. $share->getToken() === 'token';
  1892. })
  1893. );
  1894. // Post share
  1895. $this->eventDispatcher->expects($this->at(1))
  1896. ->method('dispatch')
  1897. ->with(
  1898. $this->equalTo('OCP\Share::postShare'),
  1899. $this->callback(function (GenericEvent $e) use ($path, $date) {
  1900. /** @var IShare $share */
  1901. $share = $e->getSubject();
  1902. return $share->getShareType() === IShare::TYPE_LINK &&
  1903. $share->getNode() === $path &&
  1904. $share->getSharedBy() === 'sharedBy' &&
  1905. $share->getPermissions() === \OCP\Constants::PERMISSION_ALL &&
  1906. $share->getExpirationDate() === $date &&
  1907. $share->getPassword() === 'hashed' &&
  1908. $share->getToken() === 'token' &&
  1909. $share->getId() === '42' &&
  1910. $share->getTarget() === '/target';
  1911. })
  1912. );
  1913. /** @var IShare $share */
  1914. $share = $manager->createShare($share);
  1915. $this->assertSame('shareOwner', $share->getShareOwner());
  1916. $this->assertEquals('/target', $share->getTarget());
  1917. $this->assertSame($date, $share->getExpirationDate());
  1918. $this->assertEquals('token', $share->getToken());
  1919. $this->assertEquals('hashed', $share->getPassword());
  1920. }
  1921. public function testCreateShareMail() {
  1922. $manager = $this->createManagerMock()
  1923. ->setMethods([
  1924. 'canShare',
  1925. 'generalCreateChecks',
  1926. 'linkCreateChecks',
  1927. 'pathCreateChecks',
  1928. 'validateExpirationDateLink',
  1929. 'verifyPassword',
  1930. 'setLinkParent',
  1931. ])
  1932. ->getMock();
  1933. $shareOwner = $this->createMock(IUser::class);
  1934. $shareOwner->method('getUID')->willReturn('shareOwner');
  1935. $storage = $this->createMock(Storage::class);
  1936. $path = $this->createMock(File::class);
  1937. $path->method('getOwner')->willReturn($shareOwner);
  1938. $path->method('getName')->willReturn('target');
  1939. $path->method('getId')->willReturn(1);
  1940. $path->method('getStorage')->willReturn($storage);
  1941. $share = $this->manager->newShare();
  1942. $share->setShareType(IShare::TYPE_EMAIL)
  1943. ->setNode($path)
  1944. ->setSharedBy('sharedBy')
  1945. ->setPermissions(\OCP\Constants::PERMISSION_ALL);
  1946. $manager->expects($this->once())
  1947. ->method('canShare')
  1948. ->with($share)
  1949. ->willReturn(true);
  1950. $manager->expects($this->once())
  1951. ->method('generalCreateChecks')
  1952. ->with($share);
  1953. $manager->expects($this->once())
  1954. ->method('linkCreateChecks');
  1955. $manager->expects($this->once())
  1956. ->method('pathCreateChecks')
  1957. ->with($path);
  1958. $manager->expects($this->once())
  1959. ->method('validateExpirationDateLink')
  1960. ->with($share)
  1961. ->willReturn($share);
  1962. $manager->expects($this->once())
  1963. ->method('verifyPassword');
  1964. $manager->expects($this->once())
  1965. ->method('setLinkParent');
  1966. $this->secureRandom->method('generate')
  1967. ->willReturn('token');
  1968. $this->defaultProvider
  1969. ->expects($this->once())
  1970. ->method('create')
  1971. ->with($share)
  1972. ->willReturnCallback(function (Share $share) {
  1973. return $share->setId(42);
  1974. });
  1975. // Pre share
  1976. $this->eventDispatcher->expects($this->at(0))
  1977. ->method('dispatch')
  1978. ->with(
  1979. $this->equalTo('OCP\Share::preShare'),
  1980. $this->callback(function (GenericEvent $e) use ($path) {
  1981. /** @var IShare $share */
  1982. $share = $e->getSubject();
  1983. return $share->getShareType() === IShare::TYPE_EMAIL &&
  1984. $share->getNode() === $path &&
  1985. $share->getSharedBy() === 'sharedBy' &&
  1986. $share->getPermissions() === \OCP\Constants::PERMISSION_ALL &&
  1987. $share->getExpirationDate() === null &&
  1988. $share->getPassword() === null &&
  1989. $share->getToken() === 'token';
  1990. })
  1991. );
  1992. // Post share
  1993. $this->eventDispatcher->expects($this->at(1))
  1994. ->method('dispatch')
  1995. ->with(
  1996. $this->equalTo('OCP\Share::postShare'),
  1997. $this->callback(function (GenericEvent $e) use ($path) {
  1998. /** @var IShare $share */
  1999. $share = $e->getSubject();
  2000. return $share->getShareType() === IShare::TYPE_EMAIL &&
  2001. $share->getNode() === $path &&
  2002. $share->getSharedBy() === 'sharedBy' &&
  2003. $share->getPermissions() === \OCP\Constants::PERMISSION_ALL &&
  2004. $share->getExpirationDate() === null &&
  2005. $share->getPassword() === null &&
  2006. $share->getToken() === 'token' &&
  2007. $share->getId() === '42' &&
  2008. $share->getTarget() === '/target';
  2009. })
  2010. );
  2011. /** @var IShare $share */
  2012. $share = $manager->createShare($share);
  2013. $this->assertSame('shareOwner', $share->getShareOwner());
  2014. $this->assertEquals('/target', $share->getTarget());
  2015. $this->assertEquals('token', $share->getToken());
  2016. }
  2017. public function testCreateShareHookError() {
  2018. $this->expectException(\Exception::class);
  2019. $this->expectExceptionMessage('I won\'t let you share');
  2020. $manager = $this->createManagerMock()
  2021. ->setMethods([
  2022. 'canShare',
  2023. 'generalCreateChecks',
  2024. 'userCreateChecks',
  2025. 'pathCreateChecks',
  2026. ])
  2027. ->getMock();
  2028. $shareOwner = $this->createMock(IUser::class);
  2029. $shareOwner->method('getUID')->willReturn('shareOwner');
  2030. $storage = $this->createMock(Storage::class);
  2031. $path = $this->createMock(File::class);
  2032. $path->method('getOwner')->willReturn($shareOwner);
  2033. $path->method('getName')->willReturn('target');
  2034. $path->method('getStorage')->willReturn($storage);
  2035. $share = $this->createShare(
  2036. null,
  2037. IShare::TYPE_USER,
  2038. $path,
  2039. 'sharedWith',
  2040. 'sharedBy',
  2041. null,
  2042. \OCP\Constants::PERMISSION_ALL);
  2043. $manager->expects($this->once())
  2044. ->method('canShare')
  2045. ->with($share)
  2046. ->willReturn(true);
  2047. $manager->expects($this->once())
  2048. ->method('generalCreateChecks')
  2049. ->with($share);
  2050. ;
  2051. $manager->expects($this->once())
  2052. ->method('userCreateChecks')
  2053. ->with($share);
  2054. ;
  2055. $manager->expects($this->once())
  2056. ->method('pathCreateChecks')
  2057. ->with($path);
  2058. $share->expects($this->once())
  2059. ->method('setShareOwner')
  2060. ->with('shareOwner');
  2061. $share->expects($this->once())
  2062. ->method('setTarget')
  2063. ->with('/target');
  2064. // Pre share
  2065. $this->eventDispatcher->expects($this->once())
  2066. ->method('dispatch')
  2067. ->with(
  2068. $this->equalTo('OCP\Share::preShare'),
  2069. $this->isInstanceOf(GenericEvent::class)
  2070. )->willReturnCallback(function ($name, GenericEvent $e) {
  2071. $e->setArgument('error', 'I won\'t let you share!');
  2072. $e->stopPropagation();
  2073. }
  2074. );
  2075. $manager->createShare($share);
  2076. }
  2077. public function testCreateShareOfIncomingFederatedShare() {
  2078. $manager = $this->createManagerMock()
  2079. ->setMethods(['canShare', 'generalCreateChecks', 'userCreateChecks', 'pathCreateChecks'])
  2080. ->getMock();
  2081. $shareOwner = $this->createMock(IUser::class);
  2082. $shareOwner->method('getUID')->willReturn('shareOwner');
  2083. $storage = $this->createMock(Storage::class);
  2084. $storage->method('instanceOfStorage')
  2085. ->with('OCA\Files_Sharing\External\Storage')
  2086. ->willReturn(true);
  2087. $storage2 = $this->createMock(Storage::class);
  2088. $storage2->method('instanceOfStorage')
  2089. ->with('OCA\Files_Sharing\External\Storage')
  2090. ->willReturn(false);
  2091. $path = $this->createMock(File::class);
  2092. $path->expects($this->never())->method('getOwner');
  2093. $path->method('getName')->willReturn('target');
  2094. $path->method('getStorage')->willReturn($storage);
  2095. $parent = $this->createMock(Folder::class);
  2096. $parent->method('getStorage')->willReturn($storage);
  2097. $parentParent = $this->createMock(Folder::class);
  2098. $parentParent->method('getStorage')->willReturn($storage2);
  2099. $parentParent->method('getOwner')->willReturn($shareOwner);
  2100. $path->method('getParent')->willReturn($parent);
  2101. $parent->method('getParent')->willReturn($parentParent);
  2102. $share = $this->createShare(
  2103. null,
  2104. IShare::TYPE_USER,
  2105. $path,
  2106. 'sharedWith',
  2107. 'sharedBy',
  2108. null,
  2109. \OCP\Constants::PERMISSION_ALL);
  2110. $manager->expects($this->once())
  2111. ->method('canShare')
  2112. ->with($share)
  2113. ->willReturn(true);
  2114. $manager->expects($this->once())
  2115. ->method('generalCreateChecks')
  2116. ->with($share);
  2117. ;
  2118. $manager->expects($this->once())
  2119. ->method('userCreateChecks')
  2120. ->with($share);
  2121. ;
  2122. $manager->expects($this->once())
  2123. ->method('pathCreateChecks')
  2124. ->with($path);
  2125. $this->defaultProvider
  2126. ->expects($this->once())
  2127. ->method('create')
  2128. ->with($share)
  2129. ->willReturnArgument(0);
  2130. $share->expects($this->once())
  2131. ->method('setShareOwner')
  2132. ->with('shareOwner');
  2133. $share->expects($this->once())
  2134. ->method('setTarget')
  2135. ->with('/target');
  2136. $manager->createShare($share);
  2137. }
  2138. public function testGetSharesBy() {
  2139. $share = $this->manager->newShare();
  2140. $node = $this->createMock(Folder::class);
  2141. $this->defaultProvider->expects($this->once())
  2142. ->method('getSharesBy')
  2143. ->with(
  2144. $this->equalTo('user'),
  2145. $this->equalTo(IShare::TYPE_USER),
  2146. $this->equalTo($node),
  2147. $this->equalTo(true),
  2148. $this->equalTo(1),
  2149. $this->equalTo(1)
  2150. )->willReturn([$share]);
  2151. $shares = $this->manager->getSharesBy('user', IShare::TYPE_USER, $node, true, 1, 1);
  2152. $this->assertCount(1, $shares);
  2153. $this->assertSame($share, $shares[0]);
  2154. }
  2155. /**
  2156. * Test to ensure we correctly remove expired link shares
  2157. *
  2158. * We have 8 Shares and we want the 3 first valid shares.
  2159. * share 3-6 and 8 are expired. Thus at the end of this test we should
  2160. * have received share 1,2 and 7. And from the manager. Share 3-6 should be
  2161. * deleted (as they are evaluated). but share 8 should still be there.
  2162. */
  2163. public function testGetSharesByExpiredLinkShares() {
  2164. $manager = $this->createManagerMock()
  2165. ->setMethods(['deleteShare'])
  2166. ->getMock();
  2167. /** @var \OCP\Share\IShare[] $shares */
  2168. $shares = [];
  2169. /*
  2170. * This results in an array of 8 IShare elements
  2171. */
  2172. for ($i = 0; $i < 8; $i++) {
  2173. $share = $this->manager->newShare();
  2174. $share->setId($i);
  2175. $shares[] = $share;
  2176. }
  2177. $today = new \DateTime();
  2178. $today->setTime(0, 0, 0);
  2179. /*
  2180. * Set the expiration date to today for some shares
  2181. */
  2182. $shares[2]->setExpirationDate($today);
  2183. $shares[3]->setExpirationDate($today);
  2184. $shares[4]->setExpirationDate($today);
  2185. $shares[5]->setExpirationDate($today);
  2186. /** @var \OCP\Share\IShare[] $i */
  2187. $shares2 = [];
  2188. for ($i = 0; $i < 8; $i++) {
  2189. $shares2[] = clone $shares[$i];
  2190. }
  2191. $node = $this->createMock(File::class);
  2192. /*
  2193. * Simulate the getSharesBy call.
  2194. */
  2195. $this->defaultProvider
  2196. ->method('getSharesBy')
  2197. ->willReturnCallback(function ($uid, $type, $node, $reshares, $limit, $offset) use (&$shares2) {
  2198. return array_slice($shares2, $offset, $limit);
  2199. });
  2200. /*
  2201. * Simulate the deleteShare call.
  2202. */
  2203. $manager->method('deleteShare')
  2204. ->willReturnCallback(function ($share) use (&$shares2) {
  2205. for ($i = 0; $i < count($shares2); $i++) {
  2206. if ($shares2[$i]->getId() === $share->getId()) {
  2207. array_splice($shares2, $i, 1);
  2208. break;
  2209. }
  2210. }
  2211. });
  2212. $res = $manager->getSharesBy('user', IShare::TYPE_LINK, $node, true, 3, 0);
  2213. $this->assertCount(3, $res);
  2214. $this->assertEquals($shares[0]->getId(), $res[0]->getId());
  2215. $this->assertEquals($shares[1]->getId(), $res[1]->getId());
  2216. $this->assertEquals($shares[6]->getId(), $res[2]->getId());
  2217. $this->assertCount(4, $shares2);
  2218. $this->assertEquals(0, $shares2[0]->getId());
  2219. $this->assertEquals(1, $shares2[1]->getId());
  2220. $this->assertEquals(6, $shares2[2]->getId());
  2221. $this->assertEquals(7, $shares2[3]->getId());
  2222. $this->assertSame($today, $shares[3]->getExpirationDate());
  2223. }
  2224. public function testGetShareByToken() {
  2225. $this->config
  2226. ->expects($this->once())
  2227. ->method('getAppValue')
  2228. ->with('core', 'shareapi_allow_links', 'yes')
  2229. ->willReturn('yes');
  2230. $factory = $this->createMock(IProviderFactory::class);
  2231. $manager = new Manager(
  2232. $this->logger,
  2233. $this->config,
  2234. $this->secureRandom,
  2235. $this->hasher,
  2236. $this->mountManager,
  2237. $this->groupManager,
  2238. $this->l,
  2239. $this->l10nFactory,
  2240. $factory,
  2241. $this->userManager,
  2242. $this->rootFolder,
  2243. $this->eventDispatcher,
  2244. $this->mailer,
  2245. $this->urlGenerator,
  2246. $this->defaults,
  2247. $this->dispatcher,
  2248. $this->userSession,
  2249. $this->knownUserService
  2250. );
  2251. $share = $this->createMock(IShare::class);
  2252. $factory->expects($this->once())
  2253. ->method('getProviderForType')
  2254. ->with(IShare::TYPE_LINK)
  2255. ->willReturn($this->defaultProvider);
  2256. $this->defaultProvider->expects($this->once())
  2257. ->method('getShareByToken')
  2258. ->with('token')
  2259. ->willReturn($share);
  2260. $ret = $manager->getShareByToken('token');
  2261. $this->assertSame($share, $ret);
  2262. }
  2263. public function testGetShareByTokenRoom() {
  2264. $this->config
  2265. ->expects($this->once())
  2266. ->method('getAppValue')
  2267. ->with('core', 'shareapi_allow_links', 'yes')
  2268. ->willReturn('no');
  2269. $factory = $this->createMock(IProviderFactory::class);
  2270. $manager = new Manager(
  2271. $this->logger,
  2272. $this->config,
  2273. $this->secureRandom,
  2274. $this->hasher,
  2275. $this->mountManager,
  2276. $this->groupManager,
  2277. $this->l,
  2278. $this->l10nFactory,
  2279. $factory,
  2280. $this->userManager,
  2281. $this->rootFolder,
  2282. $this->eventDispatcher,
  2283. $this->mailer,
  2284. $this->urlGenerator,
  2285. $this->defaults,
  2286. $this->dispatcher,
  2287. $this->userSession,
  2288. $this->knownUserService
  2289. );
  2290. $share = $this->createMock(IShare::class);
  2291. $roomShareProvider = $this->createMock(IShareProvider::class);
  2292. $factory->expects($this->any())
  2293. ->method('getProviderForType')
  2294. ->willReturnCallback(function ($shareType) use ($roomShareProvider) {
  2295. if ($shareType !== IShare::TYPE_ROOM) {
  2296. throw new Exception\ProviderException();
  2297. }
  2298. return $roomShareProvider;
  2299. });
  2300. $roomShareProvider->expects($this->once())
  2301. ->method('getShareByToken')
  2302. ->with('token')
  2303. ->willReturn($share);
  2304. $ret = $manager->getShareByToken('token');
  2305. $this->assertSame($share, $ret);
  2306. }
  2307. public function testGetShareByTokenWithException() {
  2308. $this->config
  2309. ->expects($this->once())
  2310. ->method('getAppValue')
  2311. ->with('core', 'shareapi_allow_links', 'yes')
  2312. ->willReturn('yes');
  2313. $factory = $this->createMock(IProviderFactory::class);
  2314. $manager = new Manager(
  2315. $this->logger,
  2316. $this->config,
  2317. $this->secureRandom,
  2318. $this->hasher,
  2319. $this->mountManager,
  2320. $this->groupManager,
  2321. $this->l,
  2322. $this->l10nFactory,
  2323. $factory,
  2324. $this->userManager,
  2325. $this->rootFolder,
  2326. $this->eventDispatcher,
  2327. $this->mailer,
  2328. $this->urlGenerator,
  2329. $this->defaults,
  2330. $this->dispatcher,
  2331. $this->userSession,
  2332. $this->knownUserService
  2333. );
  2334. $share = $this->createMock(IShare::class);
  2335. $factory->expects($this->at(0))
  2336. ->method('getProviderForType')
  2337. ->with(IShare::TYPE_LINK)
  2338. ->willReturn($this->defaultProvider);
  2339. $factory->expects($this->at(1))
  2340. ->method('getProviderForType')
  2341. ->with(IShare::TYPE_REMOTE)
  2342. ->willReturn($this->defaultProvider);
  2343. $this->defaultProvider->expects($this->at(0))
  2344. ->method('getShareByToken')
  2345. ->with('token')
  2346. ->will($this->throwException(new ShareNotFound()));
  2347. $this->defaultProvider->expects($this->at(1))
  2348. ->method('getShareByToken')
  2349. ->with('token')
  2350. ->willReturn($share);
  2351. $ret = $manager->getShareByToken('token');
  2352. $this->assertSame($share, $ret);
  2353. }
  2354. public function testGetShareByTokenExpired() {
  2355. $this->expectException(\OCP\Share\Exceptions\ShareNotFound::class);
  2356. $this->expectExceptionMessage('The requested share does not exist anymore');
  2357. $this->config
  2358. ->expects($this->once())
  2359. ->method('getAppValue')
  2360. ->with('core', 'shareapi_allow_links', 'yes')
  2361. ->willReturn('yes');
  2362. $this->l->expects($this->once())
  2363. ->method('t')
  2364. ->willReturnArgument(0);
  2365. $manager = $this->createManagerMock()
  2366. ->setMethods(['deleteShare'])
  2367. ->getMock();
  2368. $date = new \DateTime();
  2369. $date->setTime(0, 0, 0);
  2370. $share = $this->manager->newShare();
  2371. $share->setExpirationDate($date);
  2372. $this->defaultProvider->expects($this->once())
  2373. ->method('getShareByToken')
  2374. ->with('expiredToken')
  2375. ->willReturn($share);
  2376. $manager->expects($this->once())
  2377. ->method('deleteShare')
  2378. ->with($this->equalTo($share));
  2379. $manager->getShareByToken('expiredToken');
  2380. }
  2381. public function testGetShareByTokenNotExpired() {
  2382. $this->config
  2383. ->expects($this->once())
  2384. ->method('getAppValue')
  2385. ->with('core', 'shareapi_allow_links', 'yes')
  2386. ->willReturn('yes');
  2387. $date = new \DateTime();
  2388. $date->setTime(0, 0, 0);
  2389. $date->add(new \DateInterval('P2D'));
  2390. $share = $this->manager->newShare();
  2391. $share->setExpirationDate($date);
  2392. $this->defaultProvider->expects($this->once())
  2393. ->method('getShareByToken')
  2394. ->with('expiredToken')
  2395. ->willReturn($share);
  2396. $res = $this->manager->getShareByToken('expiredToken');
  2397. $this->assertSame($share, $res);
  2398. }
  2399. public function testGetShareByTokenWithPublicLinksDisabled() {
  2400. $this->expectException(\OCP\Share\Exceptions\ShareNotFound::class);
  2401. $this->config
  2402. ->expects($this->once())
  2403. ->method('getAppValue')
  2404. ->with('core', 'shareapi_allow_links', 'yes')
  2405. ->willReturn('no');
  2406. $this->manager->getShareByToken('validToken');
  2407. }
  2408. public function testGetShareByTokenPublicUploadDisabled() {
  2409. $this->config
  2410. ->expects($this->at(0))
  2411. ->method('getAppValue')
  2412. ->with('core', 'shareapi_allow_links', 'yes')
  2413. ->willReturn('yes');
  2414. $share = $this->manager->newShare();
  2415. $share->setShareType(IShare::TYPE_LINK)
  2416. ->setPermissions(\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE);
  2417. $share->setSharedWith('sharedWith');
  2418. $folder = $this->createMock(\OC\Files\Node\Folder::class);
  2419. $share->setNode($folder);
  2420. $this->config
  2421. ->expects($this->at(1))
  2422. ->method('getAppValue')
  2423. ->willReturnMap([
  2424. ['core', 'shareapi_allow_public_upload', 'yes', 'no'],
  2425. ]);
  2426. $this->defaultProvider->expects($this->once())
  2427. ->method('getShareByToken')
  2428. ->willReturn('validToken')
  2429. ->willReturn($share);
  2430. $res = $this->manager->getShareByToken('validToken');
  2431. $this->assertSame(\OCP\Constants::PERMISSION_READ, $res->getPermissions());
  2432. }
  2433. public function testCheckPasswordNoLinkShare() {
  2434. $share = $this->createMock(IShare::class);
  2435. $share->method('getShareType')->willReturn(IShare::TYPE_USER);
  2436. $this->assertFalse($this->manager->checkPassword($share, 'password'));
  2437. }
  2438. public function testCheckPasswordNoPassword() {
  2439. $share = $this->createMock(IShare::class);
  2440. $share->method('getShareType')->willReturn(IShare::TYPE_LINK);
  2441. $this->assertFalse($this->manager->checkPassword($share, 'password'));
  2442. $share->method('getPassword')->willReturn('password');
  2443. $this->assertFalse($this->manager->checkPassword($share, null));
  2444. }
  2445. public function testCheckPasswordInvalidPassword() {
  2446. $share = $this->createMock(IShare::class);
  2447. $share->method('getShareType')->willReturn(IShare::TYPE_LINK);
  2448. $share->method('getPassword')->willReturn('password');
  2449. $this->hasher->method('verify')->with('invalidpassword', 'password', '')->willReturn(false);
  2450. $this->assertFalse($this->manager->checkPassword($share, 'invalidpassword'));
  2451. }
  2452. public function testCheckPasswordValidPassword() {
  2453. $share = $this->createMock(IShare::class);
  2454. $share->method('getShareType')->willReturn(IShare::TYPE_LINK);
  2455. $share->method('getPassword')->willReturn('passwordHash');
  2456. $this->hasher->method('verify')->with('password', 'passwordHash', '')->willReturn(true);
  2457. $this->assertTrue($this->manager->checkPassword($share, 'password'));
  2458. }
  2459. public function testCheckPasswordUpdateShare() {
  2460. $share = $this->manager->newShare();
  2461. $share->setShareType(IShare::TYPE_LINK)
  2462. ->setPassword('passwordHash');
  2463. $this->hasher->method('verify')->with('password', 'passwordHash', '')
  2464. ->willReturnCallback(function ($pass, $hash, &$newHash) {
  2465. $newHash = 'newHash';
  2466. return true;
  2467. });
  2468. $this->defaultProvider->expects($this->once())
  2469. ->method('update')
  2470. ->with($this->callback(function (\OCP\Share\IShare $share) {
  2471. return $share->getPassword() === 'newHash';
  2472. }));
  2473. $this->assertTrue($this->manager->checkPassword($share, 'password'));
  2474. }
  2475. public function testUpdateShareCantChangeShareType() {
  2476. $this->expectException(\Exception::class);
  2477. $this->expectExceptionMessage('Cannot change share type');
  2478. $manager = $this->createManagerMock()
  2479. ->setMethods([
  2480. 'canShare',
  2481. 'getShareById'
  2482. ])
  2483. ->getMock();
  2484. $originalShare = $this->manager->newShare();
  2485. $originalShare->setShareType(IShare::TYPE_GROUP);
  2486. $manager->expects($this->once())->method('canShare')->willReturn(true);
  2487. $manager->expects($this->once())->method('getShareById')->with('foo:42')->willReturn($originalShare);
  2488. $share = $this->manager->newShare();
  2489. $attrs = $this->manager->newShare()->newAttributes();
  2490. $attrs->setAttribute('app1', 'perm1', true);
  2491. $share->setProviderId('foo')
  2492. ->setId('42')
  2493. ->setShareType(IShare::TYPE_USER);
  2494. $manager->updateShare($share);
  2495. }
  2496. public function testUpdateShareCantChangeRecipientForGroupShare() {
  2497. $this->expectException(\Exception::class);
  2498. $this->expectExceptionMessage('Can only update recipient on user shares');
  2499. $manager = $this->createManagerMock()
  2500. ->setMethods([
  2501. 'canShare',
  2502. 'getShareById'
  2503. ])
  2504. ->getMock();
  2505. $originalShare = $this->manager->newShare();
  2506. $originalShare->setShareType(IShare::TYPE_GROUP)
  2507. ->setSharedWith('origGroup');
  2508. $manager->expects($this->once())->method('canShare')->willReturn(true);
  2509. $manager->expects($this->once())->method('getShareById')->with('foo:42')->willReturn($originalShare);
  2510. $share = $this->manager->newShare();
  2511. $share->setProviderId('foo')
  2512. ->setId('42')
  2513. ->setShareType(IShare::TYPE_GROUP)
  2514. ->setSharedWith('newGroup');
  2515. $manager->updateShare($share);
  2516. }
  2517. public function testUpdateShareCantShareWithOwner() {
  2518. $this->expectException(\Exception::class);
  2519. $this->expectExceptionMessage('Cannot share with the share owner');
  2520. $manager = $this->createManagerMock()
  2521. ->setMethods([
  2522. 'canShare',
  2523. 'getShareById'
  2524. ])
  2525. ->getMock();
  2526. $originalShare = $this->manager->newShare();
  2527. $originalShare->setShareType(IShare::TYPE_USER)
  2528. ->setSharedWith('sharedWith');
  2529. $manager->expects($this->once())->method('canShare')->willReturn(true);
  2530. $manager->expects($this->once())->method('getShareById')->with('foo:42')->willReturn($originalShare);
  2531. $share = $this->manager->newShare();
  2532. $share->setProviderId('foo')
  2533. ->setId('42')
  2534. ->setShareType(IShare::TYPE_USER)
  2535. ->setSharedWith('newUser')
  2536. ->setShareOwner('newUser');
  2537. $manager->updateShare($share);
  2538. }
  2539. public function testUpdateShareUser() {
  2540. $this->userManager->expects($this->any())->method('userExists')->willReturn(true);
  2541. $manager = $this->createManagerMock()
  2542. ->setMethods([
  2543. 'canShare',
  2544. 'getShareById',
  2545. 'generalCreateChecks',
  2546. 'userCreateChecks',
  2547. 'pathCreateChecks',
  2548. ])
  2549. ->getMock();
  2550. $originalShare = $this->manager->newShare();
  2551. $originalShare->setShareType(IShare::TYPE_USER)
  2552. ->setSharedWith('origUser')
  2553. ->setPermissions(1);
  2554. $node = $this->createMock(File::class);
  2555. $node->method('getId')->willReturn(100);
  2556. $node->method('getPath')->willReturn('/newUser/files/myPath');
  2557. $manager->expects($this->once())->method('canShare')->willReturn(true);
  2558. $manager->expects($this->once())->method('getShareById')->with('foo:42')->willReturn($originalShare);
  2559. $share = $this->manager->newShare();
  2560. $attrs = $this->manager->newShare()->newAttributes();
  2561. $attrs->setAttribute('app1', 'perm1', true);
  2562. $share->setProviderId('foo')
  2563. ->setId('42')
  2564. ->setShareType(IShare::TYPE_USER)
  2565. ->setSharedWith('origUser')
  2566. ->setShareOwner('newUser')
  2567. ->setSharedBy('sharer')
  2568. ->setPermissions(31)
  2569. ->setAttributes($attrs)
  2570. ->setNode($node);
  2571. $this->defaultProvider->expects($this->once())
  2572. ->method('update')
  2573. ->with($share)
  2574. ->willReturn($share);
  2575. $hookListener = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  2576. \OCP\Util::connectHook('OCP\Share', 'post_set_expiration_date', $hookListener, 'post');
  2577. $hookListener->expects($this->never())->method('post');
  2578. $this->rootFolder->method('getUserFolder')->with('newUser')->willReturnSelf();
  2579. $this->rootFolder->method('getRelativePath')->with('/newUser/files/myPath')->willReturn('/myPath');
  2580. $hookListener2 = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  2581. \OCP\Util::connectHook('OCP\Share', 'post_update_permissions', $hookListener2, 'post');
  2582. $hookListener2->expects($this->once())->method('post')->with([
  2583. 'itemType' => 'file',
  2584. 'itemSource' => 100,
  2585. 'shareType' => IShare::TYPE_USER,
  2586. 'shareWith' => 'origUser',
  2587. 'uidOwner' => 'sharer',
  2588. 'permissions' => 31,
  2589. 'path' => '/myPath',
  2590. 'attributes' => $attrs->toArray(),
  2591. ]);
  2592. $manager->updateShare($share);
  2593. }
  2594. public function testUpdateShareGroup() {
  2595. $manager = $this->createManagerMock()
  2596. ->setMethods([
  2597. 'canShare',
  2598. 'getShareById',
  2599. 'generalCreateChecks',
  2600. 'groupCreateChecks',
  2601. 'pathCreateChecks',
  2602. ])
  2603. ->getMock();
  2604. $originalShare = $this->manager->newShare();
  2605. $originalShare->setShareType(IShare::TYPE_GROUP)
  2606. ->setSharedWith('origUser')
  2607. ->setPermissions(31);
  2608. $manager->expects($this->once())->method('canShare')->willReturn(true);
  2609. $manager->expects($this->once())->method('getShareById')->with('foo:42')->willReturn($originalShare);
  2610. $node = $this->createMock(File::class);
  2611. $share = $this->manager->newShare();
  2612. $share->setProviderId('foo')
  2613. ->setId('42')
  2614. ->setShareType(IShare::TYPE_GROUP)
  2615. ->setSharedWith('origUser')
  2616. ->setShareOwner('owner')
  2617. ->setNode($node)
  2618. ->setPermissions(31);
  2619. $this->defaultProvider->expects($this->once())
  2620. ->method('update')
  2621. ->with($share)
  2622. ->willReturn($share);
  2623. $hookListener = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  2624. \OCP\Util::connectHook('OCP\Share', 'post_set_expiration_date', $hookListener, 'post');
  2625. $hookListener->expects($this->never())->method('post');
  2626. $hookListener2 = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  2627. \OCP\Util::connectHook('OCP\Share', 'post_update_permissions', $hookListener2, 'post');
  2628. $hookListener2->expects($this->never())->method('post');
  2629. $manager->updateShare($share);
  2630. }
  2631. public function testUpdateShareLink() {
  2632. $manager = $this->createManagerMock()
  2633. ->setMethods([
  2634. 'canShare',
  2635. 'getShareById',
  2636. 'generalCreateChecks',
  2637. 'linkCreateChecks',
  2638. 'pathCreateChecks',
  2639. 'verifyPassword',
  2640. 'validateExpirationDateLink',
  2641. ])
  2642. ->getMock();
  2643. $originalShare = $this->manager->newShare();
  2644. $originalShare->setShareType(IShare::TYPE_LINK)
  2645. ->setPermissions(15);
  2646. $tomorrow = new \DateTime();
  2647. $tomorrow->setTime(0, 0, 0);
  2648. $tomorrow->add(new \DateInterval('P1D'));
  2649. $file = $this->createMock(File::class);
  2650. $file->method('getId')->willReturn(100);
  2651. $share = $this->manager->newShare();
  2652. $share->setProviderId('foo')
  2653. ->setId('42')
  2654. ->setShareType(IShare::TYPE_LINK)
  2655. ->setToken('token')
  2656. ->setSharedBy('owner')
  2657. ->setShareOwner('owner')
  2658. ->setPassword('password')
  2659. ->setExpirationDate($tomorrow)
  2660. ->setNode($file)
  2661. ->setPermissions(15);
  2662. $manager->expects($this->once())->method('canShare')->willReturn(true);
  2663. $manager->expects($this->once())->method('getShareById')->with('foo:42')->willReturn($originalShare);
  2664. $manager->expects($this->once())->method('validateExpirationDateLink')->with($share);
  2665. $manager->expects($this->once())->method('verifyPassword')->with('password');
  2666. $this->hasher->expects($this->once())
  2667. ->method('hash')
  2668. ->with('password')
  2669. ->willReturn('hashed');
  2670. $this->defaultProvider->expects($this->once())
  2671. ->method('update')
  2672. ->with($share)
  2673. ->willReturn($share);
  2674. $hookListener = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  2675. \OCP\Util::connectHook('OCP\Share', 'post_set_expiration_date', $hookListener, 'post');
  2676. $hookListener->expects($this->once())->method('post')->with([
  2677. 'itemType' => 'file',
  2678. 'itemSource' => 100,
  2679. 'date' => $tomorrow,
  2680. 'uidOwner' => 'owner',
  2681. ]);
  2682. $hookListener2 = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  2683. \OCP\Util::connectHook('OCP\Share', 'post_update_password', $hookListener2, 'post');
  2684. $hookListener2->expects($this->once())->method('post')->with([
  2685. 'itemType' => 'file',
  2686. 'itemSource' => 100,
  2687. 'uidOwner' => 'owner',
  2688. 'token' => 'token',
  2689. 'disabled' => false,
  2690. ]);
  2691. $hookListener3 = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  2692. \OCP\Util::connectHook('OCP\Share', 'post_update_permissions', $hookListener3, 'post');
  2693. $hookListener3->expects($this->never())->method('post');
  2694. $manager->updateShare($share);
  2695. }
  2696. public function testUpdateShareLinkEnableSendPasswordByTalkWithNoPassword() {
  2697. $this->expectException(\InvalidArgumentException::class);
  2698. $this->expectExceptionMessage('Cannot enable sending the password by Talk with an empty password');
  2699. $manager = $this->createManagerMock()
  2700. ->setMethods([
  2701. 'canShare',
  2702. 'getShareById',
  2703. 'generalCreateChecks',
  2704. 'linkCreateChecks',
  2705. 'pathCreateChecks',
  2706. 'verifyPassword',
  2707. 'validateExpirationDateLink',
  2708. ])
  2709. ->getMock();
  2710. $originalShare = $this->manager->newShare();
  2711. $originalShare->setShareType(IShare::TYPE_LINK)
  2712. ->setPermissions(15);
  2713. $tomorrow = new \DateTime();
  2714. $tomorrow->setTime(0, 0, 0);
  2715. $tomorrow->add(new \DateInterval('P1D'));
  2716. $file = $this->createMock(File::class);
  2717. $file->method('getId')->willReturn(100);
  2718. $share = $this->manager->newShare();
  2719. $share->setProviderId('foo')
  2720. ->setId('42')
  2721. ->setShareType(IShare::TYPE_LINK)
  2722. ->setToken('token')
  2723. ->setSharedBy('owner')
  2724. ->setShareOwner('owner')
  2725. ->setPassword(null)
  2726. ->setSendPasswordByTalk(true)
  2727. ->setExpirationDate($tomorrow)
  2728. ->setNode($file)
  2729. ->setPermissions(15);
  2730. $manager->expects($this->once())->method('canShare')->willReturn(true);
  2731. $manager->expects($this->once())->method('getShareById')->with('foo:42')->willReturn($originalShare);
  2732. $manager->expects($this->once())->method('generalCreateChecks')->with($share);
  2733. $manager->expects($this->once())->method('linkCreateChecks')->with($share);
  2734. $manager->expects($this->never())->method('verifyPassword');
  2735. $manager->expects($this->never())->method('pathCreateChecks');
  2736. $manager->expects($this->never())->method('validateExpirationDateLink');
  2737. $this->hasher->expects($this->never())
  2738. ->method('hash');
  2739. $this->defaultProvider->expects($this->never())
  2740. ->method('update');
  2741. $hookListener = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  2742. \OCP\Util::connectHook('OCP\Share', 'post_set_expiration_date', $hookListener, 'post');
  2743. $hookListener->expects($this->never())->method('post');
  2744. $hookListener2 = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  2745. \OCP\Util::connectHook('OCP\Share', 'post_update_password', $hookListener2, 'post');
  2746. $hookListener2->expects($this->never())->method('post');
  2747. $hookListener3 = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  2748. \OCP\Util::connectHook('OCP\Share', 'post_update_permissions', $hookListener3, 'post');
  2749. $hookListener3->expects($this->never())->method('post');
  2750. $manager->updateShare($share);
  2751. }
  2752. public function testUpdateShareMail() {
  2753. $manager = $this->createManagerMock()
  2754. ->setMethods([
  2755. 'canShare',
  2756. 'getShareById',
  2757. 'generalCreateChecks',
  2758. 'verifyPassword',
  2759. 'pathCreateChecks',
  2760. 'linkCreateChecks',
  2761. 'validateExpirationDateLink',
  2762. ])
  2763. ->getMock();
  2764. $originalShare = $this->manager->newShare();
  2765. $originalShare->setShareType(IShare::TYPE_EMAIL)
  2766. ->setPermissions(\OCP\Constants::PERMISSION_ALL);
  2767. $tomorrow = new \DateTime();
  2768. $tomorrow->setTime(0, 0, 0);
  2769. $tomorrow->add(new \DateInterval('P1D'));
  2770. $file = $this->createMock(File::class);
  2771. $file->method('getId')->willReturn(100);
  2772. $share = $this->manager->newShare();
  2773. $share->setProviderId('foo')
  2774. ->setId('42')
  2775. ->setShareType(IShare::TYPE_EMAIL)
  2776. ->setToken('token')
  2777. ->setSharedBy('owner')
  2778. ->setShareOwner('owner')
  2779. ->setPassword('password')
  2780. ->setExpirationDate($tomorrow)
  2781. ->setNode($file)
  2782. ->setPermissions(\OCP\Constants::PERMISSION_ALL);
  2783. $manager->expects($this->once())->method('canShare')->willReturn(true);
  2784. $manager->expects($this->once())->method('getShareById')->with('foo:42')->willReturn($originalShare);
  2785. $manager->expects($this->once())->method('generalCreateChecks')->with($share);
  2786. $manager->expects($this->once())->method('verifyPassword')->with('password');
  2787. $manager->expects($this->once())->method('pathCreateChecks')->with($file);
  2788. $manager->expects($this->once())->method('linkCreateChecks');
  2789. $manager->expects($this->once())->method('validateExpirationDateLink');
  2790. $this->hasher->expects($this->once())
  2791. ->method('hash')
  2792. ->with('password')
  2793. ->willReturn('hashed');
  2794. $this->defaultProvider->expects($this->once())
  2795. ->method('update')
  2796. ->with($share, 'password')
  2797. ->willReturn($share);
  2798. $hookListener = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  2799. \OCP\Util::connectHook('OCP\Share', 'post_set_expiration_date', $hookListener, 'post');
  2800. $hookListener->expects($this->once())->method('post')->with([
  2801. 'itemType' => 'file',
  2802. 'itemSource' => 100,
  2803. 'date' => $tomorrow,
  2804. 'uidOwner' => 'owner',
  2805. ]);
  2806. $hookListener2 = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  2807. \OCP\Util::connectHook('OCP\Share', 'post_update_password', $hookListener2, 'post');
  2808. $hookListener2->expects($this->once())->method('post')->with([
  2809. 'itemType' => 'file',
  2810. 'itemSource' => 100,
  2811. 'uidOwner' => 'owner',
  2812. 'token' => 'token',
  2813. 'disabled' => false,
  2814. ]);
  2815. $hookListener3 = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  2816. \OCP\Util::connectHook('OCP\Share', 'post_update_permissions', $hookListener3, 'post');
  2817. $hookListener3->expects($this->never())->method('post');
  2818. $manager->updateShare($share);
  2819. }
  2820. public function testUpdateShareMailEnableSendPasswordByTalk() {
  2821. $manager = $this->createManagerMock()
  2822. ->setMethods([
  2823. 'canShare',
  2824. 'getShareById',
  2825. 'generalCreateChecks',
  2826. 'verifyPassword',
  2827. 'pathCreateChecks',
  2828. 'linkCreateChecks',
  2829. 'validateExpirationDateLink',
  2830. ])
  2831. ->getMock();
  2832. $originalShare = $this->manager->newShare();
  2833. $originalShare->setShareType(IShare::TYPE_EMAIL)
  2834. ->setPermissions(\OCP\Constants::PERMISSION_ALL)
  2835. ->setPassword(null)
  2836. ->setSendPasswordByTalk(false);
  2837. $tomorrow = new \DateTime();
  2838. $tomorrow->setTime(0, 0, 0);
  2839. $tomorrow->add(new \DateInterval('P1D'));
  2840. $file = $this->createMock(File::class);
  2841. $file->method('getId')->willReturn(100);
  2842. $share = $this->manager->newShare();
  2843. $share->setProviderId('foo')
  2844. ->setId('42')
  2845. ->setShareType(IShare::TYPE_EMAIL)
  2846. ->setToken('token')
  2847. ->setSharedBy('owner')
  2848. ->setShareOwner('owner')
  2849. ->setPassword('password')
  2850. ->setSendPasswordByTalk(true)
  2851. ->setExpirationDate($tomorrow)
  2852. ->setNode($file)
  2853. ->setPermissions(\OCP\Constants::PERMISSION_ALL);
  2854. $manager->expects($this->once())->method('canShare')->willReturn(true);
  2855. $manager->expects($this->once())->method('getShareById')->with('foo:42')->willReturn($originalShare);
  2856. $manager->expects($this->once())->method('generalCreateChecks')->with($share);
  2857. $manager->expects($this->once())->method('verifyPassword')->with('password');
  2858. $manager->expects($this->once())->method('pathCreateChecks')->with($file);
  2859. $manager->expects($this->once())->method('linkCreateChecks');
  2860. $manager->expects($this->once())->method('validateExpirationDateLink');
  2861. $this->hasher->expects($this->once())
  2862. ->method('hash')
  2863. ->with('password')
  2864. ->willReturn('hashed');
  2865. $this->defaultProvider->expects($this->once())
  2866. ->method('update')
  2867. ->with($share, 'password')
  2868. ->willReturn($share);
  2869. $hookListener = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  2870. \OCP\Util::connectHook('OCP\Share', 'post_set_expiration_date', $hookListener, 'post');
  2871. $hookListener->expects($this->once())->method('post')->with([
  2872. 'itemType' => 'file',
  2873. 'itemSource' => 100,
  2874. 'date' => $tomorrow,
  2875. 'uidOwner' => 'owner',
  2876. ]);
  2877. $hookListener2 = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  2878. \OCP\Util::connectHook('OCP\Share', 'post_update_password', $hookListener2, 'post');
  2879. $hookListener2->expects($this->once())->method('post')->with([
  2880. 'itemType' => 'file',
  2881. 'itemSource' => 100,
  2882. 'uidOwner' => 'owner',
  2883. 'token' => 'token',
  2884. 'disabled' => false,
  2885. ]);
  2886. $hookListener3 = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  2887. \OCP\Util::connectHook('OCP\Share', 'post_update_permissions', $hookListener3, 'post');
  2888. $hookListener3->expects($this->never())->method('post');
  2889. $manager->updateShare($share);
  2890. }
  2891. public function testUpdateShareMailEnableSendPasswordByTalkWithDifferentPassword() {
  2892. $manager = $this->createManagerMock()
  2893. ->setMethods([
  2894. 'canShare',
  2895. 'getShareById',
  2896. 'generalCreateChecks',
  2897. 'verifyPassword',
  2898. 'pathCreateChecks',
  2899. 'linkCreateChecks',
  2900. 'validateExpirationDateLink',
  2901. ])
  2902. ->getMock();
  2903. $originalShare = $this->manager->newShare();
  2904. $originalShare->setShareType(IShare::TYPE_EMAIL)
  2905. ->setPermissions(\OCP\Constants::PERMISSION_ALL)
  2906. ->setPassword('anotherPasswordHash')
  2907. ->setSendPasswordByTalk(false);
  2908. $tomorrow = new \DateTime();
  2909. $tomorrow->setTime(0, 0, 0);
  2910. $tomorrow->add(new \DateInterval('P1D'));
  2911. $file = $this->createMock(File::class);
  2912. $file->method('getId')->willReturn(100);
  2913. $share = $this->manager->newShare();
  2914. $share->setProviderId('foo')
  2915. ->setId('42')
  2916. ->setShareType(IShare::TYPE_EMAIL)
  2917. ->setToken('token')
  2918. ->setSharedBy('owner')
  2919. ->setShareOwner('owner')
  2920. ->setPassword('password')
  2921. ->setSendPasswordByTalk(true)
  2922. ->setExpirationDate($tomorrow)
  2923. ->setNode($file)
  2924. ->setPermissions(\OCP\Constants::PERMISSION_ALL);
  2925. $manager->expects($this->once())->method('canShare')->willReturn(true);
  2926. $manager->expects($this->once())->method('getShareById')->with('foo:42')->willReturn($originalShare);
  2927. $manager->expects($this->once())->method('generalCreateChecks')->with($share);
  2928. $manager->expects($this->once())->method('verifyPassword')->with('password');
  2929. $manager->expects($this->once())->method('pathCreateChecks')->with($file);
  2930. $manager->expects($this->once())->method('linkCreateChecks');
  2931. $manager->expects($this->once())->method('validateExpirationDateLink');
  2932. $this->hasher->expects($this->once())
  2933. ->method('verify')
  2934. ->with('password', 'anotherPasswordHash')
  2935. ->willReturn(false);
  2936. $this->hasher->expects($this->once())
  2937. ->method('hash')
  2938. ->with('password')
  2939. ->willReturn('hashed');
  2940. $this->defaultProvider->expects($this->once())
  2941. ->method('update')
  2942. ->with($share, 'password')
  2943. ->willReturn($share);
  2944. $hookListener = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  2945. \OCP\Util::connectHook('OCP\Share', 'post_set_expiration_date', $hookListener, 'post');
  2946. $hookListener->expects($this->once())->method('post')->with([
  2947. 'itemType' => 'file',
  2948. 'itemSource' => 100,
  2949. 'date' => $tomorrow,
  2950. 'uidOwner' => 'owner',
  2951. ]);
  2952. $hookListener2 = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  2953. \OCP\Util::connectHook('OCP\Share', 'post_update_password', $hookListener2, 'post');
  2954. $hookListener2->expects($this->once())->method('post')->with([
  2955. 'itemType' => 'file',
  2956. 'itemSource' => 100,
  2957. 'uidOwner' => 'owner',
  2958. 'token' => 'token',
  2959. 'disabled' => false,
  2960. ]);
  2961. $hookListener3 = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  2962. \OCP\Util::connectHook('OCP\Share', 'post_update_permissions', $hookListener3, 'post');
  2963. $hookListener3->expects($this->never())->method('post');
  2964. $manager->updateShare($share);
  2965. }
  2966. public function testUpdateShareMailEnableSendPasswordByTalkWithNoPassword() {
  2967. $this->expectException(\InvalidArgumentException::class);
  2968. $this->expectExceptionMessage('Cannot enable sending the password by Talk with an empty password');
  2969. $manager = $this->createManagerMock()
  2970. ->setMethods([
  2971. 'canShare',
  2972. 'getShareById',
  2973. 'generalCreateChecks',
  2974. 'verifyPassword',
  2975. 'pathCreateChecks',
  2976. 'linkCreateChecks',
  2977. 'validateExpirationDateLink',
  2978. ])
  2979. ->getMock();
  2980. $originalShare = $this->manager->newShare();
  2981. $originalShare->setShareType(IShare::TYPE_EMAIL)
  2982. ->setPermissions(\OCP\Constants::PERMISSION_ALL)
  2983. ->setPassword(null)
  2984. ->setSendPasswordByTalk(false);
  2985. $tomorrow = new \DateTime();
  2986. $tomorrow->setTime(0, 0, 0);
  2987. $tomorrow->add(new \DateInterval('P1D'));
  2988. $file = $this->createMock(File::class);
  2989. $file->method('getId')->willReturn(100);
  2990. $share = $this->manager->newShare();
  2991. $share->setProviderId('foo')
  2992. ->setId('42')
  2993. ->setShareType(IShare::TYPE_EMAIL)
  2994. ->setToken('token')
  2995. ->setSharedBy('owner')
  2996. ->setShareOwner('owner')
  2997. ->setPassword(null)
  2998. ->setSendPasswordByTalk(true)
  2999. ->setExpirationDate($tomorrow)
  3000. ->setNode($file)
  3001. ->setPermissions(\OCP\Constants::PERMISSION_ALL);
  3002. $manager->expects($this->once())->method('canShare')->willReturn(true);
  3003. $manager->expects($this->once())->method('getShareById')->with('foo:42')->willReturn($originalShare);
  3004. $manager->expects($this->once())->method('generalCreateChecks')->with($share);
  3005. $manager->expects($this->never())->method('verifyPassword');
  3006. $manager->expects($this->never())->method('pathCreateChecks');
  3007. $manager->expects($this->once())->method('linkCreateChecks');
  3008. $manager->expects($this->never())->method('validateExpirationDateLink');
  3009. // If the password is empty, we have nothing to hash
  3010. $this->hasher->expects($this->never())
  3011. ->method('hash');
  3012. $this->defaultProvider->expects($this->never())
  3013. ->method('update');
  3014. $hookListener = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  3015. \OCP\Util::connectHook('OCP\Share', 'post_set_expiration_date', $hookListener, 'post');
  3016. $hookListener->expects($this->never())->method('post');
  3017. $hookListener2 = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  3018. \OCP\Util::connectHook('OCP\Share', 'post_update_password', $hookListener2, 'post');
  3019. $hookListener2->expects($this->never())->method('post');
  3020. $hookListener3 = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  3021. \OCP\Util::connectHook('OCP\Share', 'post_update_permissions', $hookListener3, 'post');
  3022. $hookListener3->expects($this->never())->method('post');
  3023. $manager->updateShare($share);
  3024. }
  3025. public function testUpdateShareMailEnableSendPasswordByTalkRemovingPassword() {
  3026. $this->expectException(\InvalidArgumentException::class);
  3027. $this->expectExceptionMessage('Cannot enable sending the password by Talk with an empty password');
  3028. $manager = $this->createManagerMock()
  3029. ->setMethods([
  3030. 'canShare',
  3031. 'getShareById',
  3032. 'generalCreateChecks',
  3033. 'verifyPassword',
  3034. 'pathCreateChecks',
  3035. 'linkCreateChecks',
  3036. 'validateExpirationDateLink',
  3037. ])
  3038. ->getMock();
  3039. $originalShare = $this->manager->newShare();
  3040. $originalShare->setShareType(IShare::TYPE_EMAIL)
  3041. ->setPermissions(\OCP\Constants::PERMISSION_ALL)
  3042. ->setPassword('passwordHash')
  3043. ->setSendPasswordByTalk(false);
  3044. $tomorrow = new \DateTime();
  3045. $tomorrow->setTime(0, 0, 0);
  3046. $tomorrow->add(new \DateInterval('P1D'));
  3047. $file = $this->createMock(File::class);
  3048. $file->method('getId')->willReturn(100);
  3049. $share = $this->manager->newShare();
  3050. $share->setProviderId('foo')
  3051. ->setId('42')
  3052. ->setShareType(IShare::TYPE_EMAIL)
  3053. ->setToken('token')
  3054. ->setSharedBy('owner')
  3055. ->setShareOwner('owner')
  3056. ->setPassword(null)
  3057. ->setSendPasswordByTalk(true)
  3058. ->setExpirationDate($tomorrow)
  3059. ->setNode($file)
  3060. ->setPermissions(\OCP\Constants::PERMISSION_ALL);
  3061. $manager->expects($this->once())->method('canShare')->willReturn(true);
  3062. $manager->expects($this->once())->method('getShareById')->with('foo:42')->willReturn($originalShare);
  3063. $manager->expects($this->once())->method('generalCreateChecks')->with($share);
  3064. $manager->expects($this->once())->method('verifyPassword');
  3065. $manager->expects($this->never())->method('pathCreateChecks');
  3066. $manager->expects($this->once())->method('linkCreateChecks');
  3067. $manager->expects($this->never())->method('validateExpirationDateLink');
  3068. // If the password is empty, we have nothing to hash
  3069. $this->hasher->expects($this->never())
  3070. ->method('hash');
  3071. $this->defaultProvider->expects($this->never())
  3072. ->method('update');
  3073. $hookListener = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  3074. \OCP\Util::connectHook('OCP\Share', 'post_set_expiration_date', $hookListener, 'post');
  3075. $hookListener->expects($this->never())->method('post');
  3076. $hookListener2 = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  3077. \OCP\Util::connectHook('OCP\Share', 'post_update_password', $hookListener2, 'post');
  3078. $hookListener2->expects($this->never())->method('post');
  3079. $hookListener3 = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  3080. \OCP\Util::connectHook('OCP\Share', 'post_update_permissions', $hookListener3, 'post');
  3081. $hookListener3->expects($this->never())->method('post');
  3082. $manager->updateShare($share);
  3083. }
  3084. public function testUpdateShareMailEnableSendPasswordByTalkRemovingPasswordWithEmptyString() {
  3085. $this->expectException(\InvalidArgumentException::class);
  3086. $this->expectExceptionMessage('Cannot enable sending the password by Talk with an empty password');
  3087. $manager = $this->createManagerMock()
  3088. ->setMethods([
  3089. 'canShare',
  3090. 'getShareById',
  3091. 'generalCreateChecks',
  3092. 'verifyPassword',
  3093. 'pathCreateChecks',
  3094. 'linkCreateChecks',
  3095. 'validateExpirationDateLink',
  3096. ])
  3097. ->getMock();
  3098. $originalShare = $this->manager->newShare();
  3099. $originalShare->setShareType(IShare::TYPE_EMAIL)
  3100. ->setPermissions(\OCP\Constants::PERMISSION_ALL)
  3101. ->setPassword('passwordHash')
  3102. ->setSendPasswordByTalk(false);
  3103. $tomorrow = new \DateTime();
  3104. $tomorrow->setTime(0, 0, 0);
  3105. $tomorrow->add(new \DateInterval('P1D'));
  3106. $file = $this->createMock(File::class);
  3107. $file->method('getId')->willReturn(100);
  3108. $share = $this->manager->newShare();
  3109. $share->setProviderId('foo')
  3110. ->setId('42')
  3111. ->setShareType(IShare::TYPE_EMAIL)
  3112. ->setToken('token')
  3113. ->setSharedBy('owner')
  3114. ->setShareOwner('owner')
  3115. ->setPassword('')
  3116. ->setSendPasswordByTalk(true)
  3117. ->setExpirationDate($tomorrow)
  3118. ->setNode($file)
  3119. ->setPermissions(\OCP\Constants::PERMISSION_ALL);
  3120. $manager->expects($this->once())->method('canShare')->willReturn(true);
  3121. $manager->expects($this->once())->method('getShareById')->with('foo:42')->willReturn($originalShare);
  3122. $manager->expects($this->once())->method('generalCreateChecks')->with($share);
  3123. $manager->expects($this->once())->method('verifyPassword');
  3124. $manager->expects($this->never())->method('pathCreateChecks');
  3125. $manager->expects($this->once())->method('linkCreateChecks');
  3126. $manager->expects($this->never())->method('validateExpirationDateLink');
  3127. // If the password is empty, we have nothing to hash
  3128. $this->hasher->expects($this->never())
  3129. ->method('hash');
  3130. $this->defaultProvider->expects($this->never())
  3131. ->method('update');
  3132. $hookListener = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  3133. \OCP\Util::connectHook('OCP\Share', 'post_set_expiration_date', $hookListener, 'post');
  3134. $hookListener->expects($this->never())->method('post');
  3135. $hookListener2 = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  3136. \OCP\Util::connectHook('OCP\Share', 'post_update_password', $hookListener2, 'post');
  3137. $hookListener2->expects($this->never())->method('post');
  3138. $hookListener3 = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  3139. \OCP\Util::connectHook('OCP\Share', 'post_update_permissions', $hookListener3, 'post');
  3140. $hookListener3->expects($this->never())->method('post');
  3141. $manager->updateShare($share);
  3142. }
  3143. public function testUpdateShareMailEnableSendPasswordByTalkWithPreviousPassword() {
  3144. $this->expectException(\InvalidArgumentException::class);
  3145. $this->expectExceptionMessage('Cannot enable sending the password by Talk without setting a new password');
  3146. $manager = $this->createManagerMock()
  3147. ->setMethods([
  3148. 'canShare',
  3149. 'getShareById',
  3150. 'generalCreateChecks',
  3151. 'verifyPassword',
  3152. 'pathCreateChecks',
  3153. 'linkCreateChecks',
  3154. 'validateExpirationDateLink',
  3155. ])
  3156. ->getMock();
  3157. $originalShare = $this->manager->newShare();
  3158. $originalShare->setShareType(IShare::TYPE_EMAIL)
  3159. ->setPermissions(\OCP\Constants::PERMISSION_ALL)
  3160. ->setPassword('password')
  3161. ->setSendPasswordByTalk(false);
  3162. $tomorrow = new \DateTime();
  3163. $tomorrow->setTime(0, 0, 0);
  3164. $tomorrow->add(new \DateInterval('P1D'));
  3165. $file = $this->createMock(File::class);
  3166. $file->method('getId')->willReturn(100);
  3167. $share = $this->manager->newShare();
  3168. $share->setProviderId('foo')
  3169. ->setId('42')
  3170. ->setShareType(IShare::TYPE_EMAIL)
  3171. ->setToken('token')
  3172. ->setSharedBy('owner')
  3173. ->setShareOwner('owner')
  3174. ->setPassword('password')
  3175. ->setSendPasswordByTalk(true)
  3176. ->setExpirationDate($tomorrow)
  3177. ->setNode($file)
  3178. ->setPermissions(\OCP\Constants::PERMISSION_ALL);
  3179. $manager->expects($this->once())->method('canShare')->willReturn(true);
  3180. $manager->expects($this->once())->method('getShareById')->with('foo:42')->willReturn($originalShare);
  3181. $manager->expects($this->once())->method('generalCreateChecks')->with($share);
  3182. $manager->expects($this->never())->method('verifyPassword');
  3183. $manager->expects($this->never())->method('pathCreateChecks');
  3184. $manager->expects($this->once())->method('linkCreateChecks');
  3185. $manager->expects($this->never())->method('validateExpirationDateLink');
  3186. // If the old & new passwords are the same, we don't do anything
  3187. $this->hasher->expects($this->never())
  3188. ->method('verify');
  3189. $this->hasher->expects($this->never())
  3190. ->method('hash');
  3191. $this->defaultProvider->expects($this->never())
  3192. ->method('update');
  3193. $hookListener = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  3194. \OCP\Util::connectHook('OCP\Share', 'post_set_expiration_date', $hookListener, 'post');
  3195. $hookListener->expects($this->never())->method('post');
  3196. $hookListener2 = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  3197. \OCP\Util::connectHook('OCP\Share', 'post_update_password', $hookListener2, 'post');
  3198. $hookListener2->expects($this->never())->method('post');
  3199. $hookListener3 = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  3200. \OCP\Util::connectHook('OCP\Share', 'post_update_permissions', $hookListener3, 'post');
  3201. $hookListener3->expects($this->never())->method('post');
  3202. $manager->updateShare($share);
  3203. }
  3204. public function testUpdateShareMailDisableSendPasswordByTalkWithPreviousPassword() {
  3205. $this->expectException(\InvalidArgumentException::class);
  3206. $this->expectExceptionMessage('Cannot disable sending the password by Talk without setting a new password');
  3207. $manager = $this->createManagerMock()
  3208. ->setMethods([
  3209. 'canShare',
  3210. 'getShareById',
  3211. 'generalCreateChecks',
  3212. 'verifyPassword',
  3213. 'pathCreateChecks',
  3214. 'linkCreateChecks',
  3215. 'validateExpirationDateLink',
  3216. ])
  3217. ->getMock();
  3218. $originalShare = $this->manager->newShare();
  3219. $originalShare->setShareType(IShare::TYPE_EMAIL)
  3220. ->setPermissions(\OCP\Constants::PERMISSION_ALL)
  3221. ->setPassword('passwordHash')
  3222. ->setSendPasswordByTalk(true);
  3223. $tomorrow = new \DateTime();
  3224. $tomorrow->setTime(0, 0, 0);
  3225. $tomorrow->add(new \DateInterval('P1D'));
  3226. $file = $this->createMock(File::class);
  3227. $file->method('getId')->willReturn(100);
  3228. $share = $this->manager->newShare();
  3229. $share->setProviderId('foo')
  3230. ->setId('42')
  3231. ->setShareType(IShare::TYPE_EMAIL)
  3232. ->setToken('token')
  3233. ->setSharedBy('owner')
  3234. ->setShareOwner('owner')
  3235. ->setPassword('passwordHash')
  3236. ->setSendPasswordByTalk(false)
  3237. ->setExpirationDate($tomorrow)
  3238. ->setNode($file)
  3239. ->setPermissions(\OCP\Constants::PERMISSION_ALL);
  3240. $manager->expects($this->once())->method('canShare')->willReturn(true);
  3241. $manager->expects($this->once())->method('getShareById')->with('foo:42')->willReturn($originalShare);
  3242. $manager->expects($this->once())->method('generalCreateChecks')->with($share);
  3243. $manager->expects($this->never())->method('verifyPassword');
  3244. $manager->expects($this->never())->method('pathCreateChecks');
  3245. $manager->expects($this->once())->method('linkCreateChecks');
  3246. $manager->expects($this->never())->method('validateExpirationDateLink');
  3247. // If the old & new passwords are the same, we don't do anything
  3248. $this->hasher->expects($this->never())
  3249. ->method('verify');
  3250. $this->hasher->expects($this->never())
  3251. ->method('hash');
  3252. $this->defaultProvider->expects($this->never())
  3253. ->method('update');
  3254. $hookListener = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  3255. \OCP\Util::connectHook('OCP\Share', 'post_set_expiration_date', $hookListener, 'post');
  3256. $hookListener->expects($this->never())->method('post');
  3257. $hookListener2 = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  3258. \OCP\Util::connectHook('OCP\Share', 'post_update_password', $hookListener2, 'post');
  3259. $hookListener2->expects($this->never())->method('post');
  3260. $hookListener3 = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  3261. \OCP\Util::connectHook('OCP\Share', 'post_update_permissions', $hookListener3, 'post');
  3262. $hookListener3->expects($this->never())->method('post');
  3263. $manager->updateShare($share);
  3264. }
  3265. public function testUpdateShareMailDisableSendPasswordByTalkWithoutChangingPassword() {
  3266. $this->expectException(\InvalidArgumentException::class);
  3267. $this->expectExceptionMessage('Cannot disable sending the password by Talk without setting a new password');
  3268. $manager = $this->createManagerMock()
  3269. ->setMethods([
  3270. 'canShare',
  3271. 'getShareById',
  3272. 'generalCreateChecks',
  3273. 'verifyPassword',
  3274. 'pathCreateChecks',
  3275. 'linkCreateChecks',
  3276. 'validateExpirationDateLink',
  3277. ])
  3278. ->getMock();
  3279. $originalShare = $this->manager->newShare();
  3280. $originalShare->setShareType(IShare::TYPE_EMAIL)
  3281. ->setPermissions(\OCP\Constants::PERMISSION_ALL)
  3282. ->setPassword('passwordHash')
  3283. ->setSendPasswordByTalk(true);
  3284. $tomorrow = new \DateTime();
  3285. $tomorrow->setTime(0, 0, 0);
  3286. $tomorrow->add(new \DateInterval('P1D'));
  3287. $file = $this->createMock(File::class);
  3288. $file->method('getId')->willReturn(100);
  3289. $share = $this->manager->newShare();
  3290. $share->setProviderId('foo')
  3291. ->setId('42')
  3292. ->setShareType(IShare::TYPE_EMAIL)
  3293. ->setToken('token')
  3294. ->setSharedBy('owner')
  3295. ->setShareOwner('owner')
  3296. ->setPassword('passwordHash')
  3297. ->setSendPasswordByTalk(false)
  3298. ->setExpirationDate($tomorrow)
  3299. ->setNode($file)
  3300. ->setPermissions(\OCP\Constants::PERMISSION_ALL);
  3301. $manager->expects($this->once())->method('canShare')->willReturn(true);
  3302. $manager->expects($this->once())->method('getShareById')->with('foo:42')->willReturn($originalShare);
  3303. $manager->expects($this->once())->method('generalCreateChecks')->with($share);
  3304. $manager->expects($this->never())->method('verifyPassword');
  3305. $manager->expects($this->never())->method('pathCreateChecks');
  3306. $manager->expects($this->once())->method('linkCreateChecks');
  3307. $manager->expects($this->never())->method('validateExpirationDateLink');
  3308. // If the old & new passwords are the same, we don't do anything
  3309. $this->hasher->expects($this->never())
  3310. ->method('verify');
  3311. $this->hasher->expects($this->never())
  3312. ->method('hash');
  3313. $this->defaultProvider->expects($this->never())
  3314. ->method('update');
  3315. $hookListener = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  3316. \OCP\Util::connectHook('OCP\Share', 'post_set_expiration_date', $hookListener, 'post');
  3317. $hookListener->expects($this->never())->method('post');
  3318. $hookListener2 = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  3319. \OCP\Util::connectHook('OCP\Share', 'post_update_password', $hookListener2, 'post');
  3320. $hookListener2->expects($this->never())->method('post');
  3321. $hookListener3 = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  3322. \OCP\Util::connectHook('OCP\Share', 'post_update_permissions', $hookListener3, 'post');
  3323. $hookListener3->expects($this->never())->method('post');
  3324. $manager->updateShare($share);
  3325. }
  3326. public function testMoveShareLink() {
  3327. $this->expectException(\InvalidArgumentException::class);
  3328. $this->expectExceptionMessage('Cannot change target of link share');
  3329. $share = $this->manager->newShare();
  3330. $share->setShareType(IShare::TYPE_LINK);
  3331. $recipient = $this->createMock(IUser::class);
  3332. $this->manager->moveShare($share, $recipient);
  3333. }
  3334. public function testMoveShareUserNotRecipient() {
  3335. $this->expectException(\InvalidArgumentException::class);
  3336. $this->expectExceptionMessage('Invalid recipient');
  3337. $share = $this->manager->newShare();
  3338. $share->setShareType(IShare::TYPE_USER);
  3339. $share->setSharedWith('sharedWith');
  3340. $this->manager->moveShare($share, 'recipient');
  3341. }
  3342. public function testMoveShareUser() {
  3343. $share = $this->manager->newShare();
  3344. $share->setShareType(IShare::TYPE_USER)
  3345. ->setId('42')
  3346. ->setProviderId('foo');
  3347. $share->setSharedWith('recipient');
  3348. $this->defaultProvider->method('move')->with($share, 'recipient')->willReturnArgument(0);
  3349. $this->manager->moveShare($share, 'recipient');
  3350. $this->addToAssertionCount(1);
  3351. }
  3352. public function testMoveShareGroupNotRecipient() {
  3353. $this->expectException(\InvalidArgumentException::class);
  3354. $this->expectExceptionMessage('Invalid recipient');
  3355. $share = $this->manager->newShare();
  3356. $share->setShareType(IShare::TYPE_GROUP);
  3357. $sharedWith = $this->createMock(IGroup::class);
  3358. $share->setSharedWith('shareWith');
  3359. $recipient = $this->createMock(IUser::class);
  3360. $sharedWith->method('inGroup')->with($recipient)->willReturn(false);
  3361. $this->groupManager->method('get')->with('shareWith')->willReturn($sharedWith);
  3362. $this->userManager->method('get')->with('recipient')->willReturn($recipient);
  3363. $this->manager->moveShare($share, 'recipient');
  3364. }
  3365. public function testMoveShareGroupNull() {
  3366. $this->expectException(\InvalidArgumentException::class);
  3367. $this->expectExceptionMessage('Group "shareWith" does not exist');
  3368. $share = $this->manager->newShare();
  3369. $share->setShareType(IShare::TYPE_GROUP);
  3370. $share->setSharedWith('shareWith');
  3371. $recipient = $this->createMock(IUser::class);
  3372. $this->groupManager->method('get')->with('shareWith')->willReturn(null);
  3373. $this->userManager->method('get')->with('recipient')->willReturn($recipient);
  3374. $this->manager->moveShare($share, 'recipient');
  3375. }
  3376. public function testMoveShareGroup() {
  3377. $share = $this->manager->newShare();
  3378. $share->setShareType(IShare::TYPE_GROUP)
  3379. ->setId('42')
  3380. ->setProviderId('foo');
  3381. $group = $this->createMock(IGroup::class);
  3382. $share->setSharedWith('group');
  3383. $recipient = $this->createMock(IUser::class);
  3384. $group->method('inGroup')->with($recipient)->willReturn(true);
  3385. $this->groupManager->method('get')->with('group')->willReturn($group);
  3386. $this->userManager->method('get')->with('recipient')->willReturn($recipient);
  3387. $this->defaultProvider->method('move')->with($share, 'recipient')->willReturnArgument(0);
  3388. $this->manager->moveShare($share, 'recipient');
  3389. $this->addToAssertionCount(1);
  3390. }
  3391. /**
  3392. * @dataProvider dataTestShareProviderExists
  3393. */
  3394. public function testShareProviderExists($shareType, $expected) {
  3395. $factory = $this->getMockBuilder('OCP\Share\IProviderFactory')->getMock();
  3396. $factory->expects($this->any())->method('getProviderForType')
  3397. ->willReturnCallback(function ($id) {
  3398. if ($id === IShare::TYPE_USER) {
  3399. return true;
  3400. }
  3401. throw new Exception\ProviderException();
  3402. });
  3403. $manager = new Manager(
  3404. $this->logger,
  3405. $this->config,
  3406. $this->secureRandom,
  3407. $this->hasher,
  3408. $this->mountManager,
  3409. $this->groupManager,
  3410. $this->l,
  3411. $this->l10nFactory,
  3412. $factory,
  3413. $this->userManager,
  3414. $this->rootFolder,
  3415. $this->eventDispatcher,
  3416. $this->mailer,
  3417. $this->urlGenerator,
  3418. $this->defaults,
  3419. $this->dispatcher,
  3420. $this->userSession,
  3421. $this->knownUserService
  3422. );
  3423. $this->assertSame($expected,
  3424. $manager->shareProviderExists($shareType)
  3425. );
  3426. }
  3427. public function dataTestShareProviderExists() {
  3428. return [
  3429. [IShare::TYPE_USER, true],
  3430. [42, false],
  3431. ];
  3432. }
  3433. public function testGetSharesInFolder() {
  3434. $factory = new DummyFactory2($this->createMock(IServerContainer::class));
  3435. $manager = new Manager(
  3436. $this->logger,
  3437. $this->config,
  3438. $this->secureRandom,
  3439. $this->hasher,
  3440. $this->mountManager,
  3441. $this->groupManager,
  3442. $this->l,
  3443. $this->l10nFactory,
  3444. $factory,
  3445. $this->userManager,
  3446. $this->rootFolder,
  3447. $this->eventDispatcher,
  3448. $this->mailer,
  3449. $this->urlGenerator,
  3450. $this->defaults,
  3451. $this->dispatcher,
  3452. $this->userSession,
  3453. $this->knownUserService
  3454. );
  3455. $factory->setProvider($this->defaultProvider);
  3456. $extraProvider = $this->createMock(IShareProvider::class);
  3457. $factory->setSecondProvider($extraProvider);
  3458. $share1 = $this->createMock(IShare::class);
  3459. $share2 = $this->createMock(IShare::class);
  3460. $share3 = $this->createMock(IShare::class);
  3461. $share4 = $this->createMock(IShare::class);
  3462. $folder = $this->createMock(Folder::class);
  3463. $this->defaultProvider->method('getSharesInFolder')
  3464. ->with(
  3465. $this->equalTo('user'),
  3466. $this->equalTo($folder),
  3467. $this->equalTo(false)
  3468. )->willReturn([
  3469. 1 => [$share1],
  3470. 2 => [$share2],
  3471. ]);
  3472. $extraProvider->method('getSharesInFolder')
  3473. ->with(
  3474. $this->equalTo('user'),
  3475. $this->equalTo($folder),
  3476. $this->equalTo(false)
  3477. )->willReturn([
  3478. 2 => [$share3],
  3479. 3 => [$share4],
  3480. ]);
  3481. $result = $manager->getSharesInFolder('user', $folder, false);
  3482. $expects = [
  3483. 1 => [$share1],
  3484. 2 => [$share2, $share3],
  3485. 3 => [$share4],
  3486. ];
  3487. $this->assertSame($expects, $result);
  3488. }
  3489. public function testGetAccessList() {
  3490. $factory = new DummyFactory2($this->createMock(IServerContainer::class));
  3491. $manager = new Manager(
  3492. $this->logger,
  3493. $this->config,
  3494. $this->secureRandom,
  3495. $this->hasher,
  3496. $this->mountManager,
  3497. $this->groupManager,
  3498. $this->l,
  3499. $this->l10nFactory,
  3500. $factory,
  3501. $this->userManager,
  3502. $this->rootFolder,
  3503. $this->eventDispatcher,
  3504. $this->mailer,
  3505. $this->urlGenerator,
  3506. $this->defaults,
  3507. $this->dispatcher,
  3508. $this->userSession,
  3509. $this->knownUserService
  3510. );
  3511. $factory->setProvider($this->defaultProvider);
  3512. $extraProvider = $this->createMock(IShareProvider::class);
  3513. $factory->setSecondProvider($extraProvider);
  3514. $nodeOwner = $this->createMock(IUser::class);
  3515. $nodeOwner->expects($this->once())
  3516. ->method('getUID')
  3517. ->willReturn('user1');
  3518. $node = $this->createMock(Node::class);
  3519. $node->expects($this->once())
  3520. ->method('getOwner')
  3521. ->willReturn($nodeOwner);
  3522. $node->method('getId')
  3523. ->willReturn(42);
  3524. $userFolder = $this->createMock(Folder::class);
  3525. $file = $this->createMock(File::class);
  3526. $folder = $this->createMock(Folder::class);
  3527. $owner = $this->createMock(IUser::class);
  3528. $owner->expects($this->once())
  3529. ->method('getUID')
  3530. ->willReturn('owner');
  3531. $file->method('getParent')
  3532. ->willReturn($folder);
  3533. $file->method('getPath')
  3534. ->willReturn('/owner/files/folder/file');
  3535. $file->method('getOwner')
  3536. ->willReturn($owner);
  3537. $file->method('getId')
  3538. ->willReturn(23);
  3539. $folder->method('getParent')
  3540. ->willReturn($userFolder);
  3541. $folder->method('getPath')
  3542. ->willReturn('/owner/files/folder');
  3543. $userFolder->method('getById')
  3544. ->with($this->equalTo(42))
  3545. ->willReturn([12 => $file]);
  3546. $userFolder->method('getPath')
  3547. ->willReturn('/user1/files');
  3548. $this->userManager->method('userExists')
  3549. ->with($this->equalTo('user1'))
  3550. ->willReturn(true);
  3551. $this->defaultProvider->method('getAccessList')
  3552. ->with(
  3553. $this->equalTo([$file, $folder]),
  3554. false
  3555. )
  3556. ->willReturn([
  3557. 'users' => [
  3558. 'user1',
  3559. 'user2',
  3560. 'user3',
  3561. '123456',
  3562. ],
  3563. 'public' => true,
  3564. ]);
  3565. $extraProvider->method('getAccessList')
  3566. ->with(
  3567. $this->equalTo([$file, $folder]),
  3568. false
  3569. )
  3570. ->willReturn([
  3571. 'users' => [
  3572. 'user3',
  3573. 'user4',
  3574. 'user5',
  3575. '234567',
  3576. ],
  3577. 'remote' => true,
  3578. ]);
  3579. $this->rootFolder->method('getUserFolder')
  3580. ->with($this->equalTo('user1'))
  3581. ->willReturn($userFolder);
  3582. $expected = [
  3583. 'users' => ['owner', 'user1', 'user2', 'user3', '123456','user4', 'user5', '234567'],
  3584. 'remote' => true,
  3585. 'public' => true,
  3586. ];
  3587. $result = $manager->getAccessList($node, true, false);
  3588. $this->assertSame($expected['public'], $result['public']);
  3589. $this->assertSame($expected['remote'], $result['remote']);
  3590. $this->assertSame($expected['users'], $result['users']);
  3591. }
  3592. public function testGetAccessListWithCurrentAccess() {
  3593. $factory = new DummyFactory2($this->createMock(IServerContainer::class));
  3594. $manager = new Manager(
  3595. $this->logger,
  3596. $this->config,
  3597. $this->secureRandom,
  3598. $this->hasher,
  3599. $this->mountManager,
  3600. $this->groupManager,
  3601. $this->l,
  3602. $this->l10nFactory,
  3603. $factory,
  3604. $this->userManager,
  3605. $this->rootFolder,
  3606. $this->eventDispatcher,
  3607. $this->mailer,
  3608. $this->urlGenerator,
  3609. $this->defaults,
  3610. $this->dispatcher,
  3611. $this->userSession,
  3612. $this->knownUserService
  3613. );
  3614. $factory->setProvider($this->defaultProvider);
  3615. $extraProvider = $this->createMock(IShareProvider::class);
  3616. $factory->setSecondProvider($extraProvider);
  3617. $nodeOwner = $this->createMock(IUser::class);
  3618. $nodeOwner->expects($this->once())
  3619. ->method('getUID')
  3620. ->willReturn('user1');
  3621. $node = $this->createMock(Node::class);
  3622. $node->expects($this->once())
  3623. ->method('getOwner')
  3624. ->willReturn($nodeOwner);
  3625. $node->method('getId')
  3626. ->willReturn(42);
  3627. $userFolder = $this->createMock(Folder::class);
  3628. $file = $this->createMock(File::class);
  3629. $owner = $this->createMock(IUser::class);
  3630. $owner->expects($this->once())
  3631. ->method('getUID')
  3632. ->willReturn('owner');
  3633. $folder = $this->createMock(Folder::class);
  3634. $file->method('getParent')
  3635. ->willReturn($folder);
  3636. $file->method('getPath')
  3637. ->willReturn('/owner/files/folder/file');
  3638. $file->method('getOwner')
  3639. ->willReturn($owner);
  3640. $file->method('getId')
  3641. ->willReturn(23);
  3642. $folder->method('getParent')
  3643. ->willReturn($userFolder);
  3644. $folder->method('getPath')
  3645. ->willReturn('/owner/files/folder');
  3646. $userFolder->method('getById')
  3647. ->with($this->equalTo(42))
  3648. ->willReturn([42 => $file]);
  3649. $userFolder->method('getPath')
  3650. ->willReturn('/user1/files');
  3651. $this->userManager->method('userExists')
  3652. ->with($this->equalTo('user1'))
  3653. ->willReturn(true);
  3654. $this->defaultProvider->method('getAccessList')
  3655. ->with(
  3656. $this->equalTo([$file, $folder]),
  3657. true
  3658. )
  3659. ->willReturn([
  3660. 'users' => [
  3661. 'user1' => [],
  3662. 'user2' => [],
  3663. 'user3' => [],
  3664. '123456' => [],
  3665. ],
  3666. 'public' => true,
  3667. ]);
  3668. $extraProvider->method('getAccessList')
  3669. ->with(
  3670. $this->equalTo([$file, $folder]),
  3671. true
  3672. )
  3673. ->willReturn([
  3674. 'users' => [
  3675. 'user3' => [],
  3676. 'user4' => [],
  3677. 'user5' => [],
  3678. '234567' => [],
  3679. ],
  3680. 'remote' => [
  3681. 'remote1',
  3682. ],
  3683. ]);
  3684. $this->rootFolder->method('getUserFolder')
  3685. ->with($this->equalTo('user1'))
  3686. ->willReturn($userFolder);
  3687. $expected = [
  3688. 'users' => [
  3689. 'owner' => [
  3690. 'node_id' => 23,
  3691. 'node_path' => '/folder/file'
  3692. ]
  3693. , 'user1' => [], 'user2' => [], 'user3' => [], '123456' => [], 'user4' => [], 'user5' => [], '234567' => []],
  3694. 'remote' => [
  3695. 'remote1',
  3696. ],
  3697. 'public' => true,
  3698. ];
  3699. $result = $manager->getAccessList($node, true, true);
  3700. $this->assertSame($expected['public'], $result['public']);
  3701. $this->assertSame($expected['remote'], $result['remote']);
  3702. $this->assertSame($expected['users'], $result['users']);
  3703. }
  3704. public function testGetAllShares() {
  3705. $factory = new DummyFactory2($this->createMock(IServerContainer::class));
  3706. $manager = new Manager(
  3707. $this->logger,
  3708. $this->config,
  3709. $this->secureRandom,
  3710. $this->hasher,
  3711. $this->mountManager,
  3712. $this->groupManager,
  3713. $this->l,
  3714. $this->l10nFactory,
  3715. $factory,
  3716. $this->userManager,
  3717. $this->rootFolder,
  3718. $this->eventDispatcher,
  3719. $this->mailer,
  3720. $this->urlGenerator,
  3721. $this->defaults,
  3722. $this->dispatcher,
  3723. $this->userSession,
  3724. $this->knownUserService
  3725. );
  3726. $factory->setProvider($this->defaultProvider);
  3727. $extraProvider = $this->createMock(IShareProvider::class);
  3728. $factory->setSecondProvider($extraProvider);
  3729. $share1 = $this->createMock(IShare::class);
  3730. $share2 = $this->createMock(IShare::class);
  3731. $share3 = $this->createMock(IShare::class);
  3732. $share4 = $this->createMock(IShare::class);
  3733. $this->defaultProvider->method('getAllShares')
  3734. ->willReturnCallback(function () use ($share1, $share2) {
  3735. yield $share1;
  3736. yield $share2;
  3737. });
  3738. $extraProvider->method('getAllShares')
  3739. ->willReturnCallback(function () use ($share3, $share4) {
  3740. yield $share3;
  3741. yield $share4;
  3742. });
  3743. // "yield from", used in "getAllShares()", does not reset the keys, so
  3744. // "use_keys" has to be disabled to collect all the values while
  3745. // ignoring the keys returned by the generator.
  3746. $result = iterator_to_array($manager->getAllShares(), $use_keys = false);
  3747. $expects = [$share1, $share2, $share3, $share4];
  3748. $this->assertSame($expects, $result);
  3749. }
  3750. public function dataCurrentUserCanEnumerateTargetUser(): array {
  3751. return [
  3752. 'Full match guest' => [true, true, false, false, false, false, false, true],
  3753. 'Full match user' => [false, true, false, false, false, false, false, true],
  3754. 'Enumeration off guest' => [true, false, false, false, false, false, false, false],
  3755. 'Enumeration off user' => [false, false, false, false, false, false, false, false],
  3756. 'Enumeration guest' => [true, false, true, false, false, false, false, true],
  3757. 'Enumeration user' => [false, false, true, false, false, false, false, true],
  3758. // Restricted enumerations guests never works
  3759. 'Guest phone' => [true, false, true, true, false, false, false, false],
  3760. 'Guest group' => [true, false, true, false, true, false, false, false],
  3761. 'Guest both' => [true, false, true, true, true, false, false, false],
  3762. // Restricted enumerations users
  3763. 'User phone but not known' => [false, false, true, true, false, false, false, false],
  3764. 'User phone known' => [false, false, true, true, false, true, false, true],
  3765. 'User group but no match' => [false, false, true, false, true, false, false, false],
  3766. 'User group with match' => [false, false, true, false, true, false, true, true],
  3767. ];
  3768. }
  3769. /**
  3770. * @dataProvider dataCurrentUserCanEnumerateTargetUser
  3771. * @param bool $expected
  3772. */
  3773. public function testCurrentUserCanEnumerateTargetUser(bool $currentUserIsGuest, bool $allowEnumerationFullMatch, bool $allowEnumeration, bool $limitEnumerationToPhone, bool $limitEnumerationToGroups, bool $isKnownToUser, bool $haveCommonGroup, bool $expected): void {
  3774. /** @var IManager|MockObject $manager */
  3775. $manager = $this->createManagerMock()
  3776. ->setMethods([
  3777. 'allowEnumerationFullMatch',
  3778. 'allowEnumeration',
  3779. 'limitEnumerationToPhone',
  3780. 'limitEnumerationToGroups',
  3781. ])
  3782. ->getMock();
  3783. $manager->method('allowEnumerationFullMatch')
  3784. ->willReturn($allowEnumerationFullMatch);
  3785. $manager->method('allowEnumeration')
  3786. ->willReturn($allowEnumeration);
  3787. $manager->method('limitEnumerationToPhone')
  3788. ->willReturn($limitEnumerationToPhone);
  3789. $manager->method('limitEnumerationToGroups')
  3790. ->willReturn($limitEnumerationToGroups);
  3791. $this->knownUserService->method('isKnownToUser')
  3792. ->with('current', 'target')
  3793. ->willReturn($isKnownToUser);
  3794. $currentUser = null;
  3795. if (!$currentUserIsGuest) {
  3796. $currentUser = $this->createMock(IUser::class);
  3797. $currentUser->method('getUID')
  3798. ->willReturn('current');
  3799. }
  3800. $targetUser = $this->createMock(IUser::class);
  3801. $targetUser->method('getUID')
  3802. ->willReturn('target');
  3803. if ($haveCommonGroup) {
  3804. $this->groupManager->method('getUserGroupIds')
  3805. ->willReturnMap([
  3806. [$targetUser, ['gid1', 'gid2']],
  3807. [$currentUser, ['gid2', 'gid3']],
  3808. ]);
  3809. } else {
  3810. $this->groupManager->method('getUserGroupIds')
  3811. ->willReturnMap([
  3812. [$targetUser, ['gid1', 'gid2']],
  3813. [$currentUser, ['gid3', 'gid4']],
  3814. ]);
  3815. }
  3816. $this->assertSame($expected, $manager->currentUserCanEnumerateTargetUser($currentUser, $targetUser));
  3817. }
  3818. }
  3819. class DummyFactory implements IProviderFactory {
  3820. /** @var IShareProvider */
  3821. protected $provider;
  3822. public function __construct(\OCP\IServerContainer $serverContainer) {
  3823. }
  3824. /**
  3825. * @param IShareProvider $provider
  3826. */
  3827. public function setProvider($provider) {
  3828. $this->provider = $provider;
  3829. }
  3830. /**
  3831. * @param string $id
  3832. * @return IShareProvider
  3833. */
  3834. public function getProvider($id) {
  3835. return $this->provider;
  3836. }
  3837. /**
  3838. * @param int $shareType
  3839. * @return IShareProvider
  3840. */
  3841. public function getProviderForType($shareType) {
  3842. return $this->provider;
  3843. }
  3844. /**
  3845. * @return IShareProvider[]
  3846. */
  3847. public function getAllProviders() {
  3848. return [$this->provider];
  3849. }
  3850. public function registerProvider(string $shareProvier): void {
  3851. }
  3852. }
  3853. class DummyFactory2 extends DummyFactory {
  3854. /** @var IShareProvider */
  3855. private $provider2;
  3856. /**
  3857. * @param IShareProvider $provider
  3858. */
  3859. public function setSecondProvider($provider) {
  3860. $this->provider2 = $provider;
  3861. }
  3862. public function getAllProviders() {
  3863. return [$this->provider, $this->provider2];
  3864. }
  3865. public function registerProvider(string $shareProvier): void {
  3866. }
  3867. }