1
0

ShareAPIControllerTest.php 151 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Bjoern Schiessle <bjoern@schiessle.org>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Daniel Calviño Sánchez <danxuliu@gmail.com>
  8. * @author Georg Ehrke <oc.list@georgehrke.com>
  9. * @author Joas Schilling <coding@schilljs.com>
  10. * @author John Molakvoæ <skjnldsv@protonmail.com>
  11. * @author Julius Härtl <jus@bitgrid.net>
  12. * @author Maxence Lange <maxence@artificial-owl.com>
  13. * @author Maxence Lange <maxence@nextcloud.com>
  14. * @author Morris Jobke <hey@morrisjobke.de>
  15. * @author Richard Steinmetz <richard@steinmetz.cloud>
  16. * @author Roeland Jago Douma <roeland@famdouma.nl>
  17. * @author Thomas Müller <thomas.mueller@tmit.eu>
  18. * @author Valdnet <47037905+Valdnet@users.noreply.github.com>
  19. * @author Vincent Petry <vincent@nextcloud.com>
  20. *
  21. * @license AGPL-3.0
  22. *
  23. * This code is free software: you can redistribute it and/or modify
  24. * it under the terms of the GNU Affero General Public License, version 3,
  25. * as published by the Free Software Foundation.
  26. *
  27. * This program is distributed in the hope that it will be useful,
  28. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  29. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  30. * GNU Affero General Public License for more details.
  31. *
  32. * You should have received a copy of the GNU Affero General Public License, version 3,
  33. * along with this program. If not, see <http://www.gnu.org/licenses/>
  34. *
  35. */
  36. namespace OCA\Files_Sharing\Tests\Controller;
  37. use OCA\Files_Sharing\Controller\ShareAPIController;
  38. use OCP\App\IAppManager;
  39. use OCP\AppFramework\Http\DataResponse;
  40. use OCP\AppFramework\OCS\OCSException;
  41. use OCP\AppFramework\OCS\OCSNotFoundException;
  42. use OCP\Files\File;
  43. use OCP\Files\Folder;
  44. use OCP\Files\IRootFolder;
  45. use OCP\Files\Mount\IMountPoint;
  46. use OCP\Files\NotFoundException;
  47. use OCP\Files\Storage;
  48. use OCP\IConfig;
  49. use OCP\IDateTimeZone;
  50. use OCP\IGroup;
  51. use OCP\IGroupManager;
  52. use OCP\IL10N;
  53. use OCP\IPreview;
  54. use OCP\IRequest;
  55. use OCP\IServerContainer;
  56. use OCP\IURLGenerator;
  57. use OCP\IUser;
  58. use OCP\IUserManager;
  59. use OCP\Lock\LockedException;
  60. use OCP\Share\Exceptions\GenericShareException;
  61. use OCP\Share\IAttributes as IShareAttributes;
  62. use OCP\Share\IManager;
  63. use OCP\Share\IShare;
  64. use OCP\UserStatus\IManager as IUserStatusManager;
  65. use Test\TestCase;
  66. /**
  67. * Class ShareAPIControllerTest
  68. *
  69. * @package OCA\Files_Sharing\Tests\Controller
  70. * @group DB
  71. */
  72. class ShareAPIControllerTest extends TestCase {
  73. /** @var string */
  74. private $appName = 'files_sharing';
  75. /** @var \OC\Share20\Manager|\PHPUnit\Framework\MockObject\MockObject */
  76. private $shareManager;
  77. /** @var IGroupManager|\PHPUnit\Framework\MockObject\MockObject */
  78. private $groupManager;
  79. /** @var IUserManager|\PHPUnit\Framework\MockObject\MockObject */
  80. private $userManager;
  81. /** @var IRequest|\PHPUnit\Framework\MockObject\MockObject */
  82. private $request;
  83. /** @var IRootFolder|\PHPUnit\Framework\MockObject\MockObject */
  84. private $rootFolder;
  85. /** @var IURLGenerator|\PHPUnit\Framework\MockObject\MockObject */
  86. private $urlGenerator;
  87. /** @var string|\PHPUnit\Framework\MockObject\MockObject */
  88. private $currentUser;
  89. /** @var ShareAPIController */
  90. private $ocs;
  91. /** @var IL10N|\PHPUnit\Framework\MockObject\MockObject */
  92. private $l;
  93. /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */
  94. private $config;
  95. /** @var IAppManager|\PHPUnit\Framework\MockObject\MockObject */
  96. private $appManager;
  97. /** @var IServerContainer|\PHPUnit\Framework\MockObject\MockObject */
  98. private $serverContainer;
  99. /** @var IUserStatusManager|\PHPUnit\Framework\MockObject\MockObject */
  100. private $userStatusManager;
  101. /** @var IPreview|\PHPUnit\Framework\MockObject\MockObject */
  102. private $previewManager;
  103. /** @var IDateTimeZone|\PHPUnit\Framework\MockObject\MockObject */
  104. private $dateTimeZone;
  105. protected function setUp(): void {
  106. $this->shareManager = $this->createMock(IManager::class);
  107. $this->shareManager
  108. ->expects($this->any())
  109. ->method('shareApiEnabled')
  110. ->willReturn(true);
  111. $this->shareManager
  112. ->expects($this->any())
  113. ->method('shareProviderExists')->willReturn(true);
  114. $this->groupManager = $this->createMock(IGroupManager::class);
  115. $this->userManager = $this->createMock(IUserManager::class);
  116. $this->request = $this->createMock(IRequest::class);
  117. $this->rootFolder = $this->createMock(IRootFolder::class);
  118. $this->urlGenerator = $this->createMock(IURLGenerator::class);
  119. $this->currentUser = 'currentUser';
  120. $this->l = $this->createMock(IL10N::class);
  121. $this->l->method('t')
  122. ->willReturnCallback(function ($text, $parameters = []) {
  123. return vsprintf($text, $parameters);
  124. });
  125. $this->config = $this->createMock(IConfig::class);
  126. $this->appManager = $this->createMock(IAppManager::class);
  127. $this->serverContainer = $this->createMock(IServerContainer::class);
  128. $this->userStatusManager = $this->createMock(IUserStatusManager::class);
  129. $this->previewManager = $this->createMock(IPreview::class);
  130. $this->previewManager->method('isAvailable')
  131. ->willReturnCallback(function ($fileInfo) {
  132. return $fileInfo->getMimeType() === 'mimeWithPreview';
  133. });
  134. $this->dateTimeZone = $this->createMock(IDateTimeZone::class);
  135. $this->ocs = new ShareAPIController(
  136. $this->appName,
  137. $this->request,
  138. $this->shareManager,
  139. $this->groupManager,
  140. $this->userManager,
  141. $this->rootFolder,
  142. $this->urlGenerator,
  143. $this->currentUser,
  144. $this->l,
  145. $this->config,
  146. $this->appManager,
  147. $this->serverContainer,
  148. $this->userStatusManager,
  149. $this->previewManager,
  150. $this->dateTimeZone,
  151. );
  152. }
  153. /**
  154. * @return ShareAPIController|\PHPUnit\Framework\MockObject\MockObject
  155. */
  156. private function mockFormatShare() {
  157. return $this->getMockBuilder(ShareAPIController::class)
  158. ->setConstructorArgs([
  159. $this->appName,
  160. $this->request,
  161. $this->shareManager,
  162. $this->groupManager,
  163. $this->userManager,
  164. $this->rootFolder,
  165. $this->urlGenerator,
  166. $this->currentUser,
  167. $this->l,
  168. $this->config,
  169. $this->appManager,
  170. $this->serverContainer,
  171. $this->userStatusManager,
  172. $this->previewManager,
  173. $this->dateTimeZone,
  174. ])->setMethods(['formatShare'])
  175. ->getMock();
  176. }
  177. private function newShare() {
  178. return \OC::$server->getShareManager()->newShare();
  179. }
  180. private function mockShareAttributes() {
  181. $formattedShareAttributes = [
  182. [
  183. 'scope' => 'permissions',
  184. 'key' => 'download',
  185. 'enabled' => true
  186. ]
  187. ];
  188. $shareAttributes = $this->createMock(IShareAttributes::class);
  189. $shareAttributes->method('toArray')->willReturn($formattedShareAttributes);
  190. $shareAttributes->method('getAttribute')->with('permissions', 'download')->willReturn(true);
  191. // send both IShare attributes class and expected json string
  192. return [$shareAttributes, \json_encode($formattedShareAttributes)];
  193. }
  194. public function testDeleteShareShareNotFound() {
  195. $this->expectException(\OCP\AppFramework\OCS\OCSNotFoundException::class);
  196. $this->expectExceptionMessage('Wrong share ID, share does not exist');
  197. $this->shareManager
  198. ->expects($this->exactly(7))
  199. ->method('getShareById')
  200. ->willReturnCallback(function ($id) {
  201. if ($id === 'ocinternal:42' || $id === 'ocRoomShare:42' || $id === 'ocFederatedSharing:42' || $id === 'ocCircleShare:42' || $id === 'ocMailShare:42' || $id === 'deck:42' || $id === 'sciencemesh:42') {
  202. throw new \OCP\Share\Exceptions\ShareNotFound();
  203. } else {
  204. throw new \Exception();
  205. }
  206. });
  207. $this->shareManager->method('outgoingServer2ServerSharesAllowed')->willReturn(true);
  208. $this->ocs->deleteShare(42);
  209. }
  210. public function testDeleteShare() {
  211. $node = $this->getMockBuilder(File::class)->getMock();
  212. $share = $this->newShare();
  213. $share->setSharedBy($this->currentUser)
  214. ->setNode($node);
  215. $this->shareManager
  216. ->expects($this->once())
  217. ->method('getShareById')
  218. ->with('ocinternal:42')
  219. ->willReturn($share);
  220. $this->shareManager
  221. ->expects($this->once())
  222. ->method('deleteShare')
  223. ->with($share);
  224. $node->expects($this->once())
  225. ->method('lock')
  226. ->with(\OCP\Lock\ILockingProvider::LOCK_SHARED);
  227. $expected = new DataResponse();
  228. $result = $this->ocs->deleteShare(42);
  229. $this->assertInstanceOf(get_class($expected), $result);
  230. $this->assertEquals($expected->getData(), $result->getData());
  231. }
  232. public function testDeleteShareLocked() {
  233. $this->expectException(\OCP\AppFramework\OCS\OCSNotFoundException::class);
  234. $this->expectExceptionMessage('Could not delete share');
  235. $node = $this->getMockBuilder(File::class)->getMock();
  236. $share = $this->newShare();
  237. $share->setNode($node);
  238. $this->shareManager
  239. ->expects($this->once())
  240. ->method('getShareById')
  241. ->with('ocinternal:42')
  242. ->willReturn($share);
  243. $this->shareManager
  244. ->expects($this->never())
  245. ->method('deleteShare')
  246. ->with($share);
  247. $node->expects($this->once())
  248. ->method('lock')
  249. ->with(\OCP\Lock\ILockingProvider::LOCK_SHARED)
  250. ->will($this->throwException(new LockedException('mypath')));
  251. $this->assertFalse($this->invokePrivate($this->ocs, 'canDeleteFromSelf', [$share]));
  252. $this->assertFalse($this->invokePrivate($this->ocs, 'canDeleteShare', [$share]));
  253. $this->ocs->deleteShare(42);
  254. }
  255. /**
  256. * You can always remove a share that was shared with you
  257. */
  258. public function testDeleteShareWithMe() {
  259. $node = $this->getMockBuilder(File::class)->getMock();
  260. $share = $this->newShare();
  261. $share->setSharedWith($this->currentUser)
  262. ->setShareType(IShare::TYPE_USER)
  263. ->setNode($node);
  264. $this->shareManager
  265. ->expects($this->once())
  266. ->method('getShareById')
  267. ->with('ocinternal:42')
  268. ->willReturn($share);
  269. $this->shareManager
  270. ->expects($this->once())
  271. ->method('deleteShare')
  272. ->with($share);
  273. $node->expects($this->once())
  274. ->method('lock')
  275. ->with(\OCP\Lock\ILockingProvider::LOCK_SHARED);
  276. $this->assertFalse($this->invokePrivate($this->ocs, 'canDeleteFromSelf', [$share]));
  277. $this->assertTrue($this->invokePrivate($this->ocs, 'canDeleteShare', [$share]));
  278. $this->ocs->deleteShare(42);
  279. }
  280. /**
  281. * You can always delete a share you own
  282. */
  283. public function testDeleteShareOwner() {
  284. $node = $this->getMockBuilder(File::class)->getMock();
  285. $share = $this->newShare();
  286. $share->setSharedBy($this->currentUser)
  287. ->setNode($node);
  288. $this->shareManager
  289. ->expects($this->once())
  290. ->method('getShareById')
  291. ->with('ocinternal:42')
  292. ->willReturn($share);
  293. $this->shareManager
  294. ->expects($this->once())
  295. ->method('deleteShare')
  296. ->with($share);
  297. $node->expects($this->once())
  298. ->method('lock')
  299. ->with(\OCP\Lock\ILockingProvider::LOCK_SHARED);
  300. $this->assertFalse($this->invokePrivate($this->ocs, 'canDeleteFromSelf', [$share]));
  301. $this->assertTrue($this->invokePrivate($this->ocs, 'canDeleteShare', [$share]));
  302. $this->ocs->deleteShare(42);
  303. }
  304. /**
  305. * You can always delete a share when you own
  306. * the file path it belong to
  307. */
  308. public function testDeleteShareFileOwner() {
  309. $node = $this->getMockBuilder(File::class)->getMock();
  310. $node->method('getId')->willReturn(1);
  311. $share = $this->newShare();
  312. $share->setShareOwner($this->currentUser)
  313. ->setNode($node);
  314. $this->shareManager
  315. ->expects($this->once())
  316. ->method('getShareById')
  317. ->with('ocinternal:42')
  318. ->willReturn($share);
  319. $this->shareManager
  320. ->expects($this->once())
  321. ->method('deleteShare')
  322. ->with($share);
  323. $node->expects($this->once())
  324. ->method('lock')
  325. ->with(\OCP\Lock\ILockingProvider::LOCK_SHARED);
  326. $this->assertFalse($this->invokePrivate($this->ocs, 'canDeleteFromSelf', [$share]));
  327. $this->assertTrue($this->invokePrivate($this->ocs, 'canDeleteShare', [$share]));
  328. $this->ocs->deleteShare(42);
  329. }
  330. /**
  331. * You can remove (the mountpoint, not the share)
  332. * a share if you're in the group the share is shared with
  333. */
  334. public function testDeleteSharedWithMyGroup() {
  335. $node = $this->getMockBuilder(File::class)->getMock();
  336. $node->method('getId')->willReturn(1);
  337. $share = $this->newShare();
  338. $share->setShareType(IShare::TYPE_GROUP)
  339. ->setSharedWith('group')
  340. ->setNode($node);
  341. $this->shareManager
  342. ->expects($this->once())
  343. ->method('getShareById')
  344. ->with('ocinternal:42')
  345. ->willReturn($share);
  346. // canDeleteShareFromSelf
  347. $user = $this->createMock(IUser::class);
  348. $group = $this->getMockBuilder('OCP\IGroup')->getMock();
  349. $this->groupManager
  350. ->method('get')
  351. ->with('group')
  352. ->willReturn($group);
  353. $this->userManager
  354. ->method('get')
  355. ->with($this->currentUser)
  356. ->willReturn($user);
  357. $group->method('inGroup')
  358. ->with($user)
  359. ->willReturn(true);
  360. $node->expects($this->once())
  361. ->method('lock')
  362. ->with(\OCP\Lock\ILockingProvider::LOCK_SHARED);
  363. $userFolder = $this->getMockBuilder('OCP\Files\Folder')->getMock();
  364. $this->rootFolder->method('getUserFolder')
  365. ->with($this->currentUser)
  366. ->willReturn($userFolder);
  367. $userFolder->method('getFirstNodeById')
  368. ->with($share->getNodeId())
  369. ->willReturn($share->getNode());
  370. $this->shareManager->expects($this->once())
  371. ->method('deleteFromSelf')
  372. ->with($share, $this->currentUser);
  373. $this->shareManager->expects($this->never())
  374. ->method('deleteShare');
  375. $this->assertTrue($this->invokePrivate($this->ocs, 'canDeleteShareFromSelf', [$share]));
  376. $this->assertFalse($this->invokePrivate($this->ocs, 'canDeleteShare', [$share]));
  377. $this->ocs->deleteShare(42);
  378. }
  379. /**
  380. * You cannot remove a share if you're not
  381. * in the group the share is shared with
  382. */
  383. public function testDeleteSharedWithGroupIDontBelongTo() {
  384. $this->expectException(\OCP\AppFramework\OCS\OCSNotFoundException::class);
  385. $this->expectExceptionMessage('Wrong share ID, share does not exist');
  386. $node = $this->getMockBuilder(File::class)->getMock();
  387. $node->method('getId')->willReturn(42);
  388. $share = $this->newShare();
  389. $share->setShareType(IShare::TYPE_GROUP)
  390. ->setSharedWith('group')
  391. ->setNode($node);
  392. $this->shareManager
  393. ->expects($this->once())
  394. ->method('getShareById')
  395. ->with('ocinternal:42')
  396. ->willReturn($share);
  397. // canDeleteShareFromSelf
  398. $user = $this->createMock(IUser::class);
  399. $group = $this->getMockBuilder('OCP\IGroup')->getMock();
  400. $this->groupManager
  401. ->method('get')
  402. ->with('group')
  403. ->willReturn($group);
  404. $this->userManager
  405. ->method('get')
  406. ->with($this->currentUser)
  407. ->willReturn($user);
  408. $group->method('inGroup')
  409. ->with($user)
  410. ->willReturn(false);
  411. $node->expects($this->once())
  412. ->method('lock')
  413. ->with(\OCP\Lock\ILockingProvider::LOCK_SHARED);
  414. $userFolder = $this->getMockBuilder('OCP\Files\Folder')->getMock();
  415. $this->rootFolder->method('getUserFolder')
  416. ->with($this->currentUser)
  417. ->willReturn($userFolder);
  418. $userFolder->method('getFirstNodeById')
  419. ->with($share->getNodeId())
  420. ->willReturn($share->getNode());
  421. $this->shareManager->expects($this->never())
  422. ->method('deleteFromSelf');
  423. $this->shareManager->expects($this->never())
  424. ->method('deleteShare');
  425. $this->assertFalse($this->invokePrivate($this->ocs, 'canDeleteShareFromSelf', [$share]));
  426. $this->assertFalse($this->invokePrivate($this->ocs, 'canDeleteShare', [$share]));
  427. $this->ocs->deleteShare(42);
  428. }
  429. /*
  430. * FIXME: Enable once we have a federated Share Provider
  431. public function testGetGetShareNotExists() {
  432. $this->shareManager
  433. ->expects($this->once())
  434. ->method('getShareById')
  435. ->with('ocinternal:42', 'currentUser')
  436. ->will($this->throwException(new \OC\Share20\Exception\ShareNotFound()));
  437. $expected = new \OC\OCS\Result(null, 404, 'wrong share ID, share does not exist.');
  438. $this->assertEquals($expected, $this->ocs->getShare(42));
  439. }
  440. */
  441. public function createShare($id, $shareType, $sharedWith, $sharedBy, $shareOwner, $path, $permissions,
  442. $shareTime, $expiration, $parent, $target, $mail_send, $note = '', $token = null,
  443. $password = null, $label = '', $attributes = null) {
  444. $share = $this->getMockBuilder(IShare::class)->getMock();
  445. $share->method('getId')->willReturn($id);
  446. $share->method('getShareType')->willReturn($shareType);
  447. $share->method('getSharedWith')->willReturn($sharedWith);
  448. $share->method('getSharedBy')->willReturn($sharedBy);
  449. $share->method('getShareOwner')->willReturn($shareOwner);
  450. $share->method('getNode')->willReturn($path);
  451. $share->method('getPermissions')->willReturn($permissions);
  452. $share->method('getNote')->willReturn($note);
  453. $share->method('getLabel')->willReturn($label);
  454. $share->method('getAttributes')->willReturn($attributes);
  455. $time = new \DateTime();
  456. $time->setTimestamp($shareTime);
  457. $share->method('getShareTime')->willReturn($time);
  458. $share->method('getExpirationDate')->willReturn($expiration);
  459. $share->method('getTarget')->willReturn($target);
  460. $share->method('getMailSend')->willReturn($mail_send);
  461. $share->method('getToken')->willReturn($token);
  462. $share->method('getPassword')->willReturn($password);
  463. if ($shareType === IShare::TYPE_USER ||
  464. $shareType === IShare::TYPE_GROUP ||
  465. $shareType === IShare::TYPE_LINK) {
  466. $share->method('getFullId')->willReturn('ocinternal:'.$id);
  467. }
  468. return $share;
  469. }
  470. public function dataGetShare() {
  471. $data = [];
  472. $cache = $this->getMockBuilder('OC\Files\Cache\Cache')
  473. ->disableOriginalConstructor()
  474. ->getMock();
  475. $cache->method('getNumericStorageId')->willReturn(101);
  476. $storage = $this->getMockBuilder(Storage::class)
  477. ->disableOriginalConstructor()
  478. ->getMock();
  479. $storage->method('getId')->willReturn('STORAGE');
  480. $storage->method('getCache')->willReturn($cache);
  481. $parentFolder = $this->getMockBuilder('OCP\Files\Folder')->getMock();
  482. $parentFolder->method('getId')->willReturn(3);
  483. $file = $this->getMockBuilder('OCP\Files\File')->getMock();
  484. $file->method('getId')->willReturn(1);
  485. $file->method('getPath')->willReturn('file');
  486. $file->method('getStorage')->willReturn($storage);
  487. $file->method('getParent')->willReturn($parentFolder);
  488. $file->method('getSize')->willReturn(123465);
  489. $file->method('getMTime')->willReturn(1234567890);
  490. $file->method('getMimeType')->willReturn('myMimeType');
  491. $folder = $this->getMockBuilder('OCP\Files\Folder')->getMock();
  492. $folder->method('getId')->willReturn(2);
  493. $folder->method('getPath')->willReturn('folder');
  494. $folder->method('getStorage')->willReturn($storage);
  495. $folder->method('getParent')->willReturn($parentFolder);
  496. $folder->method('getSize')->willReturn(123465);
  497. $folder->method('getMTime')->willReturn(1234567890);
  498. $folder->method('getMimeType')->willReturn('myFolderMimeType');
  499. [$shareAttributes, $shareAttributesReturnJson] = $this->mockShareAttributes();
  500. // File shared with user
  501. $share = $this->createShare(
  502. 100,
  503. IShare::TYPE_USER,
  504. 'userId',
  505. 'initiatorId',
  506. 'ownerId',
  507. $file,
  508. 4,
  509. 5,
  510. null,
  511. 6,
  512. 'target',
  513. 0,
  514. 'personal note',
  515. $shareAttributes,
  516. );
  517. $expected = [
  518. 'id' => 100,
  519. 'share_type' => IShare::TYPE_USER,
  520. 'share_with' => 'userId',
  521. 'share_with_displayname' => 'userDisplay',
  522. 'share_with_displayname_unique' => 'userId@example.com',
  523. 'uid_owner' => 'initiatorId',
  524. 'displayname_owner' => 'initiatorDisplay',
  525. 'item_type' => 'file',
  526. 'item_source' => 1,
  527. 'file_source' => 1,
  528. 'file_target' => 'target',
  529. 'file_parent' => 3,
  530. 'token' => null,
  531. 'expiration' => null,
  532. 'permissions' => 4,
  533. 'attributes' => $shareAttributesReturnJson,
  534. 'stime' => 5,
  535. 'parent' => null,
  536. 'storage_id' => 'STORAGE',
  537. 'path' => 'file',
  538. 'storage' => 101,
  539. 'mail_send' => 0,
  540. 'uid_file_owner' => 'ownerId',
  541. 'note' => 'personal note',
  542. 'label' => '',
  543. 'displayname_file_owner' => 'ownerDisplay',
  544. 'mimetype' => 'myMimeType',
  545. 'has_preview' => false,
  546. 'hide_download' => 0,
  547. 'can_edit' => false,
  548. 'can_delete' => false,
  549. 'item_size' => 123465,
  550. 'item_mtime' => 1234567890,
  551. 'attributes' => null,
  552. ];
  553. $data[] = [$share, $expected];
  554. // Folder shared with group
  555. $share = $this->createShare(
  556. 101,
  557. IShare::TYPE_GROUP,
  558. 'groupId',
  559. 'initiatorId',
  560. 'ownerId',
  561. $folder,
  562. 4,
  563. 5,
  564. null,
  565. 6,
  566. 'target',
  567. 0,
  568. 'personal note',
  569. $shareAttributes,
  570. );
  571. $expected = [
  572. 'id' => 101,
  573. 'share_type' => IShare::TYPE_GROUP,
  574. 'share_with' => 'groupId',
  575. 'share_with_displayname' => 'groupId',
  576. 'uid_owner' => 'initiatorId',
  577. 'displayname_owner' => 'initiatorDisplay',
  578. 'item_type' => 'folder',
  579. 'item_source' => 2,
  580. 'file_source' => 2,
  581. 'file_target' => 'target',
  582. 'file_parent' => 3,
  583. 'token' => null,
  584. 'expiration' => null,
  585. 'permissions' => 4,
  586. 'attributes' => $shareAttributesReturnJson,
  587. 'stime' => 5,
  588. 'parent' => null,
  589. 'storage_id' => 'STORAGE',
  590. 'path' => 'folder',
  591. 'storage' => 101,
  592. 'mail_send' => 0,
  593. 'uid_file_owner' => 'ownerId',
  594. 'note' => 'personal note',
  595. 'label' => '',
  596. 'displayname_file_owner' => 'ownerDisplay',
  597. 'mimetype' => 'myFolderMimeType',
  598. 'has_preview' => false,
  599. 'hide_download' => 0,
  600. 'can_edit' => false,
  601. 'can_delete' => false,
  602. 'item_size' => 123465,
  603. 'item_mtime' => 1234567890,
  604. 'attributes' => null,
  605. ];
  606. $data[] = [$share, $expected];
  607. // File shared by link with Expire
  608. $expire = \DateTime::createFromFormat('Y-m-d h:i:s', '2000-01-02 01:02:03');
  609. $share = $this->createShare(
  610. 101,
  611. IShare::TYPE_LINK,
  612. null,
  613. 'initiatorId',
  614. 'ownerId',
  615. $folder,
  616. 4,
  617. 5,
  618. $expire,
  619. 6,
  620. 'target',
  621. 0,
  622. 'personal note',
  623. 'token',
  624. 'password',
  625. 'first link share'
  626. );
  627. $expected = [
  628. 'id' => 101,
  629. 'share_type' => IShare::TYPE_LINK,
  630. 'password' => 'password',
  631. 'share_with' => 'password',
  632. 'share_with_displayname' => '(Shared link)',
  633. 'send_password_by_talk' => false,
  634. 'uid_owner' => 'initiatorId',
  635. 'displayname_owner' => 'initiatorDisplay',
  636. 'item_type' => 'folder',
  637. 'item_source' => 2,
  638. 'file_source' => 2,
  639. 'file_target' => 'target',
  640. 'file_parent' => 3,
  641. 'token' => 'token',
  642. 'expiration' => '2000-01-02 00:00:00',
  643. 'permissions' => 4,
  644. 'attributes' => null,
  645. 'stime' => 5,
  646. 'parent' => null,
  647. 'storage_id' => 'STORAGE',
  648. 'path' => 'folder',
  649. 'storage' => 101,
  650. 'mail_send' => 0,
  651. 'url' => 'url',
  652. 'uid_file_owner' => 'ownerId',
  653. 'note' => 'personal note',
  654. 'label' => 'first link share',
  655. 'displayname_file_owner' => 'ownerDisplay',
  656. 'mimetype' => 'myFolderMimeType',
  657. 'has_preview' => false,
  658. 'hide_download' => 0,
  659. 'can_edit' => false,
  660. 'can_delete' => false,
  661. 'item_size' => 123465,
  662. 'item_mtime' => 1234567890,
  663. 'attributes' => null,
  664. ];
  665. $data[] = [$share, $expected];
  666. return $data;
  667. }
  668. /**
  669. * @dataProvider dataGetShare
  670. */
  671. public function testGetShare(\OCP\Share\IShare $share, array $result) {
  672. /** @var ShareAPIController|\PHPUnit\Framework\MockObject\MockObject $ocs */
  673. $ocs = $this->getMockBuilder(ShareAPIController::class)
  674. ->setConstructorArgs([
  675. $this->appName,
  676. $this->request,
  677. $this->shareManager,
  678. $this->groupManager,
  679. $this->userManager,
  680. $this->rootFolder,
  681. $this->urlGenerator,
  682. $this->currentUser,
  683. $this->l,
  684. $this->config,
  685. $this->appManager,
  686. $this->serverContainer,
  687. $this->userStatusManager,
  688. $this->previewManager,
  689. $this->dateTimeZone,
  690. ])->setMethods(['canAccessShare'])
  691. ->getMock();
  692. $ocs->expects($this->any())
  693. ->method('canAccessShare')
  694. ->willReturn(true);
  695. $this->shareManager
  696. ->expects($this->any())
  697. ->method('getShareById')
  698. ->with($share->getFullId(), 'currentUser')
  699. ->willReturn($share);
  700. $userFolder = $this->getMockBuilder('OCP\Files\Folder')->getMock();
  701. $userFolder
  702. ->method('getRelativePath')
  703. ->willReturnArgument(0);
  704. $userFolder->method('getById')
  705. ->with($share->getNodeId())
  706. ->willReturn([$share->getNode()]);
  707. $userFolder->method('getFirstNodeById')
  708. ->with($share->getNodeId())
  709. ->willReturn($share->getNode());
  710. $this->rootFolder->method('getUserFolder')
  711. ->with($this->currentUser)
  712. ->willReturn($userFolder);
  713. $this->urlGenerator
  714. ->method('linkToRouteAbsolute')
  715. ->willReturn('url');
  716. $initiator = $this->getMockBuilder(IUser::class)->getMock();
  717. $initiator->method('getUID')->willReturn('initiatorId');
  718. $initiator->method('getDisplayName')->willReturn('initiatorDisplay');
  719. $owner = $this->getMockBuilder(IUser::class)->getMock();
  720. $owner->method('getUID')->willReturn('ownerId');
  721. $owner->method('getDisplayName')->willReturn('ownerDisplay');
  722. $user = $this->getMockBuilder(IUser::class)->getMock();
  723. $user->method('getUID')->willReturn('userId');
  724. $user->method('getDisplayName')->willReturn('userDisplay');
  725. $user->method('getSystemEMailAddress')->willReturn('userId@example.com');
  726. $group = $this->getMockBuilder('OCP\IGroup')->getMock();
  727. $group->method('getGID')->willReturn('groupId');
  728. $this->userManager->method('get')->willReturnMap([
  729. ['userId', $user],
  730. ['initiatorId', $initiator],
  731. ['ownerId', $owner],
  732. ]);
  733. $this->groupManager->method('get')->willReturnMap([
  734. ['group', $group],
  735. ]);
  736. $this->dateTimeZone->method('getTimezone')->willReturn(new \DateTimeZone('UTC'));
  737. $d = $ocs->getShare($share->getId())->getData()[0];
  738. $this->assertEquals($result, $ocs->getShare($share->getId())->getData()[0]);
  739. }
  740. public function testGetShareInvalidNode() {
  741. $this->expectException(\OCP\AppFramework\OCS\OCSNotFoundException::class);
  742. $this->expectExceptionMessage('Wrong share ID, share does not exist');
  743. $share = \OC::$server->getShareManager()->newShare();
  744. $share->setSharedBy('initiator')
  745. ->setSharedWith('recipient')
  746. ->setShareOwner('owner');
  747. $this->shareManager
  748. ->expects($this->once())
  749. ->method('getShareById')
  750. ->with('ocinternal:42', 'currentUser')
  751. ->willReturn($share);
  752. $userFolder = $this->getMockBuilder('OCP\Files\Folder')->getMock();
  753. $this->rootFolder->method('getUserFolder')
  754. ->with($this->currentUser)
  755. ->willReturn($userFolder);
  756. $this->ocs->getShare(42);
  757. }
  758. public function dataGetShares() {
  759. $folder = $this->getMockBuilder(Folder::class)->getMock();
  760. $file1 = $this->getMockBuilder(File::class)->getMock();
  761. $file1->method('getName')
  762. ->willReturn('file1');
  763. $file2 = $this->getMockBuilder(File::class)->getMock();
  764. $file2->method('getName')
  765. ->willReturn('file2');
  766. $folder->method('getDirectoryListing')
  767. ->willReturn([$file1, $file2]);
  768. $file1UserShareOwner = \OC::$server->getShareManager()->newShare();
  769. $file1UserShareOwner->setShareType(IShare::TYPE_USER)
  770. ->setSharedWith('recipient')
  771. ->setSharedBy('initiator')
  772. ->setShareOwner('currentUser')
  773. ->setPermissions(\OCP\Constants::PERMISSION_READ)
  774. ->setNode($file1)
  775. ->setId(4);
  776. $file1UserShareOwnerExpected = [
  777. 'id' => 4,
  778. 'share_type' => IShare::TYPE_USER,
  779. ];
  780. $file1UserShareInitiator = \OC::$server->getShareManager()->newShare();
  781. $file1UserShareInitiator->setShareType(IShare::TYPE_USER)
  782. ->setSharedWith('recipient')
  783. ->setSharedBy('currentUser')
  784. ->setShareOwner('owner')
  785. ->setPermissions(\OCP\Constants::PERMISSION_READ)
  786. ->setNode($file1)
  787. ->setId(8);
  788. $file1UserShareInitiatorExpected = [
  789. 'id' => 8,
  790. 'share_type' => IShare::TYPE_USER,
  791. ];
  792. $file1UserShareRecipient = \OC::$server->getShareManager()->newShare();
  793. $file1UserShareRecipient->setShareType(IShare::TYPE_USER)
  794. ->setSharedWith('currentUser')
  795. ->setSharedBy('initiator')
  796. ->setShareOwner('owner')
  797. ->setPermissions(\OCP\Constants::PERMISSION_READ)
  798. ->setNode($file1)
  799. ->setId(15);
  800. $file1UserShareRecipientExpected = [
  801. 'id' => 15,
  802. 'share_type' => IShare::TYPE_USER,
  803. ];
  804. $file1UserShareOther = \OC::$server->getShareManager()->newShare();
  805. $file1UserShareOther->setShareType(IShare::TYPE_USER)
  806. ->setSharedWith('recipient')
  807. ->setSharedBy('initiator')
  808. ->setShareOwner('owner')
  809. ->setPermissions(\OCP\Constants::PERMISSION_READ)
  810. ->setNode($file1)
  811. ->setId(16);
  812. $file1UserShareOtherExpected = [
  813. 'id' => 16,
  814. 'share_type' => IShare::TYPE_USER,
  815. ];
  816. $file1GroupShareOwner = \OC::$server->getShareManager()->newShare();
  817. $file1GroupShareOwner->setShareType(IShare::TYPE_GROUP)
  818. ->setSharedWith('recipient')
  819. ->setSharedBy('initiator')
  820. ->setShareOwner('currentUser')
  821. ->setPermissions(\OCP\Constants::PERMISSION_READ)
  822. ->setNode($file1)
  823. ->setId(23);
  824. $file1GroupShareOwnerExpected = [
  825. 'id' => 23,
  826. 'share_type' => IShare::TYPE_GROUP,
  827. ];
  828. $file1GroupShareRecipient = \OC::$server->getShareManager()->newShare();
  829. $file1GroupShareRecipient->setShareType(IShare::TYPE_GROUP)
  830. ->setSharedWith('currentUserGroup')
  831. ->setSharedBy('initiator')
  832. ->setShareOwner('owner')
  833. ->setPermissions(\OCP\Constants::PERMISSION_READ)
  834. ->setNode($file1)
  835. ->setId(42);
  836. $file1GroupShareRecipientExpected = [
  837. 'id' => 42,
  838. 'share_type' => IShare::TYPE_GROUP,
  839. ];
  840. $file1GroupShareOther = \OC::$server->getShareManager()->newShare();
  841. $file1GroupShareOther->setShareType(IShare::TYPE_GROUP)
  842. ->setSharedWith('recipient')
  843. ->setSharedBy('initiator')
  844. ->setShareOwner('owner')
  845. ->setPermissions(\OCP\Constants::PERMISSION_READ)
  846. ->setNode($file1)
  847. ->setId(108);
  848. $file1LinkShareOwner = \OC::$server->getShareManager()->newShare();
  849. $file1LinkShareOwner->setShareType(IShare::TYPE_LINK)
  850. ->setSharedWith('recipient')
  851. ->setSharedBy('initiator')
  852. ->setShareOwner('currentUser')
  853. ->setPermissions(\OCP\Constants::PERMISSION_READ)
  854. ->setNode($file1)
  855. ->setId(415);
  856. $file1LinkShareOwnerExpected = [
  857. 'id' => 415,
  858. 'share_type' => IShare::TYPE_LINK,
  859. ];
  860. $file1EmailShareOwner = \OC::$server->getShareManager()->newShare();
  861. $file1EmailShareOwner->setShareType(IShare::TYPE_EMAIL)
  862. ->setSharedWith('recipient')
  863. ->setSharedBy('initiator')
  864. ->setShareOwner('currentUser')
  865. ->setPermissions(\OCP\Constants::PERMISSION_READ)
  866. ->setNode($file1)
  867. ->setId(416);
  868. $file1EmailShareOwnerExpected = [
  869. 'id' => 416,
  870. 'share_type' => IShare::TYPE_EMAIL,
  871. ];
  872. $file1CircleShareOwner = \OC::$server->getShareManager()->newShare();
  873. $file1CircleShareOwner->setShareType(IShare::TYPE_CIRCLE)
  874. ->setSharedWith('recipient')
  875. ->setSharedBy('initiator')
  876. ->setShareOwner('currentUser')
  877. ->setPermissions(\OCP\Constants::PERMISSION_READ)
  878. ->setNode($file1)
  879. ->setId(423);
  880. $file1CircleShareOwnerExpected = [
  881. 'id' => 423,
  882. 'share_type' => IShare::TYPE_CIRCLE,
  883. ];
  884. $file1RoomShareOwner = \OC::$server->getShareManager()->newShare();
  885. $file1RoomShareOwner->setShareType(IShare::TYPE_ROOM)
  886. ->setSharedWith('recipient')
  887. ->setSharedBy('initiator')
  888. ->setShareOwner('currentUser')
  889. ->setPermissions(\OCP\Constants::PERMISSION_READ)
  890. ->setNode($file1)
  891. ->setId(442);
  892. $file1RoomShareOwnerExpected = [
  893. 'id' => 442,
  894. 'share_type' => IShare::TYPE_ROOM,
  895. ];
  896. $file1RemoteShareOwner = \OC::$server->getShareManager()->newShare();
  897. $file1RemoteShareOwner->setShareType(IShare::TYPE_REMOTE)
  898. ->setSharedWith('recipient')
  899. ->setSharedBy('initiator')
  900. ->setShareOwner('currentUser')
  901. ->setPermissions(\OCP\Constants::PERMISSION_READ)
  902. ->setExpirationDate(new \DateTime('2000-01-01T01:02:03'))
  903. ->setNode($file1)
  904. ->setId(815);
  905. $file1RemoteShareOwnerExpected = [
  906. 'id' => 815,
  907. 'share_type' => IShare::TYPE_REMOTE,
  908. ];
  909. $file1RemoteGroupShareOwner = \OC::$server->getShareManager()->newShare();
  910. $file1RemoteGroupShareOwner->setShareType(IShare::TYPE_REMOTE_GROUP)
  911. ->setSharedWith('recipient')
  912. ->setSharedBy('initiator')
  913. ->setShareOwner('currentUser')
  914. ->setPermissions(\OCP\Constants::PERMISSION_READ)
  915. ->setExpirationDate(new \DateTime('2000-01-02T01:02:03'))
  916. ->setNode($file1)
  917. ->setId(816);
  918. $file1RemoteGroupShareOwnerExpected = [
  919. 'id' => 816,
  920. 'share_type' => IShare::TYPE_REMOTE_GROUP,
  921. ];
  922. $file2UserShareOwner = \OC::$server->getShareManager()->newShare();
  923. $file2UserShareOwner->setShareType(IShare::TYPE_USER)
  924. ->setSharedWith('recipient')
  925. ->setSharedBy('initiator')
  926. ->setShareOwner('currentUser')
  927. ->setPermissions(\OCP\Constants::PERMISSION_READ)
  928. ->setNode($file2)
  929. ->setId(823);
  930. $file2UserShareOwnerExpected = [
  931. 'id' => 823,
  932. 'share_type' => IShare::TYPE_USER,
  933. ];
  934. $data = [
  935. [
  936. [
  937. 'path' => $file1,
  938. ],
  939. [
  940. 'file1' => [
  941. IShare::TYPE_USER => [$file1UserShareOwner, $file1UserShareOwner, $file1UserShareOwner],
  942. ],
  943. ],
  944. [
  945. ],
  946. [
  947. $file1UserShareOwnerExpected
  948. ]
  949. ],
  950. [
  951. [
  952. 'path' => $file1,
  953. ],
  954. [
  955. 'file1' => [
  956. IShare::TYPE_USER => [$file1UserShareOwner, $file1UserShareRecipient],
  957. ],
  958. ],
  959. [
  960. ],
  961. [
  962. $file1UserShareOwnerExpected,
  963. ]
  964. ],
  965. [
  966. [
  967. 'path' => $file1,
  968. ],
  969. [
  970. 'file1' => [
  971. IShare::TYPE_USER => [$file1UserShareOwner, $file1UserShareRecipient, $file1UserShareInitiator, $file1UserShareOther],
  972. ],
  973. ],
  974. [
  975. ],
  976. [
  977. $file1UserShareOwnerExpected,
  978. $file1UserShareInitiatorExpected,
  979. $file1UserShareOtherExpected,
  980. ]
  981. ],
  982. [
  983. [
  984. 'path' => $file1,
  985. ],
  986. [
  987. 'file1' => [
  988. IShare::TYPE_USER => [$file1UserShareRecipient, $file1UserShareInitiator, $file1UserShareOther],
  989. ],
  990. ],
  991. [
  992. ],
  993. [
  994. $file1UserShareInitiatorExpected,
  995. ]
  996. ],
  997. [
  998. [
  999. 'path' => $file1,
  1000. ],
  1001. [
  1002. 'file1' => [
  1003. IShare::TYPE_USER => [$file1UserShareOwner],
  1004. IShare::TYPE_GROUP => [$file1GroupShareRecipient],
  1005. ],
  1006. ],
  1007. [
  1008. ],
  1009. [
  1010. $file1UserShareOwnerExpected,
  1011. $file1GroupShareRecipientExpected,
  1012. ]
  1013. ],
  1014. [
  1015. [
  1016. 'path' => $file1,
  1017. ],
  1018. [
  1019. 'file1' => [
  1020. IShare::TYPE_USER => [$file1UserShareOwner],
  1021. IShare::TYPE_GROUP => [$file1GroupShareOwner],
  1022. IShare::TYPE_LINK => [$file1LinkShareOwner],
  1023. IShare::TYPE_EMAIL => [$file1EmailShareOwner],
  1024. IShare::TYPE_CIRCLE => [$file1CircleShareOwner],
  1025. IShare::TYPE_ROOM => [$file1RoomShareOwner],
  1026. IShare::TYPE_REMOTE => [$file1RemoteShareOwner],
  1027. IShare::TYPE_REMOTE_GROUP => [$file1RemoteGroupShareOwner],
  1028. ],
  1029. ],
  1030. [
  1031. ],
  1032. [
  1033. $file1UserShareOwnerExpected,
  1034. $file1GroupShareOwnerExpected,
  1035. $file1LinkShareOwnerExpected,
  1036. $file1EmailShareOwnerExpected,
  1037. $file1CircleShareOwnerExpected,
  1038. $file1RoomShareOwnerExpected,
  1039. ]
  1040. ],
  1041. [
  1042. [
  1043. 'path' => $file1,
  1044. ],
  1045. [
  1046. 'file1' => [
  1047. IShare::TYPE_USER => [$file1UserShareOwner],
  1048. IShare::TYPE_GROUP => [$file1GroupShareOwner],
  1049. IShare::TYPE_LINK => [$file1LinkShareOwner],
  1050. IShare::TYPE_EMAIL => [$file1EmailShareOwner],
  1051. IShare::TYPE_CIRCLE => [$file1CircleShareOwner],
  1052. IShare::TYPE_ROOM => [$file1RoomShareOwner],
  1053. IShare::TYPE_REMOTE => [$file1RemoteShareOwner],
  1054. IShare::TYPE_REMOTE_GROUP => [$file1RemoteGroupShareOwner],
  1055. ],
  1056. ],
  1057. [
  1058. IShare::TYPE_REMOTE => true,
  1059. IShare::TYPE_REMOTE_GROUP => true,
  1060. ],
  1061. [
  1062. $file1UserShareOwnerExpected,
  1063. $file1GroupShareOwnerExpected,
  1064. $file1LinkShareOwnerExpected,
  1065. $file1EmailShareOwnerExpected,
  1066. $file1CircleShareOwnerExpected,
  1067. $file1RoomShareOwnerExpected,
  1068. $file1RemoteShareOwnerExpected,
  1069. $file1RemoteGroupShareOwnerExpected,
  1070. ]
  1071. ],
  1072. [
  1073. [
  1074. 'path' => $folder,
  1075. 'subfiles' => 'true',
  1076. ],
  1077. [
  1078. 'file1' => [
  1079. IShare::TYPE_USER => [$file1UserShareOwner],
  1080. ],
  1081. 'file2' => [
  1082. IShare::TYPE_USER => [$file2UserShareOwner],
  1083. ],
  1084. ],
  1085. [
  1086. ],
  1087. [
  1088. $file1UserShareOwnerExpected,
  1089. $file2UserShareOwnerExpected,
  1090. ]
  1091. ],
  1092. [
  1093. [
  1094. 'path' => $folder,
  1095. 'subfiles' => 'true',
  1096. ],
  1097. [
  1098. 'file1' => [
  1099. IShare::TYPE_USER => [$file1UserShareOwner, $file1UserShareOwner, $file1UserShareOwner],
  1100. ],
  1101. ],
  1102. [
  1103. ],
  1104. [
  1105. $file1UserShareOwnerExpected,
  1106. ]
  1107. ],
  1108. [
  1109. [
  1110. 'path' => $folder,
  1111. 'subfiles' => 'true',
  1112. ],
  1113. [
  1114. 'file1' => [
  1115. IShare::TYPE_USER => [$file1UserShareOwner, $file1UserShareRecipient],
  1116. ],
  1117. ],
  1118. [
  1119. ],
  1120. [
  1121. $file1UserShareOwnerExpected
  1122. ]
  1123. ],
  1124. [
  1125. [
  1126. 'path' => $folder,
  1127. 'subfiles' => 'true',
  1128. ],
  1129. [
  1130. 'file1' => [
  1131. IShare::TYPE_USER => [$file1UserShareRecipient, $file1UserShareInitiator, $file1UserShareOther],
  1132. ],
  1133. 'file2' => [
  1134. IShare::TYPE_USER => [$file2UserShareOwner],
  1135. ],
  1136. ],
  1137. [
  1138. ],
  1139. [
  1140. $file1UserShareInitiatorExpected,
  1141. $file1UserShareOtherExpected,
  1142. $file2UserShareOwnerExpected,
  1143. ]
  1144. ],
  1145. // This might not happen in a real environment, as the combination
  1146. // of shares does not seem to be possible on a folder without
  1147. // resharing rights; if the folder has resharing rights then the
  1148. // share with others would be included too in the results.
  1149. [
  1150. [
  1151. 'path' => $folder,
  1152. 'subfiles' => 'true',
  1153. ],
  1154. [
  1155. 'file1' => [
  1156. IShare::TYPE_USER => [$file1UserShareRecipient, $file1UserShareInitiator, $file1UserShareOther],
  1157. ],
  1158. ],
  1159. [
  1160. ],
  1161. [
  1162. $file1UserShareInitiatorExpected,
  1163. ]
  1164. ],
  1165. [
  1166. [
  1167. 'path' => $folder,
  1168. 'subfiles' => 'true',
  1169. ],
  1170. [
  1171. 'file1' => [
  1172. IShare::TYPE_USER => [$file1UserShareOwner],
  1173. IShare::TYPE_GROUP => [$file1GroupShareRecipient],
  1174. ],
  1175. ],
  1176. [
  1177. ],
  1178. [
  1179. $file1UserShareOwnerExpected,
  1180. $file1GroupShareRecipientExpected,
  1181. ]
  1182. ],
  1183. [
  1184. [
  1185. 'path' => $folder,
  1186. 'subfiles' => 'true',
  1187. ],
  1188. [
  1189. 'file1' => [
  1190. IShare::TYPE_USER => [$file1UserShareOwner],
  1191. IShare::TYPE_GROUP => [$file1GroupShareOwner],
  1192. IShare::TYPE_LINK => [$file1LinkShareOwner],
  1193. IShare::TYPE_EMAIL => [$file1EmailShareOwner],
  1194. IShare::TYPE_CIRCLE => [$file1CircleShareOwner],
  1195. IShare::TYPE_ROOM => [$file1RoomShareOwner],
  1196. IShare::TYPE_REMOTE => [$file1RemoteShareOwner],
  1197. IShare::TYPE_REMOTE_GROUP => [$file1RemoteGroupShareOwner],
  1198. ],
  1199. ],
  1200. [
  1201. ],
  1202. [
  1203. $file1UserShareOwnerExpected,
  1204. $file1GroupShareOwnerExpected,
  1205. $file1LinkShareOwnerExpected,
  1206. $file1EmailShareOwnerExpected,
  1207. $file1CircleShareOwnerExpected,
  1208. $file1RoomShareOwnerExpected,
  1209. ]
  1210. ],
  1211. [
  1212. [
  1213. 'path' => $folder,
  1214. 'subfiles' => 'true',
  1215. ],
  1216. [
  1217. 'file1' => [
  1218. IShare::TYPE_USER => [$file1UserShareOwner],
  1219. IShare::TYPE_GROUP => [$file1GroupShareOwner],
  1220. IShare::TYPE_LINK => [$file1LinkShareOwner],
  1221. IShare::TYPE_EMAIL => [$file1EmailShareOwner],
  1222. IShare::TYPE_CIRCLE => [$file1CircleShareOwner],
  1223. IShare::TYPE_ROOM => [$file1RoomShareOwner],
  1224. IShare::TYPE_REMOTE => [$file1RemoteShareOwner],
  1225. IShare::TYPE_REMOTE_GROUP => [$file1RemoteGroupShareOwner],
  1226. ],
  1227. ],
  1228. [
  1229. IShare::TYPE_REMOTE => true,
  1230. IShare::TYPE_REMOTE_GROUP => true,
  1231. ],
  1232. [
  1233. $file1UserShareOwnerExpected,
  1234. $file1GroupShareOwnerExpected,
  1235. $file1LinkShareOwnerExpected,
  1236. $file1EmailShareOwnerExpected,
  1237. $file1CircleShareOwnerExpected,
  1238. $file1RoomShareOwnerExpected,
  1239. $file1RemoteShareOwnerExpected,
  1240. $file1RemoteGroupShareOwnerExpected,
  1241. ]
  1242. ],
  1243. ];
  1244. return $data;
  1245. }
  1246. /**
  1247. * @dataProvider dataGetShares
  1248. */
  1249. public function testGetShares(array $getSharesParameters, array $shares, array $extraShareTypes, array $expected) {
  1250. /** @var \OCA\Files_Sharing\Controller\ShareAPIController $ocs */
  1251. $ocs = $this->getMockBuilder(ShareAPIController::class)
  1252. ->setConstructorArgs([
  1253. $this->appName,
  1254. $this->request,
  1255. $this->shareManager,
  1256. $this->groupManager,
  1257. $this->userManager,
  1258. $this->rootFolder,
  1259. $this->urlGenerator,
  1260. $this->currentUser,
  1261. $this->l,
  1262. $this->config,
  1263. $this->appManager,
  1264. $this->serverContainer,
  1265. $this->userStatusManager,
  1266. $this->previewManager,
  1267. $this->dateTimeZone,
  1268. ])->setMethods(['formatShare'])
  1269. ->getMock();
  1270. $ocs->method('formatShare')
  1271. ->willReturnCallback(
  1272. function ($share) {
  1273. return [
  1274. 'id' => $share->getId(),
  1275. 'share_type' => $share->getShareType()
  1276. ];
  1277. }
  1278. );
  1279. $userFolder = $this->getMockBuilder('OCP\Files\Folder')->getMock();
  1280. $userFolder->method('get')
  1281. ->with('path')
  1282. ->willReturn($getSharesParameters['path']);
  1283. $this->rootFolder->method('getUserFolder')
  1284. ->with($this->currentUser)
  1285. ->willReturn($userFolder);
  1286. $this->shareManager
  1287. ->method('getSharesBy')
  1288. ->willReturnCallback(
  1289. function ($user, $shareType, $node) use ($shares) {
  1290. if (!isset($shares[$node->getName()]) || !isset($shares[$node->getName()][$shareType])) {
  1291. return [];
  1292. }
  1293. return $shares[$node->getName()][$shareType];
  1294. }
  1295. );
  1296. $this->shareManager
  1297. ->method('outgoingServer2ServerSharesAllowed')
  1298. ->willReturn($extraShareTypes[ISHARE::TYPE_REMOTE] ?? false);
  1299. $this->shareManager
  1300. ->method('outgoingServer2ServerGroupSharesAllowed')
  1301. ->willReturn($extraShareTypes[ISHARE::TYPE_REMOTE_GROUP] ?? false);
  1302. $this->groupManager
  1303. ->method('isInGroup')
  1304. ->willReturnCallback(
  1305. function ($user, $group) {
  1306. return $group === 'currentUserGroup';
  1307. }
  1308. );
  1309. $result = $ocs->getShares(
  1310. $getSharesParameters['sharedWithMe'] ?? 'false',
  1311. $getSharesParameters['reshares'] ?? 'false',
  1312. $getSharesParameters['subfiles'] ?? 'false',
  1313. 'path'
  1314. );
  1315. $this->assertEquals($expected, $result->getData());
  1316. }
  1317. public function testCanAccessShare() {
  1318. $share = $this->getMockBuilder(IShare::class)->getMock();
  1319. $share->method('getShareOwner')->willReturn($this->currentUser);
  1320. $this->assertTrue($this->invokePrivate($this->ocs, 'canAccessShare', [$share]));
  1321. $share = $this->getMockBuilder(IShare::class)->getMock();
  1322. $share->method('getSharedBy')->willReturn($this->currentUser);
  1323. $this->assertTrue($this->invokePrivate($this->ocs, 'canAccessShare', [$share]));
  1324. $share = $this->getMockBuilder(IShare::class)->getMock();
  1325. $share->method('getShareType')->willReturn(IShare::TYPE_USER);
  1326. $share->method('getSharedWith')->willReturn($this->currentUser);
  1327. $this->assertTrue($this->invokePrivate($this->ocs, 'canAccessShare', [$share]));
  1328. $file = $this->getMockBuilder(File::class)->getMock();
  1329. $userFolder = $this->getMockBuilder('OCP\Files\Folder')->getMock();
  1330. $this->rootFolder->method('getUserFolder')
  1331. ->with($this->currentUser)
  1332. ->willReturn($userFolder);
  1333. $userFolder->method('getFirstNodeById')
  1334. ->with($share->getNodeId())
  1335. ->willReturn($file);
  1336. $file->method('getPermissions')
  1337. ->will($this->onConsecutiveCalls(\OCP\Constants::PERMISSION_SHARE, \OCP\Constants::PERMISSION_READ));
  1338. // getPermissions -> share
  1339. $share = $this->getMockBuilder(IShare::class)->getMock();
  1340. $share->method('getShareType')->willReturn(IShare::TYPE_USER);
  1341. $share->method('getSharedWith')->willReturn($this->getMockBuilder(IUser::class)->getMock());
  1342. $this->assertTrue($this->invokePrivate($this->ocs, 'canAccessShare', [$share]));
  1343. // getPermissions -> read
  1344. $share = $this->getMockBuilder(IShare::class)->getMock();
  1345. $share->method('getShareType')->willReturn(IShare::TYPE_USER);
  1346. $share->method('getSharedWith')->willReturn($this->getMockBuilder(IUser::class)->getMock());
  1347. $this->assertFalse($this->invokePrivate($this->ocs, 'canAccessShare', [$share]));
  1348. $share = $this->getMockBuilder(IShare::class)->getMock();
  1349. $share->method('getShareType')->willReturn(IShare::TYPE_GROUP);
  1350. $share->method('getSharedWith')->willReturn('group');
  1351. $user = $this->createMock(IUser::class);
  1352. $this->userManager->method('get')
  1353. ->with($this->currentUser)
  1354. ->willReturn($user);
  1355. $group = $this->getMockBuilder('OCP\IGroup')->getMock();
  1356. $group->method('inGroup')->with($user)->willReturn(true);
  1357. $group2 = $this->getMockBuilder('OCP\IGroup')->getMock();
  1358. $group2->method('inGroup')->with($user)->willReturn(false);
  1359. $this->groupManager->method('get')->willReturnMap([
  1360. ['group', $group],
  1361. ['group2', $group2],
  1362. ['groupnull', null],
  1363. ]);
  1364. $this->assertTrue($this->invokePrivate($this->ocs, 'canAccessShare', [$share]));
  1365. $share = $this->createMock(IShare::class);
  1366. $share->method('getShareType')->willReturn(IShare::TYPE_GROUP);
  1367. $share->method('getSharedWith')->willReturn('group2');
  1368. $this->assertFalse($this->invokePrivate($this->ocs, 'canAccessShare', [$share]));
  1369. // null group
  1370. $share = $this->createMock(IShare::class);
  1371. $share->method('getShareType')->willReturn(IShare::TYPE_GROUP);
  1372. $share->method('getSharedWith')->willReturn('groupnull');
  1373. $this->assertFalse($this->invokePrivate($this->ocs, 'canAccessShare', [$share]));
  1374. $share = $this->createMock(IShare::class);
  1375. $share->method('getShareType')->willReturn(IShare::TYPE_LINK);
  1376. $this->assertFalse($this->invokePrivate($this->ocs, 'canAccessShare', [$share]));
  1377. }
  1378. public function dataCanAccessRoomShare() {
  1379. $result = [];
  1380. $share = $this->createMock(IShare::class);
  1381. $share->method('getShareType')->willReturn(IShare::TYPE_ROOM);
  1382. $share->method('getSharedWith')->willReturn('recipientRoom');
  1383. $result[] = [
  1384. false, $share, false, false
  1385. ];
  1386. $result[] = [
  1387. false, $share, false, true
  1388. ];
  1389. $result[] = [
  1390. true, $share, true, true
  1391. ];
  1392. $result[] = [
  1393. false, $share, true, false
  1394. ];
  1395. return $result;
  1396. }
  1397. /**
  1398. * @dataProvider dataCanAccessRoomShare
  1399. *
  1400. * @param bool $expects
  1401. * @param \OCP\Share\IShare $share
  1402. * @param bool helperAvailable
  1403. * @param bool canAccessShareByHelper
  1404. */
  1405. public function testCanAccessRoomShare(bool $expected, \OCP\Share\IShare $share, bool $helperAvailable, bool $canAccessShareByHelper) {
  1406. $userFolder = $this->getMockBuilder('OCP\Files\Folder')->getMock();
  1407. $this->rootFolder->method('getUserFolder')
  1408. ->with($this->currentUser)
  1409. ->willReturn($userFolder);
  1410. $userFolder->method('getFirstNodeById')
  1411. ->with($share->getNodeId())
  1412. ->willReturn($share->getNode());
  1413. if (!$helperAvailable) {
  1414. $this->appManager->method('isEnabledForUser')
  1415. ->with('spreed')
  1416. ->willReturn(false);
  1417. } else {
  1418. $this->appManager->method('isEnabledForUser')
  1419. ->with('spreed')
  1420. ->willReturn(true);
  1421. $helper = $this->getMockBuilder('\OCA\Talk\Share\Helper\ShareAPIController')
  1422. ->setMethods(['canAccessShare'])
  1423. ->getMock();
  1424. $helper->method('canAccessShare')
  1425. ->with($share, $this->currentUser)
  1426. ->willReturn($canAccessShareByHelper);
  1427. $this->serverContainer->method('get')
  1428. ->with('\OCA\Talk\Share\Helper\ShareAPIController')
  1429. ->willReturn($helper);
  1430. }
  1431. $this->assertEquals($expected, $this->invokePrivate($this->ocs, 'canAccessShare', [$share]));
  1432. }
  1433. public function testCreateShareNoPath() {
  1434. $this->expectException(\OCP\AppFramework\OCS\OCSNotFoundException::class);
  1435. $this->expectExceptionMessage('Please specify a file or folder path');
  1436. $this->ocs->createShare();
  1437. }
  1438. public function testCreateShareInvalidPath() {
  1439. $this->expectException(\OCP\AppFramework\OCS\OCSNotFoundException::class);
  1440. $this->expectExceptionMessage('Wrong path, file/folder does not exist');
  1441. $userFolder = $this->getMockBuilder(Folder::class)->getMock();
  1442. $this->rootFolder->expects($this->once())
  1443. ->method('getUserFolder')
  1444. ->with('currentUser')
  1445. ->willReturn($userFolder);
  1446. $userFolder->expects($this->once())
  1447. ->method('get')
  1448. ->with('invalid-path')
  1449. ->will($this->throwException(new \OCP\Files\NotFoundException()));
  1450. $this->ocs->createShare('invalid-path');
  1451. }
  1452. public function testCreateShareInvalidPermissions() {
  1453. $this->expectException(\OCP\AppFramework\OCS\OCSNotFoundException::class);
  1454. $this->expectExceptionMessage('Invalid permissions');
  1455. $share = $this->newShare();
  1456. $this->shareManager->method('newShare')->willReturn($share);
  1457. $userFolder = $this->getMockBuilder(Folder::class)->getMock();
  1458. $this->rootFolder->expects($this->once())
  1459. ->method('getUserFolder')
  1460. ->with('currentUser')
  1461. ->willReturn($userFolder);
  1462. $path = $this->getMockBuilder(File::class)->getMock();
  1463. $userFolder->expects($this->once())
  1464. ->method('get')
  1465. ->with('valid-path')
  1466. ->willReturn($path);
  1467. $userFolder->method('getById')
  1468. ->willReturn([]);
  1469. $path->expects($this->once())
  1470. ->method('lock')
  1471. ->with(\OCP\Lock\ILockingProvider::LOCK_SHARED);
  1472. $this->ocs->createShare('valid-path', 32);
  1473. }
  1474. public function testCreateShareUserNoShareWith() {
  1475. $this->expectException(\OCP\AppFramework\OCS\OCSNotFoundException::class);
  1476. $this->expectExceptionMessage('Please specify a valid account to share with');
  1477. $share = $this->newShare();
  1478. $this->shareManager->method('newShare')->willReturn($share);
  1479. [$userFolder, $path] = $this->getNonSharedUserFile();
  1480. $this->rootFolder->expects($this->exactly(2))
  1481. ->method('getUserFolder')
  1482. ->with('currentUser')
  1483. ->willReturn($userFolder);
  1484. $userFolder->expects($this->once())
  1485. ->method('get')
  1486. ->with('valid-path')
  1487. ->willReturn($path);
  1488. $userFolder->method('getById')
  1489. ->willReturn([]);
  1490. $path->expects($this->once())
  1491. ->method('lock')
  1492. ->with(\OCP\Lock\ILockingProvider::LOCK_SHARED);
  1493. $this->ocs->createShare('valid-path', \OCP\Constants::PERMISSION_ALL, IShare::TYPE_USER);
  1494. }
  1495. public function testCreateShareUserNoValidShareWith() {
  1496. $this->expectException(\OCP\AppFramework\OCS\OCSNotFoundException::class);
  1497. $this->expectExceptionMessage('Please specify a valid account to share with');
  1498. $share = $this->newShare();
  1499. $this->shareManager->method('newShare')->willReturn($share);
  1500. [$userFolder, $path] = $this->getNonSharedUserFile();
  1501. $this->rootFolder->expects($this->exactly(2))
  1502. ->method('getUserFolder')
  1503. ->with('currentUser')
  1504. ->willReturn($userFolder);
  1505. $userFolder->expects($this->once())
  1506. ->method('get')
  1507. ->with('valid-path')
  1508. ->willReturn($path);
  1509. $userFolder->method('getById')
  1510. ->willReturn([]);
  1511. $path->expects($this->once())
  1512. ->method('lock')
  1513. ->with(\OCP\Lock\ILockingProvider::LOCK_SHARED);
  1514. $this->userManager->method('userExists')
  1515. ->with('invalidUser')
  1516. ->willReturn(false);
  1517. $this->ocs->createShare('valid-path', \OCP\Constants::PERMISSION_ALL, IShare::TYPE_USER, 'invalidUser');
  1518. }
  1519. public function testCreateShareUser() {
  1520. $share = $this->newShare();
  1521. $this->shareManager->method('newShare')->willReturn($share);
  1522. /** @var \OCA\Files_Sharing\Controller\ShareAPIController $ocs */
  1523. $ocs = $this->getMockBuilder(ShareAPIController::class)
  1524. ->setConstructorArgs([
  1525. $this->appName,
  1526. $this->request,
  1527. $this->shareManager,
  1528. $this->groupManager,
  1529. $this->userManager,
  1530. $this->rootFolder,
  1531. $this->urlGenerator,
  1532. $this->currentUser,
  1533. $this->l,
  1534. $this->config,
  1535. $this->appManager,
  1536. $this->serverContainer,
  1537. $this->userStatusManager,
  1538. $this->previewManager,
  1539. $this->dateTimeZone,
  1540. ])->setMethods(['formatShare'])
  1541. ->getMock();
  1542. [$userFolder, $path] = $this->getNonSharedUserFile();
  1543. $this->rootFolder->expects($this->exactly(2))
  1544. ->method('getUserFolder')
  1545. ->with('currentUser')
  1546. ->willReturn($userFolder);
  1547. $userFolder->expects($this->once())
  1548. ->method('get')
  1549. ->with('valid-path')
  1550. ->willReturn($path);
  1551. $userFolder->method('getById')
  1552. ->willReturn([]);
  1553. $this->userManager->method('userExists')->with('validUser')->willReturn(true);
  1554. $path->expects($this->once())
  1555. ->method('lock')
  1556. ->with(\OCP\Lock\ILockingProvider::LOCK_SHARED);
  1557. $this->shareManager->method('createShare')
  1558. ->with($this->callback(function (\OCP\Share\IShare $share) use ($path) {
  1559. return $share->getNode() === $path &&
  1560. $share->getPermissions() === (
  1561. \OCP\Constants::PERMISSION_ALL &
  1562. ~\OCP\Constants::PERMISSION_DELETE &
  1563. ~\OCP\Constants::PERMISSION_CREATE
  1564. ) &&
  1565. $share->getShareType() === IShare::TYPE_USER &&
  1566. $share->getSharedWith() === 'validUser' &&
  1567. $share->getSharedBy() === 'currentUser';
  1568. }))
  1569. ->willReturnArgument(0);
  1570. $expected = new DataResponse([]);
  1571. $result = $ocs->createShare('valid-path', \OCP\Constants::PERMISSION_ALL, IShare::TYPE_USER, 'validUser');
  1572. $this->assertInstanceOf(get_class($expected), $result);
  1573. $this->assertEquals($expected->getData(), $result->getData());
  1574. }
  1575. public function testCreateShareGroupNoValidShareWith() {
  1576. $this->expectException(\OCP\AppFramework\OCS\OCSNotFoundException::class);
  1577. $this->expectExceptionMessage('Please specify a valid group');
  1578. $share = $this->newShare();
  1579. $this->shareManager->method('newShare')->willReturn($share);
  1580. $this->shareManager->method('createShare')->willReturnArgument(0);
  1581. $this->shareManager->method('allowGroupSharing')->willReturn(true);
  1582. [$userFolder, $path] = $this->getNonSharedUserFile();
  1583. $this->rootFolder->expects($this->exactly(2))
  1584. ->method('getUserFolder')
  1585. ->with('currentUser')
  1586. ->willReturn($userFolder);
  1587. $userFolder->expects($this->once())
  1588. ->method('get')
  1589. ->with('valid-path')
  1590. ->willReturn($path);
  1591. $userFolder->method('getById')
  1592. ->willReturn([]);
  1593. $path->expects($this->once())
  1594. ->method('lock')
  1595. ->with(\OCP\Lock\ILockingProvider::LOCK_SHARED);
  1596. $this->ocs->createShare('valid-path', \OCP\Constants::PERMISSION_ALL, IShare::TYPE_GROUP, 'invalidGroup');
  1597. }
  1598. public function testCreateShareGroup() {
  1599. $share = $this->newShare();
  1600. $this->shareManager->method('newShare')->willReturn($share);
  1601. /** @var ShareAPIController|\PHPUnit\Framework\MockObject\MockObject $ocs */
  1602. $ocs = $this->getMockBuilder(ShareAPIController::class)
  1603. ->setConstructorArgs([
  1604. $this->appName,
  1605. $this->request,
  1606. $this->shareManager,
  1607. $this->groupManager,
  1608. $this->userManager,
  1609. $this->rootFolder,
  1610. $this->urlGenerator,
  1611. $this->currentUser,
  1612. $this->l,
  1613. $this->config,
  1614. $this->appManager,
  1615. $this->serverContainer,
  1616. $this->userStatusManager,
  1617. $this->previewManager,
  1618. $this->dateTimeZone,
  1619. ])->setMethods(['formatShare'])
  1620. ->getMock();
  1621. $this->request
  1622. ->method('getParam')
  1623. ->willReturnMap([
  1624. ['path', null, 'valid-path'],
  1625. ['permissions', null, \OCP\Constants::PERMISSION_ALL],
  1626. ['shareType', '-1', IShare::TYPE_GROUP],
  1627. ['shareWith', null, 'validGroup'],
  1628. ]);
  1629. [$userFolder, $path] = $this->getNonSharedUserFolder();
  1630. $this->rootFolder->expects($this->exactly(2))
  1631. ->method('getUserFolder')
  1632. ->with('currentUser')
  1633. ->willReturn($userFolder);
  1634. $userFolder->expects($this->once())
  1635. ->method('get')
  1636. ->with('valid-path')
  1637. ->willReturn($path);
  1638. $userFolder->method('getById')
  1639. ->willReturn([]);
  1640. $this->groupManager->method('groupExists')->with('validGroup')->willReturn(true);
  1641. $this->shareManager->expects($this->once())
  1642. ->method('allowGroupSharing')
  1643. ->willReturn(true);
  1644. $path->expects($this->once())
  1645. ->method('lock')
  1646. ->with(\OCP\Lock\ILockingProvider::LOCK_SHARED);
  1647. $this->shareManager->method('createShare')
  1648. ->with($this->callback(function (\OCP\Share\IShare $share) use ($path) {
  1649. return $share->getNode() === $path &&
  1650. $share->getPermissions() === \OCP\Constants::PERMISSION_ALL &&
  1651. $share->getShareType() === IShare::TYPE_GROUP &&
  1652. $share->getSharedWith() === 'validGroup' &&
  1653. $share->getSharedBy() === 'currentUser';
  1654. }))
  1655. ->willReturnArgument(0);
  1656. $expected = new DataResponse([]);
  1657. $result = $ocs->createShare('valid-path', \OCP\Constants::PERMISSION_ALL, IShare::TYPE_GROUP, 'validGroup');
  1658. $this->assertInstanceOf(get_class($expected), $result);
  1659. $this->assertEquals($expected->getData(), $result->getData());
  1660. }
  1661. public function testCreateShareGroupNotAllowed() {
  1662. $this->expectException(\OCP\AppFramework\OCS\OCSNotFoundException::class);
  1663. $this->expectExceptionMessage('Group sharing is disabled by the administrator');
  1664. $share = $this->newShare();
  1665. $this->shareManager->method('newShare')->willReturn($share);
  1666. [$userFolder, $path] = $this->getNonSharedUserFolder();
  1667. $this->rootFolder->expects($this->exactly(2))
  1668. ->method('getUserFolder')
  1669. ->with('currentUser')
  1670. ->willReturn($userFolder);
  1671. $userFolder->expects($this->once())
  1672. ->method('get')
  1673. ->with('valid-path')
  1674. ->willReturn($path);
  1675. $userFolder->method('getById')
  1676. ->willReturn([]);
  1677. $this->groupManager->method('groupExists')->with('validGroup')->willReturn(true);
  1678. $this->shareManager->expects($this->once())
  1679. ->method('allowGroupSharing')
  1680. ->willReturn(false);
  1681. $this->ocs->createShare('valid-path', \OCP\Constants::PERMISSION_ALL, IShare::TYPE_GROUP, 'invalidGroup');
  1682. }
  1683. public function testCreateShareLinkNoLinksAllowed() {
  1684. $this->expectException(\OCP\AppFramework\OCS\OCSNotFoundException::class);
  1685. $this->expectExceptionMessage('Public link sharing is disabled by the administrator');
  1686. $this->request
  1687. ->method('getParam')
  1688. ->willReturnMap([
  1689. ['path', null, 'valid-path'],
  1690. ['shareType', '-1', IShare::TYPE_LINK],
  1691. ]);
  1692. $path = $this->getMockBuilder(Folder::class)->getMock();
  1693. $path->method('getId')->willReturn(42);
  1694. $storage = $this->createMock(Storage::class);
  1695. $storage->method('instanceOfStorage')
  1696. ->willReturnMap([
  1697. ['OCA\Files_Sharing\External\Storage', false],
  1698. ['OCA\Files_Sharing\SharedStorage', false],
  1699. ]);
  1700. $path->method('getStorage')->willReturn($storage);
  1701. $this->rootFolder->method('getUserFolder')->with($this->currentUser)->willReturnSelf();
  1702. $this->rootFolder->method('get')->with('valid-path')->willReturn($path);
  1703. $this->rootFolder->method('getById')
  1704. ->willReturn([]);
  1705. $this->shareManager->method('newShare')->willReturn(\OC::$server->getShareManager()->newShare());
  1706. $this->ocs->createShare('valid-path', \OCP\Constants::PERMISSION_ALL, IShare::TYPE_LINK);
  1707. }
  1708. public function testCreateShareLinkNoPublicUpload() {
  1709. $this->expectException(\OCP\AppFramework\OCS\OCSForbiddenException::class);
  1710. $this->expectExceptionMessage('Public upload disabled by the administrator');
  1711. $path = $this->getMockBuilder(Folder::class)->getMock();
  1712. $path->method('getId')->willReturn(42);
  1713. $storage = $this->createMock(Storage::class);
  1714. $storage->method('instanceOfStorage')
  1715. ->willReturnMap([
  1716. ['OCA\Files_Sharing\External\Storage', false],
  1717. ['OCA\Files_Sharing\SharedStorage', false],
  1718. ]);
  1719. $path->method('getStorage')->willReturn($storage);
  1720. $this->rootFolder->method('getUserFolder')->with($this->currentUser)->willReturnSelf();
  1721. $this->rootFolder->method('get')->with('valid-path')->willReturn($path);
  1722. $this->rootFolder->method('getById')
  1723. ->willReturn([]);
  1724. $this->shareManager->method('newShare')->willReturn(\OC::$server->getShareManager()->newShare());
  1725. $this->shareManager->method('shareApiAllowLinks')->willReturn(true);
  1726. $this->ocs->createShare('valid-path', \OCP\Constants::PERMISSION_ALL, IShare::TYPE_LINK, null, 'true');
  1727. }
  1728. public function testCreateShareLinkPublicUploadFile() {
  1729. $this->expectException(\OCP\AppFramework\OCS\OCSNotFoundException::class);
  1730. $this->expectExceptionMessage('Public upload is only possible for publicly shared folders');
  1731. $path = $this->getMockBuilder(File::class)->getMock();
  1732. $path->method('getId')->willReturn(42);
  1733. $storage = $this->createMock(Storage::class);
  1734. $storage->method('instanceOfStorage')
  1735. ->willReturnMap([
  1736. ['OCA\Files_Sharing\External\Storage', false],
  1737. ['OCA\Files_Sharing\SharedStorage', false],
  1738. ]);
  1739. $path->method('getStorage')->willReturn($storage);
  1740. $this->rootFolder->method('getUserFolder')->with($this->currentUser)->willReturnSelf();
  1741. $this->rootFolder->method('get')->with('valid-path')->willReturn($path);
  1742. $this->rootFolder->method('getById')
  1743. ->willReturn([]);
  1744. $this->shareManager->method('newShare')->willReturn(\OC::$server->getShareManager()->newShare());
  1745. $this->shareManager->method('shareApiAllowLinks')->willReturn(true);
  1746. $this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true);
  1747. $this->ocs->createShare('valid-path', \OCP\Constants::PERMISSION_ALL, IShare::TYPE_LINK, null, 'true');
  1748. }
  1749. public function testCreateShareLinkPublicUploadFolder() {
  1750. $ocs = $this->mockFormatShare();
  1751. $path = $this->getMockBuilder(Folder::class)->getMock();
  1752. $path->method('getId')->willReturn(1);
  1753. $storage = $this->createMock(Storage::class);
  1754. $storage->method('instanceOfStorage')
  1755. ->willReturnMap([
  1756. ['OCA\Files_Sharing\External\Storage', false],
  1757. ['OCA\Files_Sharing\SharedStorage', false],
  1758. ]);
  1759. $path->method('getStorage')->willReturn($storage);
  1760. $this->rootFolder->method('getUserFolder')->with($this->currentUser)->willReturnSelf();
  1761. $this->rootFolder->method('get')->with('valid-path')->willReturn($path);
  1762. $this->rootFolder->method('getById')
  1763. ->willReturn([]);
  1764. $this->shareManager->method('newShare')->willReturn(\OC::$server->getShareManager()->newShare());
  1765. $this->shareManager->method('shareApiAllowLinks')->willReturn(true);
  1766. $this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true);
  1767. $this->shareManager->expects($this->once())->method('createShare')->with(
  1768. $this->callback(function (\OCP\Share\IShare $share) use ($path) {
  1769. return $share->getNode() === $path &&
  1770. $share->getShareType() === IShare::TYPE_LINK &&
  1771. $share->getPermissions() === (\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE) &&
  1772. $share->getSharedBy() === 'currentUser' &&
  1773. $share->getPassword() === null &&
  1774. $share->getExpirationDate() === null;
  1775. })
  1776. )->willReturnArgument(0);
  1777. $expected = new DataResponse([]);
  1778. $result = $ocs->createShare('valid-path', \OCP\Constants::PERMISSION_ALL, IShare::TYPE_LINK, null, 'true', '', null, '');
  1779. $this->assertInstanceOf(get_class($expected), $result);
  1780. $this->assertEquals($expected->getData(), $result->getData());
  1781. }
  1782. public function testCreateShareLinkPassword() {
  1783. $ocs = $this->mockFormatShare();
  1784. $path = $this->getMockBuilder(Folder::class)->getMock();
  1785. $path->method('getId')->willReturn(42);
  1786. $storage = $this->createMock(Storage::class);
  1787. $storage->method('instanceOfStorage')
  1788. ->willReturnMap([
  1789. ['OCA\Files_Sharing\External\Storage', false],
  1790. ['OCA\Files_Sharing\SharedStorage', false],
  1791. ]);
  1792. $path->method('getStorage')->willReturn($storage);
  1793. $this->rootFolder->method('getUserFolder')->with($this->currentUser)->willReturnSelf();
  1794. $this->rootFolder->method('get')->with('valid-path')->willReturn($path);
  1795. $this->rootFolder->method('getById')
  1796. ->willReturn([]);
  1797. $this->shareManager->method('newShare')->willReturn(\OC::$server->getShareManager()->newShare());
  1798. $this->shareManager->method('shareApiAllowLinks')->willReturn(true);
  1799. $this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true);
  1800. $this->shareManager->expects($this->once())->method('createShare')->with(
  1801. $this->callback(function (\OCP\Share\IShare $share) use ($path) {
  1802. return $share->getNode() === $path &&
  1803. $share->getShareType() === IShare::TYPE_LINK &&
  1804. $share->getPermissions() === \OCP\Constants::PERMISSION_ALL &&
  1805. $share->getSharedBy() === 'currentUser' &&
  1806. $share->getPassword() === 'password' &&
  1807. $share->getExpirationDate() === null;
  1808. })
  1809. )->willReturnArgument(0);
  1810. $expected = new DataResponse([]);
  1811. $result = $ocs->createShare('valid-path', \OCP\Constants::PERMISSION_ALL, IShare::TYPE_LINK, null, 'false', 'password', null, '');
  1812. $this->assertInstanceOf(get_class($expected), $result);
  1813. $this->assertEquals($expected->getData(), $result->getData());
  1814. }
  1815. public function testCreateShareLinkSendPasswordByTalk() {
  1816. $ocs = $this->mockFormatShare();
  1817. $path = $this->getMockBuilder(Folder::class)->getMock();
  1818. $path->method('getId')->willReturn(42);
  1819. $storage = $this->createMock(Storage::class);
  1820. $storage->method('instanceOfStorage')
  1821. ->willReturnMap([
  1822. ['OCA\Files_Sharing\External\Storage', false],
  1823. ['OCA\Files_Sharing\SharedStorage', false],
  1824. ]);
  1825. $path->method('getStorage')->willReturn($storage);
  1826. $this->rootFolder->method('getUserFolder')->with($this->currentUser)->willReturnSelf();
  1827. $this->rootFolder->method('get')->with('valid-path')->willReturn($path);
  1828. $this->rootFolder->method('getById')
  1829. ->willReturn([]);
  1830. $this->shareManager->method('newShare')->willReturn(\OC::$server->getShareManager()->newShare());
  1831. $this->shareManager->method('shareApiAllowLinks')->willReturn(true);
  1832. $this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true);
  1833. $this->appManager->method('isEnabledForUser')->with('spreed')->willReturn(true);
  1834. $this->shareManager->expects($this->once())->method('createShare')->with(
  1835. $this->callback(function (\OCP\Share\IShare $share) use ($path) {
  1836. return $share->getNode() === $path &&
  1837. $share->getShareType() === IShare::TYPE_LINK &&
  1838. $share->getPermissions() === \OCP\Constants::PERMISSION_ALL &&
  1839. $share->getSharedBy() === 'currentUser' &&
  1840. $share->getPassword() === 'password' &&
  1841. $share->getSendPasswordByTalk() === true &&
  1842. $share->getExpirationDate() === null;
  1843. })
  1844. )->willReturnArgument(0);
  1845. $expected = new DataResponse([]);
  1846. $result = $ocs->createShare('valid-path', \OCP\Constants::PERMISSION_ALL, IShare::TYPE_LINK, null, 'false', 'password', 'true', '');
  1847. $this->assertInstanceOf(get_class($expected), $result);
  1848. $this->assertEquals($expected->getData(), $result->getData());
  1849. }
  1850. public function testCreateShareLinkSendPasswordByTalkWithTalkDisabled() {
  1851. $this->expectException(\OCP\AppFramework\OCS\OCSForbiddenException::class);
  1852. $this->expectExceptionMessage('Sharing valid-path sending the password by Nextcloud Talk failed because Nextcloud Talk is not enabled');
  1853. $ocs = $this->mockFormatShare();
  1854. $path = $this->getMockBuilder(Folder::class)->getMock();
  1855. $path->method('getId')->willReturn(42);
  1856. $storage = $this->createMock(Storage::class);
  1857. $storage->method('instanceOfStorage')
  1858. ->willReturnMap([
  1859. ['OCA\Files_Sharing\External\Storage', false],
  1860. ['OCA\Files_Sharing\SharedStorage', false],
  1861. ]);
  1862. $path->method('getStorage')->willReturn($storage);
  1863. $path->method('getPath')->willReturn('valid-path');
  1864. $this->rootFolder->method('getUserFolder')->with($this->currentUser)->willReturnSelf();
  1865. $this->rootFolder->method('get')->with('valid-path')->willReturn($path);
  1866. $this->rootFolder->method('getById')
  1867. ->willReturn([]);
  1868. $this->shareManager->method('newShare')->willReturn(\OC::$server->getShareManager()->newShare());
  1869. $this->shareManager->method('shareApiAllowLinks')->willReturn(true);
  1870. $this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true);
  1871. $this->appManager->method('isEnabledForUser')->with('spreed')->willReturn(false);
  1872. $this->shareManager->expects($this->never())->method('createShare');
  1873. $ocs->createShare('valid-path', \OCP\Constants::PERMISSION_ALL, IShare::TYPE_LINK, null, 'false', 'password', 'true', '');
  1874. }
  1875. public function testCreateShareValidExpireDate() {
  1876. $ocs = $this->mockFormatShare();
  1877. $this->request
  1878. ->method('getParam')
  1879. ->willReturnMap([
  1880. ['path', null, 'valid-path'],
  1881. ['shareType', '-1', IShare::TYPE_LINK],
  1882. ['publicUpload', null, 'false'],
  1883. ['expireDate', '', '2000-01-01'],
  1884. ['password', '', ''],
  1885. ]);
  1886. $path = $this->getMockBuilder(Folder::class)->getMock();
  1887. $path->method('getId')->willReturn(42);
  1888. $storage = $this->createMock(Storage::class);
  1889. $storage->method('instanceOfStorage')
  1890. ->willReturnMap([
  1891. ['OCA\Files_Sharing\External\Storage', false],
  1892. ['OCA\Files_Sharing\SharedStorage', false],
  1893. ]);
  1894. $path->method('getStorage')->willReturn($storage);
  1895. $this->rootFolder->method('getUserFolder')->with($this->currentUser)->willReturnSelf();
  1896. $this->rootFolder->method('get')->with('valid-path')->willReturn($path);
  1897. $this->rootFolder->method('getById')
  1898. ->willReturn([]);
  1899. $this->shareManager->method('newShare')->willReturn(\OC::$server->getShareManager()->newShare());
  1900. $this->shareManager->method('shareApiAllowLinks')->willReturn(true);
  1901. $this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true);
  1902. $this->shareManager->expects($this->once())->method('createShare')->with(
  1903. $this->callback(function (\OCP\Share\IShare $share) use ($path) {
  1904. $date = new \DateTime('2000-01-01');
  1905. $date->setTime(0, 0, 0);
  1906. return $share->getNode() === $path &&
  1907. $share->getShareType() === IShare::TYPE_LINK &&
  1908. $share->getPermissions() === \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_SHARE &&
  1909. $share->getSharedBy() === 'currentUser' &&
  1910. $share->getPassword() === null &&
  1911. $share->getExpirationDate() == $date;
  1912. })
  1913. )->willReturnArgument(0);
  1914. $expected = new DataResponse([]);
  1915. $result = $ocs->createShare('valid-path', null, IShare::TYPE_LINK, null, 'false', '', null, '2000-01-01');
  1916. $this->assertInstanceOf(get_class($expected), $result);
  1917. $this->assertEquals($expected->getData(), $result->getData());
  1918. }
  1919. public function testCreateShareInvalidExpireDate() {
  1920. $this->expectException(\OCP\AppFramework\OCS\OCSNotFoundException::class);
  1921. $this->expectExceptionMessage('Invalid date, date format must be YYYY-MM-DD');
  1922. $ocs = $this->mockFormatShare();
  1923. $path = $this->getMockBuilder(Folder::class)->getMock();
  1924. $path->method('getId')->willReturn(42);
  1925. $storage = $this->createMock(Storage::class);
  1926. $storage->method('instanceOfStorage')
  1927. ->willReturnMap([
  1928. ['OCA\Files_Sharing\External\Storage', false],
  1929. ['OCA\Files_Sharing\SharedStorage', false],
  1930. ]);
  1931. $path->method('getStorage')->willReturn($storage);
  1932. $this->rootFolder->method('getUserFolder')->with($this->currentUser)->willReturnSelf();
  1933. $this->rootFolder->method('get')->with('valid-path')->willReturn($path);
  1934. $this->rootFolder->method('getById')
  1935. ->willReturn([]);
  1936. $this->shareManager->method('newShare')->willReturn(\OC::$server->getShareManager()->newShare());
  1937. $this->shareManager->method('shareApiAllowLinks')->willReturn(true);
  1938. $this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true);
  1939. $ocs->createShare('valid-path', \OCP\Constants::PERMISSION_ALL, IShare::TYPE_LINK, null, 'false', '', null, 'a1b2d3');
  1940. }
  1941. public function testCreateShareRemote() {
  1942. $share = $this->newShare();
  1943. $this->shareManager->method('newShare')->willReturn($share);
  1944. /** @var \OCA\Files_Sharing\Controller\ShareAPIController $ocs */
  1945. $ocs = $this->getMockBuilder(ShareAPIController::class)
  1946. ->setConstructorArgs([
  1947. $this->appName,
  1948. $this->request,
  1949. $this->shareManager,
  1950. $this->groupManager,
  1951. $this->userManager,
  1952. $this->rootFolder,
  1953. $this->urlGenerator,
  1954. $this->currentUser,
  1955. $this->l,
  1956. $this->config,
  1957. $this->appManager,
  1958. $this->serverContainer,
  1959. $this->userStatusManager,
  1960. $this->previewManager,
  1961. $this->dateTimeZone,
  1962. ])->setMethods(['formatShare'])
  1963. ->getMock();
  1964. [$userFolder, $path] = $this->getNonSharedUserFile();
  1965. $this->rootFolder->expects($this->exactly(2))
  1966. ->method('getUserFolder')
  1967. ->with('currentUser')
  1968. ->willReturn($userFolder);
  1969. $userFolder->expects($this->once())
  1970. ->method('get')
  1971. ->with('valid-path')
  1972. ->willReturn($path);
  1973. $userFolder->method('getById')
  1974. ->willReturn([]);
  1975. $this->userManager->method('userExists')->with('validUser')->willReturn(true);
  1976. $path->expects($this->once())
  1977. ->method('lock')
  1978. ->with(\OCP\Lock\ILockingProvider::LOCK_SHARED);
  1979. $this->shareManager->method('createShare')
  1980. ->with($this->callback(function (\OCP\Share\IShare $share) use ($path) {
  1981. return $share->getNode() === $path &&
  1982. $share->getPermissions() === (
  1983. \OCP\Constants::PERMISSION_ALL &
  1984. ~\OCP\Constants::PERMISSION_DELETE &
  1985. ~\OCP\Constants::PERMISSION_CREATE
  1986. ) &&
  1987. $share->getShareType() === IShare::TYPE_REMOTE &&
  1988. $share->getSharedWith() === 'user@example.org' &&
  1989. $share->getSharedBy() === 'currentUser';
  1990. }))
  1991. ->willReturnArgument(0);
  1992. $this->shareManager->method('outgoingServer2ServerSharesAllowed')->willReturn(true);
  1993. $expected = new DataResponse([]);
  1994. $result = $ocs->createShare('valid-path', \OCP\Constants::PERMISSION_ALL, IShare::TYPE_REMOTE, 'user@example.org');
  1995. $this->assertInstanceOf(get_class($expected), $result);
  1996. $this->assertEquals($expected->getData(), $result->getData());
  1997. }
  1998. public function testCreateShareRemoteGroup() {
  1999. $share = $this->newShare();
  2000. $this->shareManager->method('newShare')->willReturn($share);
  2001. /** @var \OCA\Files_Sharing\Controller\ShareAPIController $ocs */
  2002. $ocs = $this->getMockBuilder(ShareAPIController::class)
  2003. ->setConstructorArgs([
  2004. $this->appName,
  2005. $this->request,
  2006. $this->shareManager,
  2007. $this->groupManager,
  2008. $this->userManager,
  2009. $this->rootFolder,
  2010. $this->urlGenerator,
  2011. $this->currentUser,
  2012. $this->l,
  2013. $this->config,
  2014. $this->appManager,
  2015. $this->serverContainer,
  2016. $this->userStatusManager,
  2017. $this->previewManager,
  2018. $this->dateTimeZone,
  2019. ])->setMethods(['formatShare'])
  2020. ->getMock();
  2021. [$userFolder, $path] = $this->getNonSharedUserFile();
  2022. $this->rootFolder->expects($this->exactly(2))
  2023. ->method('getUserFolder')
  2024. ->with('currentUser')
  2025. ->willReturn($userFolder);
  2026. $userFolder->expects($this->once())
  2027. ->method('get')
  2028. ->with('valid-path')
  2029. ->willReturn($path);
  2030. $userFolder->method('getById')
  2031. ->willReturn([]);
  2032. $this->userManager->method('userExists')->with('validUser')->willReturn(true);
  2033. $path->expects($this->once())
  2034. ->method('lock')
  2035. ->with(\OCP\Lock\ILockingProvider::LOCK_SHARED);
  2036. $this->shareManager->method('createShare')
  2037. ->with($this->callback(function (\OCP\Share\IShare $share) use ($path) {
  2038. return $share->getNode() === $path &&
  2039. $share->getPermissions() === (
  2040. \OCP\Constants::PERMISSION_ALL &
  2041. ~\OCP\Constants::PERMISSION_DELETE &
  2042. ~\OCP\Constants::PERMISSION_CREATE
  2043. ) &&
  2044. $share->getShareType() === IShare::TYPE_REMOTE_GROUP &&
  2045. $share->getSharedWith() === 'group@example.org' &&
  2046. $share->getSharedBy() === 'currentUser';
  2047. }))
  2048. ->willReturnArgument(0);
  2049. $this->shareManager->method('outgoingServer2ServerGroupSharesAllowed')->willReturn(true);
  2050. $expected = new DataResponse([]);
  2051. $result = $ocs->createShare('valid-path', \OCP\Constants::PERMISSION_ALL, IShare::TYPE_REMOTE_GROUP, 'group@example.org');
  2052. $this->assertInstanceOf(get_class($expected), $result);
  2053. $this->assertEquals($expected->getData(), $result->getData());
  2054. }
  2055. public function testCreateShareRoom() {
  2056. $ocs = $this->mockFormatShare();
  2057. $share = $this->newShare();
  2058. $this->shareManager->method('newShare')->willReturn($share);
  2059. [$userFolder, $path] = $this->getNonSharedUserFile();
  2060. $this->rootFolder->expects($this->exactly(2))
  2061. ->method('getUserFolder')
  2062. ->with('currentUser')
  2063. ->willReturn($userFolder);
  2064. $userFolder->expects($this->once())
  2065. ->method('get')
  2066. ->with('valid-path')
  2067. ->willReturn($path);
  2068. $userFolder->method('getById')
  2069. ->willReturn([]);
  2070. $path->expects($this->once())
  2071. ->method('lock')
  2072. ->with(\OCP\Lock\ILockingProvider::LOCK_SHARED);
  2073. $this->appManager->method('isEnabledForUser')
  2074. ->with('spreed')
  2075. ->willReturn(true);
  2076. $helper = $this->getMockBuilder('\OCA\Talk\Share\Helper\ShareAPIController')
  2077. ->setMethods(['createShare'])
  2078. ->getMock();
  2079. $helper->method('createShare')
  2080. ->with(
  2081. $share,
  2082. 'recipientRoom',
  2083. \OCP\Constants::PERMISSION_ALL &
  2084. ~\OCP\Constants::PERMISSION_DELETE &
  2085. ~\OCP\Constants::PERMISSION_CREATE,
  2086. ''
  2087. )->willReturnCallback(
  2088. function ($share) {
  2089. $share->setSharedWith('recipientRoom');
  2090. $share->setPermissions(
  2091. \OCP\Constants::PERMISSION_ALL &
  2092. ~\OCP\Constants::PERMISSION_DELETE &
  2093. ~\OCP\Constants::PERMISSION_CREATE
  2094. );
  2095. }
  2096. );
  2097. $this->serverContainer->method('get')
  2098. ->with('\OCA\Talk\Share\Helper\ShareAPIController')
  2099. ->willReturn($helper);
  2100. $this->shareManager->method('createShare')
  2101. ->with($this->callback(function (\OCP\Share\IShare $share) use ($path) {
  2102. return $share->getNode() === $path &&
  2103. $share->getPermissions() === (
  2104. \OCP\Constants::PERMISSION_ALL &
  2105. ~\OCP\Constants::PERMISSION_DELETE &
  2106. ~\OCP\Constants::PERMISSION_CREATE
  2107. ) &&
  2108. $share->getShareType() === IShare::TYPE_ROOM &&
  2109. $share->getSharedWith() === 'recipientRoom' &&
  2110. $share->getSharedBy() === 'currentUser';
  2111. }))
  2112. ->willReturnArgument(0);
  2113. $expected = new DataResponse([]);
  2114. $result = $ocs->createShare('valid-path', \OCP\Constants::PERMISSION_ALL, IShare::TYPE_ROOM, 'recipientRoom');
  2115. $this->assertInstanceOf(get_class($expected), $result);
  2116. $this->assertEquals($expected->getData(), $result->getData());
  2117. }
  2118. public function testCreateShareRoomHelperNotAvailable() {
  2119. $this->expectException(\OCP\AppFramework\OCS\OCSForbiddenException::class);
  2120. $this->expectExceptionMessage('Sharing valid-path failed because the back end does not support room shares');
  2121. $ocs = $this->mockFormatShare();
  2122. $share = $this->newShare();
  2123. $this->shareManager->method('newShare')->willReturn($share);
  2124. [$userFolder, $path] = $this->getNonSharedUserFolder();
  2125. $this->rootFolder->expects($this->exactly(2))
  2126. ->method('getUserFolder')
  2127. ->with('currentUser')
  2128. ->willReturn($userFolder);
  2129. $path->method('getPath')->willReturn('valid-path');
  2130. $userFolder->expects($this->once())
  2131. ->method('get')
  2132. ->with('valid-path')
  2133. ->willReturn($path);
  2134. $userFolder->method('getById')
  2135. ->willReturn([]);
  2136. $path->expects($this->once())
  2137. ->method('lock')
  2138. ->with(\OCP\Lock\ILockingProvider::LOCK_SHARED);
  2139. $this->appManager->method('isEnabledForUser')
  2140. ->with('spreed')
  2141. ->willReturn(false);
  2142. $this->shareManager->expects($this->never())->method('createShare');
  2143. $ocs->createShare('valid-path', \OCP\Constants::PERMISSION_ALL, IShare::TYPE_ROOM, 'recipientRoom');
  2144. }
  2145. public function testCreateShareRoomHelperThrowException() {
  2146. $this->expectException(\OCP\AppFramework\OCS\OCSNotFoundException::class);
  2147. $this->expectExceptionMessage('Exception thrown by the helper');
  2148. $ocs = $this->mockFormatShare();
  2149. $share = $this->newShare();
  2150. $share->setSharedBy('currentUser');
  2151. $this->shareManager->method('newShare')->willReturn($share);
  2152. [$userFolder, $path] = $this->getNonSharedUserFile();
  2153. $this->rootFolder->expects($this->exactly(2))
  2154. ->method('getUserFolder')
  2155. ->with('currentUser')
  2156. ->willReturn($userFolder);
  2157. $userFolder->expects($this->once())
  2158. ->method('get')
  2159. ->with('valid-path')
  2160. ->willReturn($path);
  2161. $userFolder->method('getById')
  2162. ->willReturn([]);
  2163. $path->expects($this->once())
  2164. ->method('lock')
  2165. ->with(\OCP\Lock\ILockingProvider::LOCK_SHARED);
  2166. $this->appManager->method('isEnabledForUser')
  2167. ->with('spreed')
  2168. ->willReturn(true);
  2169. $helper = $this->getMockBuilder('\OCA\Talk\Share\Helper\ShareAPIController')
  2170. ->setMethods(['createShare'])
  2171. ->getMock();
  2172. $helper->method('createShare')
  2173. ->with(
  2174. $share,
  2175. 'recipientRoom',
  2176. \OCP\Constants::PERMISSION_ALL &
  2177. ~\OCP\Constants::PERMISSION_DELETE &
  2178. ~\OCP\Constants::PERMISSION_CREATE,
  2179. ''
  2180. )->willReturnCallback(
  2181. function ($share) {
  2182. throw new OCSNotFoundException("Exception thrown by the helper");
  2183. }
  2184. );
  2185. $this->serverContainer->method('get')
  2186. ->with('\OCA\Talk\Share\Helper\ShareAPIController')
  2187. ->willReturn($helper);
  2188. $this->shareManager->expects($this->never())->method('createShare');
  2189. $ocs->createShare('valid-path', \OCP\Constants::PERMISSION_ALL, IShare::TYPE_ROOM, 'recipientRoom');
  2190. }
  2191. /**
  2192. * Test for https://github.com/owncloud/core/issues/22587
  2193. * TODO: Remove once proper solution is in place
  2194. */
  2195. public function testCreateReshareOfFederatedMountNoDeletePermissions() {
  2196. $share = \OC::$server->getShareManager()->newShare();
  2197. $this->shareManager->method('newShare')->willReturn($share);
  2198. /** @var ShareAPIController|\PHPUnit\Framework\MockObject\MockObject $ocs */
  2199. $ocs = $this->getMockBuilder(ShareAPIController::class)
  2200. ->setConstructorArgs([
  2201. $this->appName,
  2202. $this->request,
  2203. $this->shareManager,
  2204. $this->groupManager,
  2205. $this->userManager,
  2206. $this->rootFolder,
  2207. $this->urlGenerator,
  2208. $this->currentUser,
  2209. $this->l,
  2210. $this->config,
  2211. $this->appManager,
  2212. $this->serverContainer,
  2213. $this->userStatusManager,
  2214. $this->previewManager,
  2215. $this->dateTimeZone,
  2216. ])->setMethods(['formatShare'])
  2217. ->getMock();
  2218. $userFolder = $this->getMockBuilder(Folder::class)->getMock();
  2219. $this->rootFolder->expects($this->exactly(2))
  2220. ->method('getUserFolder')
  2221. ->with('currentUser')
  2222. ->willReturn($userFolder);
  2223. $path = $this->getMockBuilder(Folder::class)->getMock();
  2224. $path->method('getId')->willReturn(42);
  2225. $storage = $this->createMock(Storage::class);
  2226. $storage->method('instanceOfStorage')
  2227. ->willReturnMap([
  2228. ['OCA\Files_Sharing\External\Storage', true],
  2229. ['OCA\Files_Sharing\SharedStorage', false],
  2230. ]);
  2231. $userFolder->method('getStorage')->willReturn($storage);
  2232. $path->method('getStorage')->willReturn($storage);
  2233. $path->method('getPermissions')->willReturn(\OCP\Constants::PERMISSION_READ);
  2234. $userFolder->expects($this->once())
  2235. ->method('get')
  2236. ->with('valid-path')
  2237. ->willReturn($path);
  2238. $userFolder->method('getById')
  2239. ->willReturn([]);
  2240. $this->userManager->method('userExists')->with('validUser')->willReturn(true);
  2241. $this->shareManager
  2242. ->expects($this->once())
  2243. ->method('createShare')
  2244. ->with($this->callback(function (\OCP\Share\IShare $share) {
  2245. return $share->getPermissions() === \OCP\Constants::PERMISSION_READ;
  2246. }))
  2247. ->willReturnArgument(0);
  2248. $ocs->createShare('valid-path', \OCP\Constants::PERMISSION_ALL, IShare::TYPE_USER, 'validUser');
  2249. }
  2250. public function testUpdateShareCantAccess() {
  2251. $this->expectException(\OCP\AppFramework\OCS\OCSNotFoundException::class);
  2252. $this->expectExceptionMessage('Wrong share ID, share does not exist');
  2253. [$userFolder, $node] = $this->getNonSharedUserFolder();
  2254. $share = $this->newShare();
  2255. $share->setNode($node);
  2256. $node->expects($this->once())
  2257. ->method('lock')
  2258. ->with(\OCP\Lock\ILockingProvider::LOCK_SHARED);
  2259. $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
  2260. $this->rootFolder->method('getUserFolder')
  2261. ->with($this->currentUser)
  2262. ->willReturn($userFolder);
  2263. $userFolder->method('getFirstNodeById')
  2264. ->with($share->getNodeId())
  2265. ->willReturn($share->getNode());
  2266. $this->ocs->updateShare(42);
  2267. }
  2268. public function testUpdateNoParametersLink() {
  2269. $this->expectException(\OCP\AppFramework\OCS\OCSBadRequestException::class);
  2270. $this->expectExceptionMessage('Wrong or no update parameter given');
  2271. $node = $this->getMockBuilder(Folder::class)->getMock();
  2272. $share = $this->newShare();
  2273. $share->setPermissions(\OCP\Constants::PERMISSION_ALL)
  2274. ->setSharedBy($this->currentUser)
  2275. ->setShareType(IShare::TYPE_LINK)
  2276. ->setNode($node);
  2277. $node->expects($this->once())
  2278. ->method('lock')
  2279. ->with(\OCP\Lock\ILockingProvider::LOCK_SHARED);
  2280. $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
  2281. $this->ocs->updateShare(42);
  2282. }
  2283. public function testUpdateNoParametersOther() {
  2284. $this->expectException(\OCP\AppFramework\OCS\OCSBadRequestException::class);
  2285. $this->expectExceptionMessage('Wrong or no update parameter given');
  2286. $node = $this->getMockBuilder(Folder::class)->getMock();
  2287. $share = $this->newShare();
  2288. $share->setPermissions(\OCP\Constants::PERMISSION_ALL)
  2289. ->setSharedBy($this->currentUser)
  2290. ->setShareType(IShare::TYPE_GROUP)
  2291. ->setNode($node);
  2292. $node->expects($this->once())
  2293. ->method('lock')
  2294. ->with(\OCP\Lock\ILockingProvider::LOCK_SHARED);
  2295. $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
  2296. $this->ocs->updateShare(42);
  2297. }
  2298. public function testUpdateLinkShareClear() {
  2299. $ocs = $this->mockFormatShare();
  2300. [$userFolder, $node] = $this->getNonSharedUserFolder();
  2301. $node->method('getId')
  2302. ->willReturn(42);
  2303. $share = $this->newShare();
  2304. $share->setPermissions(\OCP\Constants::PERMISSION_ALL)
  2305. ->setSharedBy($this->currentUser)
  2306. ->setShareType(IShare::TYPE_LINK)
  2307. ->setPassword('password')
  2308. ->setExpirationDate(new \DateTime())
  2309. ->setNote('note')
  2310. ->setLabel('label')
  2311. ->setHideDownload(true)
  2312. ->setPermissions(\OCP\Constants::PERMISSION_ALL)
  2313. ->setNode($node);
  2314. $node->expects($this->once())
  2315. ->method('lock')
  2316. ->with(\OCP\Lock\ILockingProvider::LOCK_SHARED);
  2317. $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
  2318. $this->shareManager->expects($this->once())->method('updateShare')->with(
  2319. $this->callback(function (\OCP\Share\IShare $share) {
  2320. return $share->getPermissions() === \OCP\Constants::PERMISSION_READ &&
  2321. $share->getPassword() === null &&
  2322. $share->getExpirationDate() === null &&
  2323. // Once set a note or a label are never back to null, only to an
  2324. // empty string.
  2325. $share->getNote() === '' &&
  2326. $share->getLabel() === '' &&
  2327. $share->getHideDownload() === false;
  2328. })
  2329. )->willReturnArgument(0);
  2330. $this->shareManager->method('getSharedWith')
  2331. ->willReturn([]);
  2332. $this->rootFolder->method('getUserFolder')
  2333. ->with($this->currentUser)
  2334. ->willReturn($userFolder);
  2335. $userFolder->method('getFirstNodeById')
  2336. ->with(42)
  2337. ->willReturn($node);
  2338. $mountPoint = $this->createMock(IMountPoint::class);
  2339. $node->method('getMountPoint')
  2340. ->willReturn($mountPoint);
  2341. $mountPoint->method('getStorageRootId')
  2342. ->willReturn(42);
  2343. $expected = new DataResponse([]);
  2344. $result = $ocs->updateShare(42, null, '', null, 'false', '', '', '', 'false');
  2345. $this->assertInstanceOf(get_class($expected), $result);
  2346. $this->assertEquals($expected->getData(), $result->getData());
  2347. }
  2348. public function testUpdateLinkShareSet() {
  2349. $ocs = $this->mockFormatShare();
  2350. [$userFolder, $folder] = $this->getNonSharedUserFolder();
  2351. $folder->method('getId')
  2352. ->willReturn(42);
  2353. $share = \OC::$server->getShareManager()->newShare();
  2354. $share->setPermissions(\OCP\Constants::PERMISSION_ALL)
  2355. ->setSharedBy($this->currentUser)
  2356. ->setShareType(IShare::TYPE_LINK)
  2357. ->setNode($folder);
  2358. $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
  2359. $this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true);
  2360. $this->shareManager->expects($this->once())->method('updateShare')->with(
  2361. $this->callback(function (\OCP\Share\IShare $share) {
  2362. $date = new \DateTime('2000-01-01');
  2363. $date->setTime(0, 0, 0);
  2364. return $share->getPermissions() === (\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE) &&
  2365. $share->getPassword() === 'password' &&
  2366. $share->getExpirationDate() == $date &&
  2367. $share->getNote() === 'note' &&
  2368. $share->getLabel() === 'label' &&
  2369. $share->getHideDownload() === true;
  2370. })
  2371. )->willReturnArgument(0);
  2372. $this->shareManager->method('getSharedWith')
  2373. ->willReturn([]);
  2374. $this->rootFolder->method('getUserFolder')
  2375. ->with($this->currentUser)
  2376. ->willReturn($userFolder);
  2377. $userFolder->method('getFirstNodeById')
  2378. ->with(42)
  2379. ->willReturn($folder);
  2380. $mountPoint = $this->createMock(IMountPoint::class);
  2381. $folder->method('getMountPoint')
  2382. ->willReturn($mountPoint);
  2383. $mountPoint->method('getStorageRootId')
  2384. ->willReturn(42);
  2385. $expected = new DataResponse([]);
  2386. $result = $ocs->updateShare(42, null, 'password', null, 'true', '2000-01-01', 'note', 'label', 'true');
  2387. $this->assertInstanceOf(get_class($expected), $result);
  2388. $this->assertEquals($expected->getData(), $result->getData());
  2389. }
  2390. /**
  2391. * @dataProvider publicUploadParamsProvider
  2392. */
  2393. public function testUpdateLinkShareEnablePublicUpload($permissions, $publicUpload, $expireDate, $password) {
  2394. $ocs = $this->mockFormatShare();
  2395. [$userFolder, $folder] = $this->getNonSharedUserFolder();
  2396. $folder->method('getId')
  2397. ->willReturn(42);
  2398. $share = \OC::$server->getShareManager()->newShare();
  2399. $share->setPermissions(\OCP\Constants::PERMISSION_ALL)
  2400. ->setSharedBy($this->currentUser)
  2401. ->setShareType(IShare::TYPE_LINK)
  2402. ->setPassword('password')
  2403. ->setNode($folder);
  2404. $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
  2405. $this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true);
  2406. $this->shareManager->method('getSharedWith')->willReturn([]);
  2407. $this->shareManager->expects($this->once())->method('updateShare')->with(
  2408. $this->callback(function (\OCP\Share\IShare $share) {
  2409. return $share->getPermissions() === (\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE) &&
  2410. $share->getPassword() === 'password' &&
  2411. $share->getExpirationDate() === null;
  2412. })
  2413. )->willReturnArgument(0);
  2414. $this->rootFolder->method('getUserFolder')
  2415. ->with($this->currentUser)
  2416. ->willReturn($userFolder);
  2417. $userFolder->method('getFirstNodeById')
  2418. ->with(42)
  2419. ->willReturn($folder);
  2420. $mountPoint = $this->createMock(IMountPoint::class);
  2421. $folder->method('getMountPoint')
  2422. ->willReturn($mountPoint);
  2423. $mountPoint->method('getStorageRootId')
  2424. ->willReturn(42);
  2425. $expected = new DataResponse([]);
  2426. $result = $ocs->updateShare(42, $permissions, $password, null, $publicUpload, $expireDate);
  2427. $this->assertInstanceOf(get_class($expected), $result);
  2428. $this->assertEquals($expected->getData(), $result->getData());
  2429. }
  2430. public function publicLinkValidPermissionsProvider() {
  2431. return [
  2432. [\OCP\Constants::PERMISSION_CREATE],
  2433. [\OCP\Constants::PERMISSION_READ],
  2434. [\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_UPDATE],
  2435. [\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_DELETE],
  2436. [\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_CREATE],
  2437. ];
  2438. }
  2439. /**
  2440. * @dataProvider publicLinkValidPermissionsProvider
  2441. */
  2442. public function testUpdateLinkShareSetCRUDPermissions($permissions) {
  2443. $ocs = $this->mockFormatShare();
  2444. [$userFolder, $folder] = $this->getNonSharedUserFolder();
  2445. $folder->method('getId')
  2446. ->willReturn(42);
  2447. $share = \OC::$server->getShareManager()->newShare();
  2448. $share->setPermissions(\OCP\Constants::PERMISSION_ALL)
  2449. ->setSharedBy($this->currentUser)
  2450. ->setShareType(IShare::TYPE_LINK)
  2451. ->setPassword('password')
  2452. ->setNode($folder);
  2453. $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
  2454. $this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true);
  2455. $this->shareManager->method('getSharedWith')->willReturn([]);
  2456. $this->shareManager
  2457. ->expects($this->any())
  2458. ->method('updateShare')
  2459. ->willReturnArgument(0);
  2460. $this->rootFolder->method('getUserFolder')
  2461. ->with($this->currentUser)
  2462. ->willReturn($userFolder);
  2463. $userFolder->method('getFirstNodeById')
  2464. ->with(42)
  2465. ->willReturn($folder);
  2466. $mountPoint = $this->createMock(IMountPoint::class);
  2467. $folder->method('getMountPoint')
  2468. ->willReturn($mountPoint);
  2469. $mountPoint->method('getStorageRootId')
  2470. ->willReturn(42);
  2471. $expected = new DataResponse([]);
  2472. $result = $ocs->updateShare(42, $permissions, 'password', null, 'true', null);
  2473. $this->assertInstanceOf(get_class($expected), $result);
  2474. $this->assertEquals($expected->getData(), $result->getData());
  2475. }
  2476. public function publicLinkInvalidPermissionsProvider1() {
  2477. return [
  2478. [\OCP\Constants::PERMISSION_DELETE],
  2479. [\OCP\Constants::PERMISSION_UPDATE],
  2480. [\OCP\Constants::PERMISSION_SHARE],
  2481. ];
  2482. }
  2483. /**
  2484. * @dataProvider publicLinkInvalidPermissionsProvider1
  2485. */
  2486. public function testUpdateLinkShareSetInvalidCRUDPermissions1($permissions) {
  2487. $this->expectException(\OCP\AppFramework\OCS\OCSBadRequestException::class);
  2488. $this->expectExceptionMessage('Share must at least have READ or CREATE permissions');
  2489. $this->testUpdateLinkShareSetCRUDPermissions($permissions);
  2490. }
  2491. public function publicLinkInvalidPermissionsProvider2() {
  2492. return [
  2493. [\OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_DELETE],
  2494. [\OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE],
  2495. ];
  2496. }
  2497. /**
  2498. * @dataProvider publicLinkInvalidPermissionsProvider2
  2499. */
  2500. public function testUpdateLinkShareSetInvalidCRUDPermissions2($permissions) {
  2501. $this->expectException(\OCP\AppFramework\OCS\OCSBadRequestException::class);
  2502. $this->expectExceptionMessage('Share must have READ permission if UPDATE or DELETE permission is set');
  2503. $this->testUpdateLinkShareSetCRUDPermissions($permissions);
  2504. }
  2505. public function testUpdateLinkShareInvalidDate() {
  2506. $this->expectException(\OCP\AppFramework\OCS\OCSBadRequestException::class);
  2507. $this->expectExceptionMessage('Invalid date. Format must be YYYY-MM-DD');
  2508. $ocs = $this->mockFormatShare();
  2509. [$userFolder, $folder] = $this->getNonSharedUserFolder();
  2510. $userFolder->method('getFirstNodeById')
  2511. ->with(42)
  2512. ->willReturn($folder);
  2513. $this->rootFolder->method('getUserFolder')
  2514. ->with($this->currentUser)
  2515. ->willReturn($userFolder);
  2516. $folder->method('getId')
  2517. ->willReturn(42);
  2518. $share = \OC::$server->getShareManager()->newShare();
  2519. $share->setPermissions(\OCP\Constants::PERMISSION_ALL)
  2520. ->setSharedBy($this->currentUser)
  2521. ->setShareType(IShare::TYPE_LINK)
  2522. ->setNode($folder);
  2523. $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
  2524. $this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true);
  2525. $ocs->updateShare(42, null, 'password', null, 'true', '2000-01-a');
  2526. }
  2527. public function publicUploadParamsProvider() {
  2528. return [
  2529. [null, 'true', null, 'password'],
  2530. // legacy had no delete
  2531. [
  2532. \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE,
  2533. 'true', null, 'password'
  2534. ],
  2535. // correct
  2536. [
  2537. \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE,
  2538. null, null, 'password'
  2539. ],
  2540. ];
  2541. }
  2542. /**
  2543. * @dataProvider publicUploadParamsProvider
  2544. */
  2545. public function testUpdateLinkSharePublicUploadNotAllowed($permissions, $publicUpload, $expireDate, $password) {
  2546. $this->expectException(\OCP\AppFramework\OCS\OCSForbiddenException::class);
  2547. $this->expectExceptionMessage('Public upload disabled by the administrator');
  2548. $ocs = $this->mockFormatShare();
  2549. [$userFolder, $folder] = $this->getNonSharedUserFolder();
  2550. $userFolder->method('getFirstNodeById')
  2551. ->with(42)
  2552. ->willReturn($folder);
  2553. $this->rootFolder->method('getUserFolder')
  2554. ->with($this->currentUser)
  2555. ->willReturn($userFolder);
  2556. $folder->method('getId')->willReturn(42);
  2557. $share = \OC::$server->getShareManager()->newShare();
  2558. $share->setPermissions(\OCP\Constants::PERMISSION_ALL)
  2559. ->setSharedBy($this->currentUser)
  2560. ->setShareType(IShare::TYPE_LINK)
  2561. ->setNode($folder);
  2562. $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
  2563. $this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(false);
  2564. $ocs->updateShare(42, $permissions, $password, null, $publicUpload, $expireDate);
  2565. }
  2566. public function testUpdateLinkSharePublicUploadOnFile() {
  2567. $this->expectException(\OCP\AppFramework\OCS\OCSBadRequestException::class);
  2568. $this->expectExceptionMessage('Public upload is only possible for publicly shared folders');
  2569. $ocs = $this->mockFormatShare();
  2570. $file = $this->getMockBuilder(File::class)->getMock();
  2571. $file->method('getId')
  2572. ->willReturn(42);
  2573. [$userFolder, $folder] = $this->getNonSharedUserFolder();
  2574. $userFolder->method('getFirstNodeById')
  2575. ->with(42)
  2576. ->willReturn($folder);
  2577. $this->rootFolder->method('getUserFolder')
  2578. ->with($this->currentUser)
  2579. ->willReturn($userFolder);
  2580. $share = \OC::$server->getShareManager()->newShare();
  2581. $share->setPermissions(\OCP\Constants::PERMISSION_ALL)
  2582. ->setSharedBy($this->currentUser)
  2583. ->setShareType(IShare::TYPE_LINK)
  2584. ->setNode($file);
  2585. $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
  2586. $this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true);
  2587. $ocs->updateShare(42, null, 'password', null, 'true', '');
  2588. }
  2589. public function testUpdateLinkSharePasswordDoesNotChangeOther(): void {
  2590. $ocs = $this->mockFormatShare();
  2591. $date = new \DateTime('2000-01-01');
  2592. $date->setTime(0, 0, 0);
  2593. [$userFolder, $node] = $this->getNonSharedUserFolder();
  2594. $node->method('getId')->willReturn(42);
  2595. $userFolder->method('getFirstNodeById')
  2596. ->with(42)
  2597. ->willReturn($node);
  2598. $this->rootFolder->method('getUserFolder')
  2599. ->with($this->currentUser)
  2600. ->willReturn($userFolder);
  2601. $share = $this->newShare();
  2602. $share->setPermissions(\OCP\Constants::PERMISSION_ALL)
  2603. ->setSharedBy($this->currentUser)
  2604. ->setShareType(IShare::TYPE_LINK)
  2605. ->setPassword('password')
  2606. ->setSendPasswordByTalk(true)
  2607. ->setExpirationDate($date)
  2608. ->setNote('note')
  2609. ->setLabel('label')
  2610. ->setHideDownload(true)
  2611. ->setPermissions(\OCP\Constants::PERMISSION_ALL)
  2612. ->setNode($node);
  2613. $node->expects($this->once())
  2614. ->method('lock')
  2615. ->with(\OCP\Lock\ILockingProvider::LOCK_SHARED);
  2616. $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
  2617. $this->shareManager->expects($this->once())->method('updateShare')->with(
  2618. $this->callback(function (\OCP\Share\IShare $share) use ($date) {
  2619. return $share->getPermissions() === \OCP\Constants::PERMISSION_ALL &&
  2620. $share->getPassword() === 'newpassword' &&
  2621. $share->getSendPasswordByTalk() === true &&
  2622. $share->getExpirationDate() === $date &&
  2623. $share->getNote() === 'note' &&
  2624. $share->getLabel() === 'label' &&
  2625. $share->getHideDownload() === true;
  2626. })
  2627. )->willReturnArgument(0);
  2628. $expected = new DataResponse([]);
  2629. $result = $ocs->updateShare(42, null, 'newpassword', null, null, null, null, null, null);
  2630. $this->assertInstanceOf(get_class($expected), $result);
  2631. $this->assertEquals($expected->getData(), $result->getData());
  2632. }
  2633. public function testUpdateLinkShareSendPasswordByTalkDoesNotChangeOther() {
  2634. $ocs = $this->mockFormatShare();
  2635. $date = new \DateTime('2000-01-01');
  2636. $date->setTime(0, 0, 0);
  2637. [$userFolder, $node] = $this->getNonSharedUserFolder();
  2638. $userFolder->method('getFirstNodeById')
  2639. ->with(42)
  2640. ->willReturn($node);
  2641. $this->rootFolder->method('getUserFolder')
  2642. ->with($this->currentUser)
  2643. ->willReturn($userFolder);
  2644. $node->method('getId')->willReturn(42);
  2645. $share = $this->newShare();
  2646. $share->setPermissions(\OCP\Constants::PERMISSION_ALL)
  2647. ->setSharedBy($this->currentUser)
  2648. ->setShareType(IShare::TYPE_LINK)
  2649. ->setPassword('password')
  2650. ->setSendPasswordByTalk(false)
  2651. ->setExpirationDate($date)
  2652. ->setNote('note')
  2653. ->setLabel('label')
  2654. ->setHideDownload(true)
  2655. ->setPermissions(\OCP\Constants::PERMISSION_ALL)
  2656. ->setNode($node);
  2657. $node->expects($this->once())
  2658. ->method('lock')
  2659. ->with(\OCP\Lock\ILockingProvider::LOCK_SHARED);
  2660. $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
  2661. $this->appManager->method('isEnabledForUser')->with('spreed')->willReturn(true);
  2662. $this->shareManager->expects($this->once())->method('updateShare')->with(
  2663. $this->callback(function (\OCP\Share\IShare $share) use ($date) {
  2664. return $share->getPermissions() === \OCP\Constants::PERMISSION_ALL &&
  2665. $share->getPassword() === 'password' &&
  2666. $share->getSendPasswordByTalk() === true &&
  2667. $share->getExpirationDate() === $date &&
  2668. $share->getNote() === 'note' &&
  2669. $share->getLabel() === 'label' &&
  2670. $share->getHideDownload() === true;
  2671. })
  2672. )->willReturnArgument(0);
  2673. $expected = new DataResponse([]);
  2674. $result = $ocs->updateShare(42, null, null, 'true', null, null, null, null, null);
  2675. $this->assertInstanceOf(get_class($expected), $result);
  2676. $this->assertEquals($expected->getData(), $result->getData());
  2677. }
  2678. public function testUpdateLinkShareSendPasswordByTalkWithTalkDisabledDoesNotChangeOther() {
  2679. $this->expectException(\OCP\AppFramework\OCS\OCSForbiddenException::class);
  2680. $this->expectExceptionMessage('"Sending the password by Nextcloud Talk" for sharing a file or folder failed because Nextcloud Talk is not enabled.');
  2681. $ocs = $this->mockFormatShare();
  2682. $date = new \DateTime('2000-01-01');
  2683. $date->setTime(0, 0, 0);
  2684. [$userFolder, $node] = $this->getNonSharedUserFolder();
  2685. $userFolder->method('getFirstNodeById')
  2686. ->with(42)
  2687. ->willReturn($node);
  2688. $this->rootFolder->method('getUserFolder')
  2689. ->with($this->currentUser)
  2690. ->willReturn($userFolder);
  2691. $node->method('getId')->willReturn(42);
  2692. $share = $this->newShare();
  2693. $share->setPermissions(\OCP\Constants::PERMISSION_ALL)
  2694. ->setSharedBy($this->currentUser)
  2695. ->setShareType(IShare::TYPE_LINK)
  2696. ->setPassword('password')
  2697. ->setSendPasswordByTalk(false)
  2698. ->setExpirationDate($date)
  2699. ->setNote('note')
  2700. ->setLabel('label')
  2701. ->setHideDownload(true)
  2702. ->setPermissions(\OCP\Constants::PERMISSION_ALL)
  2703. ->setNode($node);
  2704. $node->expects($this->once())
  2705. ->method('lock')
  2706. ->with(\OCP\Lock\ILockingProvider::LOCK_SHARED);
  2707. $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
  2708. $this->appManager->method('isEnabledForUser')->with('spreed')->willReturn(false);
  2709. $this->shareManager->expects($this->never())->method('updateShare');
  2710. $ocs->updateShare(42, null, null, 'true', null, null, null, null, null);
  2711. }
  2712. public function testUpdateLinkShareDoNotSendPasswordByTalkDoesNotChangeOther() {
  2713. $ocs = $this->mockFormatShare();
  2714. $date = new \DateTime('2000-01-01');
  2715. $date->setTime(0, 0, 0);
  2716. [$userFolder, $node] = $this->getNonSharedUserFolder();
  2717. $userFolder->method('getFirstNodeById')
  2718. ->with(42)
  2719. ->willReturn($node);
  2720. $this->rootFolder->method('getUserFolder')
  2721. ->with($this->currentUser)
  2722. ->willReturn($userFolder);
  2723. $node->method('getId')->willReturn(42);
  2724. $share = $this->newShare();
  2725. $share->setPermissions(\OCP\Constants::PERMISSION_ALL)
  2726. ->setSharedBy($this->currentUser)
  2727. ->setShareType(IShare::TYPE_LINK)
  2728. ->setPassword('password')
  2729. ->setSendPasswordByTalk(true)
  2730. ->setExpirationDate($date)
  2731. ->setNote('note')
  2732. ->setLabel('label')
  2733. ->setHideDownload(true)
  2734. ->setPermissions(\OCP\Constants::PERMISSION_ALL)
  2735. ->setNode($node);
  2736. $node->expects($this->once())
  2737. ->method('lock')
  2738. ->with(\OCP\Lock\ILockingProvider::LOCK_SHARED);
  2739. $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
  2740. $this->appManager->method('isEnabledForUser')->with('spreed')->willReturn(true);
  2741. $this->shareManager->expects($this->once())->method('updateShare')->with(
  2742. $this->callback(function (\OCP\Share\IShare $share) use ($date) {
  2743. return $share->getPermissions() === \OCP\Constants::PERMISSION_ALL &&
  2744. $share->getPassword() === 'password' &&
  2745. $share->getSendPasswordByTalk() === false &&
  2746. $share->getExpirationDate() === $date &&
  2747. $share->getNote() === 'note' &&
  2748. $share->getLabel() === 'label' &&
  2749. $share->getHideDownload() === true;
  2750. })
  2751. )->willReturnArgument(0);
  2752. $expected = new DataResponse([]);
  2753. $result = $ocs->updateShare(42, null, null, 'false', null, null, null, null, null);
  2754. $this->assertInstanceOf(get_class($expected), $result);
  2755. $this->assertEquals($expected->getData(), $result->getData());
  2756. }
  2757. public function testUpdateLinkShareDoNotSendPasswordByTalkWithTalkDisabledDoesNotChangeOther() {
  2758. $ocs = $this->mockFormatShare();
  2759. $date = new \DateTime('2000-01-01');
  2760. $date->setTime(0, 0, 0);
  2761. [$userFolder, $node] = $this->getNonSharedUserFolder();
  2762. $node->method('getId')
  2763. ->willReturn(42);
  2764. $share = $this->newShare();
  2765. $share->setPermissions(\OCP\Constants::PERMISSION_ALL)
  2766. ->setSharedBy($this->currentUser)
  2767. ->setShareType(IShare::TYPE_LINK)
  2768. ->setPassword('password')
  2769. ->setSendPasswordByTalk(true)
  2770. ->setExpirationDate($date)
  2771. ->setNote('note')
  2772. ->setLabel('label')
  2773. ->setHideDownload(true)
  2774. ->setPermissions(\OCP\Constants::PERMISSION_ALL)
  2775. ->setNode($node);
  2776. $node->expects($this->once())
  2777. ->method('lock')
  2778. ->with(\OCP\Lock\ILockingProvider::LOCK_SHARED);
  2779. $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
  2780. $this->appManager->method('isEnabledForUser')->with('spreed')->willReturn(false);
  2781. $this->shareManager->expects($this->once())->method('updateShare')->with(
  2782. $this->callback(function (\OCP\Share\IShare $share) use ($date) {
  2783. return $share->getPermissions() === \OCP\Constants::PERMISSION_ALL &&
  2784. $share->getPassword() === 'password' &&
  2785. $share->getSendPasswordByTalk() === false &&
  2786. $share->getExpirationDate() === $date &&
  2787. $share->getNote() === 'note' &&
  2788. $share->getLabel() === 'label' &&
  2789. $share->getHideDownload() === true;
  2790. })
  2791. )->willReturnArgument(0);
  2792. $this->rootFolder->method('getUserFolder')
  2793. ->with($this->currentUser)
  2794. ->willReturn($userFolder);
  2795. $userFolder->method('getFirstNodeById')
  2796. ->with(42)
  2797. ->willReturn($node);
  2798. $mountPoint = $this->createMock(IMountPoint::class);
  2799. $node->method('getMountPoint')
  2800. ->willReturn($mountPoint);
  2801. $mountPoint->method('getStorageRootId')
  2802. ->willReturn(42);
  2803. $mountPoint = $this->createMock(IMountPoint::class);
  2804. $node->method('getMountPoint')
  2805. ->willReturn($mountPoint);
  2806. $mountPoint->method('getStorageRootId')
  2807. ->willReturn(42);
  2808. $expected = new DataResponse([]);
  2809. $result = $ocs->updateShare(42, null, null, 'false', null, null, null, null, null);
  2810. $this->assertInstanceOf(get_class($expected), $result);
  2811. $this->assertEquals($expected->getData(), $result->getData());
  2812. }
  2813. public function testUpdateLinkShareExpireDateDoesNotChangeOther() {
  2814. $ocs = $this->mockFormatShare();
  2815. [$userFolder, $node] = $this->getNonSharedUserFolder();
  2816. $node->method('getId')
  2817. ->willReturn(42);
  2818. $share = $this->newShare();
  2819. $share->setPermissions(\OCP\Constants::PERMISSION_ALL)
  2820. ->setSharedBy($this->currentUser)
  2821. ->setShareType(IShare::TYPE_LINK)
  2822. ->setPassword('password')
  2823. ->setSendPasswordByTalk(true)
  2824. ->setExpirationDate(new \DateTime())
  2825. ->setNote('note')
  2826. ->setLabel('label')
  2827. ->setHideDownload(true)
  2828. ->setPermissions(\OCP\Constants::PERMISSION_ALL)
  2829. ->setNode($node);
  2830. $node->expects($this->once())
  2831. ->method('lock')
  2832. ->with(\OCP\Lock\ILockingProvider::LOCK_SHARED);
  2833. $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
  2834. $this->shareManager->expects($this->once())->method('updateShare')->with(
  2835. $this->callback(function (\OCP\Share\IShare $share) {
  2836. $date = new \DateTime('2010-12-23');
  2837. $date->setTime(0, 0, 0);
  2838. return $share->getPermissions() === \OCP\Constants::PERMISSION_ALL &&
  2839. $share->getPassword() === 'password' &&
  2840. $share->getSendPasswordByTalk() === true &&
  2841. $share->getExpirationDate() == $date &&
  2842. $share->getNote() === 'note' &&
  2843. $share->getLabel() === 'label' &&
  2844. $share->getHideDownload() === true;
  2845. })
  2846. )->willReturnArgument(0);
  2847. $this->rootFolder->method('getUserFolder')
  2848. ->with($this->currentUser)
  2849. ->willReturn($userFolder);
  2850. $userFolder->method('getFirstNodeById')
  2851. ->with(42)
  2852. ->willReturn($node);
  2853. $mountPoint = $this->createMock(IMountPoint::class);
  2854. $node->method('getMountPoint')
  2855. ->willReturn($mountPoint);
  2856. $mountPoint->method('getStorageRootId')
  2857. ->willReturn(42);
  2858. $expected = new DataResponse([]);
  2859. $result = $ocs->updateShare(42, null, null, null, null, '2010-12-23', null, null, null);
  2860. $this->assertInstanceOf(get_class($expected), $result);
  2861. $this->assertEquals($expected->getData(), $result->getData());
  2862. }
  2863. public function testUpdateLinkSharePublicUploadDoesNotChangeOther() {
  2864. $ocs = $this->mockFormatShare();
  2865. $date = new \DateTime('2000-01-01');
  2866. [$userFolder, $folder] = $this->getNonSharedUserFolder();
  2867. $folder->method('getId')
  2868. ->willReturn(42);
  2869. $share = \OC::$server->getShareManager()->newShare();
  2870. $share->setPermissions(\OCP\Constants::PERMISSION_ALL)
  2871. ->setSharedBy($this->currentUser)
  2872. ->setShareType(IShare::TYPE_LINK)
  2873. ->setPassword('password')
  2874. ->setSendPasswordByTalk(true)
  2875. ->setExpirationDate($date)
  2876. ->setNote('note')
  2877. ->setLabel('label')
  2878. ->setHideDownload(true)
  2879. ->setPermissions(\OCP\Constants::PERMISSION_ALL)
  2880. ->setNode($folder);
  2881. $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
  2882. $this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true);
  2883. $this->shareManager->expects($this->once())->method('updateShare')->with(
  2884. $this->callback(function (\OCP\Share\IShare $share) use ($date) {
  2885. return $share->getPermissions() === (\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE) &&
  2886. $share->getPassword() === 'password' &&
  2887. $share->getSendPasswordByTalk() === true &&
  2888. $share->getExpirationDate() === $date &&
  2889. $share->getNote() === 'note' &&
  2890. $share->getLabel() === 'label' &&
  2891. $share->getHideDownload() === true;
  2892. })
  2893. )->willReturnArgument(0);
  2894. $this->shareManager->method('getSharedWith')
  2895. ->willReturn([]);
  2896. $this->rootFolder->method('getUserFolder')
  2897. ->with($this->currentUser)
  2898. ->willReturn($userFolder);
  2899. $userFolder->method('getFirstNodeById')
  2900. ->with(42)
  2901. ->willReturn($folder);
  2902. $mountPoint = $this->createMock(IMountPoint::class);
  2903. $folder->method('getMountPoint')
  2904. ->willReturn($mountPoint);
  2905. $mountPoint->method('getStorageRootId')
  2906. ->willReturn(42);
  2907. $expected = new DataResponse([]);
  2908. $result = $ocs->updateShare(42, null, null, null, 'true', null, null, null, null);
  2909. $this->assertInstanceOf(get_class($expected), $result);
  2910. $this->assertEquals($expected->getData(), $result->getData());
  2911. }
  2912. public function testUpdateLinkSharePermissions() {
  2913. $ocs = $this->mockFormatShare();
  2914. $date = new \DateTime('2000-01-01');
  2915. [$userFolder, $folder] = $this->getNonSharedUserFolder();
  2916. $folder->method('getId')
  2917. ->willReturn(42);
  2918. $share = \OC::$server->getShareManager()->newShare();
  2919. $share->setPermissions(\OCP\Constants::PERMISSION_ALL)
  2920. ->setSharedBy($this->currentUser)
  2921. ->setShareType(IShare::TYPE_LINK)
  2922. ->setPassword('password')
  2923. ->setSendPasswordByTalk(true)
  2924. ->setExpirationDate($date)
  2925. ->setNote('note')
  2926. ->setLabel('label')
  2927. ->setHideDownload(true)
  2928. ->setPermissions(\OCP\Constants::PERMISSION_ALL)
  2929. ->setNode($folder);
  2930. $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
  2931. $this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true);
  2932. $this->shareManager->expects($this->once())->method('updateShare')->with(
  2933. $this->callback(function (\OCP\Share\IShare $share) use ($date): bool {
  2934. return $share->getPermissions() === (\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE) &&
  2935. $share->getPassword() === 'password' &&
  2936. $share->getSendPasswordByTalk() === true &&
  2937. $share->getExpirationDate() === $date &&
  2938. $share->getNote() === 'note' &&
  2939. $share->getLabel() === 'label' &&
  2940. $share->getHideDownload() === true;
  2941. })
  2942. )->willReturnArgument(0);
  2943. $this->shareManager->method('getSharedWith')->willReturn([]);
  2944. $this->rootFolder->method('getUserFolder')
  2945. ->with($this->currentUser)
  2946. ->willReturn($userFolder);
  2947. $userFolder->method('getFirstNodeById')
  2948. ->with(42)
  2949. ->willReturn($folder);
  2950. $mountPoint = $this->createMock(IMountPoint::class);
  2951. $folder->method('getMountPoint')
  2952. ->willReturn($mountPoint);
  2953. $mountPoint->method('getStorageRootId')
  2954. ->willReturn(42);
  2955. $expected = new DataResponse([]);
  2956. $result = $ocs->updateShare(42, 7, null, null, 'true', null, null, null, null);
  2957. $this->assertInstanceOf(get_class($expected), $result);
  2958. $this->assertEquals($expected->getData(), $result->getData());
  2959. }
  2960. public function testUpdateLinkSharePermissionsShare() {
  2961. $ocs = $this->mockFormatShare();
  2962. $date = new \DateTime('2000-01-01');
  2963. [$userFolder, $folder] = $this->getNonSharedUserFolder();
  2964. $folder->method('getId')
  2965. ->willReturn(42);
  2966. $share = \OC::$server->getShareManager()->newShare();
  2967. $share->setPermissions(\OCP\Constants::PERMISSION_ALL)
  2968. ->setSharedBy($this->currentUser)
  2969. ->setShareType(IShare::TYPE_LINK)
  2970. ->setPassword('password')
  2971. ->setSendPasswordByTalk(true)
  2972. ->setExpirationDate($date)
  2973. ->setNote('note')
  2974. ->setLabel('label')
  2975. ->setHideDownload(true)
  2976. ->setPermissions(\OCP\Constants::PERMISSION_READ)
  2977. ->setNode($folder);
  2978. $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
  2979. $this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true);
  2980. $this->shareManager->expects($this->once())->method('updateShare')->with(
  2981. $this->callback(function (\OCP\Share\IShare $share) use ($date) {
  2982. return $share->getPermissions() === (\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE) &&
  2983. $share->getPassword() === 'password' &&
  2984. $share->getSendPasswordByTalk() === true &&
  2985. $share->getExpirationDate() === $date &&
  2986. $share->getNote() === 'note' &&
  2987. $share->getLabel() === 'label' &&
  2988. $share->getHideDownload() === true;
  2989. })
  2990. )->willReturnArgument(0);
  2991. $this->rootFolder->method('getUserFolder')
  2992. ->with($this->currentUser)
  2993. ->willReturn($userFolder);
  2994. $userFolder->method('getFirstNodeById')
  2995. ->with(42)
  2996. ->willReturn($folder);
  2997. $mountPoint = $this->createMock(IMountPoint::class);
  2998. $folder->method('getMountPoint')
  2999. ->willReturn($mountPoint);
  3000. $mountPoint->method('getStorageRootId')
  3001. ->willReturn(42);
  3002. $this->shareManager->method('getSharedWith')->willReturn([]);
  3003. $expected = new DataResponse([]);
  3004. $result = $ocs->updateShare(42, 31, null, null, null, null, null, null, null);
  3005. $this->assertInstanceOf(get_class($expected), $result);
  3006. $this->assertEquals($expected->getData(), $result->getData());
  3007. }
  3008. public function testUpdateOtherPermissions() {
  3009. $ocs = $this->mockFormatShare();
  3010. [$userFolder, $file] = $this->getNonSharedUserFolder();
  3011. $file->method('getId')
  3012. ->willReturn(42);
  3013. $share = \OC::$server->getShareManager()->newShare();
  3014. $share->setPermissions(\OCP\Constants::PERMISSION_ALL)
  3015. ->setSharedBy($this->currentUser)
  3016. ->setShareType(IShare::TYPE_USER)
  3017. ->setNode($file);
  3018. $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
  3019. $this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true);
  3020. $this->shareManager->expects($this->once())->method('updateShare')->with(
  3021. $this->callback(function (\OCP\Share\IShare $share) {
  3022. return $share->getPermissions() === \OCP\Constants::PERMISSION_ALL;
  3023. })
  3024. )->willReturnArgument(0);
  3025. $this->shareManager->method('getSharedWith')->willReturn([]);
  3026. [$userFolder, $folder] = $this->getNonSharedUserFolder();
  3027. $this->rootFolder->method('getUserFolder')
  3028. ->with($this->currentUser)
  3029. ->willReturn($userFolder);
  3030. $userFolder->method('getFirstNodeById')
  3031. ->with(42)
  3032. ->willReturn($file);
  3033. $mountPoint = $this->createMock(IMountPoint::class);
  3034. $file->method('getMountPoint')
  3035. ->willReturn($mountPoint);
  3036. $mountPoint->method('getStorageRootId')
  3037. ->willReturn(42);
  3038. $expected = new DataResponse([]);
  3039. $result = $ocs->updateShare(42, 31, null, null, null, null);
  3040. $this->assertInstanceOf(get_class($expected), $result);
  3041. $this->assertEquals($expected->getData(), $result->getData());
  3042. }
  3043. public function testUpdateShareCannotIncreasePermissions() {
  3044. $ocs = $this->mockFormatShare();
  3045. [$userFolder, $folder] = $this->getNonSharedUserFolder();
  3046. $folder->method('getId')
  3047. ->willReturn(42);
  3048. $share = \OC::$server->getShareManager()->newShare();
  3049. $share
  3050. ->setId(42)
  3051. ->setSharedBy($this->currentUser)
  3052. ->setShareOwner('anotheruser')
  3053. ->setShareType(IShare::TYPE_GROUP)
  3054. ->setSharedWith('group1')
  3055. ->setPermissions(\OCP\Constants::PERMISSION_READ)
  3056. ->setNode($folder);
  3057. // note: updateShare will modify the received instance but getSharedWith will reread from the database,
  3058. // so their values will be different
  3059. $incomingShare = \OC::$server->getShareManager()->newShare();
  3060. $incomingShare
  3061. ->setId(42)
  3062. ->setSharedBy($this->currentUser)
  3063. ->setShareOwner('anotheruser')
  3064. ->setShareType(IShare::TYPE_GROUP)
  3065. ->setSharedWith('group1')
  3066. ->setPermissions(\OCP\Constants::PERMISSION_READ)
  3067. ->setNode($folder);
  3068. $this->request
  3069. ->method('getParam')
  3070. ->willReturnMap([
  3071. ['permissions', null, '31'],
  3072. ]);
  3073. $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
  3074. $this->shareManager->expects($this->any())
  3075. ->method('getSharedWith')
  3076. ->willReturnMap([
  3077. ['currentUser', IShare::TYPE_USER, $share->getNode(), -1, 0, []],
  3078. ['currentUser', IShare::TYPE_GROUP, $share->getNode(), -1, 0, [$incomingShare]],
  3079. ['currentUser', IShare::TYPE_ROOM, $share->getNode(), -1, 0, []]
  3080. ]);
  3081. $this->rootFolder->method('getUserFolder')
  3082. ->with($this->currentUser)
  3083. ->willReturn($userFolder);
  3084. $userFolder->method('getFirstNodeById')
  3085. ->with(42)
  3086. ->willReturn($folder);
  3087. $mountPoint = $this->createMock(IMountPoint::class);
  3088. $folder->method('getMountPoint')
  3089. ->willReturn($mountPoint);
  3090. $mountPoint->method('getStorageRootId')
  3091. ->willReturn(42);
  3092. $this->shareManager->expects($this->once())
  3093. ->method('updateShare')
  3094. ->with($share)
  3095. ->willThrowException(new GenericShareException('Cannot increase permissions of path/file', 'Cannot increase permissions of path/file', 404));
  3096. try {
  3097. $ocs->updateShare(42, 31);
  3098. $this->fail();
  3099. } catch (OCSException $e) {
  3100. $this->assertEquals('Cannot increase permissions of path/file', $e->getMessage());
  3101. }
  3102. }
  3103. public function testUpdateShareCanIncreasePermissionsIfOwner() {
  3104. $ocs = $this->mockFormatShare();
  3105. [$userFolder, $folder] = $this->getNonSharedUserFolder();
  3106. $folder->method('getId')
  3107. ->willReturn(42);
  3108. $share = \OC::$server->getShareManager()->newShare();
  3109. $share
  3110. ->setId(42)
  3111. ->setSharedBy($this->currentUser)
  3112. ->setShareOwner($this->currentUser)
  3113. ->setShareType(IShare::TYPE_GROUP)
  3114. ->setSharedWith('group1')
  3115. ->setPermissions(\OCP\Constants::PERMISSION_READ)
  3116. ->setNode($folder);
  3117. // note: updateShare will modify the received instance but getSharedWith will reread from the database,
  3118. // so their values will be different
  3119. $incomingShare = \OC::$server->getShareManager()->newShare();
  3120. $incomingShare
  3121. ->setId(42)
  3122. ->setSharedBy($this->currentUser)
  3123. ->setShareOwner($this->currentUser)
  3124. ->setShareType(IShare::TYPE_GROUP)
  3125. ->setSharedWith('group1')
  3126. ->setPermissions(\OCP\Constants::PERMISSION_READ)
  3127. ->setNode($folder);
  3128. $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
  3129. $this->shareManager->expects($this->any())
  3130. ->method('getSharedWith')
  3131. ->willReturnMap([
  3132. ['currentUser', IShare::TYPE_USER, $share->getNode(), -1, 0, []],
  3133. ['currentUser', IShare::TYPE_GROUP, $share->getNode(), -1, 0, [$incomingShare]]
  3134. ]);
  3135. $this->shareManager->expects($this->once())
  3136. ->method('updateShare')
  3137. ->with($share)
  3138. ->willReturn($share);
  3139. $this->rootFolder->method('getUserFolder')
  3140. ->with($this->currentUser)
  3141. ->willReturn($userFolder);
  3142. $userFolder->method('getFirstNodeById')
  3143. ->with(42)
  3144. ->willReturn($folder);
  3145. $mountPoint = $this->createMock(IMountPoint::class);
  3146. $folder->method('getMountPoint')
  3147. ->willReturn($mountPoint);
  3148. $mountPoint->method('getStorageRootId')
  3149. ->willReturn(42);
  3150. $result = $ocs->updateShare(42, 31);
  3151. $this->assertInstanceOf(DataResponse::class, $result);
  3152. }
  3153. public function dataFormatShare() {
  3154. $file = $this->getMockBuilder(File::class)->getMock();
  3155. $folder = $this->getMockBuilder(Folder::class)->getMock();
  3156. $parent = $this->getMockBuilder(Folder::class)->getMock();
  3157. $fileWithPreview = $this->getMockBuilder(File::class)->getMock();
  3158. $file->method('getMimeType')->willReturn('myMimeType');
  3159. $folder->method('getMimeType')->willReturn('myFolderMimeType');
  3160. $fileWithPreview->method('getMimeType')->willReturn('mimeWithPreview');
  3161. $file->method('getPath')->willReturn('file');
  3162. $folder->method('getPath')->willReturn('folder');
  3163. $fileWithPreview->method('getPath')->willReturn('fileWithPreview');
  3164. $parent->method('getId')->willReturn(1);
  3165. $folder->method('getId')->willReturn(2);
  3166. $file->method('getId')->willReturn(3);
  3167. $fileWithPreview->method('getId')->willReturn(4);
  3168. $file->method('getParent')->willReturn($parent);
  3169. $folder->method('getParent')->willReturn($parent);
  3170. $fileWithPreview->method('getParent')->willReturn($parent);
  3171. $file->method('getSize')->willReturn(123456);
  3172. $folder->method('getSize')->willReturn(123456);
  3173. $fileWithPreview->method('getSize')->willReturn(123456);
  3174. $file->method('getMTime')->willReturn(1234567890);
  3175. $folder->method('getMTime')->willReturn(1234567890);
  3176. $fileWithPreview->method('getMTime')->willReturn(1234567890);
  3177. $cache = $this->getMockBuilder('OCP\Files\Cache\ICache')->getMock();
  3178. $cache->method('getNumericStorageId')->willReturn(100);
  3179. $storage = $this->createMock(Storage::class);
  3180. $storage->method('getId')->willReturn('storageId');
  3181. $storage->method('getCache')->willReturn($cache);
  3182. $file->method('getStorage')->willReturn($storage);
  3183. $folder->method('getStorage')->willReturn($storage);
  3184. $fileWithPreview->method('getStorage')->willReturn($storage);
  3185. $owner = $this->getMockBuilder(IUser::class)->getMock();
  3186. $owner->method('getDisplayName')->willReturn('ownerDN');
  3187. $initiator = $this->getMockBuilder(IUser::class)->getMock();
  3188. $initiator->method('getDisplayName')->willReturn('initiatorDN');
  3189. $recipient = $this->getMockBuilder(IUser::class)->getMock();
  3190. $recipient->method('getDisplayName')->willReturn('recipientDN');
  3191. $recipient->method('getSystemEMailAddress')->willReturn('recipient');
  3192. [$shareAttributes, $shareAttributesReturnJson] = $this->mockShareAttributes();
  3193. $result = [];
  3194. $share = \OC::$server->getShareManager()->newShare();
  3195. $share->setShareType(IShare::TYPE_USER)
  3196. ->setSharedWith('recipient')
  3197. ->setSharedBy('initiator')
  3198. ->setShareOwner('owner')
  3199. ->setPermissions(\OCP\Constants::PERMISSION_READ)
  3200. ->setAttributes($shareAttributes)
  3201. ->setNode($file)
  3202. ->setShareTime(new \DateTime('2000-01-01T00:01:02'))
  3203. ->setTarget('myTarget')
  3204. ->setNote('personal note')
  3205. ->setId(42);
  3206. // User backend down
  3207. $result[] = [
  3208. [
  3209. 'id' => '42',
  3210. 'share_type' => IShare::TYPE_USER,
  3211. 'uid_owner' => 'initiator',
  3212. 'displayname_owner' => 'initiator',
  3213. 'permissions' => 1,
  3214. 'attributes' => $shareAttributesReturnJson,
  3215. 'stime' => 946684862,
  3216. 'parent' => null,
  3217. 'expiration' => null,
  3218. 'token' => null,
  3219. 'uid_file_owner' => 'owner',
  3220. 'displayname_file_owner' => 'owner',
  3221. 'path' => 'file',
  3222. 'item_type' => 'file',
  3223. 'storage_id' => 'storageId',
  3224. 'storage' => 100,
  3225. 'item_source' => 3,
  3226. 'file_source' => 3,
  3227. 'file_parent' => 1,
  3228. 'file_target' => 'myTarget',
  3229. 'share_with' => 'recipient',
  3230. 'share_with_displayname' => 'recipient',
  3231. 'share_with_displayname_unique' => 'recipient',
  3232. 'note' => 'personal note',
  3233. 'label' => '',
  3234. 'mail_send' => 0,
  3235. 'mimetype' => 'myMimeType',
  3236. 'has_preview' => false,
  3237. 'hide_download' => 0,
  3238. 'can_edit' => false,
  3239. 'can_delete' => false,
  3240. 'item_size' => 123456,
  3241. 'item_mtime' => 1234567890,
  3242. 'attributes' => '[{"scope":"permissions","key":"download","enabled":true}]',
  3243. ], $share, [], false
  3244. ];
  3245. // User backend up
  3246. $result[] = [
  3247. [
  3248. 'id' => '42',
  3249. 'share_type' => IShare::TYPE_USER,
  3250. 'uid_owner' => 'initiator',
  3251. 'displayname_owner' => 'initiatorDN',
  3252. 'permissions' => 1,
  3253. 'attributes' => $shareAttributesReturnJson,
  3254. 'stime' => 946684862,
  3255. 'parent' => null,
  3256. 'expiration' => null,
  3257. 'token' => null,
  3258. 'uid_file_owner' => 'owner',
  3259. 'displayname_file_owner' => 'ownerDN',
  3260. 'note' => 'personal note',
  3261. 'label' => '',
  3262. 'path' => 'file',
  3263. 'item_type' => 'file',
  3264. 'storage_id' => 'storageId',
  3265. 'storage' => 100,
  3266. 'item_source' => 3,
  3267. 'file_source' => 3,
  3268. 'file_parent' => 1,
  3269. 'file_target' => 'myTarget',
  3270. 'share_with' => 'recipient',
  3271. 'share_with_displayname' => 'recipientDN',
  3272. 'share_with_displayname_unique' => 'recipient',
  3273. 'mail_send' => 0,
  3274. 'mimetype' => 'myMimeType',
  3275. 'has_preview' => false,
  3276. 'hide_download' => 0,
  3277. 'can_edit' => false,
  3278. 'can_delete' => false,
  3279. 'item_size' => 123456,
  3280. 'item_mtime' => 1234567890,
  3281. 'attributes' => '[{"scope":"permissions","key":"download","enabled":true}]',
  3282. ], $share, [
  3283. ['owner', $owner],
  3284. ['initiator', $initiator],
  3285. ['recipient', $recipient],
  3286. ], false
  3287. ];
  3288. $share = \OC::$server->getShareManager()->newShare();
  3289. $share->setShareType(IShare::TYPE_USER)
  3290. ->setSharedWith('recipient')
  3291. ->setSharedBy('initiator')
  3292. ->setShareOwner('owner')
  3293. ->setPermissions(\OCP\Constants::PERMISSION_READ)
  3294. ->setNode($file)
  3295. ->setShareTime(new \DateTime('2000-01-01T00:01:02'))
  3296. ->setTarget('myTarget')
  3297. ->setNote('personal note')
  3298. ->setId(42);
  3299. // User backend down
  3300. $result[] = [
  3301. [
  3302. 'id' => '42',
  3303. 'share_type' => IShare::TYPE_USER,
  3304. 'uid_owner' => 'initiator',
  3305. 'displayname_owner' => 'initiator',
  3306. 'permissions' => 1,
  3307. 'attributes' => null,
  3308. 'stime' => 946684862,
  3309. 'parent' => null,
  3310. 'expiration' => null,
  3311. 'token' => null,
  3312. 'uid_file_owner' => 'owner',
  3313. 'displayname_file_owner' => 'owner',
  3314. 'note' => 'personal note',
  3315. 'label' => '',
  3316. 'path' => 'file',
  3317. 'item_type' => 'file',
  3318. 'storage_id' => 'storageId',
  3319. 'storage' => 100,
  3320. 'item_source' => 3,
  3321. 'file_source' => 3,
  3322. 'file_parent' => 1,
  3323. 'file_target' => 'myTarget',
  3324. 'share_with' => 'recipient',
  3325. 'share_with_displayname' => 'recipient',
  3326. 'share_with_displayname_unique' => 'recipient',
  3327. 'mail_send' => 0,
  3328. 'mimetype' => 'myMimeType',
  3329. 'has_preview' => false,
  3330. 'hide_download' => 0,
  3331. 'can_edit' => false,
  3332. 'can_delete' => false,
  3333. 'item_size' => 123456,
  3334. 'item_mtime' => 1234567890,
  3335. 'attributes' => null,
  3336. ], $share, [], false
  3337. ];
  3338. $share = \OC::$server->getShareManager()->newShare();
  3339. $share->setShareType(IShare::TYPE_USER)
  3340. ->setSharedWith('recipient')
  3341. ->setSharedBy('initiator')
  3342. ->setShareOwner('currentUser')
  3343. ->setPermissions(\OCP\Constants::PERMISSION_READ)
  3344. ->setNode($file)
  3345. ->setShareTime(new \DateTime('2000-01-01T00:01:02'))
  3346. ->setTarget('myTarget')
  3347. ->setNote('personal note')
  3348. ->setId(42);
  3349. // User backend down
  3350. $result[] = [
  3351. [
  3352. 'id' => '42',
  3353. 'share_type' => IShare::TYPE_USER,
  3354. 'uid_owner' => 'initiator',
  3355. 'displayname_owner' => 'initiator',
  3356. 'permissions' => 1,
  3357. 'attributes' => null,
  3358. 'stime' => 946684862,
  3359. 'parent' => null,
  3360. 'expiration' => null,
  3361. 'token' => null,
  3362. 'uid_file_owner' => 'currentUser',
  3363. 'displayname_file_owner' => 'currentUser',
  3364. 'note' => 'personal note',
  3365. 'label' => '',
  3366. 'path' => 'file',
  3367. 'item_type' => 'file',
  3368. 'storage_id' => 'storageId',
  3369. 'storage' => 100,
  3370. 'item_source' => 3,
  3371. 'file_source' => 3,
  3372. 'file_parent' => 1,
  3373. 'file_target' => 'myTarget',
  3374. 'share_with' => 'recipient',
  3375. 'share_with_displayname' => 'recipient',
  3376. 'share_with_displayname_unique' => 'recipient',
  3377. 'mail_send' => 0,
  3378. 'mimetype' => 'myMimeType',
  3379. 'has_preview' => false,
  3380. 'hide_download' => 0,
  3381. 'can_edit' => true,
  3382. 'can_delete' => true,
  3383. 'item_size' => 123456,
  3384. 'item_mtime' => 1234567890,
  3385. 'attributes' => null,
  3386. ], $share, [], false
  3387. ];
  3388. // with existing group
  3389. $share = \OC::$server->getShareManager()->newShare();
  3390. $share->setShareType(IShare::TYPE_GROUP)
  3391. ->setSharedWith('recipientGroup')
  3392. ->setSharedBy('initiator')
  3393. ->setShareOwner('owner')
  3394. ->setPermissions(\OCP\Constants::PERMISSION_READ)
  3395. ->setNode($file)
  3396. ->setShareTime(new \DateTime('2000-01-01T00:01:02'))
  3397. ->setTarget('myTarget')
  3398. ->setNote('personal note')
  3399. ->setId(42);
  3400. $result[] = [
  3401. [
  3402. 'id' => '42',
  3403. 'share_type' => IShare::TYPE_GROUP,
  3404. 'uid_owner' => 'initiator',
  3405. 'displayname_owner' => 'initiator',
  3406. 'permissions' => 1,
  3407. 'attributes' => null,
  3408. 'stime' => 946684862,
  3409. 'parent' => null,
  3410. 'expiration' => null,
  3411. 'token' => null,
  3412. 'uid_file_owner' => 'owner',
  3413. 'displayname_file_owner' => 'owner',
  3414. 'note' => 'personal note',
  3415. 'label' => '',
  3416. 'path' => 'file',
  3417. 'item_type' => 'file',
  3418. 'storage_id' => 'storageId',
  3419. 'storage' => 100,
  3420. 'item_source' => 3,
  3421. 'file_source' => 3,
  3422. 'file_parent' => 1,
  3423. 'file_target' => 'myTarget',
  3424. 'share_with' => 'recipientGroup',
  3425. 'share_with_displayname' => 'recipientGroupDisplayName',
  3426. 'mail_send' => 0,
  3427. 'mimetype' => 'myMimeType',
  3428. 'has_preview' => false,
  3429. 'hide_download' => 0,
  3430. 'can_edit' => false,
  3431. 'can_delete' => false,
  3432. 'item_size' => 123456,
  3433. 'item_mtime' => 1234567890,
  3434. 'attributes' => null,
  3435. ], $share, [], false
  3436. ];
  3437. // with unknown group / no group backend
  3438. $share = \OC::$server->getShareManager()->newShare();
  3439. $share->setShareType(IShare::TYPE_GROUP)
  3440. ->setSharedWith('recipientGroup2')
  3441. ->setSharedBy('initiator')
  3442. ->setShareOwner('owner')
  3443. ->setPermissions(\OCP\Constants::PERMISSION_READ)
  3444. ->setNode($file)
  3445. ->setShareTime(new \DateTime('2000-01-01T00:01:02'))
  3446. ->setTarget('myTarget')
  3447. ->setNote('personal note')
  3448. ->setId(42);
  3449. $result[] = [
  3450. [
  3451. 'id' => '42',
  3452. 'share_type' => IShare::TYPE_GROUP,
  3453. 'uid_owner' => 'initiator',
  3454. 'displayname_owner' => 'initiator',
  3455. 'permissions' => 1,
  3456. 'stime' => 946684862,
  3457. 'parent' => null,
  3458. 'expiration' => null,
  3459. 'token' => null,
  3460. 'uid_file_owner' => 'owner',
  3461. 'displayname_file_owner' => 'owner',
  3462. 'note' => 'personal note',
  3463. 'label' => '',
  3464. 'path' => 'file',
  3465. 'item_type' => 'file',
  3466. 'storage_id' => 'storageId',
  3467. 'storage' => 100,
  3468. 'item_source' => 3,
  3469. 'file_source' => 3,
  3470. 'file_parent' => 1,
  3471. 'file_target' => 'myTarget',
  3472. 'share_with' => 'recipientGroup2',
  3473. 'share_with_displayname' => 'recipientGroup2',
  3474. 'mail_send' => 0,
  3475. 'mimetype' => 'myMimeType',
  3476. 'has_preview' => false,
  3477. 'hide_download' => 0,
  3478. 'can_edit' => false,
  3479. 'can_delete' => false,
  3480. 'item_size' => 123456,
  3481. 'item_mtime' => 1234567890,
  3482. 'attributes' => null,
  3483. ], $share, [], false
  3484. ];
  3485. $share = \OC::$server->getShareManager()->newShare();
  3486. $share->setShareType(IShare::TYPE_LINK)
  3487. ->setSharedBy('initiator')
  3488. ->setShareOwner('owner')
  3489. ->setPermissions(\OCP\Constants::PERMISSION_READ)
  3490. ->setNode($file)
  3491. ->setShareTime(new \DateTime('2000-01-01T00:01:02'))
  3492. ->setTarget('myTarget')
  3493. ->setPassword('mypassword')
  3494. ->setExpirationDate(new \DateTime('2001-01-02T00:00:00'))
  3495. ->setToken('myToken')
  3496. ->setNote('personal note')
  3497. ->setLabel('new link share')
  3498. ->setId(42);
  3499. $result[] = [
  3500. [
  3501. 'id' => '42',
  3502. 'share_type' => IShare::TYPE_LINK,
  3503. 'uid_owner' => 'initiator',
  3504. 'displayname_owner' => 'initiator',
  3505. 'permissions' => 1,
  3506. 'attributes' => null,
  3507. 'stime' => 946684862,
  3508. 'parent' => null,
  3509. 'expiration' => '2001-01-02 00:00:00',
  3510. 'token' => 'myToken',
  3511. 'uid_file_owner' => 'owner',
  3512. 'displayname_file_owner' => 'owner',
  3513. 'note' => 'personal note',
  3514. 'label' => 'new link share',
  3515. 'path' => 'file',
  3516. 'item_type' => 'file',
  3517. 'storage_id' => 'storageId',
  3518. 'storage' => 100,
  3519. 'item_source' => 3,
  3520. 'file_source' => 3,
  3521. 'file_parent' => 1,
  3522. 'file_target' => 'myTarget',
  3523. 'password' => 'mypassword',
  3524. 'share_with' => 'mypassword',
  3525. 'share_with_displayname' => '(Shared link)',
  3526. 'send_password_by_talk' => false,
  3527. 'mail_send' => 0,
  3528. 'url' => 'myLink',
  3529. 'mimetype' => 'myMimeType',
  3530. 'has_preview' => false,
  3531. 'hide_download' => 0,
  3532. 'can_edit' => false,
  3533. 'can_delete' => false,
  3534. 'item_size' => 123456,
  3535. 'item_mtime' => 1234567890,
  3536. 'attributes' => null,
  3537. ], $share, [], false
  3538. ];
  3539. $share = \OC::$server->getShareManager()->newShare();
  3540. $share->setShareType(IShare::TYPE_LINK)
  3541. ->setSharedBy('initiator')
  3542. ->setShareOwner('owner')
  3543. ->setPermissions(\OCP\Constants::PERMISSION_READ)
  3544. ->setNode($file)
  3545. ->setShareTime(new \DateTime('2000-01-01T00:01:02'))
  3546. ->setTarget('myTarget')
  3547. ->setPassword('mypassword')
  3548. ->setSendPasswordByTalk(true)
  3549. ->setExpirationDate(new \DateTime('2001-01-02T00:00:00'))
  3550. ->setToken('myToken')
  3551. ->setNote('personal note')
  3552. ->setLabel('new link share')
  3553. ->setId(42);
  3554. $result[] = [
  3555. [
  3556. 'id' => '42',
  3557. 'share_type' => IShare::TYPE_LINK,
  3558. 'uid_owner' => 'initiator',
  3559. 'displayname_owner' => 'initiator',
  3560. 'permissions' => 1,
  3561. 'stime' => 946684862,
  3562. 'parent' => null,
  3563. 'expiration' => '2001-01-02 00:00:00',
  3564. 'token' => 'myToken',
  3565. 'uid_file_owner' => 'owner',
  3566. 'displayname_file_owner' => 'owner',
  3567. 'note' => 'personal note',
  3568. 'label' => 'new link share',
  3569. 'path' => 'file',
  3570. 'item_type' => 'file',
  3571. 'storage_id' => 'storageId',
  3572. 'storage' => 100,
  3573. 'item_source' => 3,
  3574. 'file_source' => 3,
  3575. 'file_parent' => 1,
  3576. 'file_target' => 'myTarget',
  3577. 'password' => 'mypassword',
  3578. 'share_with' => 'mypassword',
  3579. 'share_with_displayname' => '(Shared link)',
  3580. 'send_password_by_talk' => true,
  3581. 'mail_send' => 0,
  3582. 'url' => 'myLink',
  3583. 'mimetype' => 'myMimeType',
  3584. 'has_preview' => false,
  3585. 'hide_download' => 0,
  3586. 'can_edit' => false,
  3587. 'can_delete' => false,
  3588. 'item_size' => 123456,
  3589. 'item_mtime' => 1234567890,
  3590. 'attributes' => null,
  3591. ], $share, [], false
  3592. ];
  3593. $share = \OC::$server->getShareManager()->newShare();
  3594. $share->setShareType(IShare::TYPE_REMOTE)
  3595. ->setSharedBy('initiator')
  3596. ->setSharedWith('user@server.com')
  3597. ->setShareOwner('owner')
  3598. ->setPermissions(\OCP\Constants::PERMISSION_READ)
  3599. ->setNode($folder)
  3600. ->setShareTime(new \DateTime('2000-01-01T00:01:02'))
  3601. ->setExpirationDate(new \DateTime('2001-02-03T04:05:06'))
  3602. ->setTarget('myTarget')
  3603. ->setNote('personal note')
  3604. ->setId(42);
  3605. $result[] = [
  3606. [
  3607. 'id' => '42',
  3608. 'share_type' => IShare::TYPE_REMOTE,
  3609. 'uid_owner' => 'initiator',
  3610. 'displayname_owner' => 'initiator',
  3611. 'permissions' => 1,
  3612. 'stime' => 946684862,
  3613. 'parent' => null,
  3614. 'expiration' => '2001-02-03 00:00:00',
  3615. 'token' => null,
  3616. 'uid_file_owner' => 'owner',
  3617. 'displayname_file_owner' => 'owner',
  3618. 'note' => 'personal note',
  3619. 'label' => '',
  3620. 'path' => 'folder',
  3621. 'item_type' => 'folder',
  3622. 'storage_id' => 'storageId',
  3623. 'storage' => 100,
  3624. 'item_source' => 2,
  3625. 'file_source' => 2,
  3626. 'file_parent' => 1,
  3627. 'file_target' => 'myTarget',
  3628. 'share_with' => 'user@server.com',
  3629. 'share_with_displayname' => 'foobar',
  3630. 'mail_send' => 0,
  3631. 'mimetype' => 'myFolderMimeType',
  3632. 'has_preview' => false,
  3633. 'hide_download' => 0,
  3634. 'can_edit' => false,
  3635. 'can_delete' => false,
  3636. 'item_size' => 123456,
  3637. 'item_mtime' => 1234567890,
  3638. 'attributes' => null,
  3639. ], $share, [], false
  3640. ];
  3641. $share = \OC::$server->getShareManager()->newShare();
  3642. $share->setShareType(IShare::TYPE_REMOTE_GROUP)
  3643. ->setSharedBy('initiator')
  3644. ->setSharedWith('user@server.com')
  3645. ->setShareOwner('owner')
  3646. ->setPermissions(\OCP\Constants::PERMISSION_READ)
  3647. ->setNode($folder)
  3648. ->setShareTime(new \DateTime('2000-01-01T00:01:02'))
  3649. ->setExpirationDate(new \DateTime('2001-02-03T04:05:06'))
  3650. ->setTarget('myTarget')
  3651. ->setNote('personal note')
  3652. ->setId(42);
  3653. $result[] = [
  3654. [
  3655. 'id' => '42',
  3656. 'share_type' => IShare::TYPE_REMOTE_GROUP,
  3657. 'uid_owner' => 'initiator',
  3658. 'displayname_owner' => 'initiator',
  3659. 'permissions' => 1,
  3660. 'stime' => 946684862,
  3661. 'parent' => null,
  3662. 'expiration' => '2001-02-03 00:00:00',
  3663. 'token' => null,
  3664. 'uid_file_owner' => 'owner',
  3665. 'displayname_file_owner' => 'owner',
  3666. 'note' => 'personal note',
  3667. 'label' => '',
  3668. 'path' => 'folder',
  3669. 'item_type' => 'folder',
  3670. 'storage_id' => 'storageId',
  3671. 'storage' => 100,
  3672. 'item_source' => 2,
  3673. 'file_source' => 2,
  3674. 'file_parent' => 1,
  3675. 'file_target' => 'myTarget',
  3676. 'share_with' => 'user@server.com',
  3677. 'share_with_displayname' => 'foobar',
  3678. 'mail_send' => 0,
  3679. 'mimetype' => 'myFolderMimeType',
  3680. 'has_preview' => false,
  3681. 'hide_download' => 0,
  3682. 'can_edit' => false,
  3683. 'can_delete' => false,
  3684. 'item_size' => 123456,
  3685. 'item_mtime' => 1234567890,
  3686. 'attributes' => null,
  3687. ], $share, [], false
  3688. ];
  3689. // Circle with id, display name and avatar set by the Circles app
  3690. $share = \OC::$server->getShareManager()->newShare();
  3691. $share->setShareType(IShare::TYPE_CIRCLE)
  3692. ->setSharedBy('initiator')
  3693. ->setSharedWith('Circle (Public circle, circleOwner) [4815162342]')
  3694. ->setSharedWithDisplayName('The display name')
  3695. ->setSharedWithAvatar('path/to/the/avatar')
  3696. ->setShareOwner('owner')
  3697. ->setPermissions(\OCP\Constants::PERMISSION_READ)
  3698. ->setNode($folder)
  3699. ->setShareTime(new \DateTime('2000-01-01T00:01:02'))
  3700. ->setTarget('myTarget')
  3701. ->setId(42);
  3702. $result[] = [
  3703. [
  3704. 'id' => '42',
  3705. 'share_type' => IShare::TYPE_CIRCLE,
  3706. 'uid_owner' => 'initiator',
  3707. 'displayname_owner' => 'initiator',
  3708. 'permissions' => 1,
  3709. 'attributes' => null,
  3710. 'stime' => 946684862,
  3711. 'parent' => null,
  3712. 'expiration' => null,
  3713. 'token' => null,
  3714. 'uid_file_owner' => 'owner',
  3715. 'displayname_file_owner' => 'owner',
  3716. 'note' => '',
  3717. 'label' => '',
  3718. 'path' => 'folder',
  3719. 'item_type' => 'folder',
  3720. 'storage_id' => 'storageId',
  3721. 'storage' => 100,
  3722. 'item_source' => 2,
  3723. 'file_source' => 2,
  3724. 'file_parent' => 1,
  3725. 'file_target' => 'myTarget',
  3726. 'share_with' => '4815162342',
  3727. 'share_with_displayname' => 'The display name',
  3728. 'share_with_avatar' => 'path/to/the/avatar',
  3729. 'mail_send' => 0,
  3730. 'mimetype' => 'myFolderMimeType',
  3731. 'has_preview' => false,
  3732. 'hide_download' => 0,
  3733. 'can_edit' => false,
  3734. 'can_delete' => false,
  3735. 'item_size' => 123456,
  3736. 'item_mtime' => 1234567890,
  3737. 'attributes' => null,
  3738. ], $share, [], false
  3739. ];
  3740. // Circle with id set by the Circles app
  3741. $share = \OC::$server->getShareManager()->newShare();
  3742. $share->setShareType(IShare::TYPE_CIRCLE)
  3743. ->setSharedBy('initiator')
  3744. ->setSharedWith('Circle (Public circle, circleOwner) [4815162342]')
  3745. ->setShareOwner('owner')
  3746. ->setPermissions(\OCP\Constants::PERMISSION_READ)
  3747. ->setNode($folder)
  3748. ->setShareTime(new \DateTime('2000-01-01T00:01:02'))
  3749. ->setTarget('myTarget')
  3750. ->setId(42);
  3751. $result[] = [
  3752. [
  3753. 'id' => '42',
  3754. 'share_type' => IShare::TYPE_CIRCLE,
  3755. 'uid_owner' => 'initiator',
  3756. 'displayname_owner' => 'initiator',
  3757. 'permissions' => 1,
  3758. 'stime' => 946684862,
  3759. 'parent' => null,
  3760. 'expiration' => null,
  3761. 'token' => null,
  3762. 'uid_file_owner' => 'owner',
  3763. 'displayname_file_owner' => 'owner',
  3764. 'note' => '',
  3765. 'label' => '',
  3766. 'path' => 'folder',
  3767. 'item_type' => 'folder',
  3768. 'storage_id' => 'storageId',
  3769. 'storage' => 100,
  3770. 'item_source' => 2,
  3771. 'file_source' => 2,
  3772. 'file_parent' => 1,
  3773. 'file_target' => 'myTarget',
  3774. 'share_with' => '4815162342',
  3775. 'share_with_displayname' => 'Circle (Public circle, circleOwner)',
  3776. 'share_with_avatar' => '',
  3777. 'mail_send' => 0,
  3778. 'mimetype' => 'myFolderMimeType',
  3779. 'has_preview' => false,
  3780. 'hide_download' => 0,
  3781. 'can_edit' => false,
  3782. 'can_delete' => false,
  3783. 'item_size' => 123456,
  3784. 'item_mtime' => 1234567890,
  3785. 'attributes' => null,
  3786. ], $share, [], false
  3787. ];
  3788. // Circle with id not set by the Circles app
  3789. $share = \OC::$server->getShareManager()->newShare();
  3790. $share->setShareType(IShare::TYPE_CIRCLE)
  3791. ->setSharedBy('initiator')
  3792. ->setSharedWith('Circle (Public circle, circleOwner)')
  3793. ->setShareOwner('owner')
  3794. ->setPermissions(\OCP\Constants::PERMISSION_READ)
  3795. ->setNode($folder)
  3796. ->setShareTime(new \DateTime('2000-01-01T00:01:02'))
  3797. ->setTarget('myTarget')
  3798. ->setId(42);
  3799. $result[] = [
  3800. [
  3801. 'id' => '42',
  3802. 'share_type' => IShare::TYPE_CIRCLE,
  3803. 'uid_owner' => 'initiator',
  3804. 'displayname_owner' => 'initiator',
  3805. 'permissions' => 1,
  3806. 'stime' => 946684862,
  3807. 'parent' => null,
  3808. 'expiration' => null,
  3809. 'token' => null,
  3810. 'uid_file_owner' => 'owner',
  3811. 'displayname_file_owner' => 'owner',
  3812. 'note' => '',
  3813. 'label' => '',
  3814. 'path' => 'folder',
  3815. 'item_type' => 'folder',
  3816. 'storage_id' => 'storageId',
  3817. 'storage' => 100,
  3818. 'item_source' => 2,
  3819. 'file_source' => 2,
  3820. 'file_parent' => 1,
  3821. 'file_target' => 'myTarget',
  3822. 'share_with' => 'Circle',
  3823. 'share_with_displayname' => 'Circle (Public circle, circleOwner)',
  3824. 'share_with_avatar' => '',
  3825. 'mail_send' => 0,
  3826. 'mimetype' => 'myFolderMimeType',
  3827. 'has_preview' => false,
  3828. 'hide_download' => 0,
  3829. 'can_edit' => false,
  3830. 'can_delete' => false,
  3831. 'item_size' => 123456,
  3832. 'item_mtime' => 1234567890,
  3833. 'attributes' => null,
  3834. ], $share, [], false
  3835. ];
  3836. $share = \OC::$server->getShareManager()->newShare();
  3837. $share->setShareType(IShare::TYPE_USER)
  3838. ->setSharedBy('initiator')
  3839. ->setSharedWith('recipient')
  3840. ->setShareOwner('owner')
  3841. ->setPermissions(\OCP\Constants::PERMISSION_READ)
  3842. ->setShareTime(new \DateTime('2000-01-01T00:01:02'))
  3843. ->setTarget('myTarget')
  3844. ->setNote('personal note')
  3845. ->setId(42);
  3846. $result[] = [
  3847. [], $share, [], true
  3848. ];
  3849. $share = \OC::$server->getShareManager()->newShare();
  3850. $share->setShareType(IShare::TYPE_EMAIL)
  3851. ->setSharedBy('initiator')
  3852. ->setSharedWith('user@server.com')
  3853. ->setShareOwner('owner')
  3854. ->setPermissions(\OCP\Constants::PERMISSION_READ)
  3855. ->setNode($folder)
  3856. ->setShareTime(new \DateTime('2000-01-01T00:01:02'))
  3857. ->setTarget('myTarget')
  3858. ->setId(42)
  3859. ->setPassword('password');
  3860. $result[] = [
  3861. [
  3862. 'id' => '42',
  3863. 'share_type' => IShare::TYPE_EMAIL,
  3864. 'uid_owner' => 'initiator',
  3865. 'displayname_owner' => 'initiator',
  3866. 'permissions' => 1,
  3867. 'stime' => 946684862,
  3868. 'parent' => null,
  3869. 'expiration' => null,
  3870. 'token' => null,
  3871. 'uid_file_owner' => 'owner',
  3872. 'displayname_file_owner' => 'owner',
  3873. 'note' => '',
  3874. 'label' => '',
  3875. 'path' => 'folder',
  3876. 'item_type' => 'folder',
  3877. 'storage_id' => 'storageId',
  3878. 'storage' => 100,
  3879. 'item_source' => 2,
  3880. 'file_source' => 2,
  3881. 'file_parent' => 1,
  3882. 'file_target' => 'myTarget',
  3883. 'share_with' => 'user@server.com',
  3884. 'share_with_displayname' => 'mail display name',
  3885. 'mail_send' => 0,
  3886. 'mimetype' => 'myFolderMimeType',
  3887. 'has_preview' => false,
  3888. 'password' => 'password',
  3889. 'send_password_by_talk' => false,
  3890. 'hide_download' => 0,
  3891. 'can_edit' => false,
  3892. 'can_delete' => false,
  3893. 'password_expiration_time' => null,
  3894. 'item_size' => 123456,
  3895. 'item_mtime' => 1234567890,
  3896. 'attributes' => null,
  3897. ], $share, [], false
  3898. ];
  3899. $share = \OC::$server->getShareManager()->newShare();
  3900. $share->setShareType(IShare::TYPE_EMAIL)
  3901. ->setSharedBy('initiator')
  3902. ->setSharedWith('user@server.com')
  3903. ->setShareOwner('owner')
  3904. ->setPermissions(\OCP\Constants::PERMISSION_READ)
  3905. ->setNode($folder)
  3906. ->setShareTime(new \DateTime('2000-01-01T00:01:02'))
  3907. ->setTarget('myTarget')
  3908. ->setId(42)
  3909. ->setPassword('password')
  3910. ->setSendPasswordByTalk(true);
  3911. $result[] = [
  3912. [
  3913. 'id' => '42',
  3914. 'share_type' => IShare::TYPE_EMAIL,
  3915. 'uid_owner' => 'initiator',
  3916. 'displayname_owner' => 'initiator',
  3917. 'permissions' => 1,
  3918. 'stime' => 946684862,
  3919. 'parent' => null,
  3920. 'expiration' => null,
  3921. 'token' => null,
  3922. 'uid_file_owner' => 'owner',
  3923. 'displayname_file_owner' => 'owner',
  3924. 'note' => '',
  3925. 'label' => '',
  3926. 'path' => 'folder',
  3927. 'item_type' => 'folder',
  3928. 'storage_id' => 'storageId',
  3929. 'storage' => 100,
  3930. 'item_source' => 2,
  3931. 'file_source' => 2,
  3932. 'file_parent' => 1,
  3933. 'file_target' => 'myTarget',
  3934. 'share_with' => 'user@server.com',
  3935. 'share_with_displayname' => 'mail display name',
  3936. 'mail_send' => 0,
  3937. 'mimetype' => 'myFolderMimeType',
  3938. 'has_preview' => false,
  3939. 'password' => 'password',
  3940. 'send_password_by_talk' => true,
  3941. 'hide_download' => 0,
  3942. 'can_edit' => false,
  3943. 'can_delete' => false,
  3944. 'password_expiration_time' => null,
  3945. 'item_size' => 123456,
  3946. 'item_mtime' => 1234567890,
  3947. 'attributes' => null,
  3948. ], $share, [], false
  3949. ];
  3950. // Preview is available
  3951. $share = \OC::$server->getShareManager()->newShare();
  3952. $share->setShareType(IShare::TYPE_USER)
  3953. ->setSharedWith('recipient')
  3954. ->setSharedBy('initiator')
  3955. ->setShareOwner('currentUser')
  3956. ->setPermissions(\OCP\Constants::PERMISSION_READ)
  3957. ->setNode($fileWithPreview)
  3958. ->setShareTime(new \DateTime('2000-01-01T00:01:02'))
  3959. ->setTarget('myTarget')
  3960. ->setNote('personal note')
  3961. ->setId(42);
  3962. $result[] = [
  3963. [
  3964. 'id' => '42',
  3965. 'share_type' => IShare::TYPE_USER,
  3966. 'uid_owner' => 'initiator',
  3967. 'displayname_owner' => 'initiator',
  3968. 'permissions' => 1,
  3969. 'stime' => 946684862,
  3970. 'parent' => null,
  3971. 'expiration' => null,
  3972. 'token' => null,
  3973. 'uid_file_owner' => 'currentUser',
  3974. 'displayname_file_owner' => 'currentUser',
  3975. 'note' => 'personal note',
  3976. 'label' => '',
  3977. 'path' => 'fileWithPreview',
  3978. 'item_type' => 'file',
  3979. 'storage_id' => 'storageId',
  3980. 'storage' => 100,
  3981. 'item_source' => 4,
  3982. 'file_source' => 4,
  3983. 'file_parent' => 1,
  3984. 'file_target' => 'myTarget',
  3985. 'share_with' => 'recipient',
  3986. 'share_with_displayname' => 'recipient',
  3987. 'share_with_displayname_unique' => 'recipient',
  3988. 'mail_send' => 0,
  3989. 'mimetype' => 'mimeWithPreview',
  3990. 'has_preview' => true,
  3991. 'hide_download' => 0,
  3992. 'can_edit' => true,
  3993. 'can_delete' => true,
  3994. 'item_size' => 123456,
  3995. 'item_mtime' => 1234567890,
  3996. 'attributes' => null,
  3997. ], $share, [], false
  3998. ];
  3999. return $result;
  4000. }
  4001. /**
  4002. * @dataProvider dataFormatShare
  4003. *
  4004. * @param array $expects
  4005. * @param \OCP\Share\IShare $share
  4006. * @param array $users
  4007. * @param $exception
  4008. */
  4009. public function testFormatShare(array $expects, \OCP\Share\IShare $share, array $users, $exception) {
  4010. $this->userManager->method('get')->willReturnMap($users);
  4011. $recipientGroup = $this->createMock(IGroup::class);
  4012. $recipientGroup->method('getDisplayName')->willReturn('recipientGroupDisplayName');
  4013. $this->groupManager->method('get')->willReturnMap([
  4014. ['recipientGroup', $recipientGroup],
  4015. ]);
  4016. $this->urlGenerator->method('linkToRouteAbsolute')
  4017. ->with('files_sharing.sharecontroller.showShare', ['token' => 'myToken'])
  4018. ->willReturn('myLink');
  4019. $this->rootFolder->method('getUserFolder')
  4020. ->with($this->currentUser)
  4021. ->willReturnSelf();
  4022. $this->dateTimeZone->method('getTimezone')->willReturn(new \DateTimeZone('UTC'));
  4023. if (!$exception) {
  4024. $this->rootFolder->method('getFirstNodeById')
  4025. ->with($share->getNodeId())
  4026. ->willReturn($share->getNode());
  4027. $this->rootFolder->method('getRelativePath')
  4028. ->with($share->getNode()->getPath())
  4029. ->willReturnArgument(0);
  4030. }
  4031. $cm = $this->createMock(\OCP\Contacts\IManager::class);
  4032. $this->overwriteService(\OCP\Contacts\IManager::class, $cm);
  4033. $cm->method('search')
  4034. ->willReturnMap([
  4035. ['user@server.com', ['CLOUD'], [
  4036. 'limit' => 1,
  4037. 'enumeration' => false,
  4038. 'strict_search' => true,
  4039. ],
  4040. [
  4041. [
  4042. 'CLOUD' => [
  4043. 'user@server.com',
  4044. ],
  4045. 'FN' => 'foobar',
  4046. ],
  4047. ],
  4048. ],
  4049. ['user@server.com', ['EMAIL'], [
  4050. 'limit' => 1,
  4051. 'enumeration' => false,
  4052. 'strict_search' => true,
  4053. ],
  4054. [
  4055. [
  4056. 'EMAIL' => [
  4057. 'user@server.com',
  4058. ],
  4059. 'FN' => 'mail display name',
  4060. ],
  4061. ],
  4062. ],
  4063. ]);
  4064. try {
  4065. $result = $this->invokePrivate($this->ocs, 'formatShare', [$share]);
  4066. $this->assertFalse($exception);
  4067. $this->assertEquals($expects, $result);
  4068. } catch (NotFoundException $e) {
  4069. $this->assertTrue($exception);
  4070. }
  4071. }
  4072. public function dataFormatRoomShare() {
  4073. $file = $this->getMockBuilder(File::class)->getMock();
  4074. $parent = $this->getMockBuilder(Folder::class)->getMock();
  4075. $file->method('getMimeType')->willReturn('myMimeType');
  4076. $file->method('getPath')->willReturn('file');
  4077. $parent->method('getId')->willReturn(1);
  4078. $file->method('getId')->willReturn(3);
  4079. $file->method('getParent')->willReturn($parent);
  4080. $file->method('getSize')->willReturn(123456);
  4081. $file->method('getMTime')->willReturn(1234567890);
  4082. $cache = $this->getMockBuilder('OCP\Files\Cache\ICache')->getMock();
  4083. $cache->method('getNumericStorageId')->willReturn(100);
  4084. $storage = $this->createMock(Storage::class);
  4085. $storage->method('getId')->willReturn('storageId');
  4086. $storage->method('getCache')->willReturn($cache);
  4087. $file->method('getStorage')->willReturn($storage);
  4088. $result = [];
  4089. $share = \OC::$server->getShareManager()->newShare();
  4090. $share->setShareType(IShare::TYPE_ROOM)
  4091. ->setSharedWith('recipientRoom')
  4092. ->setSharedBy('initiator')
  4093. ->setShareOwner('owner')
  4094. ->setPermissions(\OCP\Constants::PERMISSION_READ)
  4095. ->setNode($file)
  4096. ->setShareTime(new \DateTime('2000-01-01T00:01:02'))
  4097. ->setTarget('myTarget')
  4098. ->setNote('personal note')
  4099. ->setId(42);
  4100. $result[] = [
  4101. [
  4102. 'id' => '42',
  4103. 'share_type' => IShare::TYPE_ROOM,
  4104. 'uid_owner' => 'initiator',
  4105. 'displayname_owner' => 'initiator',
  4106. 'permissions' => 1,
  4107. 'stime' => 946684862,
  4108. 'parent' => null,
  4109. 'expiration' => null,
  4110. 'token' => null,
  4111. 'uid_file_owner' => 'owner',
  4112. 'displayname_file_owner' => 'owner',
  4113. 'note' => 'personal note',
  4114. 'path' => 'file',
  4115. 'item_type' => 'file',
  4116. 'storage_id' => 'storageId',
  4117. 'storage' => 100,
  4118. 'item_source' => 3,
  4119. 'file_source' => 3,
  4120. 'file_parent' => 1,
  4121. 'file_target' => 'myTarget',
  4122. 'share_with' => 'recipientRoom',
  4123. 'share_with_displayname' => '',
  4124. 'mail_send' => 0,
  4125. 'mimetype' => 'myMimeType',
  4126. 'has_preview' => false,
  4127. 'hide_download' => 0,
  4128. 'label' => '',
  4129. 'can_edit' => false,
  4130. 'can_delete' => false,
  4131. 'item_size' => 123456,
  4132. 'item_mtime' => 1234567890,
  4133. 'attributes' => null,
  4134. ], $share, false, []
  4135. ];
  4136. $share = \OC::$server->getShareManager()->newShare();
  4137. $share->setShareType(IShare::TYPE_ROOM)
  4138. ->setSharedWith('recipientRoom')
  4139. ->setSharedBy('initiator')
  4140. ->setShareOwner('owner')
  4141. ->setPermissions(\OCP\Constants::PERMISSION_READ)
  4142. ->setNode($file)
  4143. ->setShareTime(new \DateTime('2000-01-01T00:01:02'))
  4144. ->setTarget('myTarget')
  4145. ->setNote('personal note')
  4146. ->setId(42);
  4147. $result[] = [
  4148. [
  4149. 'id' => '42',
  4150. 'share_type' => IShare::TYPE_ROOM,
  4151. 'uid_owner' => 'initiator',
  4152. 'displayname_owner' => 'initiator',
  4153. 'permissions' => 1,
  4154. 'stime' => 946684862,
  4155. 'parent' => null,
  4156. 'expiration' => null,
  4157. 'token' => null,
  4158. 'uid_file_owner' => 'owner',
  4159. 'displayname_file_owner' => 'owner',
  4160. 'note' => 'personal note',
  4161. 'path' => 'file',
  4162. 'item_type' => 'file',
  4163. 'storage_id' => 'storageId',
  4164. 'storage' => 100,
  4165. 'item_source' => 3,
  4166. 'file_source' => 3,
  4167. 'file_parent' => 1,
  4168. 'file_target' => 'myTarget',
  4169. 'share_with' => 'recipientRoom',
  4170. 'share_with_displayname' => 'recipientRoomName',
  4171. 'mail_send' => 0,
  4172. 'mimetype' => 'myMimeType',
  4173. 'has_preview' => false,
  4174. 'hide_download' => 0,
  4175. 'label' => '',
  4176. 'can_edit' => false,
  4177. 'can_delete' => false,
  4178. 'item_size' => 123456,
  4179. 'item_mtime' => 1234567890,
  4180. 'attributes' => null,
  4181. ], $share, true, [
  4182. 'share_with_displayname' => 'recipientRoomName'
  4183. ]
  4184. ];
  4185. return $result;
  4186. }
  4187. /**
  4188. * @dataProvider dataFormatRoomShare
  4189. *
  4190. * @param array $expects
  4191. * @param \OCP\Share\IShare $share
  4192. * @param bool $helperAvailable
  4193. * @param array $formatShareByHelper
  4194. */
  4195. public function testFormatRoomShare(array $expects, \OCP\Share\IShare $share, bool $helperAvailable, array $formatShareByHelper) {
  4196. $this->rootFolder->method('getUserFolder')
  4197. ->with($this->currentUser)
  4198. ->willReturnSelf();
  4199. $this->rootFolder->method('getFirstNodeById')
  4200. ->with($share->getNodeId())
  4201. ->willReturn($share->getNode());
  4202. $this->rootFolder->method('getRelativePath')
  4203. ->with($share->getNode()->getPath())
  4204. ->willReturnArgument(0);
  4205. if (!$helperAvailable) {
  4206. $this->appManager->method('isEnabledForUser')
  4207. ->with('spreed')
  4208. ->willReturn(false);
  4209. } else {
  4210. $this->appManager->method('isEnabledForUser')
  4211. ->with('spreed')
  4212. ->willReturn(true);
  4213. $helper = $this->getMockBuilder('\OCA\Talk\Share\Helper\ShareAPIController')
  4214. ->setMethods(['formatShare'])
  4215. ->getMock();
  4216. $helper->method('formatShare')
  4217. ->with($share)
  4218. ->willReturn($formatShareByHelper);
  4219. $this->serverContainer->method('get')
  4220. ->with('\OCA\Talk\Share\Helper\ShareAPIController')
  4221. ->willReturn($helper);
  4222. }
  4223. $result = $this->invokePrivate($this->ocs, 'formatShare', [$share]);
  4224. $this->assertEquals($expects, $result);
  4225. }
  4226. private function getNonSharedUserFolder(): array {
  4227. $node = $this->getMockBuilder(Folder::class)->getMock();
  4228. $userFolder = $this->getMockBuilder(Folder::class)->getMock();
  4229. $storage = $this->createMock(Storage::class);
  4230. $storage->method('instanceOfStorage')
  4231. ->willReturnMap([
  4232. ['OCA\Files_Sharing\External\Storage', false],
  4233. ['OCA\Files_Sharing\SharedStorage', false],
  4234. ]);
  4235. $userFolder->method('getStorage')->willReturn($storage);
  4236. $node->method('getStorage')->willReturn($storage);
  4237. $node->method('getId')->willReturn(42);
  4238. return [$userFolder, $node];
  4239. }
  4240. private function getNonSharedUserFile(): array {
  4241. $node = $this->getMockBuilder(File::class)->getMock();
  4242. $userFolder = $this->getMockBuilder(Folder::class)->getMock();
  4243. $storage = $this->createMock(Storage::class);
  4244. $storage->method('instanceOfStorage')
  4245. ->willReturnMap([
  4246. ['OCA\Files_Sharing\External\Storage', false],
  4247. ['OCA\Files_Sharing\SharedStorage', false],
  4248. ]);
  4249. $userFolder->method('getStorage')->willReturn($storage);
  4250. $node->method('getStorage')->willReturn($storage);
  4251. $node->method('getId')->willReturn(42);
  4252. return [$userFolder, $node];
  4253. }
  4254. }