ManagerTest.php 147 KB

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