ShareAPIControllerTest.php 148 KB

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