ShareAPIControllerTest.php 149 KB

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