ShareAPIControllerTest.php 112 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  9. * @author Thomas Müller <thomas.mueller@tmit.eu>
  10. * @author Vincent Petry <pvince81@owncloud.com>
  11. *
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. namespace OCA\Files_Sharing\Tests\Controller;
  28. use OCP\App\IAppManager;
  29. use OCP\AppFramework\Http\DataResponse;
  30. use OCP\AppFramework\OCS\OCSNotFoundException;
  31. use OCP\Files\File;
  32. use OCP\Files\Folder;
  33. use OCP\Files\Mount\IMountPoint;
  34. use OCP\Files\Storage;
  35. use OCP\IConfig;
  36. use OCP\IL10N;
  37. use OCA\Files_Sharing\Controller\ShareAPIController;
  38. use OCP\Files\NotFoundException;
  39. use OCP\IGroupManager;
  40. use OCP\IUserManager;
  41. use OCP\IServerContainer;
  42. use OCP\IRequest;
  43. use OCP\IURLGenerator;
  44. use OCP\IUser;
  45. use OCP\Files\IRootFolder;
  46. use OCP\Lock\LockedException;
  47. use OCP\Share\IManager;
  48. use OCP\Share;
  49. use Test\TestCase;
  50. use OCP\Share\IShare;
  51. /**
  52. * Class ShareAPIControllerTest
  53. *
  54. * @package OCA\Files_Sharing\Tests\Controller
  55. * @group DB
  56. */
  57. class ShareAPIControllerTest extends TestCase {
  58. /** @var string */
  59. private $appName = 'files_sharing';
  60. /** @var \OC\Share20\Manager|\PHPUnit_Framework_MockObject_MockObject */
  61. private $shareManager;
  62. /** @var IGroupManager|\PHPUnit_Framework_MockObject_MockObject */
  63. private $groupManager;
  64. /** @var IUserManager|\PHPUnit_Framework_MockObject_MockObject */
  65. private $userManager;
  66. /** @var IRequest|\PHPUnit_Framework_MockObject_MockObject */
  67. private $request;
  68. /** @var IRootFolder|\PHPUnit_Framework_MockObject_MockObject */
  69. private $rootFolder;
  70. /** @var IURLGenerator|\PHPUnit_Framework_MockObject_MockObject */
  71. private $urlGenerator;
  72. /** @var string|\PHPUnit_Framework_MockObject_MockObject */
  73. private $currentUser;
  74. /** @var ShareAPIController */
  75. private $ocs;
  76. /** @var IL10N|\PHPUnit_Framework_MockObject_MockObject */
  77. private $l;
  78. /** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */
  79. private $config;
  80. /** @var IAppManager|\PHPUnit_Framework_MockObject_MockObject */
  81. private $appManager;
  82. /** @var IServerContainer|\PHPUnit_Framework_MockObject_MockObject */
  83. private $serverContainer;
  84. protected function setUp() {
  85. $this->shareManager = $this->createMock(IManager::class);
  86. $this->shareManager
  87. ->expects($this->any())
  88. ->method('shareApiEnabled')
  89. ->willReturn(true);
  90. $this->groupManager = $this->createMock(IGroupManager::class);
  91. $this->userManager = $this->createMock(IUserManager::class);
  92. $this->request = $this->createMock(IRequest::class);
  93. $this->rootFolder = $this->createMock(IRootFolder::class);
  94. $this->urlGenerator = $this->createMock(IURLGenerator::class);
  95. $this->currentUser = 'currentUser';
  96. $this->l = $this->createMock(IL10N::class);
  97. $this->l->method('t')
  98. ->will($this->returnCallback(function($text, $parameters = []) {
  99. return vsprintf($text, $parameters);
  100. }));
  101. $this->config = $this->createMock(IConfig::class);
  102. $this->appManager = $this->createMock(IAppManager::class);
  103. $this->serverContainer = $this->createMock(IServerContainer::class);
  104. $this->ocs = new ShareAPIController(
  105. $this->appName,
  106. $this->request,
  107. $this->shareManager,
  108. $this->groupManager,
  109. $this->userManager,
  110. $this->rootFolder,
  111. $this->urlGenerator,
  112. $this->currentUser,
  113. $this->l,
  114. $this->config,
  115. $this->appManager,
  116. $this->serverContainer
  117. );
  118. }
  119. /**
  120. * @return ShareAPIController|\PHPUnit_Framework_MockObject_MockObject
  121. */
  122. private function mockFormatShare() {
  123. return $this->getMockBuilder(ShareAPIController::class)
  124. ->setConstructorArgs([
  125. $this->appName,
  126. $this->request,
  127. $this->shareManager,
  128. $this->groupManager,
  129. $this->userManager,
  130. $this->rootFolder,
  131. $this->urlGenerator,
  132. $this->currentUser,
  133. $this->l,
  134. $this->config,
  135. $this->appManager,
  136. $this->serverContainer
  137. ])->setMethods(['formatShare'])
  138. ->getMock();
  139. }
  140. private function newShare() {
  141. return \OC::$server->getShareManager()->newShare();
  142. }
  143. /**
  144. * @expectedException \OCP\AppFramework\OCS\OCSNotFoundException
  145. * @expectedExceptionMessage Wrong share ID, share doesn't exist
  146. */
  147. public function testDeleteShareShareNotFound() {
  148. $this->shareManager
  149. ->expects($this->exactly(3))
  150. ->method('getShareById')
  151. ->will($this->returnCallback(function($id) {
  152. if ($id === 'ocinternal:42' || $id === 'ocRoomShare:42' || $id === 'ocFederatedSharing:42') {
  153. throw new \OCP\Share\Exceptions\ShareNotFound();
  154. } else {
  155. throw new \Exception();
  156. }
  157. }));
  158. $this->shareManager->method('outgoingServer2ServerSharesAllowed')->willReturn(true);
  159. $this->ocs->deleteShare(42);
  160. }
  161. public function testDeleteShare() {
  162. $node = $this->getMockBuilder(File::class)->getMock();
  163. $share = $this->newShare();
  164. $share->setSharedBy($this->currentUser)
  165. ->setNode($node);
  166. $this->shareManager
  167. ->expects($this->once())
  168. ->method('getShareById')
  169. ->with('ocinternal:42')
  170. ->willReturn($share);
  171. $this->shareManager
  172. ->expects($this->once())
  173. ->method('deleteShare')
  174. ->with($share);
  175. $node->expects($this->once())
  176. ->method('lock')
  177. ->with(\OCP\Lock\ILockingProvider::LOCK_SHARED);
  178. $expected = new DataResponse();
  179. $result = $this->ocs->deleteShare(42);
  180. $this->assertInstanceOf(get_class($expected), $result);
  181. $this->assertEquals($expected->getData(), $result->getData());
  182. }
  183. /**
  184. * @expectedException \OCP\AppFramework\OCS\OCSNotFoundException
  185. * @expectedExceptionMessage could not delete share
  186. */
  187. public function testDeleteShareLocked() {
  188. $node = $this->getMockBuilder(File::class)->getMock();
  189. $share = $this->newShare();
  190. $share->setSharedBy($this->currentUser)
  191. ->setNode($node);
  192. $this->shareManager
  193. ->expects($this->once())
  194. ->method('getShareById')
  195. ->with('ocinternal:42')
  196. ->willReturn($share);
  197. $this->shareManager
  198. ->expects($this->never())
  199. ->method('deleteShare')
  200. ->with($share);
  201. $node->expects($this->once())
  202. ->method('lock')
  203. ->with(\OCP\Lock\ILockingProvider::LOCK_SHARED)
  204. ->will($this->throwException(new LockedException('mypath')));
  205. $this->ocs->deleteShare(42);
  206. }
  207. /*
  208. * FIXME: Enable once we have a federated Share Provider
  209. public function testGetGetShareNotExists() {
  210. $this->shareManager
  211. ->expects($this->once())
  212. ->method('getShareById')
  213. ->with('ocinternal:42', 'currentUser')
  214. ->will($this->throwException(new \OC\Share20\Exception\ShareNotFound()));
  215. $expected = new \OC\OCS\Result(null, 404, 'wrong share ID, share doesn\'t exist.');
  216. $this->assertEquals($expected, $this->ocs->getShare(42));
  217. }
  218. */
  219. public function createShare($id, $shareType, $sharedWith, $sharedBy, $shareOwner, $path, $permissions,
  220. $shareTime, $expiration, $parent, $target, $mail_send, $note = '', $token=null,
  221. $password=null, $label = '') {
  222. $share = $this->getMockBuilder(IShare::class)->getMock();
  223. $share->method('getId')->willReturn($id);
  224. $share->method('getShareType')->willReturn($shareType);
  225. $share->method('getSharedWith')->willReturn($sharedWith);
  226. $share->method('getSharedBy')->willReturn($sharedBy);
  227. $share->method('getShareOwner')->willReturn($shareOwner);
  228. $share->method('getNode')->willReturn($path);
  229. $share->method('getPermissions')->willReturn($permissions);
  230. $share->method('getNote')->willReturn($note);
  231. $share->method('getLabel')->willReturn($label);
  232. $time = new \DateTime();
  233. $time->setTimestamp($shareTime);
  234. $share->method('getShareTime')->willReturn($time);
  235. $share->method('getExpirationDate')->willReturn($expiration);
  236. $share->method('getTarget')->willReturn($target);
  237. $share->method('getMailSend')->willReturn($mail_send);
  238. $share->method('getToken')->willReturn($token);
  239. $share->method('getPassword')->willReturn($password);
  240. if ($shareType === \OCP\Share::SHARE_TYPE_USER ||
  241. $shareType === \OCP\Share::SHARE_TYPE_GROUP ||
  242. $shareType === \OCP\Share::SHARE_TYPE_LINK) {
  243. $share->method('getFullId')->willReturn('ocinternal:'.$id);
  244. }
  245. return $share;
  246. }
  247. public function dataGetShare() {
  248. $data = [];
  249. $cache = $this->getMockBuilder('OC\Files\Cache\Cache')
  250. ->disableOriginalConstructor()
  251. ->getMock();
  252. $cache->method('getNumericStorageId')->willReturn(101);
  253. $storage = $this->getMockBuilder(Storage::class)
  254. ->disableOriginalConstructor()
  255. ->getMock();
  256. $storage->method('getId')->willReturn('STORAGE');
  257. $storage->method('getCache')->willReturn($cache);
  258. $parentFolder = $this->getMockBuilder('OCP\Files\Folder')->getMock();
  259. $parentFolder->method('getId')->willReturn(3);
  260. $file = $this->getMockBuilder('OCP\Files\File')->getMock();
  261. $file->method('getId')->willReturn(1);
  262. $file->method('getPath')->willReturn('file');
  263. $file->method('getStorage')->willReturn($storage);
  264. $file->method('getParent')->willReturn($parentFolder);
  265. $file->method('getMimeType')->willReturn('myMimeType');
  266. $folder = $this->getMockBuilder('OCP\Files\Folder')->getMock();
  267. $folder->method('getId')->willReturn(2);
  268. $folder->method('getPath')->willReturn('folder');
  269. $folder->method('getStorage')->willReturn($storage);
  270. $folder->method('getParent')->willReturn($parentFolder);
  271. $folder->method('getMimeType')->willReturn('myFolderMimeType');
  272. // File shared with user
  273. $share = $this->createShare(
  274. 100,
  275. \OCP\Share::SHARE_TYPE_USER,
  276. 'userId',
  277. 'initiatorId',
  278. 'ownerId',
  279. $file,
  280. 4,
  281. 5,
  282. null,
  283. 6,
  284. 'target',
  285. 0,
  286. 'personal note'
  287. );
  288. $expected = [
  289. 'id' => 100,
  290. 'share_type' => \OCP\Share::SHARE_TYPE_USER,
  291. 'share_with' => 'userId',
  292. 'share_with_displayname' => 'userDisplay',
  293. 'uid_owner' => 'initiatorId',
  294. 'displayname_owner' => 'initiatorDisplay',
  295. 'item_type' => 'file',
  296. 'item_source' => 1,
  297. 'file_source' => 1,
  298. 'file_target' => 'target',
  299. 'file_parent' => 3,
  300. 'token' => null,
  301. 'expiration' => null,
  302. 'permissions' => 4,
  303. 'stime' => 5,
  304. 'parent' => null,
  305. 'storage_id' => 'STORAGE',
  306. 'path' => 'file',
  307. 'storage' => 101,
  308. 'mail_send' => 0,
  309. 'uid_file_owner' => 'ownerId',
  310. 'note' => 'personal note',
  311. 'label' => '',
  312. 'displayname_file_owner' => 'ownerDisplay',
  313. 'mimetype' => 'myMimeType',
  314. 'hide_download' => 0,
  315. ];
  316. $data[] = [$share, $expected];
  317. // Folder shared with group
  318. $share = $this->createShare(
  319. 101,
  320. \OCP\Share::SHARE_TYPE_GROUP,
  321. 'groupId',
  322. 'initiatorId',
  323. 'ownerId',
  324. $folder,
  325. 4,
  326. 5,
  327. null,
  328. 6,
  329. 'target',
  330. 0,
  331. 'personal note'
  332. );
  333. $expected = [
  334. 'id' => 101,
  335. 'share_type' => \OCP\Share::SHARE_TYPE_GROUP,
  336. 'share_with' => 'groupId',
  337. 'share_with_displayname' => 'groupId',
  338. 'uid_owner' => 'initiatorId',
  339. 'displayname_owner' => 'initiatorDisplay',
  340. 'item_type' => 'folder',
  341. 'item_source' => 2,
  342. 'file_source' => 2,
  343. 'file_target' => 'target',
  344. 'file_parent' => 3,
  345. 'token' => null,
  346. 'expiration' => null,
  347. 'permissions' => 4,
  348. 'stime' => 5,
  349. 'parent' => null,
  350. 'storage_id' => 'STORAGE',
  351. 'path' => 'folder',
  352. 'storage' => 101,
  353. 'mail_send' => 0,
  354. 'uid_file_owner' => 'ownerId',
  355. 'note' => 'personal note',
  356. 'label' => '',
  357. 'displayname_file_owner' => 'ownerDisplay',
  358. 'mimetype' => 'myFolderMimeType',
  359. 'hide_download' => 0,
  360. ];
  361. $data[] = [$share, $expected];
  362. // File shared by link with Expire
  363. $expire = \DateTime::createFromFormat('Y-m-d h:i:s', '2000-01-02 01:02:03');
  364. $share = $this->createShare(
  365. 101,
  366. \OCP\Share::SHARE_TYPE_LINK,
  367. null,
  368. 'initiatorId',
  369. 'ownerId',
  370. $folder,
  371. 4,
  372. 5,
  373. $expire,
  374. 6,
  375. 'target',
  376. 0,
  377. 'personal note',
  378. 'token',
  379. 'password',
  380. 'first link share'
  381. );
  382. $expected = [
  383. 'id' => 101,
  384. 'share_type' => \OCP\Share::SHARE_TYPE_LINK,
  385. 'password' => 'password',
  386. 'share_with' => 'password',
  387. 'share_with_displayname' => 'password',
  388. 'send_password_by_talk' => false,
  389. 'uid_owner' => 'initiatorId',
  390. 'displayname_owner' => 'initiatorDisplay',
  391. 'item_type' => 'folder',
  392. 'item_source' => 2,
  393. 'file_source' => 2,
  394. 'file_target' => 'target',
  395. 'file_parent' => 3,
  396. 'token' => 'token',
  397. 'expiration' => '2000-01-02 00:00:00',
  398. 'permissions' => 4,
  399. 'stime' => 5,
  400. 'parent' => null,
  401. 'storage_id' => 'STORAGE',
  402. 'path' => 'folder',
  403. 'storage' => 101,
  404. 'mail_send' => 0,
  405. 'url' => 'url',
  406. 'uid_file_owner' => 'ownerId',
  407. 'note' => 'personal note',
  408. 'label' => 'first link share',
  409. 'displayname_file_owner' => 'ownerDisplay',
  410. 'mimetype' => 'myFolderMimeType',
  411. 'hide_download' => 0,
  412. ];
  413. $data[] = [$share, $expected];
  414. return $data;
  415. }
  416. /**
  417. * @dataProvider dataGetShare
  418. */
  419. public function testGetShare(\OCP\Share\IShare $share, array $result) {
  420. /** @var ShareAPIController|\PHPUnit_Framework_MockObject_MockObject $ocs */
  421. $ocs = $this->getMockBuilder(ShareAPIController::class)
  422. ->setConstructorArgs([
  423. $this->appName,
  424. $this->request,
  425. $this->shareManager,
  426. $this->groupManager,
  427. $this->userManager,
  428. $this->rootFolder,
  429. $this->urlGenerator,
  430. $this->currentUser,
  431. $this->l,
  432. $this->config,
  433. $this->appManager,
  434. $this->serverContainer
  435. ])->setMethods(['canAccessShare'])
  436. ->getMock();
  437. $ocs->expects($this->any())
  438. ->method('canAccessShare')
  439. ->willReturn(true);
  440. $this->shareManager
  441. ->expects($this->any())
  442. ->method('getShareById')
  443. ->with($share->getFullId(), 'currentUser')
  444. ->willReturn($share);
  445. $userFolder = $this->getMockBuilder('OCP\Files\Folder')->getMock();
  446. $userFolder
  447. ->method('getRelativePath')
  448. ->will($this->returnArgument(0));
  449. $userFolder->method('getById')
  450. ->with($share->getNodeId())
  451. ->willReturn([$share->getNode()]);
  452. $this->rootFolder->method('getUserFolder')
  453. ->with($this->currentUser)
  454. ->willReturn($userFolder);
  455. $this->urlGenerator
  456. ->method('linkToRouteAbsolute')
  457. ->willReturn('url');
  458. $initiator = $this->getMockBuilder(IUser::class)->getMock();
  459. $initiator->method('getUID')->willReturn('initiatorId');
  460. $initiator->method('getDisplayName')->willReturn('initiatorDisplay');
  461. $owner = $this->getMockBuilder(IUser::class)->getMock();
  462. $owner->method('getUID')->willReturn('ownerId');
  463. $owner->method('getDisplayName')->willReturn('ownerDisplay');
  464. $user = $this->getMockBuilder(IUser::class)->getMock();
  465. $user->method('getUID')->willReturn('userId');
  466. $user->method('getDisplayName')->willReturn('userDisplay');
  467. $group = $this->getMockBuilder('OCP\IGroup')->getMock();
  468. $group->method('getGID')->willReturn('groupId');
  469. $this->userManager->method('get')->will($this->returnValueMap([
  470. ['userId', $user],
  471. ['initiatorId', $initiator],
  472. ['ownerId', $owner],
  473. ]));
  474. $this->groupManager->method('get')->will($this->returnValueMap([
  475. ['group', $group],
  476. ]));
  477. $d = $ocs->getShare($share->getId())->getData()[0];
  478. $this->assertEquals($result, $ocs->getShare($share->getId())->getData()[0]);
  479. }
  480. /**
  481. * @expectedException \OCP\AppFramework\OCS\OCSNotFoundException
  482. * @expectedExceptionMessage Wrong share ID, share doesn't exist
  483. */
  484. public function testGetShareInvalidNode() {
  485. $share = \OC::$server->getShareManager()->newShare();
  486. $share->setSharedBy('initiator')
  487. ->setSharedWith('recipient')
  488. ->setShareOwner('owner');
  489. $this->shareManager
  490. ->expects($this->once())
  491. ->method('getShareById')
  492. ->with('ocinternal:42', 'currentUser')
  493. ->willReturn($share);
  494. $this->ocs->getShare(42);
  495. }
  496. public function testCanAccessShare() {
  497. $share = $this->getMockBuilder(IShare::class)->getMock();
  498. $share->method('getShareOwner')->willReturn($this->currentUser);
  499. $this->assertTrue($this->invokePrivate($this->ocs, 'canAccessShare', [$share]));
  500. $share = $this->getMockBuilder(IShare::class)->getMock();
  501. $share->method('getSharedBy')->willReturn($this->currentUser);
  502. $this->assertTrue($this->invokePrivate($this->ocs, 'canAccessShare', [$share]));
  503. $share = $this->getMockBuilder(IShare::class)->getMock();
  504. $share->method('getShareType')->willReturn(\OCP\Share::SHARE_TYPE_USER);
  505. $share->method('getSharedWith')->willReturn($this->currentUser);
  506. $this->assertTrue($this->invokePrivate($this->ocs, 'canAccessShare', [$share]));
  507. $share = $this->getMockBuilder(IShare::class)->getMock();
  508. $share->method('getShareType')->willReturn(\OCP\Share::SHARE_TYPE_USER);
  509. $share->method('getSharedWith')->willReturn($this->getMockBuilder(IUser::class)->getMock());
  510. $this->assertFalse($this->invokePrivate($this->ocs, 'canAccessShare', [$share]));
  511. $share = $this->getMockBuilder(IShare::class)->getMock();
  512. $share->method('getShareType')->willReturn(\OCP\Share::SHARE_TYPE_GROUP);
  513. $share->method('getSharedWith')->willReturn('group');
  514. $user = $this->createMock(IUser::class);
  515. $this->userManager->method('get')
  516. ->with($this->currentUser)
  517. ->willReturn($user);
  518. $group = $this->getMockBuilder('OCP\IGroup')->getMock();
  519. $group->method('inGroup')->with($user)->willReturn(true);
  520. $group2 = $this->getMockBuilder('OCP\IGroup')->getMock();
  521. $group2->method('inGroup')->with($user)->willReturn(false);
  522. $this->groupManager->method('get')->will($this->returnValueMap([
  523. ['group', $group],
  524. ['group2', $group2],
  525. ['groupnull', null],
  526. ]));
  527. $this->assertTrue($this->invokePrivate($this->ocs, 'canAccessShare', [$share]));
  528. $share = $this->createMock(IShare::class);
  529. $share->method('getShareType')->willReturn(\OCP\Share::SHARE_TYPE_GROUP);
  530. $share->method('getSharedWith')->willReturn('group2');
  531. $this->assertFalse($this->invokePrivate($this->ocs, 'canAccessShare', [$share]));
  532. // null group
  533. $share = $this->createMock(IShare::class);
  534. $share->method('getShareType')->willReturn(\OCP\Share::SHARE_TYPE_GROUP);
  535. $share->method('getSharedWith')->willReturn('groupnull');
  536. $this->assertFalse($this->invokePrivate($this->ocs, 'canAccessShare', [$share]));
  537. $share = $this->createMock(IShare::class);
  538. $share->method('getShareType')->willReturn(\OCP\Share::SHARE_TYPE_LINK);
  539. $this->assertFalse($this->invokePrivate($this->ocs, 'canAccessShare', [$share]));
  540. }
  541. public function dataCanAccessRoomShare() {
  542. $result = [];
  543. $share = $this->createMock(IShare::class);
  544. $share->method('getShareType')->willReturn(\OCP\Share::SHARE_TYPE_ROOM);
  545. $share->method('getSharedWith')->willReturn('recipientRoom');
  546. $result[] = [
  547. false, $share, false, false
  548. ];
  549. $result[] = [
  550. false, $share, false, true
  551. ];
  552. $result[] = [
  553. true, $share, true, true
  554. ];
  555. $result[] = [
  556. false, $share, true, false
  557. ];
  558. return $result;
  559. }
  560. /**
  561. * @dataProvider dataCanAccessRoomShare
  562. *
  563. * @param bool $expects
  564. * @param \OCP\Share\IShare $share
  565. * @param bool helperAvailable
  566. * @param bool canAccessShareByHelper
  567. */
  568. public function testCanAccessRoomShare(bool $expected, \OCP\Share\IShare $share, bool $helperAvailable, bool $canAccessShareByHelper) {
  569. if (!$helperAvailable) {
  570. $this->appManager->method('isEnabledForUser')
  571. ->with('spreed')
  572. ->willReturn(false);
  573. } else {
  574. $this->appManager->method('isEnabledForUser')
  575. ->with('spreed')
  576. ->willReturn(true);
  577. $helper = $this->getMockBuilder('\OCA\Spreed\Share\Helper\ShareAPIController')
  578. ->setMethods(array('canAccessShare'))
  579. ->getMock();
  580. $helper->method('canAccessShare')
  581. ->with($share, $this->currentUser)
  582. ->willReturn($canAccessShareByHelper);
  583. $this->serverContainer->method('query')
  584. ->with('\OCA\Spreed\Share\Helper\ShareAPIController')
  585. ->willReturn($helper);
  586. }
  587. $this->assertEquals($expected, $this->invokePrivate($this->ocs, 'canAccessShare', [$share]));
  588. }
  589. /**
  590. * @expectedException \OCP\AppFramework\OCS\OCSNotFoundException
  591. * @expectedExceptionMessage Please specify a file or folder path
  592. */
  593. public function testCreateShareNoPath() {
  594. $this->ocs->createShare();
  595. }
  596. /**
  597. * @expectedException \OCP\AppFramework\OCS\OCSNotFoundException
  598. * @expectedExceptionMessage Wrong path, file/folder doesn't exist
  599. */
  600. public function testCreateShareInvalidPath() {
  601. $userFolder = $this->getMockBuilder(Folder::class)->getMock();
  602. $this->rootFolder->expects($this->once())
  603. ->method('getUserFolder')
  604. ->with('currentUser')
  605. ->willReturn($userFolder);
  606. $userFolder->expects($this->once())
  607. ->method('get')
  608. ->with('invalid-path')
  609. ->will($this->throwException(new \OCP\Files\NotFoundException()));
  610. $this->ocs->createShare('invalid-path');
  611. }
  612. /**
  613. * @expectedException \OCP\AppFramework\OCS\OCSNotFoundException
  614. * @expectedExceptionMessage invalid permissions
  615. */
  616. public function testCreateShareInvalidPermissions() {
  617. $share = $this->newShare();
  618. $this->shareManager->method('newShare')->willReturn($share);
  619. $userFolder = $this->getMockBuilder(Folder::class)->getMock();
  620. $this->rootFolder->expects($this->once())
  621. ->method('getUserFolder')
  622. ->with('currentUser')
  623. ->willReturn($userFolder);
  624. $path = $this->getMockBuilder(File::class)->getMock();
  625. $userFolder->expects($this->once())
  626. ->method('get')
  627. ->with('valid-path')
  628. ->willReturn($path);
  629. $path->expects($this->once())
  630. ->method('lock')
  631. ->with(\OCP\Lock\ILockingProvider::LOCK_SHARED);
  632. $this->ocs->createShare('valid-path', 32);
  633. }
  634. /**
  635. * @expectedException \OCP\AppFramework\OCS\OCSNotFoundException
  636. * @expectedExceptionMessage Please specify a valid user
  637. */
  638. public function testCreateShareUserNoShareWith() {
  639. $share = $this->newShare();
  640. $this->shareManager->method('newShare')->willReturn($share);
  641. $userFolder = $this->getMockBuilder(Folder::class)->getMock();
  642. $this->rootFolder->expects($this->once())
  643. ->method('getUserFolder')
  644. ->with('currentUser')
  645. ->willReturn($userFolder);
  646. $path = $this->getMockBuilder(File::class)->getMock();
  647. $storage = $this->getMockBuilder(Storage::class)->getMock();
  648. $storage->method('instanceOfStorage')
  649. ->with('OCA\Files_Sharing\External\Storage')
  650. ->willReturn(false);
  651. $path->method('getStorage')->willReturn($storage);
  652. $userFolder->expects($this->once())
  653. ->method('get')
  654. ->with('valid-path')
  655. ->willReturn($path);
  656. $path->expects($this->once())
  657. ->method('lock')
  658. ->with(\OCP\Lock\ILockingProvider::LOCK_SHARED);
  659. $this->ocs->createShare('valid-path', \OCP\Constants::PERMISSION_ALL, \OCP\Share::SHARE_TYPE_USER);
  660. }
  661. /**
  662. * @expectedException \OCP\AppFramework\OCS\OCSNotFoundException
  663. * @expectedExceptionMessage Please specify a valid user
  664. */
  665. public function testCreateShareUserNoValidShareWith() {
  666. $share = $this->newShare();
  667. $this->shareManager->method('newShare')->willReturn($share);
  668. $userFolder = $this->getMockBuilder(Folder::class)->getMock();
  669. $this->rootFolder->expects($this->once())
  670. ->method('getUserFolder')
  671. ->with('currentUser')
  672. ->willReturn($userFolder);
  673. $path = $this->getMockBuilder(File::class)->getMock();
  674. $storage = $this->getMockBuilder(Storage::class)->getMock();
  675. $storage->method('instanceOfStorage')
  676. ->with('OCA\Files_Sharing\External\Storage')
  677. ->willReturn(false);
  678. $path->method('getStorage')->willReturn($storage);
  679. $userFolder->expects($this->once())
  680. ->method('get')
  681. ->with('valid-path')
  682. ->willReturn($path);
  683. $path->expects($this->once())
  684. ->method('lock')
  685. ->with(\OCP\Lock\ILockingProvider::LOCK_SHARED);
  686. $this->userManager->method('userExists')
  687. ->with('invalidUser')
  688. ->willReturn(false);
  689. $this->ocs->createShare('valid-path', \OCP\Constants::PERMISSION_ALL, \OCP\Share::SHARE_TYPE_USER, 'invalidUser');
  690. }
  691. public function testCreateShareUser() {
  692. $share = $this->newShare();
  693. $this->shareManager->method('newShare')->willReturn($share);
  694. /** @var \OCA\Files_Sharing\Controller\ShareAPIController $ocs */
  695. $ocs = $this->getMockBuilder(ShareAPIController::class)
  696. ->setConstructorArgs([
  697. $this->appName,
  698. $this->request,
  699. $this->shareManager,
  700. $this->groupManager,
  701. $this->userManager,
  702. $this->rootFolder,
  703. $this->urlGenerator,
  704. $this->currentUser,
  705. $this->l,
  706. $this->config,
  707. $this->appManager,
  708. $this->serverContainer
  709. ])->setMethods(['formatShare'])
  710. ->getMock();
  711. $userFolder = $this->getMockBuilder(Folder::class)->getMock();
  712. $this->rootFolder->expects($this->once())
  713. ->method('getUserFolder')
  714. ->with('currentUser')
  715. ->willReturn($userFolder);
  716. $path = $this->getMockBuilder(File::class)->getMock();
  717. $storage = $this->getMockBuilder(Storage::class)->getMock();
  718. $storage->method('instanceOfStorage')
  719. ->with('OCA\Files_Sharing\External\Storage')
  720. ->willReturn(false);
  721. $path->method('getStorage')->willReturn($storage);
  722. $userFolder->expects($this->once())
  723. ->method('get')
  724. ->with('valid-path')
  725. ->willReturn($path);
  726. $this->userManager->method('userExists')->with('validUser')->willReturn(true);
  727. $path->expects($this->once())
  728. ->method('lock')
  729. ->with(\OCP\Lock\ILockingProvider::LOCK_SHARED);
  730. $this->shareManager->method('createShare')
  731. ->with($this->callback(function (\OCP\Share\IShare $share) use ($path) {
  732. return $share->getNode() === $path &&
  733. $share->getPermissions() === (
  734. \OCP\Constants::PERMISSION_ALL &
  735. ~\OCP\Constants::PERMISSION_DELETE &
  736. ~\OCP\Constants::PERMISSION_CREATE
  737. ) &&
  738. $share->getShareType() === \OCP\Share::SHARE_TYPE_USER &&
  739. $share->getSharedWith() === 'validUser' &&
  740. $share->getSharedBy() === 'currentUser';
  741. }))
  742. ->will($this->returnArgument(0));
  743. $expected = new DataResponse([]);
  744. $result = $ocs->createShare('valid-path', \OCP\Constants::PERMISSION_ALL, \OCP\Share::SHARE_TYPE_USER, 'validUser');
  745. $this->assertInstanceOf(get_class($expected), $result);
  746. $this->assertEquals($expected->getData(), $result->getData());
  747. }
  748. /**
  749. * @expectedException \OCP\AppFramework\OCS\OCSNotFoundException
  750. * @expectedExceptionMessage Please specify a valid group
  751. */
  752. public function testCreateShareGroupNoValidShareWith() {
  753. $share = $this->newShare();
  754. $this->shareManager->method('newShare')->willReturn($share);
  755. $this->shareManager->method('createShare')->will($this->returnArgument(0));
  756. $this->shareManager->method('allowGroupSharing')->willReturn(true);
  757. $userFolder = $this->getMockBuilder(Folder::class)->getMock();
  758. $this->rootFolder->expects($this->once())
  759. ->method('getUserFolder')
  760. ->with('currentUser')
  761. ->willReturn($userFolder);
  762. $path = $this->getMockBuilder(File::class)->getMock();
  763. $storage = $this->getMockBuilder(Storage::class)->getMock();
  764. $storage->method('instanceOfStorage')
  765. ->with('OCA\Files_Sharing\External\Storage')
  766. ->willReturn(false);
  767. $path->method('getStorage')->willReturn($storage);
  768. $userFolder->expects($this->once())
  769. ->method('get')
  770. ->with('valid-path')
  771. ->willReturn($path);
  772. $path->expects($this->once())
  773. ->method('lock')
  774. ->with(\OCP\Lock\ILockingProvider::LOCK_SHARED);
  775. $this->ocs->createShare('valid-path', \OCP\Constants::PERMISSION_ALL, \OCP\Share::SHARE_TYPE_GROUP, 'invalidGroup');
  776. }
  777. public function testCreateShareGroup() {
  778. $share = $this->newShare();
  779. $this->shareManager->method('newShare')->willReturn($share);
  780. /** @var ShareAPIController|\PHPUnit_Framework_MockObject_MockObject $ocs */
  781. $ocs = $this->getMockBuilder(ShareAPIController::class)
  782. ->setConstructorArgs([
  783. $this->appName,
  784. $this->request,
  785. $this->shareManager,
  786. $this->groupManager,
  787. $this->userManager,
  788. $this->rootFolder,
  789. $this->urlGenerator,
  790. $this->currentUser,
  791. $this->l,
  792. $this->config,
  793. $this->appManager,
  794. $this->serverContainer
  795. ])->setMethods(['formatShare'])
  796. ->getMock();
  797. $this->request
  798. ->method('getParam')
  799. ->will($this->returnValueMap([
  800. ['path', null, 'valid-path'],
  801. ['permissions', null, \OCP\Constants::PERMISSION_ALL],
  802. ['shareType', '-1', \OCP\Share::SHARE_TYPE_GROUP],
  803. ['shareWith', null, 'validGroup'],
  804. ]));
  805. $userFolder = $this->getMockBuilder(Folder::class)->getMock();
  806. $this->rootFolder->expects($this->once())
  807. ->method('getUserFolder')
  808. ->with('currentUser')
  809. ->willReturn($userFolder);
  810. $path = $this->getMockBuilder(Folder::class)->getMock();
  811. $storage = $this->getMockBuilder(Storage::class)->getMock();
  812. $storage->method('instanceOfStorage')
  813. ->with('OCA\Files_Sharing\External\Storage')
  814. ->willReturn(false);
  815. $path->method('getStorage')->willReturn($storage);
  816. $userFolder->expects($this->once())
  817. ->method('get')
  818. ->with('valid-path')
  819. ->willReturn($path);
  820. $this->groupManager->method('groupExists')->with('validGroup')->willReturn(true);
  821. $this->shareManager->expects($this->once())
  822. ->method('allowGroupSharing')
  823. ->willReturn(true);
  824. $path->expects($this->once())
  825. ->method('lock')
  826. ->with(\OCP\Lock\ILockingProvider::LOCK_SHARED);
  827. $this->shareManager->method('createShare')
  828. ->with($this->callback(function (\OCP\Share\IShare $share) use ($path) {
  829. return $share->getNode() === $path &&
  830. $share->getPermissions() === \OCP\Constants::PERMISSION_ALL &&
  831. $share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP &&
  832. $share->getSharedWith() === 'validGroup' &&
  833. $share->getSharedBy() === 'currentUser';
  834. }))
  835. ->will($this->returnArgument(0));
  836. $expected = new DataResponse([]);
  837. $result = $ocs->createShare('valid-path', \OCP\Constants::PERMISSION_ALL, \OCP\Share::SHARE_TYPE_GROUP, 'validGroup');
  838. $this->assertInstanceOf(get_class($expected), $result);
  839. $this->assertEquals($expected->getData(), $result->getData());
  840. }
  841. /**
  842. * @expectedException \OCP\AppFramework\OCS\OCSNotFoundException
  843. * @expectedExceptionMessage Group sharing is disabled by the administrator
  844. */
  845. public function testCreateShareGroupNotAllowed() {
  846. $share = $this->newShare();
  847. $this->shareManager->method('newShare')->willReturn($share);
  848. $userFolder = $this->getMockBuilder(Folder::class)->getMock();
  849. $this->rootFolder->expects($this->once())
  850. ->method('getUserFolder')
  851. ->with('currentUser')
  852. ->willReturn($userFolder);
  853. $path = $this->getMockBuilder(Folder::class)->getMock();
  854. $storage = $this->getMockBuilder(Storage::class)->getMock();
  855. $storage->method('instanceOfStorage')
  856. ->with('OCA\Files_Sharing\External\Storage')
  857. ->willReturn(false);
  858. $path->method('getStorage')->willReturn($storage);
  859. $userFolder->expects($this->once())
  860. ->method('get')
  861. ->with('valid-path')
  862. ->willReturn($path);
  863. $this->groupManager->method('groupExists')->with('validGroup')->willReturn(true);
  864. $this->shareManager->expects($this->once())
  865. ->method('allowGroupSharing')
  866. ->willReturn(false);
  867. $this->ocs->createShare('valid-path', \OCP\Constants::PERMISSION_ALL, \OCP\Share::SHARE_TYPE_GROUP, 'invalidGroup');
  868. }
  869. /**
  870. * @expectedException \OCP\AppFramework\OCS\OCSNotFoundException
  871. * @expectedExceptionMessage Public link sharing is disabled by the administrator
  872. */
  873. public function testCreateShareLinkNoLinksAllowed() {
  874. $this->request
  875. ->method('getParam')
  876. ->will($this->returnValueMap([
  877. ['path', null, 'valid-path'],
  878. ['shareType', '-1', \OCP\Share::SHARE_TYPE_LINK],
  879. ]));
  880. $path = $this->getMockBuilder(Folder::class)->getMock();
  881. $storage = $this->getMockBuilder(Storage::class)->getMock();
  882. $storage->method('instanceOfStorage')
  883. ->with('OCA\Files_Sharing\External\Storage')
  884. ->willReturn(false);
  885. $path->method('getStorage')->willReturn($storage);
  886. $this->rootFolder->method('getUserFolder')->with($this->currentUser)->will($this->returnSelf());
  887. $this->rootFolder->method('get')->with('valid-path')->willReturn($path);
  888. $this->shareManager->method('newShare')->willReturn(\OC::$server->getShareManager()->newShare());
  889. $this->ocs->createShare('valid-path', \OCP\Constants::PERMISSION_ALL, \OCP\Share::SHARE_TYPE_LINK);
  890. }
  891. /**
  892. * @expectedException \OCP\AppFramework\OCS\OCSForbiddenException
  893. * @expectedExceptionMessage Public upload disabled by the administrator
  894. */
  895. public function testCreateShareLinkNoPublicUpload() {
  896. $path = $this->getMockBuilder(Folder::class)->getMock();
  897. $storage = $this->getMockBuilder(Storage::class)->getMock();
  898. $storage->method('instanceOfStorage')
  899. ->with('OCA\Files_Sharing\External\Storage')
  900. ->willReturn(false);
  901. $path->method('getStorage')->willReturn($storage);
  902. $this->rootFolder->method('getUserFolder')->with($this->currentUser)->will($this->returnSelf());
  903. $this->rootFolder->method('get')->with('valid-path')->willReturn($path);
  904. $this->shareManager->method('newShare')->willReturn(\OC::$server->getShareManager()->newShare());
  905. $this->shareManager->method('shareApiAllowLinks')->willReturn(true);
  906. $this->ocs->createShare('valid-path', \OCP\Constants::PERMISSION_ALL, \OCP\Share::SHARE_TYPE_LINK, null, 'true');
  907. }
  908. /**
  909. * @expectedException \OCP\AppFramework\OCS\OCSNotFoundException
  910. * @expectedExceptionMessage Public upload is only possible for publicly shared folders
  911. */
  912. public function testCreateShareLinkPublicUploadFile() {
  913. $path = $this->getMockBuilder(File::class)->getMock();
  914. $storage = $this->getMockBuilder(Storage::class)->getMock();
  915. $storage->method('instanceOfStorage')
  916. ->with('OCA\Files_Sharing\External\Storage')
  917. ->willReturn(false);
  918. $path->method('getStorage')->willReturn($storage);
  919. $this->rootFolder->method('getUserFolder')->with($this->currentUser)->will($this->returnSelf());
  920. $this->rootFolder->method('get')->with('valid-path')->willReturn($path);
  921. $this->shareManager->method('newShare')->willReturn(\OC::$server->getShareManager()->newShare());
  922. $this->shareManager->method('shareApiAllowLinks')->willReturn(true);
  923. $this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true);
  924. $this->ocs->createShare('valid-path', \OCP\Constants::PERMISSION_ALL, \OCP\Share::SHARE_TYPE_LINK, null, 'true');
  925. }
  926. public function testCreateShareLinkPublicUploadFolder() {
  927. $ocs = $this->mockFormatShare();
  928. $path = $this->getMockBuilder(Folder::class)->getMock();
  929. $storage = $this->getMockBuilder(Storage::class)->getMock();
  930. $storage->method('instanceOfStorage')
  931. ->with('OCA\Files_Sharing\External\Storage')
  932. ->willReturn(false);
  933. $path->method('getStorage')->willReturn($storage);
  934. $this->rootFolder->method('getUserFolder')->with($this->currentUser)->will($this->returnSelf());
  935. $this->rootFolder->method('get')->with('valid-path')->willReturn($path);
  936. $this->shareManager->method('newShare')->willReturn(\OC::$server->getShareManager()->newShare());
  937. $this->shareManager->method('shareApiAllowLinks')->willReturn(true);
  938. $this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true);
  939. $this->shareManager->expects($this->once())->method('createShare')->with(
  940. $this->callback(function (\OCP\Share\IShare $share) use ($path) {
  941. return $share->getNode() === $path &&
  942. $share->getShareType() === \OCP\Share::SHARE_TYPE_LINK &&
  943. $share->getPermissions() === (\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE) &&
  944. $share->getSharedBy() === 'currentUser' &&
  945. $share->getPassword() === null &&
  946. $share->getExpirationDate() === null;
  947. })
  948. )->will($this->returnArgument(0));
  949. $expected = new DataResponse([]);
  950. $result = $ocs->createShare('valid-path', \OCP\Constants::PERMISSION_ALL, \OCP\Share::SHARE_TYPE_LINK, null, 'true', '', null, '');
  951. $this->assertInstanceOf(get_class($expected), $result);
  952. $this->assertEquals($expected->getData(), $result->getData());
  953. }
  954. public function testCreateShareLinkPassword() {
  955. $ocs = $this->mockFormatShare();
  956. $path = $this->getMockBuilder(Folder::class)->getMock();
  957. $storage = $this->getMockBuilder(Storage::class)->getMock();
  958. $storage->method('instanceOfStorage')
  959. ->with('OCA\Files_Sharing\External\Storage')
  960. ->willReturn(false);
  961. $path->method('getStorage')->willReturn($storage);
  962. $this->rootFolder->method('getUserFolder')->with($this->currentUser)->will($this->returnSelf());
  963. $this->rootFolder->method('get')->with('valid-path')->willReturn($path);
  964. $this->shareManager->method('newShare')->willReturn(\OC::$server->getShareManager()->newShare());
  965. $this->shareManager->method('shareApiAllowLinks')->willReturn(true);
  966. $this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true);
  967. $this->shareManager->expects($this->once())->method('createShare')->with(
  968. $this->callback(function (\OCP\Share\IShare $share) use ($path) {
  969. return $share->getNode() === $path &&
  970. $share->getShareType() === \OCP\Share::SHARE_TYPE_LINK &&
  971. $share->getPermissions() === \OCP\Constants::PERMISSION_READ &&
  972. $share->getSharedBy() === 'currentUser' &&
  973. $share->getPassword() === 'password' &&
  974. $share->getExpirationDate() === null;
  975. })
  976. )->will($this->returnArgument(0));
  977. $expected = new DataResponse([]);
  978. $result = $ocs->createShare('valid-path', \OCP\Constants::PERMISSION_ALL, \OCP\Share::SHARE_TYPE_LINK, null, 'false', 'password', null, '');
  979. $this->assertInstanceOf(get_class($expected), $result);
  980. $this->assertEquals($expected->getData(), $result->getData());
  981. }
  982. public function testCreateShareLinkSendPasswordByTalk() {
  983. $ocs = $this->mockFormatShare();
  984. $path = $this->getMockBuilder(Folder::class)->getMock();
  985. $storage = $this->getMockBuilder(Storage::class)->getMock();
  986. $storage->method('instanceOfStorage')
  987. ->with('OCA\Files_Sharing\External\Storage')
  988. ->willReturn(false);
  989. $path->method('getStorage')->willReturn($storage);
  990. $this->rootFolder->method('getUserFolder')->with($this->currentUser)->will($this->returnSelf());
  991. $this->rootFolder->method('get')->with('valid-path')->willReturn($path);
  992. $this->shareManager->method('newShare')->willReturn(\OC::$server->getShareManager()->newShare());
  993. $this->shareManager->method('shareApiAllowLinks')->willReturn(true);
  994. $this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true);
  995. $this->appManager->method('isEnabledForUser')->with('spreed')->willReturn(true);
  996. $this->shareManager->expects($this->once())->method('createShare')->with(
  997. $this->callback(function (\OCP\Share\IShare $share) use ($path) {
  998. return $share->getNode() === $path &&
  999. $share->getShareType() === \OCP\Share::SHARE_TYPE_LINK &&
  1000. $share->getPermissions() === \OCP\Constants::PERMISSION_READ &&
  1001. $share->getSharedBy() === 'currentUser' &&
  1002. $share->getPassword() === 'password' &&
  1003. $share->getSendPasswordByTalk() === true &&
  1004. $share->getExpirationDate() === null;
  1005. })
  1006. )->will($this->returnArgument(0));
  1007. $expected = new DataResponse([]);
  1008. $result = $ocs->createShare('valid-path', \OCP\Constants::PERMISSION_ALL, \OCP\Share::SHARE_TYPE_LINK, null, 'false', 'password', 'true', '');
  1009. $this->assertInstanceOf(get_class($expected), $result);
  1010. $this->assertEquals($expected->getData(), $result->getData());
  1011. }
  1012. /**
  1013. * @expectedException \OCP\AppFramework\OCS\OCSForbiddenException
  1014. * @expectedExceptionMessage Sharing valid-path sending the password by Nextcloud Talk failed because Nextcloud Talk is not enabled
  1015. */
  1016. public function testCreateShareLinkSendPasswordByTalkWithTalkDisabled() {
  1017. $ocs = $this->mockFormatShare();
  1018. $path = $this->getMockBuilder(Folder::class)->getMock();
  1019. $storage = $this->getMockBuilder(Storage::class)->getMock();
  1020. $storage->method('instanceOfStorage')
  1021. ->with('OCA\Files_Sharing\External\Storage')
  1022. ->willReturn(false);
  1023. $path->method('getStorage')->willReturn($storage);
  1024. $path->method('getPath')->willReturn('valid-path');
  1025. $this->rootFolder->method('getUserFolder')->with($this->currentUser)->will($this->returnSelf());
  1026. $this->rootFolder->method('get')->with('valid-path')->willReturn($path);
  1027. $this->shareManager->method('newShare')->willReturn(\OC::$server->getShareManager()->newShare());
  1028. $this->shareManager->method('shareApiAllowLinks')->willReturn(true);
  1029. $this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true);
  1030. $this->appManager->method('isEnabledForUser')->with('spreed')->willReturn(false);
  1031. $this->shareManager->expects($this->never())->method('createShare');
  1032. $ocs->createShare('valid-path', \OCP\Constants::PERMISSION_ALL, \OCP\Share::SHARE_TYPE_LINK, null, 'false', 'password', 'true', '');
  1033. }
  1034. public function testCreateShareValidExpireDate() {
  1035. $ocs = $this->mockFormatShare();
  1036. $this->request
  1037. ->method('getParam')
  1038. ->will($this->returnValueMap([
  1039. ['path', null, 'valid-path'],
  1040. ['shareType', '-1', \OCP\Share::SHARE_TYPE_LINK],
  1041. ['publicUpload', null, 'false'],
  1042. ['expireDate', '', '2000-01-01'],
  1043. ['password', '', ''],
  1044. ]));
  1045. $path = $this->getMockBuilder(Folder::class)->getMock();
  1046. $storage = $this->getMockBuilder(Storage::class)->getMock();
  1047. $storage->method('instanceOfStorage')
  1048. ->with('OCA\Files_Sharing\External\Storage')
  1049. ->willReturn(false);
  1050. $path->method('getStorage')->willReturn($storage);
  1051. $this->rootFolder->method('getUserFolder')->with($this->currentUser)->will($this->returnSelf());
  1052. $this->rootFolder->method('get')->with('valid-path')->willReturn($path);
  1053. $this->shareManager->method('newShare')->willReturn(\OC::$server->getShareManager()->newShare());
  1054. $this->shareManager->method('shareApiAllowLinks')->willReturn(true);
  1055. $this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true);
  1056. $this->shareManager->expects($this->once())->method('createShare')->with(
  1057. $this->callback(function (\OCP\Share\IShare $share) use ($path) {
  1058. $date = new \DateTime('2000-01-01');
  1059. $date->setTime(0,0,0);
  1060. return $share->getNode() === $path &&
  1061. $share->getShareType() === \OCP\Share::SHARE_TYPE_LINK &&
  1062. $share->getPermissions() === \OCP\Constants::PERMISSION_READ &&
  1063. $share->getSharedBy() === 'currentUser' &&
  1064. $share->getPassword() === null &&
  1065. $share->getExpirationDate() == $date;
  1066. })
  1067. )->will($this->returnArgument(0));
  1068. $expected = new DataResponse([]);
  1069. $result = $ocs->createShare('valid-path', \OCP\Constants::PERMISSION_ALL, \OCP\Share::SHARE_TYPE_LINK, null, 'false', '', null, '2000-01-01');
  1070. $this->assertInstanceOf(get_class($expected), $result);
  1071. $this->assertEquals($expected->getData(), $result->getData());
  1072. }
  1073. /**
  1074. * @expectedException \OCP\AppFramework\OCS\OCSNotFoundException
  1075. * @expectedExceptionMessage Invalid date, date format must be YYYY-MM-DD
  1076. */
  1077. public function testCreateShareInvalidExpireDate() {
  1078. $ocs = $this->mockFormatShare();
  1079. $path = $this->getMockBuilder(Folder::class)->getMock();
  1080. $storage = $this->getMockBuilder(Storage::class)->getMock();
  1081. $storage->method('instanceOfStorage')
  1082. ->with('OCA\Files_Sharing\External\Storage')
  1083. ->willReturn(false);
  1084. $path->method('getStorage')->willReturn($storage);
  1085. $this->rootFolder->method('getUserFolder')->with($this->currentUser)->will($this->returnSelf());
  1086. $this->rootFolder->method('get')->with('valid-path')->willReturn($path);
  1087. $this->shareManager->method('newShare')->willReturn(\OC::$server->getShareManager()->newShare());
  1088. $this->shareManager->method('shareApiAllowLinks')->willReturn(true);
  1089. $this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true);
  1090. $ocs->createShare('valid-path', \OCP\Constants::PERMISSION_ALL, \OCP\Share::SHARE_TYPE_LINK, null, 'false', '', null, 'a1b2d3');
  1091. }
  1092. public function testCreateShareRoom() {
  1093. $ocs = $this->mockFormatShare();
  1094. $share = $this->newShare();
  1095. $this->shareManager->method('newShare')->willReturn($share);
  1096. $userFolder = $this->getMockBuilder(Folder::class)->getMock();
  1097. $this->rootFolder->expects($this->once())
  1098. ->method('getUserFolder')
  1099. ->with('currentUser')
  1100. ->willReturn($userFolder);
  1101. $path = $this->getMockBuilder(File::class)->getMock();
  1102. $storage = $this->getMockBuilder(Storage::class)->getMock();
  1103. $storage->method('instanceOfStorage')
  1104. ->with('OCA\Files_Sharing\External\Storage')
  1105. ->willReturn(false);
  1106. $path->method('getStorage')->willReturn($storage);
  1107. $userFolder->expects($this->once())
  1108. ->method('get')
  1109. ->with('valid-path')
  1110. ->willReturn($path);
  1111. $path->expects($this->once())
  1112. ->method('lock')
  1113. ->with(\OCP\Lock\ILockingProvider::LOCK_SHARED);
  1114. $this->appManager->method('isEnabledForUser')
  1115. ->with('spreed')
  1116. ->willReturn(true);
  1117. $helper = $this->getMockBuilder('\OCA\Spreed\Share\Helper\ShareAPIController')
  1118. ->setMethods(array('createShare'))
  1119. ->getMock();
  1120. $helper->method('createShare')
  1121. ->with(
  1122. $share,
  1123. 'recipientRoom',
  1124. \OCP\Constants::PERMISSION_ALL &
  1125. ~\OCP\Constants::PERMISSION_DELETE &
  1126. ~\OCP\Constants::PERMISSION_CREATE,
  1127. ''
  1128. )->will($this->returnCallback(
  1129. function ($share) {
  1130. $share->setSharedWith('recipientRoom');
  1131. $share->setPermissions(
  1132. \OCP\Constants::PERMISSION_ALL &
  1133. ~\OCP\Constants::PERMISSION_DELETE &
  1134. ~\OCP\Constants::PERMISSION_CREATE
  1135. );
  1136. }
  1137. ));
  1138. $this->serverContainer->method('query')
  1139. ->with('\OCA\Spreed\Share\Helper\ShareAPIController')
  1140. ->willReturn($helper);
  1141. $this->shareManager->method('createShare')
  1142. ->with($this->callback(function (\OCP\Share\IShare $share) use ($path) {
  1143. return $share->getNode() === $path &&
  1144. $share->getPermissions() === (
  1145. \OCP\Constants::PERMISSION_ALL &
  1146. ~\OCP\Constants::PERMISSION_DELETE &
  1147. ~\OCP\Constants::PERMISSION_CREATE
  1148. ) &&
  1149. $share->getShareType() === \OCP\Share::SHARE_TYPE_ROOM &&
  1150. $share->getSharedWith() === 'recipientRoom' &&
  1151. $share->getSharedBy() === 'currentUser';
  1152. }))
  1153. ->will($this->returnArgument(0));
  1154. $expected = new DataResponse([]);
  1155. $result = $ocs->createShare('valid-path', \OCP\Constants::PERMISSION_ALL, \OCP\Share::SHARE_TYPE_ROOM, 'recipientRoom');
  1156. $this->assertInstanceOf(get_class($expected), $result);
  1157. $this->assertEquals($expected->getData(), $result->getData());
  1158. }
  1159. /**
  1160. * @expectedException \OCP\AppFramework\OCS\OCSForbiddenException
  1161. * @expectedExceptionMessage Sharing valid-path failed because the back end does not support room shares
  1162. */
  1163. public function testCreateShareRoomHelperNotAvailable() {
  1164. $ocs = $this->mockFormatShare();
  1165. $share = $this->newShare();
  1166. $this->shareManager->method('newShare')->willReturn($share);
  1167. $userFolder = $this->getMockBuilder(Folder::class)->getMock();
  1168. $this->rootFolder->expects($this->once())
  1169. ->method('getUserFolder')
  1170. ->with('currentUser')
  1171. ->willReturn($userFolder);
  1172. $path = $this->getMockBuilder(File::class)->getMock();
  1173. $storage = $this->getMockBuilder(Storage::class)->getMock();
  1174. $storage->method('instanceOfStorage')
  1175. ->with('OCA\Files_Sharing\External\Storage')
  1176. ->willReturn(false);
  1177. $path->method('getStorage')->willReturn($storage);
  1178. $path->method('getPath')->willReturn('valid-path');
  1179. $userFolder->expects($this->once())
  1180. ->method('get')
  1181. ->with('valid-path')
  1182. ->willReturn($path);
  1183. $path->expects($this->once())
  1184. ->method('lock')
  1185. ->with(\OCP\Lock\ILockingProvider::LOCK_SHARED);
  1186. $this->appManager->method('isEnabledForUser')
  1187. ->with('spreed')
  1188. ->willReturn(false);
  1189. $this->shareManager->expects($this->never())->method('createShare');
  1190. $ocs->createShare('valid-path', \OCP\Constants::PERMISSION_ALL, \OCP\Share::SHARE_TYPE_ROOM, 'recipientRoom');
  1191. }
  1192. /**
  1193. * @expectedException \OCP\AppFramework\OCS\OCSNotFoundException
  1194. * @expectedExceptionMessage Exception thrown by the helper
  1195. */
  1196. public function testCreateShareRoomHelperThrowException() {
  1197. $ocs = $this->mockFormatShare();
  1198. $share = $this->newShare();
  1199. $this->shareManager->method('newShare')->willReturn($share);
  1200. $userFolder = $this->getMockBuilder(Folder::class)->getMock();
  1201. $this->rootFolder->expects($this->once())
  1202. ->method('getUserFolder')
  1203. ->with('currentUser')
  1204. ->willReturn($userFolder);
  1205. $path = $this->getMockBuilder(File::class)->getMock();
  1206. $storage = $this->getMockBuilder(Storage::class)->getMock();
  1207. $storage->method('instanceOfStorage')
  1208. ->with('OCA\Files_Sharing\External\Storage')
  1209. ->willReturn(false);
  1210. $path->method('getStorage')->willReturn($storage);
  1211. $userFolder->expects($this->once())
  1212. ->method('get')
  1213. ->with('valid-path')
  1214. ->willReturn($path);
  1215. $path->expects($this->once())
  1216. ->method('lock')
  1217. ->with(\OCP\Lock\ILockingProvider::LOCK_SHARED);
  1218. $this->appManager->method('isEnabledForUser')
  1219. ->with('spreed')
  1220. ->willReturn(true);
  1221. $helper = $this->getMockBuilder('\OCA\Spreed\Share\Helper\ShareAPIController')
  1222. ->setMethods(array('createShare'))
  1223. ->getMock();
  1224. $helper->method('createShare')
  1225. ->with(
  1226. $share,
  1227. 'recipientRoom',
  1228. \OCP\Constants::PERMISSION_ALL &
  1229. ~\OCP\Constants::PERMISSION_DELETE &
  1230. ~\OCP\Constants::PERMISSION_CREATE,
  1231. ''
  1232. )->will($this->returnCallback(
  1233. function ($share) {
  1234. throw new OCSNotFoundException("Exception thrown by the helper");
  1235. }
  1236. ));
  1237. $this->serverContainer->method('query')
  1238. ->with('\OCA\Spreed\Share\Helper\ShareAPIController')
  1239. ->willReturn($helper);
  1240. $this->shareManager->expects($this->never())->method('createShare');
  1241. $ocs->createShare('valid-path', \OCP\Constants::PERMISSION_ALL, \OCP\Share::SHARE_TYPE_ROOM, 'recipientRoom');
  1242. }
  1243. /**
  1244. * Test for https://github.com/owncloud/core/issues/22587
  1245. * TODO: Remove once proper solution is in place
  1246. */
  1247. public function testCreateReshareOfFederatedMountNoDeletePermissions() {
  1248. $share = \OC::$server->getShareManager()->newShare();
  1249. $this->shareManager->method('newShare')->willReturn($share);
  1250. /** @var ShareAPIController|\PHPUnit_Framework_MockObject_MockObject $ocs */
  1251. $ocs = $this->getMockBuilder(ShareAPIController::class)
  1252. ->setConstructorArgs([
  1253. $this->appName,
  1254. $this->request,
  1255. $this->shareManager,
  1256. $this->groupManager,
  1257. $this->userManager,
  1258. $this->rootFolder,
  1259. $this->urlGenerator,
  1260. $this->currentUser,
  1261. $this->l,
  1262. $this->config,
  1263. $this->appManager,
  1264. $this->serverContainer
  1265. ])->setMethods(['formatShare'])
  1266. ->getMock();
  1267. $userFolder = $this->getMockBuilder(Folder::class)->getMock();
  1268. $this->rootFolder->expects($this->once())
  1269. ->method('getUserFolder')
  1270. ->with('currentUser')
  1271. ->willReturn($userFolder);
  1272. $path = $this->getMockBuilder(Folder::class)->getMock();
  1273. $storage = $this->getMockBuilder(Storage::class)->getMock();
  1274. $storage->method('instanceOfStorage')
  1275. ->with('OCA\Files_Sharing\External\Storage')
  1276. ->willReturn(true);
  1277. $path->method('getStorage')->willReturn($storage);
  1278. $path->method('getPermissions')->willReturn(\OCP\Constants::PERMISSION_READ);
  1279. $userFolder->expects($this->once())
  1280. ->method('get')
  1281. ->with('valid-path')
  1282. ->willReturn($path);
  1283. $this->userManager->method('userExists')->with('validUser')->willReturn(true);
  1284. $this->shareManager
  1285. ->expects($this->once())
  1286. ->method('createShare')
  1287. ->with($this->callback(function (\OCP\Share\IShare $share) {
  1288. return $share->getPermissions() === \OCP\Constants::PERMISSION_READ;
  1289. }))
  1290. ->will($this->returnArgument(0));
  1291. $ocs->createShare('valid-path', \OCP\Constants::PERMISSION_ALL, \OCP\Share::SHARE_TYPE_USER, 'validUser');
  1292. }
  1293. /**
  1294. * @expectedException \OCP\AppFramework\OCS\OCSNotFoundException
  1295. * @expectedExceptionMessage Wrong share ID, share doesn't exist
  1296. */
  1297. public function testUpdateShareCantAccess() {
  1298. $node = $this->getMockBuilder(Folder::class)->getMock();
  1299. $share = $this->newShare();
  1300. $share->setNode($node);
  1301. $node->expects($this->once())
  1302. ->method('lock')
  1303. ->with(\OCP\Lock\ILockingProvider::LOCK_SHARED);
  1304. $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
  1305. $this->ocs->updateShare(42);
  1306. }
  1307. /**
  1308. * @expectedException \OCP\AppFramework\OCS\OCSBadRequestException
  1309. * @expectedExceptionMessage Wrong or no update parameter given
  1310. */
  1311. public function testUpdateNoParametersLink() {
  1312. $node = $this->getMockBuilder(Folder::class)->getMock();
  1313. $share = $this->newShare();
  1314. $share->setPermissions(\OCP\Constants::PERMISSION_ALL)
  1315. ->setSharedBy($this->currentUser)
  1316. ->setShareType(\OCP\Share::SHARE_TYPE_LINK)
  1317. ->setNode($node);
  1318. $node->expects($this->once())
  1319. ->method('lock')
  1320. ->with(\OCP\Lock\ILockingProvider::LOCK_SHARED);
  1321. $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
  1322. $this->ocs->updateShare(42);
  1323. }
  1324. /**
  1325. * @expectedException \OCP\AppFramework\OCS\OCSBadRequestException
  1326. * @expectedExceptionMessage Wrong or no update parameter given
  1327. */
  1328. public function testUpdateNoParametersOther() {
  1329. $node = $this->getMockBuilder(Folder::class)->getMock();
  1330. $share = $this->newShare();
  1331. $share->setPermissions(\OCP\Constants::PERMISSION_ALL)
  1332. ->setSharedBy($this->currentUser)
  1333. ->setShareType(\OCP\Share::SHARE_TYPE_GROUP)
  1334. ->setNode($node);
  1335. $node->expects($this->once())
  1336. ->method('lock')
  1337. ->with(\OCP\Lock\ILockingProvider::LOCK_SHARED);
  1338. $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
  1339. $this->ocs->updateShare(42);
  1340. }
  1341. public function testUpdateLinkShareClear() {
  1342. $ocs = $this->mockFormatShare();
  1343. $node = $this->getMockBuilder(Folder::class)->getMock();
  1344. $node->method('getId')
  1345. ->willReturn(42);
  1346. $share = $this->newShare();
  1347. $share->setPermissions(\OCP\Constants::PERMISSION_ALL)
  1348. ->setSharedBy($this->currentUser)
  1349. ->setShareType(\OCP\Share::SHARE_TYPE_LINK)
  1350. ->setPassword('password')
  1351. ->setExpirationDate(new \DateTime())
  1352. ->setNote('note')
  1353. ->setLabel('label')
  1354. ->setHideDownload(true)
  1355. ->setPermissions(\OCP\Constants::PERMISSION_ALL)
  1356. ->setNode($node);
  1357. $node->expects($this->once())
  1358. ->method('lock')
  1359. ->with(\OCP\Lock\ILockingProvider::LOCK_SHARED);
  1360. $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
  1361. $this->shareManager->expects($this->once())->method('updateShare')->with(
  1362. $this->callback(function (\OCP\Share\IShare $share) {
  1363. return $share->getPermissions() === \OCP\Constants::PERMISSION_READ &&
  1364. $share->getPassword() === null &&
  1365. $share->getExpirationDate() === null &&
  1366. // Once set a note or a label are never back to null, only to an
  1367. // empty string.
  1368. $share->getNote() === '' &&
  1369. $share->getLabel() === '' &&
  1370. $share->getHideDownload() === false;
  1371. })
  1372. )->will($this->returnArgument(0));
  1373. $this->shareManager->method('getSharedWith')
  1374. ->willReturn([]);
  1375. $userFolder = $this->createMock(Folder::class);
  1376. $this->rootFolder->method('getUserFolder')
  1377. ->with($this->currentUser)
  1378. ->willReturn($userFolder);
  1379. $userFolder->method('getById')
  1380. ->with(42)
  1381. ->willReturn([$node]);
  1382. $mountPoint = $this->createMock(IMountPoint::class);
  1383. $node->method('getMountPoint')
  1384. ->willReturn($mountPoint);
  1385. $mountPoint->method('getStorageRootId')
  1386. ->willReturn(42);
  1387. $expected = new DataResponse([]);
  1388. $result = $ocs->updateShare(42, null, '', null, 'false', '', '', '', 'false');
  1389. $this->assertInstanceOf(get_class($expected), $result);
  1390. $this->assertEquals($expected->getData(), $result->getData());
  1391. }
  1392. public function testUpdateLinkShareSet() {
  1393. $ocs = $this->mockFormatShare();
  1394. $folder = $this->getMockBuilder(Folder::class)->getMock();
  1395. $folder->method('getId')
  1396. ->willReturn(42);
  1397. $share = \OC::$server->getShareManager()->newShare();
  1398. $share->setPermissions(\OCP\Constants::PERMISSION_ALL)
  1399. ->setSharedBy($this->currentUser)
  1400. ->setShareType(\OCP\Share::SHARE_TYPE_LINK)
  1401. ->setNode($folder);
  1402. $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
  1403. $this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true);
  1404. $this->shareManager->expects($this->once())->method('updateShare')->with(
  1405. $this->callback(function (\OCP\Share\IShare $share) {
  1406. $date = new \DateTime('2000-01-01');
  1407. $date->setTime(0,0,0);
  1408. return $share->getPermissions() === (\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE) &&
  1409. $share->getPassword() === 'password' &&
  1410. $share->getExpirationDate() == $date &&
  1411. $share->getNote() === 'note' &&
  1412. $share->getLabel() === 'label' &&
  1413. $share->getHideDownload() === true;
  1414. })
  1415. )->will($this->returnArgument(0));
  1416. $this->shareManager->method('getSharedWith')
  1417. ->willReturn([]);
  1418. $userFolder = $this->createMock(Folder::class);
  1419. $this->rootFolder->method('getUserFolder')
  1420. ->with($this->currentUser)
  1421. ->willReturn($userFolder);
  1422. $userFolder->method('getById')
  1423. ->with(42)
  1424. ->willReturn([$folder]);
  1425. $mountPoint = $this->createMock(IMountPoint::class);
  1426. $folder->method('getMountPoint')
  1427. ->willReturn($mountPoint);
  1428. $mountPoint->method('getStorageRootId')
  1429. ->willReturn(42);
  1430. $expected = new DataResponse([]);
  1431. $result = $ocs->updateShare(42, null, 'password', null, 'true', '2000-01-01', 'note', 'label', 'true');
  1432. $this->assertInstanceOf(get_class($expected), $result);
  1433. $this->assertEquals($expected->getData(), $result->getData());
  1434. }
  1435. /**
  1436. * @dataProvider publicUploadParamsProvider
  1437. */
  1438. public function testUpdateLinkShareEnablePublicUpload($permissions, $publicUpload, $expireDate, $password) {
  1439. $ocs = $this->mockFormatShare();
  1440. $folder = $this->getMockBuilder(Folder::class)->getMock();
  1441. $folder->method('getId')
  1442. ->willReturn(42);
  1443. $share = \OC::$server->getShareManager()->newShare();
  1444. $share->setPermissions(\OCP\Constants::PERMISSION_ALL)
  1445. ->setSharedBy($this->currentUser)
  1446. ->setShareType(\OCP\Share::SHARE_TYPE_LINK)
  1447. ->setPassword('password')
  1448. ->setNode($folder);
  1449. $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
  1450. $this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true);
  1451. $this->shareManager->method('getSharedWith')->willReturn([]);
  1452. $this->shareManager->expects($this->once())->method('updateShare')->with(
  1453. $this->callback(function (\OCP\Share\IShare $share) {
  1454. return $share->getPermissions() === (\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE) &&
  1455. $share->getPassword() === 'password' &&
  1456. $share->getExpirationDate() === null;
  1457. })
  1458. )->will($this->returnArgument(0));
  1459. $userFolder = $this->createMock(Folder::class);
  1460. $this->rootFolder->method('getUserFolder')
  1461. ->with($this->currentUser)
  1462. ->willReturn($userFolder);
  1463. $userFolder->method('getById')
  1464. ->with(42)
  1465. ->willReturn([$folder]);
  1466. $mountPoint = $this->createMock(IMountPoint::class);
  1467. $folder->method('getMountPoint')
  1468. ->willReturn($mountPoint);
  1469. $mountPoint->method('getStorageRootId')
  1470. ->willReturn(42);
  1471. $expected = new DataResponse([]);
  1472. $result = $ocs->updateShare(42, $permissions, $password, null, $publicUpload, $expireDate);
  1473. $this->assertInstanceOf(get_class($expected), $result);
  1474. $this->assertEquals($expected->getData(), $result->getData());
  1475. }
  1476. /**
  1477. * @expectedException \OCP\AppFramework\OCS\OCSBadRequestException
  1478. * @expectedExceptionMessage Invalid date. Format must be YYYY-MM-DD
  1479. */
  1480. public function testUpdateLinkShareInvalidDate() {
  1481. $ocs = $this->mockFormatShare();
  1482. $folder = $this->getMockBuilder(Folder::class)->getMock();
  1483. $share = \OC::$server->getShareManager()->newShare();
  1484. $share->setPermissions(\OCP\Constants::PERMISSION_ALL)
  1485. ->setSharedBy($this->currentUser)
  1486. ->setShareType(\OCP\Share::SHARE_TYPE_LINK)
  1487. ->setNode($folder);
  1488. $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
  1489. $this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true);
  1490. $ocs->updateShare(42, null, 'password', null, 'true', '2000-01-a');
  1491. }
  1492. public function publicUploadParamsProvider() {
  1493. return [
  1494. [null, 'true', null, 'password'],
  1495. // legacy had no delete
  1496. [
  1497. \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE,
  1498. null, null, 'password'
  1499. ],
  1500. // correct
  1501. [
  1502. \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE,
  1503. null, null, 'password'
  1504. ],
  1505. ];
  1506. }
  1507. /**
  1508. * @dataProvider publicUploadParamsProvider
  1509. * @expectedException \OCP\AppFramework\OCS\OCSForbiddenException
  1510. * @expectedExceptionMessage Public upload disabled by the administrator
  1511. */
  1512. public function testUpdateLinkSharePublicUploadNotAllowed($permissions, $publicUpload, $expireDate, $password) {
  1513. $ocs = $this->mockFormatShare();
  1514. $folder = $this->getMockBuilder(Folder::class)->getMock();
  1515. $share = \OC::$server->getShareManager()->newShare();
  1516. $share->setPermissions(\OCP\Constants::PERMISSION_ALL)
  1517. ->setSharedBy($this->currentUser)
  1518. ->setShareType(\OCP\Share::SHARE_TYPE_LINK)
  1519. ->setNode($folder);
  1520. $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
  1521. $this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(false);
  1522. $ocs->updateShare(42, $permissions, $password, null, $publicUpload, $expireDate);
  1523. }
  1524. /**
  1525. * @expectedException \OCP\AppFramework\OCS\OCSBadRequestException
  1526. * @expectedExceptionMessage Public upload is only possible for publicly shared folders
  1527. */
  1528. public function testUpdateLinkSharePublicUploadOnFile() {
  1529. $ocs = $this->mockFormatShare();
  1530. $file = $this->getMockBuilder(File::class)->getMock();
  1531. $share = \OC::$server->getShareManager()->newShare();
  1532. $share->setPermissions(\OCP\Constants::PERMISSION_ALL)
  1533. ->setSharedBy($this->currentUser)
  1534. ->setShareType(\OCP\Share::SHARE_TYPE_LINK)
  1535. ->setNode($file);
  1536. $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
  1537. $this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true);
  1538. $ocs->updateShare(42, null, 'password', null, 'true', '');
  1539. }
  1540. public function testUpdateLinkSharePasswordDoesNotChangeOther() {
  1541. $ocs = $this->mockFormatShare();
  1542. $date = new \DateTime('2000-01-01');
  1543. $date->setTime(0,0,0);
  1544. $node = $this->getMockBuilder(File::class)->getMock();
  1545. $share = $this->newShare();
  1546. $share->setPermissions(\OCP\Constants::PERMISSION_ALL)
  1547. ->setSharedBy($this->currentUser)
  1548. ->setShareType(\OCP\Share::SHARE_TYPE_LINK)
  1549. ->setPassword('password')
  1550. ->setSendPasswordByTalk(true)
  1551. ->setExpirationDate($date)
  1552. ->setNote('note')
  1553. ->setLabel('label')
  1554. ->setHideDownload(true)
  1555. ->setPermissions(\OCP\Constants::PERMISSION_ALL)
  1556. ->setNode($node);
  1557. $node->expects($this->once())
  1558. ->method('lock')
  1559. ->with(\OCP\Lock\ILockingProvider::LOCK_SHARED);
  1560. $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
  1561. $this->shareManager->expects($this->once())->method('updateShare')->with(
  1562. $this->callback(function (\OCP\Share\IShare $share) use ($date) {
  1563. return $share->getPermissions() === \OCP\Constants::PERMISSION_ALL &&
  1564. $share->getPassword() === 'newpassword' &&
  1565. $share->getSendPasswordByTalk() === true &&
  1566. $share->getExpirationDate() === $date &&
  1567. $share->getNote() === 'note' &&
  1568. $share->getLabel() === 'label' &&
  1569. $share->getHideDownload() === true;
  1570. })
  1571. )->will($this->returnArgument(0));
  1572. $expected = new DataResponse([]);
  1573. $result = $ocs->updateShare(42, null, 'newpassword', null, null, null, null, null, null);
  1574. $this->assertInstanceOf(get_class($expected), $result);
  1575. $this->assertEquals($expected->getData(), $result->getData());
  1576. }
  1577. public function testUpdateLinkShareSendPasswordByTalkDoesNotChangeOther() {
  1578. $ocs = $this->mockFormatShare();
  1579. $date = new \DateTime('2000-01-01');
  1580. $date->setTime(0,0,0);
  1581. $node = $this->getMockBuilder(File::class)->getMock();
  1582. $share = $this->newShare();
  1583. $share->setPermissions(\OCP\Constants::PERMISSION_ALL)
  1584. ->setSharedBy($this->currentUser)
  1585. ->setShareType(\OCP\Share::SHARE_TYPE_LINK)
  1586. ->setPassword('password')
  1587. ->setSendPasswordByTalk(false)
  1588. ->setExpirationDate($date)
  1589. ->setNote('note')
  1590. ->setLabel('label')
  1591. ->setHideDownload(true)
  1592. ->setPermissions(\OCP\Constants::PERMISSION_ALL)
  1593. ->setNode($node);
  1594. $node->expects($this->once())
  1595. ->method('lock')
  1596. ->with(\OCP\Lock\ILockingProvider::LOCK_SHARED);
  1597. $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
  1598. $this->appManager->method('isEnabledForUser')->with('spreed')->willReturn(true);
  1599. $this->shareManager->expects($this->once())->method('updateShare')->with(
  1600. $this->callback(function (\OCP\Share\IShare $share) use ($date) {
  1601. return $share->getPermissions() === \OCP\Constants::PERMISSION_ALL &&
  1602. $share->getPassword() === 'password' &&
  1603. $share->getSendPasswordByTalk() === true &&
  1604. $share->getExpirationDate() === $date &&
  1605. $share->getNote() === 'note' &&
  1606. $share->getLabel() === 'label' &&
  1607. $share->getHideDownload() === true;
  1608. })
  1609. )->will($this->returnArgument(0));
  1610. $expected = new DataResponse([]);
  1611. $result = $ocs->updateShare(42, null, null, 'true', null, null, null, null, null);
  1612. $this->assertInstanceOf(get_class($expected), $result);
  1613. $this->assertEquals($expected->getData(), $result->getData());
  1614. }
  1615. /**
  1616. * @expectedException \OCP\AppFramework\OCS\OCSForbiddenException
  1617. * @expectedExceptionMessage Sharing sending the password by Nextcloud Talk failed because Nextcloud Talk is not enabled
  1618. */
  1619. public function testUpdateLinkShareSendPasswordByTalkWithTalkDisabledDoesNotChangeOther() {
  1620. $ocs = $this->mockFormatShare();
  1621. $date = new \DateTime('2000-01-01');
  1622. $date->setTime(0,0,0);
  1623. $node = $this->getMockBuilder(File::class)->getMock();
  1624. $share = $this->newShare();
  1625. $share->setPermissions(\OCP\Constants::PERMISSION_ALL)
  1626. ->setSharedBy($this->currentUser)
  1627. ->setShareType(\OCP\Share::SHARE_TYPE_LINK)
  1628. ->setPassword('password')
  1629. ->setSendPasswordByTalk(false)
  1630. ->setExpirationDate($date)
  1631. ->setNote('note')
  1632. ->setLabel('label')
  1633. ->setHideDownload(true)
  1634. ->setPermissions(\OCP\Constants::PERMISSION_ALL)
  1635. ->setNode($node);
  1636. $node->expects($this->once())
  1637. ->method('lock')
  1638. ->with(\OCP\Lock\ILockingProvider::LOCK_SHARED);
  1639. $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
  1640. $this->appManager->method('isEnabledForUser')->with('spreed')->willReturn(false);
  1641. $this->shareManager->expects($this->never())->method('updateShare');
  1642. $ocs->updateShare(42, null, null, 'true', null, null, null, null, null);
  1643. }
  1644. public function testUpdateLinkShareDoNotSendPasswordByTalkDoesNotChangeOther() {
  1645. $ocs = $this->mockFormatShare();
  1646. $date = new \DateTime('2000-01-01');
  1647. $date->setTime(0,0,0);
  1648. $node = $this->getMockBuilder(File::class)->getMock();
  1649. $share = $this->newShare();
  1650. $share->setPermissions(\OCP\Constants::PERMISSION_ALL)
  1651. ->setSharedBy($this->currentUser)
  1652. ->setShareType(\OCP\Share::SHARE_TYPE_LINK)
  1653. ->setPassword('password')
  1654. ->setSendPasswordByTalk(true)
  1655. ->setExpirationDate($date)
  1656. ->setNote('note')
  1657. ->setLabel('label')
  1658. ->setHideDownload(true)
  1659. ->setPermissions(\OCP\Constants::PERMISSION_ALL)
  1660. ->setNode($node);
  1661. $node->expects($this->once())
  1662. ->method('lock')
  1663. ->with(\OCP\Lock\ILockingProvider::LOCK_SHARED);
  1664. $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
  1665. $this->appManager->method('isEnabledForUser')->with('spreed')->willReturn(true);
  1666. $this->shareManager->expects($this->once())->method('updateShare')->with(
  1667. $this->callback(function (\OCP\Share\IShare $share) use ($date) {
  1668. return $share->getPermissions() === \OCP\Constants::PERMISSION_ALL &&
  1669. $share->getPassword() === 'password' &&
  1670. $share->getSendPasswordByTalk() === false &&
  1671. $share->getExpirationDate() === $date &&
  1672. $share->getNote() === 'note' &&
  1673. $share->getLabel() === 'label' &&
  1674. $share->getHideDownload() === true;
  1675. })
  1676. )->will($this->returnArgument(0));
  1677. $expected = new DataResponse([]);
  1678. $result = $ocs->updateShare(42, null, null, 'false', null, null, null, null, null);
  1679. $this->assertInstanceOf(get_class($expected), $result);
  1680. $this->assertEquals($expected->getData(), $result->getData());
  1681. }
  1682. public function testUpdateLinkShareDoNotSendPasswordByTalkWithTalkDisabledDoesNotChangeOther() {
  1683. $ocs = $this->mockFormatShare();
  1684. $date = new \DateTime('2000-01-01');
  1685. $date->setTime(0,0,0);
  1686. $node = $this->getMockBuilder(File::class)->getMock();
  1687. $node->method('getId')
  1688. ->willReturn(42);
  1689. $share = $this->newShare();
  1690. $share->setPermissions(\OCP\Constants::PERMISSION_ALL)
  1691. ->setSharedBy($this->currentUser)
  1692. ->setShareType(\OCP\Share::SHARE_TYPE_LINK)
  1693. ->setPassword('password')
  1694. ->setSendPasswordByTalk(true)
  1695. ->setExpirationDate($date)
  1696. ->setNote('note')
  1697. ->setLabel('label')
  1698. ->setHideDownload(true)
  1699. ->setPermissions(\OCP\Constants::PERMISSION_ALL)
  1700. ->setNode($node);
  1701. $node->expects($this->once())
  1702. ->method('lock')
  1703. ->with(\OCP\Lock\ILockingProvider::LOCK_SHARED);
  1704. $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
  1705. $this->appManager->method('isEnabledForUser')->with('spreed')->willReturn(false);
  1706. $this->shareManager->expects($this->once())->method('updateShare')->with(
  1707. $this->callback(function (\OCP\Share\IShare $share) use ($date) {
  1708. return $share->getPermissions() === \OCP\Constants::PERMISSION_ALL &&
  1709. $share->getPassword() === 'password' &&
  1710. $share->getSendPasswordByTalk() === false &&
  1711. $share->getExpirationDate() === $date &&
  1712. $share->getNote() === 'note' &&
  1713. $share->getLabel() === 'label' &&
  1714. $share->getHideDownload() === true;
  1715. })
  1716. )->will($this->returnArgument(0));
  1717. $userFolder = $this->createMock(Folder::class);
  1718. $this->rootFolder->method('getUserFolder')
  1719. ->with($this->currentUser)
  1720. ->willReturn($userFolder);
  1721. $userFolder->method('getById')
  1722. ->with(42)
  1723. ->willReturn([$node]);
  1724. $mountPoint = $this->createMock(IMountPoint::class);
  1725. $node->method('getMountPoint')
  1726. ->willReturn($mountPoint);
  1727. $mountPoint->method('getStorageRootId')
  1728. ->willReturn(42);
  1729. $mountPoint = $this->createMock(IMountPoint::class);
  1730. $node->method('getMountPoint')
  1731. ->willReturn($mountPoint);
  1732. $mountPoint->method('getStorageRootId')
  1733. ->willReturn(42);
  1734. $expected = new DataResponse([]);
  1735. $result = $ocs->updateShare(42, null, null, 'false', null, null, null, null, null);
  1736. $this->assertInstanceOf(get_class($expected), $result);
  1737. $this->assertEquals($expected->getData(), $result->getData());
  1738. }
  1739. public function testUpdateLinkShareExpireDateDoesNotChangeOther() {
  1740. $ocs = $this->mockFormatShare();
  1741. $node = $this->getMockBuilder(File::class)->getMock();
  1742. $node->method('getId')
  1743. ->willReturn(42);
  1744. $share = $this->newShare();
  1745. $share->setPermissions(\OCP\Constants::PERMISSION_ALL)
  1746. ->setSharedBy($this->currentUser)
  1747. ->setShareType(\OCP\Share::SHARE_TYPE_LINK)
  1748. ->setPassword('password')
  1749. ->setSendPasswordByTalk(true)
  1750. ->setExpirationDate(new \DateTime())
  1751. ->setNote('note')
  1752. ->setLabel('label')
  1753. ->setHideDownload(true)
  1754. ->setPermissions(\OCP\Constants::PERMISSION_ALL)
  1755. ->setNode($node);
  1756. $node->expects($this->once())
  1757. ->method('lock')
  1758. ->with(\OCP\Lock\ILockingProvider::LOCK_SHARED);
  1759. $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
  1760. $this->shareManager->expects($this->once())->method('updateShare')->with(
  1761. $this->callback(function (\OCP\Share\IShare $share) {
  1762. $date = new \DateTime('2010-12-23');
  1763. $date->setTime(0,0,0);
  1764. return $share->getPermissions() === \OCP\Constants::PERMISSION_ALL &&
  1765. $share->getPassword() === 'password' &&
  1766. $share->getSendPasswordByTalk() === true &&
  1767. $share->getExpirationDate() == $date &&
  1768. $share->getNote() === 'note' &&
  1769. $share->getLabel() === 'label' &&
  1770. $share->getHideDownload() === true;
  1771. })
  1772. )->will($this->returnArgument(0));
  1773. $userFolder = $this->createMock(Folder::class);
  1774. $this->rootFolder->method('getUserFolder')
  1775. ->with($this->currentUser)
  1776. ->willReturn($userFolder);
  1777. $userFolder->method('getById')
  1778. ->with(42)
  1779. ->willReturn([$node]);
  1780. $mountPoint = $this->createMock(IMountPoint::class);
  1781. $node->method('getMountPoint')
  1782. ->willReturn($mountPoint);
  1783. $mountPoint->method('getStorageRootId')
  1784. ->willReturn(42);
  1785. $expected = new DataResponse([]);
  1786. $result = $ocs->updateShare(42, null, null, null, null, '2010-12-23', null, null, null);
  1787. $this->assertInstanceOf(get_class($expected), $result);
  1788. $this->assertEquals($expected->getData(), $result->getData());
  1789. }
  1790. public function testUpdateLinkSharePublicUploadDoesNotChangeOther() {
  1791. $ocs = $this->mockFormatShare();
  1792. $date = new \DateTime('2000-01-01');
  1793. $folder = $this->getMockBuilder(Folder::class)->getMock();
  1794. $folder->method('getId')
  1795. ->willReturn(42);
  1796. $share = \OC::$server->getShareManager()->newShare();
  1797. $share->setPermissions(\OCP\Constants::PERMISSION_ALL)
  1798. ->setSharedBy($this->currentUser)
  1799. ->setShareType(\OCP\Share::SHARE_TYPE_LINK)
  1800. ->setPassword('password')
  1801. ->setSendPasswordByTalk(true)
  1802. ->setExpirationDate($date)
  1803. ->setNote('note')
  1804. ->setLabel('label')
  1805. ->setHideDownload(true)
  1806. ->setPermissions(\OCP\Constants::PERMISSION_ALL)
  1807. ->setNode($folder);
  1808. $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
  1809. $this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true);
  1810. $this->shareManager->expects($this->once())->method('updateShare')->with(
  1811. $this->callback(function (\OCP\Share\IShare $share) use ($date) {
  1812. return $share->getPermissions() === (\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE) &&
  1813. $share->getPassword() === 'password' &&
  1814. $share->getSendPasswordByTalk() === true &&
  1815. $share->getExpirationDate() === $date &&
  1816. $share->getNote() === 'note' &&
  1817. $share->getLabel() === 'label' &&
  1818. $share->getHideDownload() === true;
  1819. })
  1820. )->will($this->returnArgument(0));
  1821. $this->shareManager->method('getSharedWith')
  1822. ->willReturn([]);
  1823. $userFolder = $this->createMock(Folder::class);
  1824. $this->rootFolder->method('getUserFolder')
  1825. ->with($this->currentUser)
  1826. ->willReturn($userFolder);
  1827. $userFolder->method('getById')
  1828. ->with(42)
  1829. ->willReturn([$folder]);
  1830. $mountPoint = $this->createMock(IMountPoint::class);
  1831. $folder->method('getMountPoint')
  1832. ->willReturn($mountPoint);
  1833. $mountPoint->method('getStorageRootId')
  1834. ->willReturn(42);
  1835. $expected = new DataResponse([]);
  1836. $result = $ocs->updateShare(42, null, null, null, 'true', null, null, null, null);
  1837. $this->assertInstanceOf(get_class($expected), $result);
  1838. $this->assertEquals($expected->getData(), $result->getData());
  1839. }
  1840. public function testUpdateLinkSharePermissions() {
  1841. $ocs = $this->mockFormatShare();
  1842. $date = new \DateTime('2000-01-01');
  1843. $folder = $this->getMockBuilder(Folder::class)->getMock();
  1844. $folder->method('getId')
  1845. ->willReturn(42);
  1846. $share = \OC::$server->getShareManager()->newShare();
  1847. $share->setPermissions(\OCP\Constants::PERMISSION_ALL)
  1848. ->setSharedBy($this->currentUser)
  1849. ->setShareType(\OCP\Share::SHARE_TYPE_LINK)
  1850. ->setPassword('password')
  1851. ->setSendPasswordByTalk(true)
  1852. ->setExpirationDate($date)
  1853. ->setNote('note')
  1854. ->setLabel('label')
  1855. ->setHideDownload(true)
  1856. ->setPermissions(\OCP\Constants::PERMISSION_ALL)
  1857. ->setNode($folder);
  1858. $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
  1859. $this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true);
  1860. $this->shareManager->expects($this->once())->method('updateShare')->with(
  1861. $this->callback(function (\OCP\Share\IShare $share) use ($date) {
  1862. return $share->getPermissions() === (\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE) &&
  1863. $share->getPassword() === 'password' &&
  1864. $share->getSendPasswordByTalk() === true &&
  1865. $share->getExpirationDate() === $date &&
  1866. $share->getNote() === 'note' &&
  1867. $share->getLabel() === 'label' &&
  1868. $share->getHideDownload() === true;
  1869. })
  1870. )->will($this->returnArgument(0));
  1871. $this->shareManager->method('getSharedWith')->willReturn([]);
  1872. $userFolder = $this->createMock(Folder::class);
  1873. $this->rootFolder->method('getUserFolder')
  1874. ->with($this->currentUser)
  1875. ->willReturn($userFolder);
  1876. $userFolder->method('getById')
  1877. ->with(42)
  1878. ->willReturn([$folder]);
  1879. $mountPoint = $this->createMock(IMountPoint::class);
  1880. $folder->method('getMountPoint')
  1881. ->willReturn($mountPoint);
  1882. $mountPoint->method('getStorageRootId')
  1883. ->willReturn(42);
  1884. $expected = new DataResponse([]);
  1885. $result = $ocs->updateShare(42, 7, null, null, null, null, null, null, null);
  1886. $this->assertInstanceOf(get_class($expected), $result);
  1887. $this->assertEquals($expected->getData(), $result->getData());
  1888. }
  1889. public function testUpdateLinkSharePermissionsShare() {
  1890. $ocs = $this->mockFormatShare();
  1891. $date = new \DateTime('2000-01-01');
  1892. $folder = $this->getMockBuilder(Folder::class)->getMock();
  1893. $folder->method('getId')
  1894. ->willReturn(42);
  1895. $share = \OC::$server->getShareManager()->newShare();
  1896. $share->setPermissions(\OCP\Constants::PERMISSION_ALL)
  1897. ->setSharedBy($this->currentUser)
  1898. ->setShareType(\OCP\Share::SHARE_TYPE_LINK)
  1899. ->setPassword('password')
  1900. ->setSendPasswordByTalk(true)
  1901. ->setExpirationDate($date)
  1902. ->setNote('note')
  1903. ->setLabel('label')
  1904. ->setHideDownload(true)
  1905. ->setPermissions(\OCP\Constants::PERMISSION_READ)
  1906. ->setNode($folder);
  1907. $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
  1908. $this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true);
  1909. $this->shareManager->expects($this->once())->method('updateShare')->with(
  1910. $this->callback(function (\OCP\Share\IShare $share) use ($date) {
  1911. return $share->getPermissions() === (\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE) &&
  1912. $share->getPassword() === 'password' &&
  1913. $share->getSendPasswordByTalk() === true &&
  1914. $share->getExpirationDate() === $date &&
  1915. $share->getNote() === 'note' &&
  1916. $share->getLabel() === 'label' &&
  1917. $share->getHideDownload() === true;
  1918. })
  1919. )->will($this->returnArgument(0));
  1920. $userFolder = $this->createMock(Folder::class);
  1921. $this->rootFolder->method('getUserFolder')
  1922. ->with($this->currentUser)
  1923. ->willReturn($userFolder);
  1924. $userFolder->method('getById')
  1925. ->with(42)
  1926. ->willReturn([$folder]);
  1927. $mountPoint = $this->createMock(IMountPoint::class);
  1928. $folder->method('getMountPoint')
  1929. ->willReturn($mountPoint);
  1930. $mountPoint->method('getStorageRootId')
  1931. ->willReturn(42);
  1932. $this->shareManager->method('getSharedWith')->willReturn([]);
  1933. $expected = new DataResponse([]);
  1934. $result = $ocs->updateShare(42, 31, null, null, null, null, null, null, null);
  1935. $this->assertInstanceOf(get_class($expected), $result);
  1936. $this->assertEquals($expected->getData(), $result->getData());
  1937. }
  1938. public function testUpdateOtherPermissions() {
  1939. $ocs = $this->mockFormatShare();
  1940. $file = $this->getMockBuilder(File::class)->getMock();
  1941. $file->method('getId')
  1942. ->willReturn(42);
  1943. $share = \OC::$server->getShareManager()->newShare();
  1944. $share->setPermissions(\OCP\Constants::PERMISSION_ALL)
  1945. ->setSharedBy($this->currentUser)
  1946. ->setShareType(\OCP\Share::SHARE_TYPE_USER)
  1947. ->setNode($file);
  1948. $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
  1949. $this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true);
  1950. $this->shareManager->expects($this->once())->method('updateShare')->with(
  1951. $this->callback(function (\OCP\Share\IShare $share) {
  1952. return $share->getPermissions() === \OCP\Constants::PERMISSION_ALL;
  1953. })
  1954. )->will($this->returnArgument(0));
  1955. $this->shareManager->method('getSharedWith')->willReturn([]);
  1956. $userFolder = $this->createMock(Folder::class);
  1957. $this->rootFolder->method('getUserFolder')
  1958. ->with($this->currentUser)
  1959. ->willReturn($userFolder);
  1960. $userFolder->method('getById')
  1961. ->with(42)
  1962. ->willReturn([$file]);
  1963. $mountPoint = $this->createMock(IMountPoint::class);
  1964. $file->method('getMountPoint')
  1965. ->willReturn($mountPoint);
  1966. $mountPoint->method('getStorageRootId')
  1967. ->willReturn(42);
  1968. $expected = new DataResponse([]);
  1969. $result = $ocs->updateShare(42, 31, null, null, null, null);
  1970. $this->assertInstanceOf(get_class($expected), $result);
  1971. $this->assertEquals($expected->getData(), $result->getData());
  1972. }
  1973. public function testUpdateShareCannotIncreasePermissions() {
  1974. $ocs = $this->mockFormatShare();
  1975. $folder = $this->createMock(Folder::class);
  1976. $folder->method('getId')
  1977. ->willReturn(42);
  1978. $share = \OC::$server->getShareManager()->newShare();
  1979. $share
  1980. ->setId(42)
  1981. ->setSharedBy($this->currentUser)
  1982. ->setShareOwner('anotheruser')
  1983. ->setShareType(\OCP\Share::SHARE_TYPE_GROUP)
  1984. ->setSharedWith('group1')
  1985. ->setPermissions(\OCP\Constants::PERMISSION_READ)
  1986. ->setNode($folder);
  1987. // note: updateShare will modify the received instance but getSharedWith will reread from the database,
  1988. // so their values will be different
  1989. $incomingShare = \OC::$server->getShareManager()->newShare();
  1990. $incomingShare
  1991. ->setId(42)
  1992. ->setSharedBy($this->currentUser)
  1993. ->setShareOwner('anotheruser')
  1994. ->setShareType(\OCP\Share::SHARE_TYPE_GROUP)
  1995. ->setSharedWith('group1')
  1996. ->setPermissions(\OCP\Constants::PERMISSION_READ)
  1997. ->setNode($folder);
  1998. $this->request
  1999. ->method('getParam')
  2000. ->will($this->returnValueMap([
  2001. ['permissions', null, '31'],
  2002. ]));
  2003. $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
  2004. $this->shareManager->expects($this->any())
  2005. ->method('getSharedWith')
  2006. ->will($this->returnValueMap([
  2007. ['currentUser', \OCP\Share::SHARE_TYPE_USER, $share->getNode(), -1, 0, []],
  2008. ['currentUser', \OCP\Share::SHARE_TYPE_GROUP, $share->getNode(), -1, 0, [$incomingShare]],
  2009. ['currentUser', \OCP\Share::SHARE_TYPE_ROOM, $share->getNode(), -1, 0, []]
  2010. ]));
  2011. $userFolder = $this->createMock(Folder::class);
  2012. $this->rootFolder->method('getUserFolder')
  2013. ->with($this->currentUser)
  2014. ->willReturn($userFolder);
  2015. $userFolder->method('getById')
  2016. ->with(42)
  2017. ->willReturn([$folder]);
  2018. $mountPoint = $this->createMock(IMountPoint::class);
  2019. $folder->method('getMountPoint')
  2020. ->willReturn($mountPoint);
  2021. $mountPoint->method('getStorageRootId')
  2022. ->willReturn(42);
  2023. $this->shareManager->expects($this->never())->method('updateShare');
  2024. try {
  2025. $ocs->updateShare(42, 31);
  2026. $this->fail();
  2027. } catch (OCSNotFoundException $e) {
  2028. $this->assertEquals('Cannot increase permissions', $e->getMessage());
  2029. }
  2030. }
  2031. public function testUpdateShareCannotIncreasePermissionsLinkShare() {
  2032. $ocs = $this->mockFormatShare();
  2033. $folder = $this->createMock(Folder::class);
  2034. $folder->method('getId')
  2035. ->willReturn(42);
  2036. $share = \OC::$server->getShareManager()->newShare();
  2037. $share
  2038. ->setId(42)
  2039. ->setSharedBy($this->currentUser)
  2040. ->setShareOwner('anotheruser')
  2041. ->setShareType(\OCP\Share::SHARE_TYPE_LINK)
  2042. ->setPermissions(\OCP\Constants::PERMISSION_READ)
  2043. ->setNode($folder);
  2044. // note: updateShare will modify the received instance but getSharedWith will reread from the database,
  2045. // so their values will be different
  2046. $incomingShare = \OC::$server->getShareManager()->newShare();
  2047. $incomingShare
  2048. ->setId(42)
  2049. ->setSharedBy($this->currentUser)
  2050. ->setShareOwner('anotheruser')
  2051. ->setShareType(\OCP\Share::SHARE_TYPE_USER)
  2052. ->setSharedWith('currentUser')
  2053. ->setPermissions(\OCP\Constants::PERMISSION_READ)
  2054. ->setNode($folder);
  2055. $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
  2056. $this->shareManager->expects($this->any())
  2057. ->method('getSharedWith')
  2058. ->will($this->returnValueMap([
  2059. ['currentUser', \OCP\Share::SHARE_TYPE_USER, $share->getNode(), -1, 0, [$incomingShare]],
  2060. ['currentUser', \OCP\Share::SHARE_TYPE_GROUP, $share->getNode(), -1, 0, []],
  2061. ['currentUser', \OCP\Share::SHARE_TYPE_ROOM, $share->getNode(), -1, 0, []]
  2062. ]));
  2063. $userFolder = $this->createMock(Folder::class);
  2064. $this->rootFolder->method('getUserFolder')
  2065. ->with($this->currentUser)
  2066. ->willReturn($userFolder);
  2067. $userFolder->method('getById')
  2068. ->with(42)
  2069. ->willReturn([$folder]);
  2070. $mountPoint = $this->createMock(IMountPoint::class);
  2071. $folder->method('getMountPoint')
  2072. ->willReturn($mountPoint);
  2073. $mountPoint->method('getStorageRootId')
  2074. ->willReturn(42);
  2075. $this->shareManager->expects($this->never())->method('updateShare');
  2076. $this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true);
  2077. try {
  2078. $ocs->updateShare(42, null, null, null, 'true');
  2079. $this->fail();
  2080. } catch (OCSNotFoundException $e) {
  2081. $this->assertEquals('Cannot increase permissions', $e->getMessage());
  2082. }
  2083. }
  2084. public function testUpdateShareCannotIncreasePermissionsRoomShare() {
  2085. $ocs = $this->mockFormatShare();
  2086. $folder = $this->createMock(Folder::class);
  2087. $folder->method('getId')
  2088. ->willReturn(42);
  2089. $share = \OC::$server->getShareManager()->newShare();
  2090. $share
  2091. ->setId(42)
  2092. ->setSharedBy($this->currentUser)
  2093. ->setShareOwner('anotheruser')
  2094. ->setShareType(\OCP\Share::SHARE_TYPE_ROOM)
  2095. ->setSharedWith('group1')
  2096. ->setPermissions(\OCP\Constants::PERMISSION_READ)
  2097. ->setNode($folder);
  2098. // note: updateShare will modify the received instance but getSharedWith will reread from the database,
  2099. // so their values will be different
  2100. $incomingShare = \OC::$server->getShareManager()->newShare();
  2101. $incomingShare
  2102. ->setId(42)
  2103. ->setSharedBy($this->currentUser)
  2104. ->setShareOwner('anotheruser')
  2105. ->setShareType(\OCP\Share::SHARE_TYPE_ROOM)
  2106. ->setSharedWith('group1')
  2107. ->setPermissions(\OCP\Constants::PERMISSION_READ)
  2108. ->setNode($folder);
  2109. $this->request
  2110. ->method('getParam')
  2111. ->will($this->returnValueMap([
  2112. ['permissions', null, '31'],
  2113. ]));
  2114. $this->shareManager
  2115. ->method('getShareById')
  2116. ->will($this->returnCallback(
  2117. function ($id) use ($share) {
  2118. if ($id !== 'ocRoomShare:42') {
  2119. throw new \OCP\Share\Exceptions\ShareNotFound();
  2120. }
  2121. return $share;
  2122. }
  2123. ));
  2124. $userFolder = $this->createMock(Folder::class);
  2125. $this->rootFolder->method('getUserFolder')
  2126. ->with($this->currentUser)
  2127. ->willReturn($userFolder);
  2128. $userFolder->method('getById')
  2129. ->with(42)
  2130. ->willReturn([$folder]);
  2131. $mountPoint = $this->createMock(IMountPoint::class);
  2132. $folder->method('getMountPoint')
  2133. ->willReturn($mountPoint);
  2134. $mountPoint->method('getStorageRootId')
  2135. ->willReturn(42);
  2136. $this->shareManager->expects($this->any())
  2137. ->method('getSharedWith')
  2138. ->will($this->returnValueMap([
  2139. ['currentUser', \OCP\Share::SHARE_TYPE_USER, $share->getNode(), -1, 0, []],
  2140. ['currentUser', \OCP\Share::SHARE_TYPE_GROUP, $share->getNode(), -1, 0, []],
  2141. ['currentUser', \OCP\Share::SHARE_TYPE_ROOM, $share->getNode(), -1, 0, [$incomingShare]]
  2142. ]));
  2143. $this->shareManager->expects($this->never())->method('updateShare');
  2144. try {
  2145. $ocs->updateShare(42, 31);
  2146. $this->fail();
  2147. } catch (OCSNotFoundException $e) {
  2148. $this->assertEquals('Cannot increase permissions', $e->getMessage());
  2149. }
  2150. }
  2151. public function testUpdateShareCanIncreasePermissionsIfOwner() {
  2152. $ocs = $this->mockFormatShare();
  2153. $folder = $this->createMock(Folder::class);
  2154. $folder->method('getId')
  2155. ->willReturn(42);
  2156. $share = \OC::$server->getShareManager()->newShare();
  2157. $share
  2158. ->setId(42)
  2159. ->setSharedBy($this->currentUser)
  2160. ->setShareOwner($this->currentUser)
  2161. ->setShareType(\OCP\Share::SHARE_TYPE_GROUP)
  2162. ->setSharedWith('group1')
  2163. ->setPermissions(\OCP\Constants::PERMISSION_READ)
  2164. ->setNode($folder);
  2165. // note: updateShare will modify the received instance but getSharedWith will reread from the database,
  2166. // so their values will be different
  2167. $incomingShare = \OC::$server->getShareManager()->newShare();
  2168. $incomingShare
  2169. ->setId(42)
  2170. ->setSharedBy($this->currentUser)
  2171. ->setShareOwner($this->currentUser)
  2172. ->setShareType(\OCP\Share::SHARE_TYPE_GROUP)
  2173. ->setSharedWith('group1')
  2174. ->setPermissions(\OCP\Constants::PERMISSION_READ)
  2175. ->setNode($folder);
  2176. $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
  2177. $this->shareManager->expects($this->any())
  2178. ->method('getSharedWith')
  2179. ->will($this->returnValueMap([
  2180. ['currentUser', \OCP\Share::SHARE_TYPE_USER, $share->getNode(), -1, 0, []],
  2181. ['currentUser', \OCP\Share::SHARE_TYPE_GROUP, $share->getNode(), -1, 0, [$incomingShare]]
  2182. ]));
  2183. $this->shareManager->expects($this->once())
  2184. ->method('updateShare')
  2185. ->with($share)
  2186. ->willReturn($share);
  2187. $userFolder = $this->createMock(Folder::class);
  2188. $this->rootFolder->method('getUserFolder')
  2189. ->with($this->currentUser)
  2190. ->willReturn($userFolder);
  2191. $userFolder->method('getById')
  2192. ->with(42)
  2193. ->willReturn([$folder]);
  2194. $mountPoint = $this->createMock(IMountPoint::class);
  2195. $folder->method('getMountPoint')
  2196. ->willReturn($mountPoint);
  2197. $mountPoint->method('getStorageRootId')
  2198. ->willReturn(42);
  2199. $result = $ocs->updateShare(42, 31);
  2200. $this->assertInstanceOf(DataResponse::class, $result);
  2201. }
  2202. public function dataFormatShare() {
  2203. $file = $this->getMockBuilder(File::class)->getMock();
  2204. $folder = $this->getMockBuilder(Folder::class)->getMock();
  2205. $parent = $this->getMockBuilder(Folder::class)->getMock();
  2206. $file->method('getMimeType')->willReturn('myMimeType');
  2207. $folder->method('getMimeType')->willReturn('myFolderMimeType');
  2208. $file->method('getPath')->willReturn('file');
  2209. $folder->method('getPath')->willReturn('folder');
  2210. $parent->method('getId')->willReturn(1);
  2211. $folder->method('getId')->willReturn(2);
  2212. $file->method('getId')->willReturn(3);
  2213. $file->method('getParent')->willReturn($parent);
  2214. $folder->method('getParent')->willReturn($parent);
  2215. $cache = $this->getMockBuilder('OCP\Files\Cache\ICache')->getMock();
  2216. $cache->method('getNumericStorageId')->willReturn(100);
  2217. $storage = $this->getMockBuilder(Storage::class)->getMock();
  2218. $storage->method('getId')->willReturn('storageId');
  2219. $storage->method('getCache')->willReturn($cache);
  2220. $file->method('getStorage')->willReturn($storage);
  2221. $folder->method('getStorage')->willReturn($storage);
  2222. $owner = $this->getMockBuilder(IUser::class)->getMock();
  2223. $owner->method('getDisplayName')->willReturn('ownerDN');
  2224. $initiator = $this->getMockBuilder(IUser::class)->getMock();
  2225. $initiator->method('getDisplayName')->willReturn('initiatorDN');
  2226. $recipient = $this->getMockBuilder(IUser::class)->getMock();
  2227. $recipient->method('getDisplayName')->willReturn('recipientDN');
  2228. $result = [];
  2229. $share = \OC::$server->getShareManager()->newShare();
  2230. $share->setShareType(\OCP\Share::SHARE_TYPE_USER)
  2231. ->setSharedWith('recipient')
  2232. ->setSharedBy('initiator')
  2233. ->setShareOwner('owner')
  2234. ->setPermissions(\OCP\Constants::PERMISSION_READ)
  2235. ->setNode($file)
  2236. ->setShareTime(new \DateTime('2000-01-01T00:01:02'))
  2237. ->setTarget('myTarget')
  2238. ->setNote('personal note')
  2239. ->setId(42);
  2240. // User backend down
  2241. $result[] = [
  2242. [
  2243. 'id' => 42,
  2244. 'share_type' => \OCP\Share::SHARE_TYPE_USER,
  2245. 'uid_owner' => 'initiator',
  2246. 'displayname_owner' => 'initiator',
  2247. 'permissions' => 1,
  2248. 'stime' => 946684862,
  2249. 'parent' => null,
  2250. 'expiration' => null,
  2251. 'token' => null,
  2252. 'uid_file_owner' => 'owner',
  2253. 'displayname_file_owner' => 'owner',
  2254. 'path' => 'file',
  2255. 'item_type' => 'file',
  2256. 'storage_id' => 'storageId',
  2257. 'storage' => 100,
  2258. 'item_source' => 3,
  2259. 'file_source' => 3,
  2260. 'file_parent' => 1,
  2261. 'file_target' => 'myTarget',
  2262. 'share_with' => 'recipient',
  2263. 'share_with_displayname' => 'recipient',
  2264. 'note' => 'personal note',
  2265. 'label' => null,
  2266. 'mail_send' => 0,
  2267. 'mimetype' => 'myMimeType',
  2268. 'hide_download' => 0,
  2269. ], $share, [], false
  2270. ];
  2271. // User backend up
  2272. $result[] = [
  2273. [
  2274. 'id' => 42,
  2275. 'share_type' => \OCP\Share::SHARE_TYPE_USER,
  2276. 'uid_owner' => 'initiator',
  2277. 'displayname_owner' => 'initiatorDN',
  2278. 'permissions' => 1,
  2279. 'stime' => 946684862,
  2280. 'parent' => null,
  2281. 'expiration' => null,
  2282. 'token' => null,
  2283. 'uid_file_owner' => 'owner',
  2284. 'displayname_file_owner' => 'ownerDN',
  2285. 'note' => 'personal note',
  2286. 'label' => null,
  2287. 'path' => 'file',
  2288. 'item_type' => 'file',
  2289. 'storage_id' => 'storageId',
  2290. 'storage' => 100,
  2291. 'item_source' => 3,
  2292. 'file_source' => 3,
  2293. 'file_parent' => 1,
  2294. 'file_target' => 'myTarget',
  2295. 'share_with' => 'recipient',
  2296. 'share_with_displayname' => 'recipientDN',
  2297. 'mail_send' => 0,
  2298. 'mimetype' => 'myMimeType',
  2299. 'hide_download' => 0,
  2300. ], $share, [
  2301. ['owner', $owner],
  2302. ['initiator', $initiator],
  2303. ['recipient', $recipient],
  2304. ], false
  2305. ];
  2306. $share = \OC::$server->getShareManager()->newShare();
  2307. $share->setShareType(\OCP\Share::SHARE_TYPE_USER)
  2308. ->setSharedWith('recipient')
  2309. ->setSharedBy('initiator')
  2310. ->setShareOwner('owner')
  2311. ->setPermissions(\OCP\Constants::PERMISSION_READ)
  2312. ->setNode($file)
  2313. ->setShareTime(new \DateTime('2000-01-01T00:01:02'))
  2314. ->setTarget('myTarget')
  2315. ->setNote('personal note')
  2316. ->setId(42);
  2317. // User backend down
  2318. $result[] = [
  2319. [
  2320. 'id' => 42,
  2321. 'share_type' => \OCP\Share::SHARE_TYPE_USER,
  2322. 'uid_owner' => 'initiator',
  2323. 'displayname_owner' => 'initiator',
  2324. 'permissions' => 1,
  2325. 'stime' => 946684862,
  2326. 'parent' => null,
  2327. 'expiration' => null,
  2328. 'token' => null,
  2329. 'uid_file_owner' => 'owner',
  2330. 'displayname_file_owner' => 'owner',
  2331. 'note' => 'personal note',
  2332. 'label' => null,
  2333. 'path' => 'file',
  2334. 'item_type' => 'file',
  2335. 'storage_id' => 'storageId',
  2336. 'storage' => 100,
  2337. 'item_source' => 3,
  2338. 'file_source' => 3,
  2339. 'file_parent' => 1,
  2340. 'file_target' => 'myTarget',
  2341. 'share_with' => 'recipient',
  2342. 'share_with_displayname' => 'recipient',
  2343. 'mail_send' => 0,
  2344. 'mimetype' => 'myMimeType',
  2345. 'hide_download' => 0,
  2346. ], $share, [], false
  2347. ];
  2348. // with existing group
  2349. $share = \OC::$server->getShareManager()->newShare();
  2350. $share->setShareType(\OCP\Share::SHARE_TYPE_GROUP)
  2351. ->setSharedWith('recipientGroup')
  2352. ->setSharedBy('initiator')
  2353. ->setShareOwner('owner')
  2354. ->setPermissions(\OCP\Constants::PERMISSION_READ)
  2355. ->setNode($file)
  2356. ->setShareTime(new \DateTime('2000-01-01T00:01:02'))
  2357. ->setTarget('myTarget')
  2358. ->setNote('personal note')
  2359. ->setId(42);
  2360. $result[] = [
  2361. [
  2362. 'id' => 42,
  2363. 'share_type' => \OCP\Share::SHARE_TYPE_GROUP,
  2364. 'uid_owner' => 'initiator',
  2365. 'displayname_owner' => 'initiator',
  2366. 'permissions' => 1,
  2367. 'stime' => 946684862,
  2368. 'parent' => null,
  2369. 'expiration' => null,
  2370. 'token' => null,
  2371. 'uid_file_owner' => 'owner',
  2372. 'displayname_file_owner' => 'owner',
  2373. 'note' => 'personal note',
  2374. 'label' => null,
  2375. 'path' => 'file',
  2376. 'item_type' => 'file',
  2377. 'storage_id' => 'storageId',
  2378. 'storage' => 100,
  2379. 'item_source' => 3,
  2380. 'file_source' => 3,
  2381. 'file_parent' => 1,
  2382. 'file_target' => 'myTarget',
  2383. 'share_with' => 'recipientGroup',
  2384. 'share_with_displayname' => 'recipientGroupDisplayName',
  2385. 'mail_send' => 0,
  2386. 'mimetype' => 'myMimeType',
  2387. 'hide_download' => 0,
  2388. ], $share, [], false
  2389. ];
  2390. // with unknown group / no group backend
  2391. $share = \OC::$server->getShareManager()->newShare();
  2392. $share->setShareType(Share::SHARE_TYPE_GROUP)
  2393. ->setSharedWith('recipientGroup2')
  2394. ->setSharedBy('initiator')
  2395. ->setShareOwner('owner')
  2396. ->setPermissions(\OCP\Constants::PERMISSION_READ)
  2397. ->setNode($file)
  2398. ->setShareTime(new \DateTime('2000-01-01T00:01:02'))
  2399. ->setTarget('myTarget')
  2400. ->setNote('personal note')
  2401. ->setId(42);
  2402. $result[] = [
  2403. [
  2404. 'id' => 42,
  2405. 'share_type' => Share::SHARE_TYPE_GROUP,
  2406. 'uid_owner' => 'initiator',
  2407. 'displayname_owner' => 'initiator',
  2408. 'permissions' => 1,
  2409. 'stime' => 946684862,
  2410. 'parent' => null,
  2411. 'expiration' => null,
  2412. 'token' => null,
  2413. 'uid_file_owner' => 'owner',
  2414. 'displayname_file_owner' => 'owner',
  2415. 'note' => 'personal note',
  2416. 'label' => null,
  2417. 'path' => 'file',
  2418. 'item_type' => 'file',
  2419. 'storage_id' => 'storageId',
  2420. 'storage' => 100,
  2421. 'item_source' => 3,
  2422. 'file_source' => 3,
  2423. 'file_parent' => 1,
  2424. 'file_target' => 'myTarget',
  2425. 'share_with' => 'recipientGroup2',
  2426. 'share_with_displayname' => 'recipientGroup2',
  2427. 'mail_send' => 0,
  2428. 'mimetype' => 'myMimeType',
  2429. 'hide_download' => 0,
  2430. ], $share, [], false
  2431. ];
  2432. $share = \OC::$server->getShareManager()->newShare();
  2433. $share->setShareType(\OCP\Share::SHARE_TYPE_LINK)
  2434. ->setSharedBy('initiator')
  2435. ->setShareOwner('owner')
  2436. ->setPermissions(\OCP\Constants::PERMISSION_READ)
  2437. ->setNode($file)
  2438. ->setShareTime(new \DateTime('2000-01-01T00:01:02'))
  2439. ->setTarget('myTarget')
  2440. ->setPassword('mypassword')
  2441. ->setExpirationDate(new \DateTime('2001-01-02T00:00:00'))
  2442. ->setToken('myToken')
  2443. ->setNote('personal note')
  2444. ->setLabel('new link share')
  2445. ->setId(42);
  2446. $result[] = [
  2447. [
  2448. 'id' => 42,
  2449. 'share_type' => \OCP\Share::SHARE_TYPE_LINK,
  2450. 'uid_owner' => 'initiator',
  2451. 'displayname_owner' => 'initiator',
  2452. 'permissions' => 1,
  2453. 'stime' => 946684862,
  2454. 'parent' => null,
  2455. 'expiration' => '2001-01-02 00:00:00',
  2456. 'token' => 'myToken',
  2457. 'uid_file_owner' => 'owner',
  2458. 'displayname_file_owner' => 'owner',
  2459. 'note' => 'personal note',
  2460. 'label' => 'new link share',
  2461. 'path' => 'file',
  2462. 'item_type' => 'file',
  2463. 'storage_id' => 'storageId',
  2464. 'storage' => 100,
  2465. 'item_source' => 3,
  2466. 'file_source' => 3,
  2467. 'file_parent' => 1,
  2468. 'file_target' => 'myTarget',
  2469. 'password' => 'mypassword',
  2470. 'share_with' => 'mypassword',
  2471. 'share_with_displayname' => 'mypassword',
  2472. 'send_password_by_talk' => false,
  2473. 'mail_send' => 0,
  2474. 'url' => 'myLink',
  2475. 'mimetype' => 'myMimeType',
  2476. 'hide_download' => 0,
  2477. ], $share, [], false
  2478. ];
  2479. $share = \OC::$server->getShareManager()->newShare();
  2480. $share->setShareType(\OCP\Share::SHARE_TYPE_LINK)
  2481. ->setSharedBy('initiator')
  2482. ->setShareOwner('owner')
  2483. ->setPermissions(\OCP\Constants::PERMISSION_READ)
  2484. ->setNode($file)
  2485. ->setShareTime(new \DateTime('2000-01-01T00:01:02'))
  2486. ->setTarget('myTarget')
  2487. ->setPassword('mypassword')
  2488. ->setSendPasswordByTalk(true)
  2489. ->setExpirationDate(new \DateTime('2001-01-02T00:00:00'))
  2490. ->setToken('myToken')
  2491. ->setNote('personal note')
  2492. ->setLabel('new link share')
  2493. ->setId(42);
  2494. $result[] = [
  2495. [
  2496. 'id' => 42,
  2497. 'share_type' => \OCP\Share::SHARE_TYPE_LINK,
  2498. 'uid_owner' => 'initiator',
  2499. 'displayname_owner' => 'initiator',
  2500. 'permissions' => 1,
  2501. 'stime' => 946684862,
  2502. 'parent' => null,
  2503. 'expiration' => '2001-01-02 00:00:00',
  2504. 'token' => 'myToken',
  2505. 'uid_file_owner' => 'owner',
  2506. 'displayname_file_owner' => 'owner',
  2507. 'note' => 'personal note',
  2508. 'label' => 'new link share',
  2509. 'path' => 'file',
  2510. 'item_type' => 'file',
  2511. 'storage_id' => 'storageId',
  2512. 'storage' => 100,
  2513. 'item_source' => 3,
  2514. 'file_source' => 3,
  2515. 'file_parent' => 1,
  2516. 'file_target' => 'myTarget',
  2517. 'password' => 'mypassword',
  2518. 'share_with' => 'mypassword',
  2519. 'share_with_displayname' => 'mypassword',
  2520. 'send_password_by_talk' => true,
  2521. 'mail_send' => 0,
  2522. 'url' => 'myLink',
  2523. 'mimetype' => 'myMimeType',
  2524. 'hide_download' => 0,
  2525. ], $share, [], false
  2526. ];
  2527. $share = \OC::$server->getShareManager()->newShare();
  2528. $share->setShareType(\OCP\Share::SHARE_TYPE_REMOTE)
  2529. ->setSharedBy('initiator')
  2530. ->setSharedWith('user@server.com')
  2531. ->setShareOwner('owner')
  2532. ->setPermissions(\OCP\Constants::PERMISSION_READ)
  2533. ->setNode($folder)
  2534. ->setShareTime(new \DateTime('2000-01-01T00:01:02'))
  2535. ->setTarget('myTarget')
  2536. ->setNote('personal note')
  2537. ->setId(42);
  2538. $result[] = [
  2539. [
  2540. 'id' => 42,
  2541. 'share_type' => \OCP\Share::SHARE_TYPE_REMOTE,
  2542. 'uid_owner' => 'initiator',
  2543. 'displayname_owner' => 'initiator',
  2544. 'permissions' => 1,
  2545. 'stime' => 946684862,
  2546. 'parent' => null,
  2547. 'expiration' => null,
  2548. 'token' => null,
  2549. 'uid_file_owner' => 'owner',
  2550. 'displayname_file_owner' => 'owner',
  2551. 'note' => 'personal note',
  2552. 'label' => null,
  2553. 'path' => 'folder',
  2554. 'item_type' => 'folder',
  2555. 'storage_id' => 'storageId',
  2556. 'storage' => 100,
  2557. 'item_source' => 2,
  2558. 'file_source' => 2,
  2559. 'file_parent' => 1,
  2560. 'file_target' => 'myTarget',
  2561. 'share_with' => 'user@server.com',
  2562. 'share_with_displayname' => 'foobar',
  2563. 'mail_send' => 0,
  2564. 'mimetype' => 'myFolderMimeType',
  2565. 'hide_download' => 0,
  2566. ], $share, [], false
  2567. ];
  2568. // Circle with id, display name and avatar set by the Circles app
  2569. $share = \OC::$server->getShareManager()->newShare();
  2570. $share->setShareType(\OCP\Share::SHARE_TYPE_CIRCLE)
  2571. ->setSharedBy('initiator')
  2572. ->setSharedWith('Circle (Public circle, circleOwner) [4815162342]')
  2573. ->setSharedWithDisplayName('The display name')
  2574. ->setSharedWithAvatar('path/to/the/avatar')
  2575. ->setShareOwner('owner')
  2576. ->setPermissions(\OCP\Constants::PERMISSION_READ)
  2577. ->setNode($folder)
  2578. ->setShareTime(new \DateTime('2000-01-01T00:01:02'))
  2579. ->setTarget('myTarget')
  2580. ->setId(42);
  2581. $result[] = [
  2582. [
  2583. 'id' => 42,
  2584. 'share_type' => \OCP\Share::SHARE_TYPE_CIRCLE,
  2585. 'uid_owner' => 'initiator',
  2586. 'displayname_owner' => 'initiator',
  2587. 'permissions' => 1,
  2588. 'stime' => 946684862,
  2589. 'parent' => null,
  2590. 'expiration' => null,
  2591. 'token' => null,
  2592. 'uid_file_owner' => 'owner',
  2593. 'displayname_file_owner' => 'owner',
  2594. 'note' => '',
  2595. 'label' => null,
  2596. 'path' => 'folder',
  2597. 'item_type' => 'folder',
  2598. 'storage_id' => 'storageId',
  2599. 'storage' => 100,
  2600. 'item_source' => 2,
  2601. 'file_source' => 2,
  2602. 'file_parent' => 1,
  2603. 'file_target' => 'myTarget',
  2604. 'share_with' => '4815162342',
  2605. 'share_with_displayname' => 'The display name',
  2606. 'share_with_avatar' => 'path/to/the/avatar',
  2607. 'mail_send' => 0,
  2608. 'mimetype' => 'myFolderMimeType',
  2609. 'hide_download' => 0,
  2610. ], $share, [], false
  2611. ];
  2612. // Circle with id set by the Circles app
  2613. $share = \OC::$server->getShareManager()->newShare();
  2614. $share->setShareType(\OCP\Share::SHARE_TYPE_CIRCLE)
  2615. ->setSharedBy('initiator')
  2616. ->setSharedWith('Circle (Public circle, circleOwner) [4815162342]')
  2617. ->setShareOwner('owner')
  2618. ->setPermissions(\OCP\Constants::PERMISSION_READ)
  2619. ->setNode($folder)
  2620. ->setShareTime(new \DateTime('2000-01-01T00:01:02'))
  2621. ->setTarget('myTarget')
  2622. ->setId(42);
  2623. $result[] = [
  2624. [
  2625. 'id' => 42,
  2626. 'share_type' => \OCP\Share::SHARE_TYPE_CIRCLE,
  2627. 'uid_owner' => 'initiator',
  2628. 'displayname_owner' => 'initiator',
  2629. 'permissions' => 1,
  2630. 'stime' => 946684862,
  2631. 'parent' => null,
  2632. 'expiration' => null,
  2633. 'token' => null,
  2634. 'uid_file_owner' => 'owner',
  2635. 'displayname_file_owner' => 'owner',
  2636. 'note' => '',
  2637. 'label' => null,
  2638. 'path' => 'folder',
  2639. 'item_type' => 'folder',
  2640. 'storage_id' => 'storageId',
  2641. 'storage' => 100,
  2642. 'item_source' => 2,
  2643. 'file_source' => 2,
  2644. 'file_parent' => 1,
  2645. 'file_target' => 'myTarget',
  2646. 'share_with' => '4815162342',
  2647. 'share_with_displayname' => 'Circle (Public circle, circleOwner)',
  2648. 'share_with_avatar' => '',
  2649. 'mail_send' => 0,
  2650. 'mimetype' => 'myFolderMimeType',
  2651. 'hide_download' => 0,
  2652. ], $share, [], false
  2653. ];
  2654. // Circle with id not set by the Circles app
  2655. $share = \OC::$server->getShareManager()->newShare();
  2656. $share->setShareType(\OCP\Share::SHARE_TYPE_CIRCLE)
  2657. ->setSharedBy('initiator')
  2658. ->setSharedWith('Circle (Public circle, circleOwner)')
  2659. ->setShareOwner('owner')
  2660. ->setPermissions(\OCP\Constants::PERMISSION_READ)
  2661. ->setNode($folder)
  2662. ->setShareTime(new \DateTime('2000-01-01T00:01:02'))
  2663. ->setTarget('myTarget')
  2664. ->setId(42);
  2665. $result[] = [
  2666. [
  2667. 'id' => 42,
  2668. 'share_type' => \OCP\Share::SHARE_TYPE_CIRCLE,
  2669. 'uid_owner' => 'initiator',
  2670. 'displayname_owner' => 'initiator',
  2671. 'permissions' => 1,
  2672. 'stime' => 946684862,
  2673. 'parent' => null,
  2674. 'expiration' => null,
  2675. 'token' => null,
  2676. 'uid_file_owner' => 'owner',
  2677. 'displayname_file_owner' => 'owner',
  2678. 'note' => '',
  2679. 'label' => null,
  2680. 'path' => 'folder',
  2681. 'item_type' => 'folder',
  2682. 'storage_id' => 'storageId',
  2683. 'storage' => 100,
  2684. 'item_source' => 2,
  2685. 'file_source' => 2,
  2686. 'file_parent' => 1,
  2687. 'file_target' => 'myTarget',
  2688. 'share_with' => 'Circle',
  2689. 'share_with_displayname' => 'Circle (Public circle, circleOwner)',
  2690. 'share_with_avatar' => '',
  2691. 'mail_send' => 0,
  2692. 'mimetype' => 'myFolderMimeType',
  2693. 'hide_download' => 0,
  2694. ], $share, [], false
  2695. ];
  2696. $share = \OC::$server->getShareManager()->newShare();
  2697. $share->setShareType(\OCP\Share::SHARE_TYPE_USER)
  2698. ->setSharedBy('initiator')
  2699. ->setSharedWith('recipient')
  2700. ->setShareOwner('owner')
  2701. ->setPermissions(\OCP\Constants::PERMISSION_READ)
  2702. ->setShareTime(new \DateTime('2000-01-01T00:01:02'))
  2703. ->setTarget('myTarget')
  2704. ->setNote('personal note')
  2705. ->setId(42);
  2706. $result[] = [
  2707. [], $share, [], true
  2708. ];
  2709. $share = \OC::$server->getShareManager()->newShare();
  2710. $share->setShareType(\OCP\Share::SHARE_TYPE_EMAIL)
  2711. ->setSharedBy('initiator')
  2712. ->setSharedWith('user@server.com')
  2713. ->setShareOwner('owner')
  2714. ->setPermissions(\OCP\Constants::PERMISSION_READ)
  2715. ->setNode($folder)
  2716. ->setShareTime(new \DateTime('2000-01-01T00:01:02'))
  2717. ->setTarget('myTarget')
  2718. ->setId(42)
  2719. ->setPassword('password');
  2720. $result[] = [
  2721. [
  2722. 'id' => 42,
  2723. 'share_type' => \OCP\Share::SHARE_TYPE_EMAIL,
  2724. 'uid_owner' => 'initiator',
  2725. 'displayname_owner' => 'initiator',
  2726. 'permissions' => 1,
  2727. 'stime' => 946684862,
  2728. 'parent' => null,
  2729. 'expiration' => null,
  2730. 'token' => null,
  2731. 'uid_file_owner' => 'owner',
  2732. 'displayname_file_owner' => 'owner',
  2733. 'note' => '',
  2734. 'label' => null,
  2735. 'path' => 'folder',
  2736. 'item_type' => 'folder',
  2737. 'storage_id' => 'storageId',
  2738. 'storage' => 100,
  2739. 'item_source' => 2,
  2740. 'file_source' => 2,
  2741. 'file_parent' => 1,
  2742. 'file_target' => 'myTarget',
  2743. 'share_with' => 'user@server.com',
  2744. 'share_with_displayname' => 'mail display name',
  2745. 'mail_send' => 0,
  2746. 'mimetype' => 'myFolderMimeType',
  2747. 'password' => 'password',
  2748. 'send_password_by_talk' => false,
  2749. 'hide_download' => 0,
  2750. ], $share, [], false
  2751. ];
  2752. $share = \OC::$server->getShareManager()->newShare();
  2753. $share->setShareType(\OCP\Share::SHARE_TYPE_EMAIL)
  2754. ->setSharedBy('initiator')
  2755. ->setSharedWith('user@server.com')
  2756. ->setShareOwner('owner')
  2757. ->setPermissions(\OCP\Constants::PERMISSION_READ)
  2758. ->setNode($folder)
  2759. ->setShareTime(new \DateTime('2000-01-01T00:01:02'))
  2760. ->setTarget('myTarget')
  2761. ->setId(42)
  2762. ->setPassword('password')
  2763. ->setSendPasswordByTalk(true);
  2764. $result[] = [
  2765. [
  2766. 'id' => 42,
  2767. 'share_type' => \OCP\Share::SHARE_TYPE_EMAIL,
  2768. 'uid_owner' => 'initiator',
  2769. 'displayname_owner' => 'initiator',
  2770. 'permissions' => 1,
  2771. 'stime' => 946684862,
  2772. 'parent' => null,
  2773. 'expiration' => null,
  2774. 'token' => null,
  2775. 'uid_file_owner' => 'owner',
  2776. 'displayname_file_owner' => 'owner',
  2777. 'note' => '',
  2778. 'label' => null,
  2779. 'path' => 'folder',
  2780. 'item_type' => 'folder',
  2781. 'storage_id' => 'storageId',
  2782. 'storage' => 100,
  2783. 'item_source' => 2,
  2784. 'file_source' => 2,
  2785. 'file_parent' => 1,
  2786. 'file_target' => 'myTarget',
  2787. 'share_with' => 'user@server.com',
  2788. 'share_with_displayname' => 'mail display name',
  2789. 'mail_send' => 0,
  2790. 'mimetype' => 'myFolderMimeType',
  2791. 'password' => 'password',
  2792. 'send_password_by_talk' => true,
  2793. 'hide_download' => 0,
  2794. ], $share, [], false
  2795. ];
  2796. return $result;
  2797. }
  2798. /**
  2799. * @dataProvider dataFormatShare
  2800. *
  2801. * @param array $expects
  2802. * @param \OCP\Share\IShare $share
  2803. * @param array $users
  2804. * @param $exception
  2805. */
  2806. public function testFormatShare(array $expects, \OCP\Share\IShare $share, array $users, $exception) {
  2807. $this->userManager->method('get')->will($this->returnValueMap($users));
  2808. $recipientGroup = $this->createMock('\OCP\IGroup');
  2809. $recipientGroup->method('getDisplayName')->willReturn('recipientGroupDisplayName');
  2810. $this->groupManager->method('get')->will($this->returnValueMap([
  2811. ['recipientGroup', $recipientGroup],
  2812. ]));
  2813. $this->urlGenerator->method('linkToRouteAbsolute')
  2814. ->with('files_sharing.sharecontroller.showShare', ['token' => 'myToken'])
  2815. ->willReturn('myLink');
  2816. $this->rootFolder->method('getUserFolder')
  2817. ->with($this->currentUser)
  2818. ->will($this->returnSelf());
  2819. if (!$exception) {
  2820. $this->rootFolder->method('getById')
  2821. ->with($share->getNodeId())
  2822. ->willReturn([$share->getNode()]);
  2823. $this->rootFolder->method('getRelativePath')
  2824. ->with($share->getNode()->getPath())
  2825. ->will($this->returnArgument(0));
  2826. }
  2827. $cm = $this->createMock(\OCP\Contacts\IManager::class);
  2828. $this->overwriteService(\OCP\Contacts\IManager::class, $cm);
  2829. $cm->method('search')
  2830. ->will($this->returnValueMap([
  2831. ['user@server.com', ['CLOUD'], [],
  2832. [
  2833. [
  2834. 'CLOUD' => [
  2835. 'user@server.com',
  2836. ],
  2837. 'FN' => 'foobar',
  2838. ],
  2839. ],
  2840. ],
  2841. ['user@server.com', ['EMAIL'], [],
  2842. [
  2843. [
  2844. 'EMAIL' => [
  2845. 'user@server.com',
  2846. ],
  2847. 'FN' => 'mail display name',
  2848. ],
  2849. ],
  2850. ],
  2851. ]));
  2852. try {
  2853. $result = $this->invokePrivate($this->ocs, 'formatShare', [$share]);
  2854. $this->assertFalse($exception);
  2855. $this->assertEquals($expects, $result);
  2856. } catch (NotFoundException $e) {
  2857. $this->assertTrue($exception);
  2858. }
  2859. }
  2860. public function dataFormatRoomShare() {
  2861. $file = $this->getMockBuilder(File::class)->getMock();
  2862. $parent = $this->getMockBuilder(Folder::class)->getMock();
  2863. $file->method('getMimeType')->willReturn('myMimeType');
  2864. $file->method('getPath')->willReturn('file');
  2865. $parent->method('getId')->willReturn(1);
  2866. $file->method('getId')->willReturn(3);
  2867. $file->method('getParent')->willReturn($parent);
  2868. $cache = $this->getMockBuilder('OCP\Files\Cache\ICache')->getMock();
  2869. $cache->method('getNumericStorageId')->willReturn(100);
  2870. $storage = $this->getMockBuilder(Storage::class)->getMock();
  2871. $storage->method('getId')->willReturn('storageId');
  2872. $storage->method('getCache')->willReturn($cache);
  2873. $file->method('getStorage')->willReturn($storage);
  2874. $result = [];
  2875. $share = \OC::$server->getShareManager()->newShare();
  2876. $share->setShareType(\OCP\Share::SHARE_TYPE_ROOM)
  2877. ->setSharedWith('recipientRoom')
  2878. ->setSharedBy('initiator')
  2879. ->setShareOwner('owner')
  2880. ->setPermissions(\OCP\Constants::PERMISSION_READ)
  2881. ->setNode($file)
  2882. ->setShareTime(new \DateTime('2000-01-01T00:01:02'))
  2883. ->setTarget('myTarget')
  2884. ->setNote('personal note')
  2885. ->setId(42);
  2886. $result[] = [
  2887. [
  2888. 'id' => 42,
  2889. 'share_type' => \OCP\Share::SHARE_TYPE_ROOM,
  2890. 'uid_owner' => 'initiator',
  2891. 'displayname_owner' => 'initiator',
  2892. 'permissions' => 1,
  2893. 'stime' => 946684862,
  2894. 'parent' => null,
  2895. 'expiration' => null,
  2896. 'token' => null,
  2897. 'uid_file_owner' => 'owner',
  2898. 'displayname_file_owner' => 'owner',
  2899. 'note' => 'personal note',
  2900. 'path' => 'file',
  2901. 'item_type' => 'file',
  2902. 'storage_id' => 'storageId',
  2903. 'storage' => 100,
  2904. 'item_source' => 3,
  2905. 'file_source' => 3,
  2906. 'file_parent' => 1,
  2907. 'file_target' => 'myTarget',
  2908. 'share_with' => 'recipientRoom',
  2909. 'share_with_displayname' => '',
  2910. 'mail_send' => 0,
  2911. 'mimetype' => 'myMimeType',
  2912. 'hide_download' => 0,
  2913. 'label' => '',
  2914. ], $share, false, []
  2915. ];
  2916. $share = \OC::$server->getShareManager()->newShare();
  2917. $share->setShareType(\OCP\Share::SHARE_TYPE_ROOM)
  2918. ->setSharedWith('recipientRoom')
  2919. ->setSharedBy('initiator')
  2920. ->setShareOwner('owner')
  2921. ->setPermissions(\OCP\Constants::PERMISSION_READ)
  2922. ->setNode($file)
  2923. ->setShareTime(new \DateTime('2000-01-01T00:01:02'))
  2924. ->setTarget('myTarget')
  2925. ->setNote('personal note')
  2926. ->setId(42);
  2927. $result[] = [
  2928. [
  2929. 'id' => 42,
  2930. 'share_type' => \OCP\Share::SHARE_TYPE_ROOM,
  2931. 'uid_owner' => 'initiator',
  2932. 'displayname_owner' => 'initiator',
  2933. 'permissions' => 1,
  2934. 'stime' => 946684862,
  2935. 'parent' => null,
  2936. 'expiration' => null,
  2937. 'token' => null,
  2938. 'uid_file_owner' => 'owner',
  2939. 'displayname_file_owner' => 'owner',
  2940. 'note' => 'personal note',
  2941. 'path' => 'file',
  2942. 'item_type' => 'file',
  2943. 'storage_id' => 'storageId',
  2944. 'storage' => 100,
  2945. 'item_source' => 3,
  2946. 'file_source' => 3,
  2947. 'file_parent' => 1,
  2948. 'file_target' => 'myTarget',
  2949. 'share_with' => 'recipientRoom',
  2950. 'share_with_displayname' => 'recipientRoomName',
  2951. 'mail_send' => 0,
  2952. 'mimetype' => 'myMimeType',
  2953. 'hide_download' => 0,
  2954. 'label' => '',
  2955. ], $share, true, [
  2956. 'share_with_displayname' => 'recipientRoomName'
  2957. ]
  2958. ];
  2959. return $result;
  2960. }
  2961. /**
  2962. * @dataProvider dataFormatRoomShare
  2963. *
  2964. * @param array $expects
  2965. * @param \OCP\Share\IShare $share
  2966. * @param bool $helperAvailable
  2967. * @param array $formatShareByHelper
  2968. */
  2969. public function testFormatRoomShare(array $expects, \OCP\Share\IShare $share, bool $helperAvailable, array $formatShareByHelper) {
  2970. $this->rootFolder->method('getUserFolder')
  2971. ->with($this->currentUser)
  2972. ->will($this->returnSelf());
  2973. $this->rootFolder->method('getById')
  2974. ->with($share->getNodeId())
  2975. ->willReturn([$share->getNode()]);
  2976. $this->rootFolder->method('getRelativePath')
  2977. ->with($share->getNode()->getPath())
  2978. ->will($this->returnArgument(0));
  2979. if (!$helperAvailable) {
  2980. $this->appManager->method('isEnabledForUser')
  2981. ->with('spreed')
  2982. ->willReturn(false);
  2983. } else {
  2984. $this->appManager->method('isEnabledForUser')
  2985. ->with('spreed')
  2986. ->willReturn(true);
  2987. $helper = $this->getMockBuilder('\OCA\Spreed\Share\Helper\ShareAPIController')
  2988. ->setMethods(array('formatShare'))
  2989. ->getMock();
  2990. $helper->method('formatShare')
  2991. ->with($share)
  2992. ->willReturn($formatShareByHelper);
  2993. $this->serverContainer->method('query')
  2994. ->with('\OCA\Spreed\Share\Helper\ShareAPIController')
  2995. ->willReturn($helper);
  2996. }
  2997. $result = $this->invokePrivate($this->ocs, 'formatShare', [$share]);
  2998. $this->assertEquals($expects, $result);
  2999. }
  3000. }