ShareAPIControllerTest.php 136 KB

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