ManagerTest.php 150 KB

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