ShareAPIControllerTest.php 153 KB

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