ShareAPIControllerTest.php 154 KB

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