ManagerTest.php 149 KB

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