ManagerTest.php 160 KB

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