You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ShareAPIControllerTest.php 132KB

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