1
0

ShareAPIControllerTest.php 147 KB

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