ShareAPIControllerTest.php 149 KB

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