ShareAPIControllerTest.php 151 KB

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