ManagerTest.php 149 KB

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