ManagerTest.php 96 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205
  1. <?php
  2. /**
  3. * @author Roeland Jago Douma <rullzer@owncloud.com>
  4. *
  5. * @copyright Copyright (c) 2015, ownCloud, Inc.
  6. * @license AGPL-3.0
  7. *
  8. * This code is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License, version 3,
  10. * as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License, version 3,
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>
  19. *
  20. */
  21. namespace Test\Share20;
  22. use OC\Files\Mount\MoveableMount;
  23. use OC\HintException;
  24. use OC\Share20\DefaultShareProvider;
  25. use OCP\Defaults;
  26. use OCP\Files\File;
  27. use OCP\Files\Folder;
  28. use OCP\Files\IRootFolder;
  29. use OCP\Files\Mount\IMountPoint;
  30. use OCP\Files\Node;
  31. use OCP\Files\Storage;
  32. use OCP\IGroup;
  33. use OCP\IServerContainer;
  34. use OCP\IURLGenerator;
  35. use OCP\IUser;
  36. use OCP\IUserManager;
  37. use OCP\L10N\IFactory;
  38. use OCP\Mail\IMailer;
  39. use OCP\Share\Exceptions\ShareNotFound;
  40. use OCP\Share\IProviderFactory;
  41. use OCP\Share\IShare;
  42. use OC\Share20\Manager;
  43. use OC\Share20\Exception;
  44. use OC\Share20\Share;
  45. use OCP\IL10N;
  46. use OCP\ILogger;
  47. use OCP\IConfig;
  48. use OCP\Share\IShareProvider;
  49. use OCP\Security\ISecureRandom;
  50. use OCP\Security\IHasher;
  51. use OCP\Files\Mount\IMountManager;
  52. use OCP\IGroupManager;
  53. use Symfony\Component\EventDispatcher\EventDispatcher;
  54. use Symfony\Component\EventDispatcher\GenericEvent;
  55. /**
  56. * Class ManagerTest
  57. *
  58. * @package Test\Share20
  59. * @group DB
  60. */
  61. class ManagerTest extends \Test\TestCase {
  62. /** @var Manager */
  63. protected $manager;
  64. /** @var ILogger|\PHPUnit_Framework_MockObject_MockObject */
  65. protected $logger;
  66. /** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */
  67. protected $config;
  68. /** @var ISecureRandom|\PHPUnit_Framework_MockObject_MockObject */
  69. protected $secureRandom;
  70. /** @var IHasher|\PHPUnit_Framework_MockObject_MockObject */
  71. protected $hasher;
  72. /** @var IShareProvider|\PHPUnit_Framework_MockObject_MockObject */
  73. protected $defaultProvider;
  74. /** @var IMountManager|\PHPUnit_Framework_MockObject_MockObject */
  75. protected $mountManager;
  76. /** @var IGroupManager|\PHPUnit_Framework_MockObject_MockObject */
  77. protected $groupManager;
  78. /** @var IL10N|\PHPUnit_Framework_MockObject_MockObject */
  79. protected $l;
  80. /** @var IFactory|\PHPUnit_Framework_MockObject_MockObject */
  81. protected $l10nFactory;
  82. /** @var DummyFactory */
  83. protected $factory;
  84. /** @var IUserManager|\PHPUnit_Framework_MockObject_MockObject */
  85. protected $userManager;
  86. /** @var IRootFolder | \PHPUnit_Framework_MockObject_MockObject */
  87. protected $rootFolder;
  88. /** @var EventDispatcher | \PHPUnit_Framework_MockObject_MockObject */
  89. protected $eventDispatcher;
  90. /** @var IMailer|\PHPUnit_Framework_MockObject_MockObject */
  91. protected $mailer;
  92. /** @var IURLGenerator|\PHPUnit_Framework_MockObject_MockObject */
  93. protected $urlGenerator;
  94. /** @var \OC_Defaults|\PHPUnit_Framework_MockObject_MockObject */
  95. protected $defaults;
  96. public function setUp() {
  97. $this->logger = $this->createMock(ILogger::class);
  98. $this->config = $this->createMock(IConfig::class);
  99. $this->secureRandom = $this->createMock(ISecureRandom::class);
  100. $this->hasher = $this->createMock(IHasher::class);
  101. $this->mountManager = $this->createMock(IMountManager::class);
  102. $this->groupManager = $this->createMock(IGroupManager::class);
  103. $this->userManager = $this->createMock(IUserManager::class);
  104. $this->rootFolder = $this->createMock(IRootFolder::class);
  105. $this->eventDispatcher = $this->createMock(EventDispatcher::class);
  106. $this->mailer = $this->createMock(IMailer::class);
  107. $this->urlGenerator = $this->createMock(IURLGenerator::class);
  108. $this->defaults = $this->createMock(\OC_Defaults::class);
  109. $this->l10nFactory = $this->createMock(IFactory::class);
  110. $this->l = $this->createMock(IL10N::class);
  111. $this->l->method('t')
  112. ->will($this->returnCallback(function($text, $parameters = []) {
  113. return vsprintf($text, $parameters);
  114. }));
  115. $this->factory = new DummyFactory(\OC::$server);
  116. $this->manager = new Manager(
  117. $this->logger,
  118. $this->config,
  119. $this->secureRandom,
  120. $this->hasher,
  121. $this->mountManager,
  122. $this->groupManager,
  123. $this->l,
  124. $this->l10nFactory,
  125. $this->factory,
  126. $this->userManager,
  127. $this->rootFolder,
  128. $this->eventDispatcher,
  129. $this->mailer,
  130. $this->urlGenerator,
  131. $this->defaults
  132. );
  133. $this->defaultProvider = $this->createMock(DefaultShareProvider::class);
  134. $this->defaultProvider->method('identifier')->willReturn('default');
  135. $this->factory->setProvider($this->defaultProvider);
  136. }
  137. /**
  138. * @return \PHPUnit_Framework_MockObject_MockBuilder
  139. */
  140. private function createManagerMock() {
  141. return $this->getMockBuilder('\OC\Share20\Manager')
  142. ->setConstructorArgs([
  143. $this->logger,
  144. $this->config,
  145. $this->secureRandom,
  146. $this->hasher,
  147. $this->mountManager,
  148. $this->groupManager,
  149. $this->l,
  150. $this->l10nFactory,
  151. $this->factory,
  152. $this->userManager,
  153. $this->rootFolder,
  154. $this->eventDispatcher,
  155. $this->mailer,
  156. $this->urlGenerator,
  157. $this->defaults
  158. ]);
  159. }
  160. /**
  161. * @expectedException \InvalidArgumentException
  162. */
  163. public function testDeleteNoShareId() {
  164. $share = $this->manager->newShare();
  165. $this->manager->deleteShare($share);
  166. }
  167. public function dataTestDelete() {
  168. $user = $this->createMock(IUser::class);
  169. $user->method('getUID')->willReturn('sharedWithUser');
  170. $group = $this->createMock(IGroup::class);
  171. $group->method('getGID')->willReturn('sharedWithGroup');
  172. return [
  173. [\OCP\Share::SHARE_TYPE_USER, 'sharedWithUser'],
  174. [\OCP\Share::SHARE_TYPE_GROUP, 'sharedWithGroup'],
  175. [\OCP\Share::SHARE_TYPE_LINK, ''],
  176. [\OCP\Share::SHARE_TYPE_REMOTE, 'foo@bar.com'],
  177. ];
  178. }
  179. /**
  180. * @dataProvider dataTestDelete
  181. */
  182. public function testDelete($shareType, $sharedWith) {
  183. $manager = $this->createManagerMock()
  184. ->setMethods(['getShareById', 'deleteChildren'])
  185. ->getMock();
  186. $path = $this->createMock(File::class);
  187. $path->method('getId')->willReturn(1);
  188. $share = $this->manager->newShare();
  189. $share->setId(42)
  190. ->setProviderId('prov')
  191. ->setShareType($shareType)
  192. ->setSharedWith($sharedWith)
  193. ->setSharedBy('sharedBy')
  194. ->setNode($path)
  195. ->setTarget('myTarget');
  196. $manager->expects($this->once())->method('deleteChildren')->with($share);
  197. $this->defaultProvider
  198. ->expects($this->once())
  199. ->method('delete')
  200. ->with($share);
  201. $this->eventDispatcher->expects($this->at(0))
  202. ->method('dispatch')
  203. ->with(
  204. 'OCP\Share::preUnshare',
  205. $this->callBack(function(GenericEvent $e) use ($share) {
  206. return $e->getSubject() === $share;
  207. })
  208. );
  209. $this->eventDispatcher->expects($this->at(1))
  210. ->method('dispatch')
  211. ->with(
  212. 'OCP\Share::postUnshare',
  213. $this->callBack(function(GenericEvent $e) use ($share) {
  214. return $e->getSubject() === $share &&
  215. $e->getArgument('deletedShares') === [$share];
  216. })
  217. );
  218. $manager->deleteShare($share);
  219. }
  220. public function testDeleteLazyShare() {
  221. $manager = $this->createManagerMock()
  222. ->setMethods(['getShareById', 'deleteChildren'])
  223. ->getMock();
  224. $share = $this->manager->newShare();
  225. $share->setId(42)
  226. ->setProviderId('prov')
  227. ->setShareType(\OCP\Share::SHARE_TYPE_USER)
  228. ->setSharedWith('sharedWith')
  229. ->setSharedBy('sharedBy')
  230. ->setShareOwner('shareOwner')
  231. ->setTarget('myTarget')
  232. ->setNodeId(1)
  233. ->setNodeType('file');
  234. $this->rootFolder->expects($this->never())->method($this->anything());
  235. $manager->expects($this->once())->method('deleteChildren')->with($share);
  236. $this->defaultProvider
  237. ->expects($this->once())
  238. ->method('delete')
  239. ->with($share);
  240. $this->eventDispatcher->expects($this->at(0))
  241. ->method('dispatch')
  242. ->with(
  243. 'OCP\Share::preUnshare',
  244. $this->callBack(function(GenericEvent $e) use ($share) {
  245. return $e->getSubject() === $share;
  246. })
  247. );
  248. $this->eventDispatcher->expects($this->at(1))
  249. ->method('dispatch')
  250. ->with(
  251. 'OCP\Share::postUnshare',
  252. $this->callBack(function(GenericEvent $e) use ($share) {
  253. return $e->getSubject() === $share &&
  254. $e->getArgument('deletedShares') === [$share];
  255. })
  256. );
  257. $manager->deleteShare($share);
  258. }
  259. public function testDeleteNested() {
  260. $manager = $this->createManagerMock()
  261. ->setMethods(['getShareById'])
  262. ->getMock();
  263. $path = $this->createMock(File::class);
  264. $path->method('getId')->willReturn(1);
  265. $share1 = $this->manager->newShare();
  266. $share1->setId(42)
  267. ->setProviderId('prov')
  268. ->setShareType(\OCP\Share::SHARE_TYPE_USER)
  269. ->setSharedWith('sharedWith1')
  270. ->setSharedBy('sharedBy1')
  271. ->setNode($path)
  272. ->setTarget('myTarget1');
  273. $share2 = $this->manager->newShare();
  274. $share2->setId(43)
  275. ->setProviderId('prov')
  276. ->setShareType(\OCP\Share::SHARE_TYPE_GROUP)
  277. ->setSharedWith('sharedWith2')
  278. ->setSharedBy('sharedBy2')
  279. ->setNode($path)
  280. ->setTarget('myTarget2')
  281. ->setParent(42);
  282. $share3 = $this->manager->newShare();
  283. $share3->setId(44)
  284. ->setProviderId('prov')
  285. ->setShareType(\OCP\Share::SHARE_TYPE_LINK)
  286. ->setSharedBy('sharedBy3')
  287. ->setNode($path)
  288. ->setTarget('myTarget3')
  289. ->setParent(43);
  290. $this->defaultProvider
  291. ->method('getChildren')
  292. ->will($this->returnValueMap([
  293. [$share1, [$share2]],
  294. [$share2, [$share3]],
  295. [$share3, []],
  296. ]));
  297. $this->defaultProvider
  298. ->method('delete')
  299. ->withConsecutive($share3, $share2, $share1);
  300. $this->eventDispatcher->expects($this->at(0))
  301. ->method('dispatch')
  302. ->with(
  303. 'OCP\Share::preUnshare',
  304. $this->callBack(function(GenericEvent $e) use ($share1) {
  305. return $e->getSubject() === $share1;
  306. })
  307. );
  308. $this->eventDispatcher->expects($this->at(1))
  309. ->method('dispatch')
  310. ->with(
  311. 'OCP\Share::postUnshare',
  312. $this->callBack(function(GenericEvent $e) use ($share1, $share2, $share3) {
  313. return $e->getSubject() === $share1 &&
  314. $e->getArgument('deletedShares') === [$share3, $share2, $share1];
  315. })
  316. );
  317. $manager->deleteShare($share1);
  318. }
  319. public function testDeleteFromSelf() {
  320. $manager = $this->createManagerMock()
  321. ->setMethods(['getShareById'])
  322. ->getMock();
  323. $recipientId = 'unshareFrom';
  324. $share = $this->manager->newShare();
  325. $share->setId(42)
  326. ->setProviderId('prov')
  327. ->setShareType(\OCP\Share::SHARE_TYPE_USER)
  328. ->setSharedWith('sharedWith')
  329. ->setSharedBy('sharedBy')
  330. ->setShareOwner('shareOwner')
  331. ->setTarget('myTarget')
  332. ->setNodeId(1)
  333. ->setNodeType('file');
  334. $this->defaultProvider
  335. ->expects($this->once())
  336. ->method('deleteFromSelf')
  337. ->with($share, $recipientId);
  338. $this->eventDispatcher->expects($this->at(0))
  339. ->method('dispatch')
  340. ->with(
  341. 'OCP\Share::postUnshareFromSelf',
  342. $this->callBack(function(GenericEvent $e) use ($share) {
  343. return $e->getSubject() === $share;
  344. })
  345. );
  346. $manager->deleteFromSelf($share, $recipientId);
  347. }
  348. public function testDeleteChildren() {
  349. $manager = $this->createManagerMock()
  350. ->setMethods(['deleteShare'])
  351. ->getMock();
  352. $share = $this->createMock(IShare::class);
  353. $share->method('getShareType')->willReturn(\OCP\Share::SHARE_TYPE_USER);
  354. $child1 = $this->createMock(IShare::class);
  355. $child1->method('getShareType')->willReturn(\OCP\Share::SHARE_TYPE_USER);
  356. $child2 = $this->createMock(IShare::class);
  357. $child2->method('getShareType')->willReturn(\OCP\Share::SHARE_TYPE_USER);
  358. $child3 = $this->createMock(IShare::class);
  359. $child3->method('getShareType')->willReturn(\OCP\Share::SHARE_TYPE_USER);
  360. $shares = [
  361. $child1,
  362. $child2,
  363. $child3,
  364. ];
  365. $this->defaultProvider
  366. ->expects($this->exactly(4))
  367. ->method('getChildren')
  368. ->will($this->returnCallback(function($_share) use ($share, $shares) {
  369. if ($_share === $share) {
  370. return $shares;
  371. }
  372. return [];
  373. }));
  374. $this->defaultProvider
  375. ->expects($this->exactly(3))
  376. ->method('delete')
  377. ->withConsecutive($child1, $child2, $child3);
  378. $result = self::invokePrivate($manager, 'deleteChildren', [$share]);
  379. $this->assertSame($shares, $result);
  380. }
  381. public function testGetShareById() {
  382. $share = $this->createMock(IShare::class);
  383. $this->defaultProvider
  384. ->expects($this->once())
  385. ->method('getShareById')
  386. ->with(42)
  387. ->willReturn($share);
  388. $this->assertEquals($share, $this->manager->getShareById('default:42'));
  389. }
  390. /**
  391. * @expectedException \OCP\Share\Exceptions\ShareNotFound
  392. */
  393. public function testGetExpiredShareById() {
  394. $manager = $this->createManagerMock()
  395. ->setMethods(['deleteShare'])
  396. ->getMock();
  397. $date = new \DateTime();
  398. $date->setTime(0,0,0);
  399. $share = $this->manager->newShare();
  400. $share->setExpirationDate($date)
  401. ->setShareType(\OCP\Share::SHARE_TYPE_LINK);
  402. $this->defaultProvider->expects($this->once())
  403. ->method('getShareById')
  404. ->with('42')
  405. ->willReturn($share);
  406. $manager->expects($this->once())
  407. ->method('deleteShare')
  408. ->with($share);
  409. $manager->getShareById('default:42');
  410. }
  411. /**
  412. * @expectedException \InvalidArgumentException
  413. * @expectedExceptionMessage Passwords are enforced for link shares
  414. */
  415. public function testVerifyPasswordNullButEnforced() {
  416. $this->config->method('getAppValue')->will($this->returnValueMap([
  417. ['core', 'shareapi_enforce_links_password', 'no', 'yes'],
  418. ]));
  419. self::invokePrivate($this->manager, 'verifyPassword', [null]);
  420. }
  421. public function testVerifyPasswordNull() {
  422. $this->config->method('getAppValue')->will($this->returnValueMap([
  423. ['core', 'shareapi_enforce_links_password', 'no', 'no'],
  424. ]));
  425. $result = self::invokePrivate($this->manager, 'verifyPassword', [null]);
  426. $this->assertNull($result);
  427. }
  428. public function testVerifyPasswordHook() {
  429. $this->config->method('getAppValue')->will($this->returnValueMap([
  430. ['core', 'shareapi_enforce_links_password', 'no', 'no'],
  431. ]));
  432. $this->eventDispatcher->expects($this->once())->method('dispatch')
  433. ->willReturnCallback(function($eventName, GenericEvent $event) {
  434. $this->assertSame('OCP\PasswordPolicy::validate', $eventName);
  435. $this->assertSame('password', $event->getSubject());
  436. }
  437. );
  438. $result = self::invokePrivate($this->manager, 'verifyPassword', ['password']);
  439. $this->assertNull($result);
  440. }
  441. /**
  442. * @expectedException \Exception
  443. * @expectedExceptionMessage password not accepted
  444. */
  445. public function testVerifyPasswordHookFails() {
  446. $this->config->method('getAppValue')->will($this->returnValueMap([
  447. ['core', 'shareapi_enforce_links_password', 'no', 'no'],
  448. ]));
  449. $this->eventDispatcher->expects($this->once())->method('dispatch')
  450. ->willReturnCallback(function($eventName, GenericEvent $event) {
  451. $this->assertSame('OCP\PasswordPolicy::validate', $eventName);
  452. $this->assertSame('password', $event->getSubject());
  453. throw new HintException('message', 'password not accepted');
  454. }
  455. );
  456. self::invokePrivate($this->manager, 'verifyPassword', ['password']);
  457. }
  458. public function createShare($id, $type, $path, $sharedWith, $sharedBy, $shareOwner,
  459. $permissions, $expireDate = null, $password = null) {
  460. $share = $this->createMock(IShare::class);
  461. $share->method('getShareType')->willReturn($type);
  462. $share->method('getSharedWith')->willReturn($sharedWith);
  463. $share->method('getSharedBy')->willReturn($sharedBy);
  464. $share->method('getShareOwner')->willReturn($shareOwner);
  465. $share->method('getNode')->willReturn($path);
  466. $share->method('getPermissions')->willReturn($permissions);
  467. $share->method('getExpirationDate')->willReturn($expireDate);
  468. $share->method('getPassword')->willReturn($password);
  469. return $share;
  470. }
  471. public function dataGeneralChecks() {
  472. $user0 = 'user0';
  473. $user2 = 'user1';
  474. $group0 = 'group0';
  475. $file = $this->createMock(File::class);
  476. $node = $this->createMock(Node::class);
  477. $data = [
  478. [$this->createShare(null, \OCP\Share::SHARE_TYPE_USER, $file, null, $user0, $user0, 31, null, null), 'SharedWith is not a valid user', true],
  479. [$this->createShare(null, \OCP\Share::SHARE_TYPE_USER, $file, $group0, $user0, $user0, 31, null, null), 'SharedWith is not a valid user', true],
  480. [$this->createShare(null, \OCP\Share::SHARE_TYPE_USER, $file, 'foo@bar.com', $user0, $user0, 31, null, null), 'SharedWith is not a valid user', true],
  481. [$this->createShare(null, \OCP\Share::SHARE_TYPE_GROUP, $file, null, $user0, $user0, 31, null, null), 'SharedWith is not a valid group', true],
  482. [$this->createShare(null, \OCP\Share::SHARE_TYPE_GROUP, $file, $user2, $user0, $user0, 31, null, null), 'SharedWith is not a valid group', true],
  483. [$this->createShare(null, \OCP\Share::SHARE_TYPE_GROUP, $file, 'foo@bar.com', $user0, $user0, 31, null, null), 'SharedWith is not a valid group', true],
  484. [$this->createShare(null, \OCP\Share::SHARE_TYPE_LINK, $file, $user2, $user0, $user0, 31, null, null), 'SharedWith should be empty', true],
  485. [$this->createShare(null, \OCP\Share::SHARE_TYPE_LINK, $file, $group0, $user0, $user0, 31, null, null), 'SharedWith should be empty', true],
  486. [$this->createShare(null, \OCP\Share::SHARE_TYPE_LINK, $file, 'foo@bar.com', $user0, $user0, 31, null, null), 'SharedWith should be empty', true],
  487. [$this->createShare(null, -1, $file, null, $user0, $user0, 31, null, null), 'unknown share type', true],
  488. [$this->createShare(null, \OCP\Share::SHARE_TYPE_USER, $file, $user2, null, $user0, 31, null, null), 'SharedBy should be set', true],
  489. [$this->createShare(null, \OCP\Share::SHARE_TYPE_GROUP, $file, $group0, null, $user0, 31, null, null), 'SharedBy should be set', true],
  490. [$this->createShare(null, \OCP\Share::SHARE_TYPE_LINK, $file, null, null, $user0, 31, null, null), 'SharedBy should be set', true],
  491. [$this->createShare(null, \OCP\Share::SHARE_TYPE_USER, $file, $user0, $user0, $user0, 31, null, null), 'Can’t share with yourself', true],
  492. [$this->createShare(null, \OCP\Share::SHARE_TYPE_USER, null, $user2, $user0, $user0, 31, null, null), 'Path should be set', true],
  493. [$this->createShare(null, \OCP\Share::SHARE_TYPE_GROUP, null, $group0, $user0, $user0, 31, null, null), 'Path should be set', true],
  494. [$this->createShare(null, \OCP\Share::SHARE_TYPE_LINK, null, null, $user0, $user0, 31, null, null), 'Path should be set', true],
  495. [$this->createShare(null, \OCP\Share::SHARE_TYPE_USER, $node, $user2, $user0, $user0, 31, null, null), 'Path should be either a file or a folder', true],
  496. [$this->createShare(null, \OCP\Share::SHARE_TYPE_GROUP, $node, $group0, $user0, $user0, 31, null, null), 'Path should be either a file or a folder', true],
  497. [$this->createShare(null, \OCP\Share::SHARE_TYPE_LINK, $node, null, $user0, $user0, 31, null, null), 'Path should be either a file or a folder', true],
  498. ];
  499. $nonShareAble = $this->createMock(Folder::class);
  500. $nonShareAble->method('isShareable')->willReturn(false);
  501. $nonShareAble->method('getPath')->willReturn('path');
  502. $data[] = [$this->createShare(null, \OCP\Share::SHARE_TYPE_USER, $nonShareAble, $user2, $user0, $user0, 31, null, null), 'You are not allowed to share path', true];
  503. $data[] = [$this->createShare(null, \OCP\Share::SHARE_TYPE_GROUP, $nonShareAble, $group0, $user0, $user0, 31, null, null), 'You are not allowed to share path', true];
  504. $data[] = [$this->createShare(null, \OCP\Share::SHARE_TYPE_LINK, $nonShareAble, null, $user0, $user0, 31, null, null), 'You are not allowed to share path', true];
  505. $limitedPermssions = $this->createMock(File::class);
  506. $limitedPermssions->method('isShareable')->willReturn(true);
  507. $limitedPermssions->method('getPermissions')->willReturn(\OCP\Constants::PERMISSION_READ);
  508. $limitedPermssions->method('getPath')->willReturn('path');
  509. $data[] = [$this->createShare(null, \OCP\Share::SHARE_TYPE_USER, $limitedPermssions, $user2, $user0, $user0, null, null, null), 'A share requires permissions', true];
  510. $data[] = [$this->createShare(null, \OCP\Share::SHARE_TYPE_GROUP, $limitedPermssions, $group0, $user0, $user0, null, null, null), 'A share requires permissions', true];
  511. $data[] = [$this->createShare(null, \OCP\Share::SHARE_TYPE_LINK, $limitedPermssions, null, $user0, $user0, null, null, null), 'A share requires permissions', true];
  512. $mount = $this->createMock(MoveableMount::class);
  513. $limitedPermssions->method('getMountPoint')->willReturn($mount);
  514. $data[] = [$this->createShare(null, \OCP\Share::SHARE_TYPE_USER, $limitedPermssions, $user2, $user0, $user0, 31, null, null), 'Can’t increase permissions of path', true];
  515. $data[] = [$this->createShare(null, \OCP\Share::SHARE_TYPE_GROUP, $limitedPermssions, $group0, $user0, $user0, 17, null, null), 'Can’t increase permissions of path', true];
  516. $data[] = [$this->createShare(null, \OCP\Share::SHARE_TYPE_LINK, $limitedPermssions, null, $user0, $user0, 3, null, null), 'Can’t increase permissions of path', true];
  517. $nonMoveableMountPermssions = $this->createMock(Folder::class);
  518. $nonMoveableMountPermssions->method('isShareable')->willReturn(true);
  519. $nonMoveableMountPermssions->method('getPermissions')->willReturn(\OCP\Constants::PERMISSION_READ);
  520. $nonMoveableMountPermssions->method('getPath')->willReturn('path');
  521. $data[] = [$this->createShare(null, \OCP\Share::SHARE_TYPE_USER, $nonMoveableMountPermssions, $user2, $user0, $user0, 11, null, null), 'Can’t increase permissions of path', false];
  522. $data[] = [$this->createShare(null, \OCP\Share::SHARE_TYPE_GROUP, $nonMoveableMountPermssions, $group0, $user0, $user0, 11, null, null), 'Can’t increase permissions of path', false];
  523. $rootFolder = $this->createMock(Folder::class);
  524. $rootFolder->method('isShareable')->willReturn(true);
  525. $rootFolder->method('getPermissions')->willReturn(\OCP\Constants::PERMISSION_ALL);
  526. $rootFolder->method('getPath')->willReturn('myrootfolder');
  527. $data[] = [$this->createShare(null, \OCP\Share::SHARE_TYPE_USER, $rootFolder, $user2, $user0, $user0, 30, null, null), 'You can’t share your root folder', true];
  528. $data[] = [$this->createShare(null, \OCP\Share::SHARE_TYPE_GROUP, $rootFolder, $group0, $user0, $user0, 2, null, null), 'You can’t share your root folder', true];
  529. $data[] = [$this->createShare(null, \OCP\Share::SHARE_TYPE_LINK, $rootFolder, null, $user0, $user0, 16, null, null), 'You can’t share your root folder', true];
  530. $allPermssions = $this->createMock(Folder::class);
  531. $allPermssions->method('isShareable')->willReturn(true);
  532. $allPermssions->method('getPermissions')->willReturn(\OCP\Constants::PERMISSION_ALL);
  533. $data[] = [$this->createShare(null, \OCP\Share::SHARE_TYPE_USER, $allPermssions, $user2, $user0, $user0, 30, null, null), 'Shares need at least read permissions', true];
  534. $data[] = [$this->createShare(null, \OCP\Share::SHARE_TYPE_GROUP, $allPermssions, $group0, $user0, $user0, 2, null, null), 'Shares need at least read permissions', true];
  535. $data[] = [$this->createShare(null, \OCP\Share::SHARE_TYPE_USER, $allPermssions, $user2, $user0, $user0, 31, null, null), null, false];
  536. $data[] = [$this->createShare(null, \OCP\Share::SHARE_TYPE_GROUP, $allPermssions, $group0, $user0, $user0, 3, null, null), null, false];
  537. $data[] = [$this->createShare(null, \OCP\Share::SHARE_TYPE_LINK, $allPermssions, null, $user0, $user0, 17, null, null), null, false];
  538. return $data;
  539. }
  540. /**
  541. * @dataProvider dataGeneralChecks
  542. *
  543. * @param $share
  544. * @param $exceptionMessage
  545. * @param $exception
  546. */
  547. public function testGeneralChecks($share, $exceptionMessage, $exception) {
  548. $thrown = null;
  549. $this->userManager->method('userExists')->will($this->returnValueMap([
  550. ['user0', true],
  551. ['user1', true],
  552. ]));
  553. $this->groupManager->method('groupExists')->will($this->returnValueMap([
  554. ['group0', true],
  555. ]));
  556. $userFolder = $this->createMock(Folder::class);
  557. $userFolder->method('getPath')->willReturn('myrootfolder');
  558. $this->rootFolder->method('getUserFolder')->willReturn($userFolder);
  559. try {
  560. self::invokePrivate($this->manager, 'generalCreateChecks', [$share]);
  561. $thrown = false;
  562. } catch (\OCP\Share\Exceptions\GenericShareException $e) {
  563. $this->assertEquals($exceptionMessage, $e->getHint());
  564. $thrown = true;
  565. } catch(\InvalidArgumentException $e) {
  566. $this->assertEquals($exceptionMessage, $e->getMessage());
  567. $thrown = true;
  568. }
  569. $this->assertSame($exception, $thrown);
  570. }
  571. /**
  572. * @expectedException \InvalidArgumentException
  573. * @expectedExceptionMessage You can’t share your root folder
  574. */
  575. public function testGeneralCheckShareRoot() {
  576. $thrown = null;
  577. $this->userManager->method('userExists')->will($this->returnValueMap([
  578. ['user0', true],
  579. ['user1', true],
  580. ]));
  581. $userFolder = $this->createMock(Folder::class);
  582. $userFolder->method('isSubNode')->with($userFolder)->willReturn(false);
  583. $this->rootFolder->method('getUserFolder')->willReturn($userFolder);
  584. $share = $this->manager->newShare();
  585. $share->setShareType(\OCP\Share::SHARE_TYPE_USER)
  586. ->setSharedWith('user0')
  587. ->setSharedBy('user1')
  588. ->setNode($userFolder);
  589. self::invokePrivate($this->manager, 'generalCreateChecks', [$share]);
  590. }
  591. /**
  592. * @expectedException \OCP\Share\Exceptions\GenericShareException
  593. * @expectedExceptionMessage Expiration date is in the past
  594. */
  595. public function testvalidateExpirationDateInPast() {
  596. // Expire date in the past
  597. $past = new \DateTime();
  598. $past->sub(new \DateInterval('P1D'));
  599. $share = $this->manager->newShare();
  600. $share->setExpirationDate($past);
  601. self::invokePrivate($this->manager, 'validateExpirationDate', [$share]);
  602. }
  603. /**
  604. * @expectedException \InvalidArgumentException
  605. * @expectedExceptionMessage Expiration date is enforced
  606. */
  607. public function testvalidateExpirationDateEnforceButNotSet() {
  608. $share = $this->manager->newShare();
  609. $share->setProviderId('foo')->setId('bar');
  610. $this->config->method('getAppValue')
  611. ->will($this->returnValueMap([
  612. ['core', 'shareapi_default_expire_date', 'no', 'yes'],
  613. ['core', 'shareapi_enforce_expire_date', 'no', 'yes'],
  614. ]));
  615. self::invokePrivate($this->manager, 'validateExpirationDate', [$share]);
  616. }
  617. public function testvalidateExpirationDateEnforceButNotEnabledAndNotSet() {
  618. $share = $this->manager->newShare();
  619. $share->setProviderId('foo')->setId('bar');
  620. $this->config->method('getAppValue')
  621. ->will($this->returnValueMap([
  622. ['core', 'shareapi_enforce_expire_date', 'no', 'yes'],
  623. ]));
  624. self::invokePrivate($this->manager, 'validateExpirationDate', [$share]);
  625. $this->assertNull($share->getExpirationDate());
  626. }
  627. public function testvalidateExpirationDateEnforceButNotSetNewShare() {
  628. $share = $this->manager->newShare();
  629. $this->config->method('getAppValue')
  630. ->will($this->returnValueMap([
  631. ['core', 'shareapi_enforce_expire_date', 'no', 'yes'],
  632. ['core', 'shareapi_expire_after_n_days', '7', '3'],
  633. ['core', 'shareapi_default_expire_date', 'no', 'yes'],
  634. ]));
  635. $expected = new \DateTime();
  636. $expected->setTime(0,0,0);
  637. $expected->add(new \DateInterval('P3D'));
  638. self::invokePrivate($this->manager, 'validateExpirationDate', [$share]);
  639. $this->assertNotNull($share->getExpirationDate());
  640. $this->assertEquals($expected, $share->getExpirationDate());
  641. }
  642. public function testvalidateExpirationDateEnforceToFarIntoFuture() {
  643. // Expire date in the past
  644. $future = new \DateTime();
  645. $future->add(new \DateInterval('P7D'));
  646. $share = $this->manager->newShare();
  647. $share->setExpirationDate($future);
  648. $this->config->method('getAppValue')
  649. ->will($this->returnValueMap([
  650. ['core', 'shareapi_enforce_expire_date', 'no', 'yes'],
  651. ['core', 'shareapi_expire_after_n_days', '7', '3'],
  652. ]));
  653. try {
  654. self::invokePrivate($this->manager, 'validateExpirationDate', [$share]);
  655. $this->addToAssertionCount(1);
  656. } catch (\OCP\Share\Exceptions\GenericShareException $e) {
  657. $this->assertEquals('Cannot set expiration date more than 3 days in the future', $e->getMessage());
  658. $this->assertEquals('Cannot set expiration date more than 3 days in the future', $e->getHint());
  659. $this->assertEquals(404, $e->getCode());
  660. }
  661. }
  662. public function testvalidateExpirationDateEnforceValid() {
  663. // Expire date in the past
  664. $future = new \DateTime();
  665. $future->add(new \DateInterval('P2D'));
  666. $future->setTime(0,0,0);
  667. $expected = clone $future;
  668. $future->setTime(1,2,3);
  669. $share = $this->manager->newShare();
  670. $share->setExpirationDate($future);
  671. $this->config->method('getAppValue')
  672. ->will($this->returnValueMap([
  673. ['core', 'shareapi_enforce_expire_date', 'no', 'yes'],
  674. ['core', 'shareapi_expire_after_n_days', '7', '3'],
  675. ]));
  676. $hookListner = $this->getMockBuilder('Dummy')->setMethods(['listener'])->getMock();
  677. \OCP\Util::connectHook('\OC\Share', 'verifyExpirationDate', $hookListner, 'listener');
  678. $hookListner->expects($this->once())->method('listener')->with($this->callback(function ($data) use ($future) {
  679. return $data['expirationDate'] == $future;
  680. }));
  681. self::invokePrivate($this->manager, 'validateExpirationDate', [$share]);
  682. $this->assertEquals($expected, $share->getExpirationDate());
  683. }
  684. public function testvalidateExpirationDateNoDateNoDefaultNull() {
  685. $date = new \DateTime();
  686. $date->add(new \DateInterval('P5D'));
  687. $expected = clone $date;
  688. $expected->setTime(0,0,0);
  689. $share = $this->manager->newShare();
  690. $share->setExpirationDate($date);
  691. $hookListner = $this->getMockBuilder('Dummy')->setMethods(['listener'])->getMock();
  692. \OCP\Util::connectHook('\OC\Share', 'verifyExpirationDate', $hookListner, 'listener');
  693. $hookListner->expects($this->once())->method('listener')->with($this->callback(function ($data) use ($expected) {
  694. return $data['expirationDate'] == $expected && $data['passwordSet'] === false;
  695. }));
  696. self::invokePrivate($this->manager, 'validateExpirationDate', [$share]);
  697. $this->assertEquals($expected, $share->getExpirationDate());
  698. }
  699. public function testvalidateExpirationDateNoDateNoDefault() {
  700. $hookListner = $this->getMockBuilder('Dummy')->setMethods(['listener'])->getMock();
  701. \OCP\Util::connectHook('\OC\Share', 'verifyExpirationDate', $hookListner, 'listener');
  702. $hookListner->expects($this->once())->method('listener')->with($this->callback(function ($data) {
  703. return $data['expirationDate'] === null && $data['passwordSet'] === true;
  704. }));
  705. $share = $this->manager->newShare();
  706. $share->setPassword('password');
  707. self::invokePrivate($this->manager, 'validateExpirationDate', [$share]);
  708. $this->assertNull($share->getExpirationDate());
  709. }
  710. public function testvalidateExpirationDateNoDateDefault() {
  711. $future = new \DateTime();
  712. $future->add(new \DateInterval('P3D'));
  713. $future->setTime(0,0,0);
  714. $expected = clone $future;
  715. $share = $this->manager->newShare();
  716. $share->setExpirationDate($future);
  717. $this->config->method('getAppValue')
  718. ->will($this->returnValueMap([
  719. ['core', 'shareapi_default_expire_date', 'no', 'yes'],
  720. ['core', 'shareapi_expire_after_n_days', '7', '3'],
  721. ]));
  722. $hookListner = $this->getMockBuilder('Dummy')->setMethods(['listener'])->getMock();
  723. \OCP\Util::connectHook('\OC\Share', 'verifyExpirationDate', $hookListner, 'listener');
  724. $hookListner->expects($this->once())->method('listener')->with($this->callback(function ($data) use ($expected) {
  725. return $data['expirationDate'] == $expected;
  726. }));
  727. self::invokePrivate($this->manager, 'validateExpirationDate', [$share]);
  728. $this->assertEquals($expected, $share->getExpirationDate());
  729. }
  730. public function testValidateExpirationDateHookModification() {
  731. $nextWeek = new \DateTime();
  732. $nextWeek->add(new \DateInterval('P7D'));
  733. $nextWeek->setTime(0,0,0);
  734. $save = clone $nextWeek;
  735. $hookListner = $this->getMockBuilder('Dummy')->setMethods(['listener'])->getMock();
  736. \OCP\Util::connectHook('\OC\Share', 'verifyExpirationDate', $hookListner, 'listener');
  737. $hookListner->expects($this->once())->method('listener')->will($this->returnCallback(function ($data) {
  738. $data['expirationDate']->sub(new \DateInterval('P2D'));
  739. }));
  740. $share = $this->manager->newShare();
  741. $share->setExpirationDate($nextWeek);
  742. self::invokePrivate($this->manager, 'validateExpirationDate', [$share]);
  743. $save->sub(new \DateInterval('P2D'));
  744. $this->assertEquals($save, $share->getExpirationDate());
  745. }
  746. /**
  747. * @expectedException \Exception
  748. * @expectedExceptionMessage Invalid date!
  749. */
  750. public function testValidateExpirationDateHookException() {
  751. $nextWeek = new \DateTime();
  752. $nextWeek->add(new \DateInterval('P7D'));
  753. $nextWeek->setTime(0,0,0);
  754. $share = $this->manager->newShare();
  755. $share->setExpirationDate($nextWeek);
  756. $hookListner = $this->getMockBuilder('Dummy')->setMethods(['listener'])->getMock();
  757. \OCP\Util::connectHook('\OC\Share', 'verifyExpirationDate', $hookListner, 'listener');
  758. $hookListner->expects($this->once())->method('listener')->will($this->returnCallback(function ($data) {
  759. $data['accepted'] = false;
  760. $data['message'] = 'Invalid date!';
  761. }));
  762. self::invokePrivate($this->manager, 'validateExpirationDate', [$share]);
  763. }
  764. public function testValidateExpirationDateExistingShareNoDefault() {
  765. $share = $this->manager->newShare();
  766. $share->setId('42')->setProviderId('foo');
  767. $this->config->method('getAppValue')
  768. ->will($this->returnValueMap([
  769. ['core', 'shareapi_default_expire_date', 'no', 'yes'],
  770. ['core', 'shareapi_expire_after_n_days', '7', '6'],
  771. ]));
  772. self::invokePrivate($this->manager, 'validateExpirationDate', [$share]);
  773. $this->assertEquals(null, $share->getExpirationDate());
  774. }
  775. /**
  776. * @expectedException Exception
  777. * @expectedExceptionMessage Sharing is only allowed with group members
  778. */
  779. public function testUserCreateChecksShareWithGroupMembersOnlyDifferentGroups() {
  780. $share = $this->manager->newShare();
  781. $sharedBy = $this->createMock(IUser::class);
  782. $sharedWith = $this->createMock(IUser::class);
  783. $share->setSharedBy('sharedBy')->setSharedWith('sharedWith');
  784. $this->groupManager
  785. ->method('getUserGroupIds')
  786. ->will(
  787. $this->returnValueMap([
  788. [$sharedBy, ['group1']],
  789. [$sharedWith, ['group2']],
  790. ])
  791. );
  792. $this->userManager->method('get')->will($this->returnValueMap([
  793. ['sharedBy', $sharedBy],
  794. ['sharedWith', $sharedWith],
  795. ]));
  796. $this->config
  797. ->method('getAppValue')
  798. ->will($this->returnValueMap([
  799. ['core', 'shareapi_only_share_with_group_members', 'no', 'yes'],
  800. ]));
  801. self::invokePrivate($this->manager, 'userCreateChecks', [$share]);
  802. }
  803. public function testUserCreateChecksShareWithGroupMembersOnlySharedGroup() {
  804. $share = $this->manager->newShare();
  805. $sharedBy = $this->createMock(IUser::class);
  806. $sharedWith = $this->createMock(IUser::class);
  807. $share->setSharedBy('sharedBy')->setSharedWith('sharedWith');
  808. $path = $this->createMock(Node::class);
  809. $share->setNode($path);
  810. $this->groupManager
  811. ->method('getUserGroupIds')
  812. ->will(
  813. $this->returnValueMap([
  814. [$sharedBy, ['group1', 'group3']],
  815. [$sharedWith, ['group2', 'group3']],
  816. ])
  817. );
  818. $this->userManager->method('get')->will($this->returnValueMap([
  819. ['sharedBy', $sharedBy],
  820. ['sharedWith', $sharedWith],
  821. ]));
  822. $this->config
  823. ->method('getAppValue')
  824. ->will($this->returnValueMap([
  825. ['core', 'shareapi_only_share_with_group_members', 'no', 'yes'],
  826. ]));
  827. $this->defaultProvider
  828. ->method('getSharesByPath')
  829. ->with($path)
  830. ->willReturn([]);
  831. self::invokePrivate($this->manager, 'userCreateChecks', [$share]);
  832. $this->addToAssertionCount(1);
  833. }
  834. /**
  835. * @expectedException Exception
  836. * @expectedExceptionMessage Path is already shared with this user
  837. */
  838. public function testUserCreateChecksIdenticalShareExists() {
  839. $share = $this->manager->newShare();
  840. $share2 = $this->manager->newShare();
  841. $sharedWith = $this->createMock(IUser::class);
  842. $path = $this->createMock(Node::class);
  843. $share->setSharedWith('sharedWith')->setNode($path)
  844. ->setProviderId('foo')->setId('bar');
  845. $share2->setSharedWith('sharedWith')->setNode($path)
  846. ->setProviderId('foo')->setId('baz');
  847. $this->defaultProvider
  848. ->method('getSharesByPath')
  849. ->with($path)
  850. ->willReturn([$share2]);
  851. self::invokePrivate($this->manager, 'userCreateChecks', [$share]);
  852. }
  853. /**
  854. * @expectedException Exception
  855. * @expectedExceptionMessage Path is already shared with this user
  856. */
  857. public function testUserCreateChecksIdenticalPathSharedViaGroup() {
  858. $share = $this->manager->newShare();
  859. $sharedWith = $this->createMock(IUser::class);
  860. $sharedWith->method('getUID')->willReturn('sharedWith');
  861. $this->userManager->method('get')->with('sharedWith')->willReturn($sharedWith);
  862. $path = $this->createMock(Node::class);
  863. $share->setSharedWith('sharedWith')
  864. ->setNode($path)
  865. ->setShareOwner('shareOwner')
  866. ->setProviderId('foo')
  867. ->setId('bar');
  868. $share2 = $this->manager->newShare();
  869. $share2->setShareType(\OCP\Share::SHARE_TYPE_GROUP)
  870. ->setShareOwner('shareOwner2')
  871. ->setProviderId('foo')
  872. ->setId('baz')
  873. ->setSharedWith('group');
  874. $group = $this->createMock(IGroup::class);
  875. $group->method('inGroup')
  876. ->with($sharedWith)
  877. ->willReturn(true);
  878. $this->groupManager->method('get')->with('group')->willReturn($group);
  879. $this->defaultProvider
  880. ->method('getSharesByPath')
  881. ->with($path)
  882. ->willReturn([$share2]);
  883. self::invokePrivate($this->manager, 'userCreateChecks', [$share]);
  884. }
  885. public function testUserCreateChecksIdenticalPathSharedViaDeletedGroup() {
  886. $share = $this->manager->newShare();
  887. $sharedWith = $this->createMock(IUser::class);
  888. $sharedWith->method('getUID')->willReturn('sharedWith');
  889. $this->userManager->method('get')->with('sharedWith')->willReturn($sharedWith);
  890. $path = $this->createMock(Node::class);
  891. $share->setSharedWith('sharedWith')
  892. ->setNode($path)
  893. ->setShareOwner('shareOwner')
  894. ->setProviderId('foo')
  895. ->setId('bar');
  896. $share2 = $this->manager->newShare();
  897. $share2->setShareType(\OCP\Share::SHARE_TYPE_GROUP)
  898. ->setShareOwner('shareOwner2')
  899. ->setProviderId('foo')
  900. ->setId('baz')
  901. ->setSharedWith('group');
  902. $this->groupManager->method('get')->with('group')->willReturn(null);
  903. $this->defaultProvider
  904. ->method('getSharesByPath')
  905. ->with($path)
  906. ->willReturn([$share2]);
  907. $this->assertNull($this->invokePrivate($this->manager, 'userCreateChecks', [$share]));
  908. }
  909. public function testUserCreateChecksIdenticalPathNotSharedWithUser() {
  910. $share = $this->manager->newShare();
  911. $sharedWith = $this->createMock(IUser::class);
  912. $path = $this->createMock(Node::class);
  913. $share->setSharedWith('sharedWith')
  914. ->setNode($path)
  915. ->setShareOwner('shareOwner')
  916. ->setProviderId('foo')
  917. ->setId('bar');
  918. $this->userManager->method('get')->with('sharedWith')->willReturn($sharedWith);
  919. $share2 = $this->manager->newShare();
  920. $share2->setShareType(\OCP\Share::SHARE_TYPE_GROUP)
  921. ->setShareOwner('shareOwner2')
  922. ->setProviderId('foo')
  923. ->setId('baz');
  924. $group = $this->createMock(IGroup::class);
  925. $group->method('inGroup')
  926. ->with($sharedWith)
  927. ->willReturn(false);
  928. $this->groupManager->method('get')->with('group')->willReturn($group);
  929. $share2->setSharedWith('group');
  930. $this->defaultProvider
  931. ->method('getSharesByPath')
  932. ->with($path)
  933. ->willReturn([$share2]);
  934. self::invokePrivate($this->manager, 'userCreateChecks', [$share]);
  935. $this->addToAssertionCount(1);
  936. }
  937. /**
  938. * @expectedException Exception
  939. * @expectedExceptionMessage Group sharing is now allowed
  940. */
  941. public function testGroupCreateChecksShareWithGroupMembersGroupSharingNotAllowed() {
  942. $share = $this->manager->newShare();
  943. $this->config
  944. ->method('getAppValue')
  945. ->will($this->returnValueMap([
  946. ['core', 'shareapi_allow_group_sharing', 'yes', 'no'],
  947. ]));
  948. self::invokePrivate($this->manager, 'groupCreateChecks', [$share]);
  949. }
  950. /**
  951. * @expectedException Exception
  952. * @expectedExceptionMessage Sharing is only allowed within your own groups
  953. */
  954. public function testGroupCreateChecksShareWithGroupMembersOnlyNotInGroup() {
  955. $share = $this->manager->newShare();
  956. $user = $this->createMock(IUser::class);
  957. $group = $this->createMock(IGroup::class);
  958. $share->setSharedBy('user')->setSharedWith('group');
  959. $group->method('inGroup')->with($user)->willReturn(false);
  960. $this->groupManager->method('get')->with('group')->willReturn($group);
  961. $this->userManager->method('get')->with('user')->willReturn($user);
  962. $this->config
  963. ->method('getAppValue')
  964. ->will($this->returnValueMap([
  965. ['core', 'shareapi_only_share_with_group_members', 'no', 'yes'],
  966. ['core', 'shareapi_allow_group_sharing', 'yes', 'yes'],
  967. ]));
  968. self::invokePrivate($this->manager, 'groupCreateChecks', [$share]);
  969. }
  970. /**
  971. * @expectedException Exception
  972. * @expectedExceptionMessage Sharing is only allowed within your own groups
  973. */
  974. public function testGroupCreateChecksShareWithGroupMembersOnlyNullGroup() {
  975. $share = $this->manager->newShare();
  976. $user = $this->createMock(IUser::class);
  977. $share->setSharedBy('user')->setSharedWith('group');
  978. $this->groupManager->method('get')->with('group')->willReturn(null);
  979. $this->userManager->method('get')->with('user')->willReturn($user);
  980. $this->config
  981. ->method('getAppValue')
  982. ->will($this->returnValueMap([
  983. ['core', 'shareapi_only_share_with_group_members', 'no', 'yes'],
  984. ['core', 'shareapi_allow_group_sharing', 'yes', 'yes'],
  985. ]));
  986. $this->assertNull($this->invokePrivate($this->manager, 'groupCreateChecks', [$share]));
  987. }
  988. public function testGroupCreateChecksShareWithGroupMembersOnlyInGroup() {
  989. $share = $this->manager->newShare();
  990. $user = $this->createMock(IUser::class);
  991. $group = $this->createMock(IGroup::class);
  992. $share->setSharedBy('user')->setSharedWith('group');
  993. $this->userManager->method('get')->with('user')->willReturn($user);
  994. $this->groupManager->method('get')->with('group')->willReturn($group);
  995. $group->method('inGroup')->with($user)->willReturn(true);
  996. $path = $this->createMock(Node::class);
  997. $share->setNode($path);
  998. $this->defaultProvider->method('getSharesByPath')
  999. ->with($path)
  1000. ->willReturn([]);
  1001. $this->config
  1002. ->method('getAppValue')
  1003. ->will($this->returnValueMap([
  1004. ['core', 'shareapi_only_share_with_group_members', 'no', 'yes'],
  1005. ['core', 'shareapi_allow_group_sharing', 'yes', 'yes'],
  1006. ]));
  1007. self::invokePrivate($this->manager, 'groupCreateChecks', [$share]);
  1008. $this->addToAssertionCount(1);
  1009. }
  1010. /**
  1011. * @expectedException Exception
  1012. * @expectedExceptionMessage Path is already shared with this group
  1013. */
  1014. public function testGroupCreateChecksPathAlreadySharedWithSameGroup() {
  1015. $share = $this->manager->newShare();
  1016. $path = $this->createMock(Node::class);
  1017. $share->setSharedWith('sharedWith')
  1018. ->setNode($path)
  1019. ->setProviderId('foo')
  1020. ->setId('bar');
  1021. $share2 = $this->manager->newShare();
  1022. $share2->setSharedWith('sharedWith')
  1023. ->setProviderId('foo')
  1024. ->setId('baz');
  1025. $this->defaultProvider->method('getSharesByPath')
  1026. ->with($path)
  1027. ->willReturn([$share2]);
  1028. $this->config
  1029. ->method('getAppValue')
  1030. ->will($this->returnValueMap([
  1031. ['core', 'shareapi_allow_group_sharing', 'yes', 'yes'],
  1032. ]));
  1033. self::invokePrivate($this->manager, 'groupCreateChecks', [$share]);
  1034. }
  1035. public function testGroupCreateChecksPathAlreadySharedWithDifferentGroup() {
  1036. $share = $this->manager->newShare();
  1037. $share->setSharedWith('sharedWith');
  1038. $path = $this->createMock(Node::class);
  1039. $share->setNode($path);
  1040. $share2 = $this->manager->newShare();
  1041. $share2->setSharedWith('sharedWith2');
  1042. $this->defaultProvider->method('getSharesByPath')
  1043. ->with($path)
  1044. ->willReturn([$share2]);
  1045. $this->config
  1046. ->method('getAppValue')
  1047. ->will($this->returnValueMap([
  1048. ['core', 'shareapi_allow_group_sharing', 'yes', 'yes'],
  1049. ]));
  1050. self::invokePrivate($this->manager, 'groupCreateChecks', [$share]);
  1051. $this->addToAssertionCount(1);
  1052. }
  1053. /**
  1054. * @expectedException Exception
  1055. * @expectedExceptionMessage Link sharing is not allowed
  1056. */
  1057. public function testLinkCreateChecksNoLinkSharesAllowed() {
  1058. $share = $this->manager->newShare();
  1059. $this->config
  1060. ->method('getAppValue')
  1061. ->will($this->returnValueMap([
  1062. ['core', 'shareapi_allow_links', 'yes', 'no'],
  1063. ]));
  1064. self::invokePrivate($this->manager, 'linkCreateChecks', [$share]);
  1065. }
  1066. /**
  1067. * @expectedException Exception
  1068. * @expectedExceptionMessage Link shares can’t have reshare permissions
  1069. */
  1070. public function testLinkCreateChecksSharePermissions() {
  1071. $share = $this->manager->newShare();
  1072. $share->setPermissions(\OCP\Constants::PERMISSION_SHARE);
  1073. $this->config
  1074. ->method('getAppValue')
  1075. ->will($this->returnValueMap([
  1076. ['core', 'shareapi_allow_links', 'yes', 'yes'],
  1077. ]));
  1078. self::invokePrivate($this->manager, 'linkCreateChecks', [$share]);
  1079. }
  1080. /**
  1081. * @expectedException Exception
  1082. * @expectedExceptionMessage Public upload is not allowed
  1083. */
  1084. public function testLinkCreateChecksNoPublicUpload() {
  1085. $share = $this->manager->newShare();
  1086. $share->setPermissions(\OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE);
  1087. $this->config
  1088. ->method('getAppValue')
  1089. ->will($this->returnValueMap([
  1090. ['core', 'shareapi_allow_links', 'yes', 'yes'],
  1091. ['core', 'shareapi_allow_public_upload', 'yes', 'no']
  1092. ]));
  1093. self::invokePrivate($this->manager, 'linkCreateChecks', [$share]);
  1094. }
  1095. public function testLinkCreateChecksPublicUpload() {
  1096. $share = $this->manager->newShare();
  1097. $share->setPermissions(\OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE);
  1098. $this->config
  1099. ->method('getAppValue')
  1100. ->will($this->returnValueMap([
  1101. ['core', 'shareapi_allow_links', 'yes', 'yes'],
  1102. ['core', 'shareapi_allow_public_upload', 'yes', 'yes']
  1103. ]));
  1104. self::invokePrivate($this->manager, 'linkCreateChecks', [$share]);
  1105. $this->addToAssertionCount(1);
  1106. }
  1107. public function testLinkCreateChecksReadOnly() {
  1108. $share = $this->manager->newShare();
  1109. $share->setPermissions(\OCP\Constants::PERMISSION_READ);
  1110. $this->config
  1111. ->method('getAppValue')
  1112. ->will($this->returnValueMap([
  1113. ['core', 'shareapi_allow_links', 'yes', 'yes'],
  1114. ['core', 'shareapi_allow_public_upload', 'yes', 'no']
  1115. ]));
  1116. self::invokePrivate($this->manager, 'linkCreateChecks', [$share]);
  1117. $this->addToAssertionCount(1);
  1118. }
  1119. /**
  1120. * @expectedException \InvalidArgumentException
  1121. * @expectedExceptionMessage Path contains files shared with you
  1122. */
  1123. public function testPathCreateChecksContainsSharedMount() {
  1124. $path = $this->createMock(Folder::class);
  1125. $path->method('getPath')->willReturn('path');
  1126. $mount = $this->createMock(IMountPoint::class);
  1127. $storage = $this->createMock(Storage::class);
  1128. $mount->method('getStorage')->willReturn($storage);
  1129. $storage->method('instanceOfStorage')->with('\OCA\Files_Sharing\ISharedStorage')->willReturn(true);
  1130. $this->mountManager->method('findIn')->with('path')->willReturn([$mount]);
  1131. self::invokePrivate($this->manager, 'pathCreateChecks', [$path]);
  1132. }
  1133. public function testPathCreateChecksContainsNoSharedMount() {
  1134. $path = $this->createMock(Folder::class);
  1135. $path->method('getPath')->willReturn('path');
  1136. $mount = $this->createMock(IMountPoint::class);
  1137. $storage = $this->createMock(Storage::class);
  1138. $mount->method('getStorage')->willReturn($storage);
  1139. $storage->method('instanceOfStorage')->with('\OCA\Files_Sharing\ISharedStorage')->willReturn(false);
  1140. $this->mountManager->method('findIn')->with('path')->willReturn([$mount]);
  1141. self::invokePrivate($this->manager, 'pathCreateChecks', [$path]);
  1142. $this->addToAssertionCount(1);
  1143. }
  1144. public function testPathCreateChecksContainsNoFolder() {
  1145. $path = $this->createMock(File::class);
  1146. self::invokePrivate($this->manager, 'pathCreateChecks', [$path]);
  1147. $this->addToAssertionCount(1);
  1148. }
  1149. public function dataIsSharingDisabledForUser() {
  1150. $data = [];
  1151. // No exclude groups
  1152. $data[] = ['no', null, null, null, false];
  1153. // empty exclude list, user no groups
  1154. $data[] = ['yes', '', json_encode(['']), [], false];
  1155. // empty exclude list, user groups
  1156. $data[] = ['yes', '', json_encode(['']), ['group1', 'group2'], false];
  1157. // Convert old list to json
  1158. $data[] = ['yes', 'group1,group2', json_encode(['group1', 'group2']), [], false];
  1159. // Old list partly groups in common
  1160. $data[] = ['yes', 'group1,group2', json_encode(['group1', 'group2']), ['group1', 'group3'], false];
  1161. // Old list only groups in common
  1162. $data[] = ['yes', 'group1,group2', json_encode(['group1', 'group2']), ['group1'], true];
  1163. // New list partly in common
  1164. $data[] = ['yes', json_encode(['group1', 'group2']), null, ['group1', 'group3'], false];
  1165. // New list only groups in common
  1166. $data[] = ['yes', json_encode(['group1', 'group2']), null, ['group2'], true];
  1167. return $data;
  1168. }
  1169. /**
  1170. * @dataProvider dataIsSharingDisabledForUser
  1171. *
  1172. * @param string $excludeGroups
  1173. * @param string $groupList
  1174. * @param string $setList
  1175. * @param string[] $groupIds
  1176. * @param bool $expected
  1177. */
  1178. public function testIsSharingDisabledForUser($excludeGroups, $groupList, $setList, $groupIds, $expected) {
  1179. $user = $this->createMock(IUser::class);
  1180. $this->config->method('getAppValue')
  1181. ->will($this->returnValueMap([
  1182. ['core', 'shareapi_exclude_groups', 'no', $excludeGroups],
  1183. ['core', 'shareapi_exclude_groups_list', '', $groupList],
  1184. ]));
  1185. if ($setList !== null) {
  1186. $this->config->expects($this->once())
  1187. ->method('setAppValue')
  1188. ->with('core', 'shareapi_exclude_groups_list', $setList);
  1189. } else {
  1190. $this->config->expects($this->never())
  1191. ->method('setAppValue');
  1192. }
  1193. $this->groupManager->method('getUserGroupIds')
  1194. ->with($user)
  1195. ->willReturn($groupIds);
  1196. $this->userManager->method('get')->with('user')->willReturn($user);
  1197. $res = $this->manager->sharingDisabledForUser('user');
  1198. $this->assertEquals($expected, $res);
  1199. }
  1200. public function dataCanShare() {
  1201. $data = [];
  1202. /*
  1203. * [expected, sharing enabled, disabled for user]
  1204. */
  1205. $data[] = [false, 'no', false];
  1206. $data[] = [false, 'no', true];
  1207. $data[] = [true, 'yes', false];
  1208. $data[] = [false, 'yes', true];
  1209. return $data;
  1210. }
  1211. /**
  1212. * @dataProvider dataCanShare
  1213. *
  1214. * @param bool $expected
  1215. * @param string $sharingEnabled
  1216. * @param bool $disabledForUser
  1217. */
  1218. public function testCanShare($expected, $sharingEnabled, $disabledForUser) {
  1219. $this->config->method('getAppValue')
  1220. ->will($this->returnValueMap([
  1221. ['core', 'shareapi_enabled', 'yes', $sharingEnabled],
  1222. ]));
  1223. $manager = $this->createManagerMock()
  1224. ->setMethods(['sharingDisabledForUser'])
  1225. ->getMock();
  1226. $manager->method('sharingDisabledForUser')
  1227. ->with('user')
  1228. ->willReturn($disabledForUser);
  1229. $share = $this->manager->newShare();
  1230. $share->setSharedBy('user');
  1231. $exception = false;
  1232. try {
  1233. $res = self::invokePrivate($manager, 'canShare', [$share]);
  1234. } catch (\Exception $e) {
  1235. $exception = true;
  1236. }
  1237. $this->assertEquals($expected, !$exception);
  1238. }
  1239. public function testCreateShareUser() {
  1240. $manager = $this->createManagerMock()
  1241. ->setMethods(['canShare', 'generalCreateChecks', 'userCreateChecks', 'pathCreateChecks'])
  1242. ->getMock();
  1243. $shareOwner = $this->createMock(IUser::class);
  1244. $shareOwner->method('getUID')->willReturn('shareOwner');
  1245. $storage = $this->createMock(Storage::class);
  1246. $path = $this->createMock(File::class);
  1247. $path->method('getOwner')->willReturn($shareOwner);
  1248. $path->method('getName')->willReturn('target');
  1249. $path->method('getStorage')->willReturn($storage);
  1250. $share = $this->createShare(
  1251. null,
  1252. \OCP\Share::SHARE_TYPE_USER,
  1253. $path,
  1254. 'sharedWith',
  1255. 'sharedBy',
  1256. null,
  1257. \OCP\Constants::PERMISSION_ALL);
  1258. $manager->expects($this->once())
  1259. ->method('canShare')
  1260. ->with($share)
  1261. ->willReturn(true);
  1262. $manager->expects($this->once())
  1263. ->method('generalCreateChecks')
  1264. ->with($share);;
  1265. $manager->expects($this->once())
  1266. ->method('userCreateChecks')
  1267. ->with($share);;
  1268. $manager->expects($this->once())
  1269. ->method('pathCreateChecks')
  1270. ->with($path);
  1271. $this->defaultProvider
  1272. ->expects($this->once())
  1273. ->method('create')
  1274. ->with($share)
  1275. ->will($this->returnArgument(0));
  1276. $share->expects($this->once())
  1277. ->method('setShareOwner')
  1278. ->with('shareOwner');
  1279. $share->expects($this->once())
  1280. ->method('setTarget')
  1281. ->with('/target');
  1282. $manager->createShare($share);
  1283. }
  1284. public function testCreateShareGroup() {
  1285. $manager = $this->createManagerMock()
  1286. ->setMethods(['canShare', 'generalCreateChecks', 'groupCreateChecks', 'pathCreateChecks'])
  1287. ->getMock();
  1288. $shareOwner = $this->createMock(IUser::class);
  1289. $shareOwner->method('getUID')->willReturn('shareOwner');
  1290. $storage = $this->createMock(Storage::class);
  1291. $path = $this->createMock(File::class);
  1292. $path->method('getOwner')->willReturn($shareOwner);
  1293. $path->method('getName')->willReturn('target');
  1294. $path->method('getStorage')->willReturn($storage);
  1295. $share = $this->createShare(
  1296. null,
  1297. \OCP\Share::SHARE_TYPE_GROUP,
  1298. $path,
  1299. 'sharedWith',
  1300. 'sharedBy',
  1301. null,
  1302. \OCP\Constants::PERMISSION_ALL);
  1303. $manager->expects($this->once())
  1304. ->method('canShare')
  1305. ->with($share)
  1306. ->willReturn(true);
  1307. $manager->expects($this->once())
  1308. ->method('generalCreateChecks')
  1309. ->with($share);;
  1310. $manager->expects($this->once())
  1311. ->method('groupCreateChecks')
  1312. ->with($share);;
  1313. $manager->expects($this->once())
  1314. ->method('pathCreateChecks')
  1315. ->with($path);
  1316. $this->defaultProvider
  1317. ->expects($this->once())
  1318. ->method('create')
  1319. ->with($share)
  1320. ->will($this->returnArgument(0));
  1321. $share->expects($this->once())
  1322. ->method('setShareOwner')
  1323. ->with('shareOwner');
  1324. $share->expects($this->once())
  1325. ->method('setTarget')
  1326. ->with('/target');
  1327. $manager->createShare($share);
  1328. }
  1329. public function testCreateShareLink() {
  1330. $manager = $this->createManagerMock()
  1331. ->setMethods([
  1332. 'canShare',
  1333. 'generalCreateChecks',
  1334. 'linkCreateChecks',
  1335. 'pathCreateChecks',
  1336. 'validateExpirationDate',
  1337. 'verifyPassword',
  1338. 'setLinkParent',
  1339. ])
  1340. ->getMock();
  1341. $shareOwner = $this->createMock(IUser::class);
  1342. $shareOwner->method('getUID')->willReturn('shareOwner');
  1343. $storage = $this->createMock(Storage::class);
  1344. $path = $this->createMock(File::class);
  1345. $path->method('getOwner')->willReturn($shareOwner);
  1346. $path->method('getName')->willReturn('target');
  1347. $path->method('getId')->willReturn(1);
  1348. $path->method('getStorage')->willReturn($storage);
  1349. $date = new \DateTime();
  1350. $share = $this->manager->newShare();
  1351. $share->setShareType(\OCP\Share::SHARE_TYPE_LINK)
  1352. ->setNode($path)
  1353. ->setSharedBy('sharedBy')
  1354. ->setPermissions(\OCP\Constants::PERMISSION_ALL)
  1355. ->setExpirationDate($date)
  1356. ->setPassword('password');
  1357. $manager->expects($this->once())
  1358. ->method('canShare')
  1359. ->with($share)
  1360. ->willReturn(true);
  1361. $manager->expects($this->once())
  1362. ->method('generalCreateChecks')
  1363. ->with($share);;
  1364. $manager->expects($this->once())
  1365. ->method('linkCreateChecks')
  1366. ->with($share);;
  1367. $manager->expects($this->once())
  1368. ->method('pathCreateChecks')
  1369. ->with($path);
  1370. $manager->expects($this->once())
  1371. ->method('validateExpirationDate')
  1372. ->with($share);
  1373. $manager->expects($this->once())
  1374. ->method('verifyPassword')
  1375. ->with('password');
  1376. $manager->expects($this->once())
  1377. ->method('setLinkParent')
  1378. ->with($share);
  1379. $this->hasher->expects($this->once())
  1380. ->method('hash')
  1381. ->with('password')
  1382. ->willReturn('hashed');
  1383. $this->secureRandom->method('getMediumStrengthGenerator')
  1384. ->will($this->returnSelf());
  1385. $this->secureRandom->method('generate')
  1386. ->willReturn('token');
  1387. $this->defaultProvider
  1388. ->expects($this->once())
  1389. ->method('create')
  1390. ->with($share)
  1391. ->will($this->returnCallback(function(Share $share) {
  1392. return $share->setId(42);
  1393. }));
  1394. // Pre share
  1395. $this->eventDispatcher->expects($this->at(0))
  1396. ->method('dispatch')
  1397. ->with(
  1398. $this->equalTo('OCP\Share::preShare'),
  1399. $this->callback(function(GenericEvent $e) use ($path, $date) {
  1400. /** @var IShare $share */
  1401. $share = $e->getSubject();
  1402. return $share->getShareType() === \OCP\Share::SHARE_TYPE_LINK &&
  1403. $share->getNode() === $path &&
  1404. $share->getSharedBy() === 'sharedBy' &&
  1405. $share->getPermissions() === \OCP\Constants::PERMISSION_ALL &&
  1406. $share->getExpirationDate() === $date &&
  1407. $share->getPassword() === 'hashed' &&
  1408. $share->getToken() === 'token';
  1409. })
  1410. );
  1411. // Post share
  1412. $this->eventDispatcher->expects($this->at(1))
  1413. ->method('dispatch')
  1414. ->with(
  1415. $this->equalTo('OCP\Share::postShare'),
  1416. $this->callback(function(GenericEvent $e) use ($path, $date) {
  1417. /** @var IShare $share */
  1418. $share = $e->getSubject();
  1419. return $share->getShareType() === \OCP\Share::SHARE_TYPE_LINK &&
  1420. $share->getNode() === $path &&
  1421. $share->getSharedBy() === 'sharedBy' &&
  1422. $share->getPermissions() === \OCP\Constants::PERMISSION_ALL &&
  1423. $share->getExpirationDate() === $date &&
  1424. $share->getPassword() === 'hashed' &&
  1425. $share->getToken() === 'token' &&
  1426. $share->getId() === '42' &&
  1427. $share->getTarget() === '/target';
  1428. })
  1429. );
  1430. /** @var IShare $share */
  1431. $share = $manager->createShare($share);
  1432. $this->assertSame('shareOwner', $share->getShareOwner());
  1433. $this->assertEquals('/target', $share->getTarget());
  1434. $this->assertSame($date, $share->getExpirationDate());
  1435. $this->assertEquals('token', $share->getToken());
  1436. $this->assertEquals('hashed', $share->getPassword());
  1437. }
  1438. public function testCreateShareMail() {
  1439. $manager = $this->createManagerMock()
  1440. ->setMethods([
  1441. 'canShare',
  1442. 'generalCreateChecks',
  1443. 'linkCreateChecks',
  1444. 'pathCreateChecks',
  1445. 'validateExpirationDate',
  1446. 'verifyPassword',
  1447. 'setLinkParent',
  1448. ])
  1449. ->getMock();
  1450. $shareOwner = $this->createMock(IUser::class);
  1451. $shareOwner->method('getUID')->willReturn('shareOwner');
  1452. $storage = $this->createMock(Storage::class);
  1453. $path = $this->createMock(File::class);
  1454. $path->method('getOwner')->willReturn($shareOwner);
  1455. $path->method('getName')->willReturn('target');
  1456. $path->method('getId')->willReturn(1);
  1457. $path->method('getStorage')->willReturn($storage);
  1458. $share = $this->manager->newShare();
  1459. $share->setShareType(\OCP\Share::SHARE_TYPE_EMAIL)
  1460. ->setNode($path)
  1461. ->setSharedBy('sharedBy')
  1462. ->setPermissions(\OCP\Constants::PERMISSION_ALL);
  1463. $manager->expects($this->once())
  1464. ->method('canShare')
  1465. ->with($share)
  1466. ->willReturn(true);
  1467. $manager->expects($this->once())
  1468. ->method('generalCreateChecks')
  1469. ->with($share);;
  1470. $manager->expects($this->never())
  1471. ->method('linkCreateChecks');
  1472. $manager->expects($this->once())
  1473. ->method('pathCreateChecks')
  1474. ->with($path);
  1475. $manager->expects($this->never())
  1476. ->method('validateExpirationDate');
  1477. $manager->expects($this->never())
  1478. ->method('verifyPassword');
  1479. $manager->expects($this->never())
  1480. ->method('setLinkParent');
  1481. $this->secureRandom->method('getMediumStrengthGenerator')
  1482. ->will($this->returnSelf());
  1483. $this->secureRandom->method('generate')
  1484. ->willReturn('token');
  1485. $this->defaultProvider
  1486. ->expects($this->once())
  1487. ->method('create')
  1488. ->with($share)
  1489. ->will($this->returnCallback(function(Share $share) {
  1490. return $share->setId(42);
  1491. }));
  1492. // Pre share
  1493. $this->eventDispatcher->expects($this->at(0))
  1494. ->method('dispatch')
  1495. ->with(
  1496. $this->equalTo('OCP\Share::preShare'),
  1497. $this->callback(function(GenericEvent $e) use ($path) {
  1498. /** @var IShare $share */
  1499. $share = $e->getSubject();
  1500. return $share->getShareType() === \OCP\Share::SHARE_TYPE_EMAIL &&
  1501. $share->getNode() === $path &&
  1502. $share->getSharedBy() === 'sharedBy' &&
  1503. $share->getPermissions() === \OCP\Constants::PERMISSION_ALL &&
  1504. $share->getExpirationDate() === null &&
  1505. $share->getPassword() === null &&
  1506. $share->getToken() === 'token';
  1507. })
  1508. );
  1509. // Post share
  1510. $this->eventDispatcher->expects($this->at(1))
  1511. ->method('dispatch')
  1512. ->with(
  1513. $this->equalTo('OCP\Share::postShare'),
  1514. $this->callback(function(GenericEvent $e) use ($path) {
  1515. /** @var IShare $share */
  1516. $share = $e->getSubject();
  1517. return $share->getShareType() === \OCP\Share::SHARE_TYPE_EMAIL &&
  1518. $share->getNode() === $path &&
  1519. $share->getSharedBy() === 'sharedBy' &&
  1520. $share->getPermissions() === \OCP\Constants::PERMISSION_ALL &&
  1521. $share->getExpirationDate() === null &&
  1522. $share->getPassword() === null &&
  1523. $share->getToken() === 'token' &&
  1524. $share->getId() === '42' &&
  1525. $share->getTarget() === '/target';
  1526. })
  1527. );
  1528. /** @var IShare $share */
  1529. $share = $manager->createShare($share);
  1530. $this->assertSame('shareOwner', $share->getShareOwner());
  1531. $this->assertEquals('/target', $share->getTarget());
  1532. $this->assertEquals('token', $share->getToken());
  1533. }
  1534. /**
  1535. * @expectedException Exception
  1536. * @expectedExceptionMessage I won't let you share
  1537. */
  1538. public function testCreateShareHookError() {
  1539. $manager = $this->createManagerMock()
  1540. ->setMethods([
  1541. 'canShare',
  1542. 'generalCreateChecks',
  1543. 'userCreateChecks',
  1544. 'pathCreateChecks',
  1545. ])
  1546. ->getMock();
  1547. $shareOwner = $this->createMock(IUser::class);
  1548. $shareOwner->method('getUID')->willReturn('shareOwner');
  1549. $storage = $this->createMock(Storage::class);
  1550. $path = $this->createMock(File::class);
  1551. $path->method('getOwner')->willReturn($shareOwner);
  1552. $path->method('getName')->willReturn('target');
  1553. $path->method('getStorage')->willReturn($storage);
  1554. $share = $this->createShare(
  1555. null,
  1556. \OCP\Share::SHARE_TYPE_USER,
  1557. $path,
  1558. 'sharedWith',
  1559. 'sharedBy',
  1560. null,
  1561. \OCP\Constants::PERMISSION_ALL);
  1562. $manager->expects($this->once())
  1563. ->method('canShare')
  1564. ->with($share)
  1565. ->willReturn(true);
  1566. $manager->expects($this->once())
  1567. ->method('generalCreateChecks')
  1568. ->with($share);;
  1569. $manager->expects($this->once())
  1570. ->method('userCreateChecks')
  1571. ->with($share);;
  1572. $manager->expects($this->once())
  1573. ->method('pathCreateChecks')
  1574. ->with($path);
  1575. $share->expects($this->once())
  1576. ->method('setShareOwner')
  1577. ->with('shareOwner');
  1578. $share->expects($this->once())
  1579. ->method('setTarget')
  1580. ->with('/target');
  1581. // Pre share
  1582. $this->eventDispatcher->expects($this->once())
  1583. ->method('dispatch')
  1584. ->with(
  1585. $this->equalTo('OCP\Share::preShare'),
  1586. $this->isInstanceOf(GenericEvent::class)
  1587. )->will($this->returnCallback(function($name, GenericEvent $e) {
  1588. $e->setArgument('error', 'I won\'t let you share!');
  1589. $e->stopPropagation();
  1590. })
  1591. );
  1592. $manager->createShare($share);
  1593. }
  1594. public function testCreateShareOfIncommingFederatedShare() {
  1595. $manager = $this->createManagerMock()
  1596. ->setMethods(['canShare', 'generalCreateChecks', 'userCreateChecks', 'pathCreateChecks'])
  1597. ->getMock();
  1598. $shareOwner = $this->createMock(IUser::class);
  1599. $shareOwner->method('getUID')->willReturn('shareOwner');
  1600. $storage = $this->createMock(Storage::class);
  1601. $storage->method('instanceOfStorage')
  1602. ->with('OCA\Files_Sharing\External\Storage')
  1603. ->willReturn(true);
  1604. $storage2 = $this->createMock(Storage::class);
  1605. $storage2->method('instanceOfStorage')
  1606. ->with('OCA\Files_Sharing\External\Storage')
  1607. ->willReturn(false);
  1608. $path = $this->createMock(File::class);
  1609. $path->expects($this->never())->method('getOwner');
  1610. $path->method('getName')->willReturn('target');
  1611. $path->method('getStorage')->willReturn($storage);
  1612. $parent = $this->createMock(Folder::class);
  1613. $parent->method('getStorage')->willReturn($storage);
  1614. $parentParent = $this->createMock(Folder::class);
  1615. $parentParent->method('getStorage')->willReturn($storage2);
  1616. $parentParent->method('getOwner')->willReturn($shareOwner);
  1617. $path->method('getParent')->willReturn($parent);
  1618. $parent->method('getParent')->willReturn($parentParent);
  1619. $share = $this->createShare(
  1620. null,
  1621. \OCP\Share::SHARE_TYPE_USER,
  1622. $path,
  1623. 'sharedWith',
  1624. 'sharedBy',
  1625. null,
  1626. \OCP\Constants::PERMISSION_ALL);
  1627. $manager->expects($this->once())
  1628. ->method('canShare')
  1629. ->with($share)
  1630. ->willReturn(true);
  1631. $manager->expects($this->once())
  1632. ->method('generalCreateChecks')
  1633. ->with($share);;
  1634. $manager->expects($this->once())
  1635. ->method('userCreateChecks')
  1636. ->with($share);;
  1637. $manager->expects($this->once())
  1638. ->method('pathCreateChecks')
  1639. ->with($path);
  1640. $this->defaultProvider
  1641. ->expects($this->once())
  1642. ->method('create')
  1643. ->with($share)
  1644. ->will($this->returnArgument(0));
  1645. $share->expects($this->once())
  1646. ->method('setShareOwner')
  1647. ->with('shareOwner');
  1648. $share->expects($this->once())
  1649. ->method('setTarget')
  1650. ->with('/target');
  1651. $manager->createShare($share);
  1652. }
  1653. public function testGetSharesBy() {
  1654. $share = $this->manager->newShare();
  1655. $node = $this->createMock(Folder::class);
  1656. $this->defaultProvider->expects($this->once())
  1657. ->method('getSharesBy')
  1658. ->with(
  1659. $this->equalTo('user'),
  1660. $this->equalTo(\OCP\Share::SHARE_TYPE_USER),
  1661. $this->equalTo($node),
  1662. $this->equalTo(true),
  1663. $this->equalTo(1),
  1664. $this->equalTo(1)
  1665. )->willReturn([$share]);
  1666. $shares = $this->manager->getSharesBy('user', \OCP\Share::SHARE_TYPE_USER, $node, true, 1, 1);
  1667. $this->assertCount(1, $shares);
  1668. $this->assertSame($share, $shares[0]);
  1669. }
  1670. /**
  1671. * Test to ensure we correctly remove expired link shares
  1672. *
  1673. * We have 8 Shares and we want the 3 first valid shares.
  1674. * share 3-6 and 8 are expired. Thus at the end of this test we should
  1675. * have received share 1,2 and 7. And from the manager. Share 3-6 should be
  1676. * deleted (as they are evaluated). but share 8 should still be there.
  1677. */
  1678. public function testGetSharesByExpiredLinkShares() {
  1679. $manager = $this->createManagerMock()
  1680. ->setMethods(['deleteShare'])
  1681. ->getMock();
  1682. /** @var \OCP\Share\IShare[] $shares */
  1683. $shares = [];
  1684. /*
  1685. * This results in an array of 8 IShare elements
  1686. */
  1687. for ($i = 0; $i < 8; $i++) {
  1688. $share = $this->manager->newShare();
  1689. $share->setId($i);
  1690. $shares[] = $share;
  1691. }
  1692. $today = new \DateTime();
  1693. $today->setTime(0,0,0);
  1694. /*
  1695. * Set the expiration date to today for some shares
  1696. */
  1697. $shares[2]->setExpirationDate($today);
  1698. $shares[3]->setExpirationDate($today);
  1699. $shares[4]->setExpirationDate($today);
  1700. $shares[5]->setExpirationDate($today);
  1701. /** @var \OCP\Share\IShare[] $i */
  1702. $shares2 = [];
  1703. for ($i = 0; $i < 8; $i++) {
  1704. $shares2[] = clone $shares[$i];
  1705. }
  1706. $node = $this->createMock(File::class);
  1707. /*
  1708. * Simulate the getSharesBy call.
  1709. */
  1710. $this->defaultProvider
  1711. ->method('getSharesBy')
  1712. ->will($this->returnCallback(function($uid, $type, $node, $reshares, $limit, $offset) use (&$shares2) {
  1713. return array_slice($shares2, $offset, $limit);
  1714. }));
  1715. /*
  1716. * Simulate the deleteShare call.
  1717. */
  1718. $manager->method('deleteShare')
  1719. ->will($this->returnCallback(function($share) use (&$shares2) {
  1720. for($i = 0; $i < count($shares2); $i++) {
  1721. if ($shares2[$i]->getId() === $share->getId()) {
  1722. array_splice($shares2, $i, 1);
  1723. break;
  1724. }
  1725. }
  1726. }));
  1727. $res = $manager->getSharesBy('user', \OCP\Share::SHARE_TYPE_LINK, $node, true, 3, 0);
  1728. $this->assertCount(3, $res);
  1729. $this->assertEquals($shares[0]->getId(), $res[0]->getId());
  1730. $this->assertEquals($shares[1]->getId(), $res[1]->getId());
  1731. $this->assertEquals($shares[6]->getId(), $res[2]->getId());
  1732. $this->assertCount(4, $shares2);
  1733. $this->assertEquals(0, $shares2[0]->getId());
  1734. $this->assertEquals(1, $shares2[1]->getId());
  1735. $this->assertEquals(6, $shares2[2]->getId());
  1736. $this->assertEquals(7, $shares2[3]->getId());
  1737. $this->assertSame($today, $shares[3]->getExpirationDate());
  1738. }
  1739. public function testGetShareByToken() {
  1740. $this->config
  1741. ->expects($this->once())
  1742. ->method('getAppValue')
  1743. ->with('core', 'shareapi_allow_links', 'yes')
  1744. ->willReturn('yes');
  1745. $factory = $this->createMock(IProviderFactory::class);
  1746. $manager = new Manager(
  1747. $this->logger,
  1748. $this->config,
  1749. $this->secureRandom,
  1750. $this->hasher,
  1751. $this->mountManager,
  1752. $this->groupManager,
  1753. $this->l,
  1754. $this->l10nFactory,
  1755. $factory,
  1756. $this->userManager,
  1757. $this->rootFolder,
  1758. $this->eventDispatcher,
  1759. $this->mailer,
  1760. $this->urlGenerator,
  1761. $this->defaults
  1762. );
  1763. $share = $this->createMock(IShare::class);
  1764. $factory->expects($this->once())
  1765. ->method('getProviderForType')
  1766. ->with(\OCP\Share::SHARE_TYPE_LINK)
  1767. ->willReturn($this->defaultProvider);
  1768. $this->defaultProvider->expects($this->once())
  1769. ->method('getShareByToken')
  1770. ->with('token')
  1771. ->willReturn($share);
  1772. $ret = $manager->getShareByToken('token');
  1773. $this->assertSame($share, $ret);
  1774. }
  1775. public function testGetShareByTokenWithException() {
  1776. $this->config
  1777. ->expects($this->once())
  1778. ->method('getAppValue')
  1779. ->with('core', 'shareapi_allow_links', 'yes')
  1780. ->willReturn('yes');
  1781. $factory = $this->createMock(IProviderFactory::class);
  1782. $manager = new Manager(
  1783. $this->logger,
  1784. $this->config,
  1785. $this->secureRandom,
  1786. $this->hasher,
  1787. $this->mountManager,
  1788. $this->groupManager,
  1789. $this->l,
  1790. $this->l10nFactory,
  1791. $factory,
  1792. $this->userManager,
  1793. $this->rootFolder,
  1794. $this->eventDispatcher,
  1795. $this->mailer,
  1796. $this->urlGenerator,
  1797. $this->defaults
  1798. );
  1799. $share = $this->createMock(IShare::class);
  1800. $factory->expects($this->at(0))
  1801. ->method('getProviderForType')
  1802. ->with(\OCP\Share::SHARE_TYPE_LINK)
  1803. ->willReturn($this->defaultProvider);
  1804. $factory->expects($this->at(1))
  1805. ->method('getProviderForType')
  1806. ->with(\OCP\Share::SHARE_TYPE_REMOTE)
  1807. ->willReturn($this->defaultProvider);
  1808. $this->defaultProvider->expects($this->at(0))
  1809. ->method('getShareByToken')
  1810. ->with('token')
  1811. ->will($this->throwException(new ShareNotFound()));
  1812. $this->defaultProvider->expects($this->at(1))
  1813. ->method('getShareByToken')
  1814. ->with('token')
  1815. ->willReturn($share);
  1816. $ret = $manager->getShareByToken('token');
  1817. $this->assertSame($share, $ret);
  1818. }
  1819. /**
  1820. * @expectedException \OCP\Share\Exceptions\ShareNotFound
  1821. * @expectedExceptionMessage The requested share does not exist anymore
  1822. */
  1823. public function testGetShareByTokenExpired() {
  1824. $this->config
  1825. ->expects($this->once())
  1826. ->method('getAppValue')
  1827. ->with('core', 'shareapi_allow_links', 'yes')
  1828. ->willReturn('yes');
  1829. $this->l->expects($this->once())
  1830. ->method('t')
  1831. ->willReturnArgument(0);
  1832. $manager = $this->createManagerMock()
  1833. ->setMethods(['deleteShare'])
  1834. ->getMock();
  1835. $date = new \DateTime();
  1836. $date->setTime(0,0,0);
  1837. $share = $this->manager->newShare();
  1838. $share->setExpirationDate($date);
  1839. $this->defaultProvider->expects($this->once())
  1840. ->method('getShareByToken')
  1841. ->with('expiredToken')
  1842. ->willReturn($share);
  1843. $manager->expects($this->once())
  1844. ->method('deleteShare')
  1845. ->with($this->equalTo($share));
  1846. $manager->getShareByToken('expiredToken');
  1847. }
  1848. public function testGetShareByTokenNotExpired() {
  1849. $this->config
  1850. ->expects($this->once())
  1851. ->method('getAppValue')
  1852. ->with('core', 'shareapi_allow_links', 'yes')
  1853. ->willReturn('yes');
  1854. $date = new \DateTime();
  1855. $date->setTime(0,0,0);
  1856. $date->add(new \DateInterval('P2D'));
  1857. $share = $this->manager->newShare();
  1858. $share->setExpirationDate($date);
  1859. $this->defaultProvider->expects($this->once())
  1860. ->method('getShareByToken')
  1861. ->with('expiredToken')
  1862. ->willReturn($share);
  1863. $res = $this->manager->getShareByToken('expiredToken');
  1864. $this->assertSame($share, $res);
  1865. }
  1866. /**
  1867. * @expectedException \OCP\Share\Exceptions\ShareNotFound
  1868. */
  1869. public function testGetShareByTokenWithPublicLinksDisabled() {
  1870. $this->config
  1871. ->expects($this->once())
  1872. ->method('getAppValue')
  1873. ->with('core', 'shareapi_allow_links', 'yes')
  1874. ->willReturn('no');
  1875. $this->manager->getShareByToken('validToken');
  1876. }
  1877. public function testGetShareByTokenPublicUploadDisabled() {
  1878. $this->config
  1879. ->expects($this->at(0))
  1880. ->method('getAppValue')
  1881. ->with('core', 'shareapi_allow_links', 'yes')
  1882. ->willReturn('yes');
  1883. $share = $this->manager->newShare();
  1884. $share->setShareType(\OCP\Share::SHARE_TYPE_LINK)
  1885. ->setPermissions(\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE);
  1886. $this->config
  1887. ->expects($this->at(1))
  1888. ->method('getAppValue')
  1889. ->will($this->returnValueMap([
  1890. ['core', 'shareapi_allow_public_upload', 'yes', 'no'],
  1891. ]));
  1892. $this->defaultProvider->expects($this->once())
  1893. ->method('getShareByToken')
  1894. ->willReturn('validToken')
  1895. ->willReturn($share);
  1896. $res = $this->manager->getShareByToken('validToken');
  1897. $this->assertSame(\OCP\Constants::PERMISSION_READ, $res->getPermissions());
  1898. }
  1899. public function testCheckPasswordNoLinkShare() {
  1900. $share = $this->createMock(IShare::class);
  1901. $share->method('getShareType')->willReturn(\OCP\Share::SHARE_TYPE_USER);
  1902. $this->assertFalse($this->manager->checkPassword($share, 'password'));
  1903. }
  1904. public function testCheckPasswordNoPassword() {
  1905. $share = $this->createMock(IShare::class);
  1906. $share->method('getShareType')->willReturn(\OCP\Share::SHARE_TYPE_LINK);
  1907. $this->assertFalse($this->manager->checkPassword($share, 'password'));
  1908. $share->method('getPassword')->willReturn('password');
  1909. $this->assertFalse($this->manager->checkPassword($share, null));
  1910. }
  1911. public function testCheckPasswordInvalidPassword() {
  1912. $share = $this->createMock(IShare::class);
  1913. $share->method('getShareType')->willReturn(\OCP\Share::SHARE_TYPE_LINK);
  1914. $share->method('getPassword')->willReturn('password');
  1915. $this->hasher->method('verify')->with('invalidpassword', 'password', '')->willReturn(false);
  1916. $this->assertFalse($this->manager->checkPassword($share, 'invalidpassword'));
  1917. }
  1918. public function testCheckPasswordValidPassword() {
  1919. $share = $this->createMock(IShare::class);
  1920. $share->method('getShareType')->willReturn(\OCP\Share::SHARE_TYPE_LINK);
  1921. $share->method('getPassword')->willReturn('passwordHash');
  1922. $this->hasher->method('verify')->with('password', 'passwordHash', '')->willReturn(true);
  1923. $this->assertTrue($this->manager->checkPassword($share, 'password'));
  1924. }
  1925. public function testCheckPasswordUpdateShare() {
  1926. $share = $this->manager->newShare();
  1927. $share->setShareType(\OCP\Share::SHARE_TYPE_LINK)
  1928. ->setPassword('passwordHash');
  1929. $this->hasher->method('verify')->with('password', 'passwordHash', '')
  1930. ->will($this->returnCallback(function($pass, $hash, &$newHash) {
  1931. $newHash = 'newHash';
  1932. return true;
  1933. }));
  1934. $this->defaultProvider->expects($this->once())
  1935. ->method('update')
  1936. ->with($this->callback(function (\OCP\Share\IShare $share) {
  1937. return $share->getPassword() === 'newHash';
  1938. }));
  1939. $this->assertTrue($this->manager->checkPassword($share, 'password'));
  1940. }
  1941. /**
  1942. * @expectedException Exception
  1943. * @expectedExceptionMessage Can’t change share type
  1944. */
  1945. public function testUpdateShareCantChangeShareType() {
  1946. $manager = $this->createManagerMock()
  1947. ->setMethods([
  1948. 'canShare',
  1949. 'getShareById'
  1950. ])
  1951. ->getMock();
  1952. $originalShare = $this->manager->newShare();
  1953. $originalShare->setShareType(\OCP\Share::SHARE_TYPE_GROUP);
  1954. $manager->expects($this->once())->method('canShare')->willReturn(true);
  1955. $manager->expects($this->once())->method('getShareById')->with('foo:42')->willReturn($originalShare);
  1956. $share = $this->manager->newShare();
  1957. $share->setProviderId('foo')
  1958. ->setId('42')
  1959. ->setShareType(\OCP\Share::SHARE_TYPE_USER);
  1960. $manager->updateShare($share);
  1961. }
  1962. /**
  1963. * @expectedException Exception
  1964. * @expectedExceptionMessage Can only update recipient on user shares
  1965. */
  1966. public function testUpdateShareCantChangeRecipientForGroupShare() {
  1967. $manager = $this->createManagerMock()
  1968. ->setMethods([
  1969. 'canShare',
  1970. 'getShareById'
  1971. ])
  1972. ->getMock();
  1973. $originalShare = $this->manager->newShare();
  1974. $originalShare->setShareType(\OCP\Share::SHARE_TYPE_GROUP)
  1975. ->setSharedWith('origGroup');
  1976. $manager->expects($this->once())->method('canShare')->willReturn(true);
  1977. $manager->expects($this->once())->method('getShareById')->with('foo:42')->willReturn($originalShare);
  1978. $share = $this->manager->newShare();
  1979. $share->setProviderId('foo')
  1980. ->setId('42')
  1981. ->setShareType(\OCP\Share::SHARE_TYPE_GROUP)
  1982. ->setSharedWith('newGroup');
  1983. $manager->updateShare($share);
  1984. }
  1985. /**
  1986. * @expectedException Exception
  1987. * @expectedExceptionMessage Can’t share with the share owner
  1988. */
  1989. public function testUpdateShareCantShareWithOwner() {
  1990. $manager = $this->createManagerMock()
  1991. ->setMethods([
  1992. 'canShare',
  1993. 'getShareById'
  1994. ])
  1995. ->getMock();
  1996. $originalShare = $this->manager->newShare();
  1997. $originalShare->setShareType(\OCP\Share::SHARE_TYPE_USER)
  1998. ->setSharedWith('sharedWith');
  1999. $manager->expects($this->once())->method('canShare')->willReturn(true);
  2000. $manager->expects($this->once())->method('getShareById')->with('foo:42')->willReturn($originalShare);
  2001. $share = $this->manager->newShare();
  2002. $share->setProviderId('foo')
  2003. ->setId('42')
  2004. ->setShareType(\OCP\Share::SHARE_TYPE_USER)
  2005. ->setSharedWith('newUser')
  2006. ->setShareOwner('newUser');
  2007. $manager->updateShare($share);
  2008. }
  2009. public function testUpdateShareUser() {
  2010. $this->userManager->expects($this->any())->method('userExists')->willReturn(true);
  2011. $manager = $this->createManagerMock()
  2012. ->setMethods([
  2013. 'canShare',
  2014. 'getShareById',
  2015. 'generalCreateChecks',
  2016. 'userCreateChecks',
  2017. 'pathCreateChecks',
  2018. ])
  2019. ->getMock();
  2020. $originalShare = $this->manager->newShare();
  2021. $originalShare->setShareType(\OCP\Share::SHARE_TYPE_USER)
  2022. ->setSharedWith('origUser')
  2023. ->setPermissions(1);
  2024. $node = $this->createMock(File::class);
  2025. $node->method('getId')->willReturn(100);
  2026. $node->method('getPath')->willReturn('/newUser/files/myPath');
  2027. $manager->expects($this->once())->method('canShare')->willReturn(true);
  2028. $manager->expects($this->once())->method('getShareById')->with('foo:42')->willReturn($originalShare);
  2029. $share = $this->manager->newShare();
  2030. $share->setProviderId('foo')
  2031. ->setId('42')
  2032. ->setShareType(\OCP\Share::SHARE_TYPE_USER)
  2033. ->setSharedWith('origUser')
  2034. ->setShareOwner('newUser')
  2035. ->setSharedBy('sharer')
  2036. ->setPermissions(31)
  2037. ->setNode($node);
  2038. $this->defaultProvider->expects($this->once())
  2039. ->method('update')
  2040. ->with($share)
  2041. ->willReturn($share);
  2042. $hookListner = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  2043. \OCP\Util::connectHook('OCP\Share', 'post_set_expiration_date', $hookListner, 'post');
  2044. $hookListner->expects($this->never())->method('post');
  2045. $this->rootFolder->method('getUserFolder')->with('newUser')->will($this->returnSelf());
  2046. $this->rootFolder->method('getRelativePath')->with('/newUser/files/myPath')->willReturn('/myPath');
  2047. $hookListner2 = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  2048. \OCP\Util::connectHook('OCP\Share', 'post_update_permissions', $hookListner2, 'post');
  2049. $hookListner2->expects($this->once())->method('post')->with([
  2050. 'itemType' => 'file',
  2051. 'itemSource' => 100,
  2052. 'shareType' => \OCP\Share::SHARE_TYPE_USER,
  2053. 'shareWith' => 'origUser',
  2054. 'uidOwner' => 'sharer',
  2055. 'permissions' => 31,
  2056. 'path' => '/myPath',
  2057. ]);
  2058. $manager->updateShare($share);
  2059. }
  2060. public function testUpdateShareGroup() {
  2061. $manager = $this->createManagerMock()
  2062. ->setMethods([
  2063. 'canShare',
  2064. 'getShareById',
  2065. 'generalCreateChecks',
  2066. 'groupCreateChecks',
  2067. 'pathCreateChecks',
  2068. ])
  2069. ->getMock();
  2070. $originalShare = $this->manager->newShare();
  2071. $originalShare->setShareType(\OCP\Share::SHARE_TYPE_GROUP)
  2072. ->setSharedWith('origUser')
  2073. ->setPermissions(31);
  2074. $manager->expects($this->once())->method('canShare')->willReturn(true);
  2075. $manager->expects($this->once())->method('getShareById')->with('foo:42')->willReturn($originalShare);
  2076. $node = $this->createMock(File::class);
  2077. $share = $this->manager->newShare();
  2078. $share->setProviderId('foo')
  2079. ->setId('42')
  2080. ->setShareType(\OCP\Share::SHARE_TYPE_GROUP)
  2081. ->setSharedWith('origUser')
  2082. ->setShareOwner('owner')
  2083. ->setNode($node)
  2084. ->setPermissions(31);
  2085. $this->defaultProvider->expects($this->once())
  2086. ->method('update')
  2087. ->with($share)
  2088. ->willReturn($share);
  2089. $hookListner = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  2090. \OCP\Util::connectHook('OCP\Share', 'post_set_expiration_date', $hookListner, 'post');
  2091. $hookListner->expects($this->never())->method('post');
  2092. $hookListner2 = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  2093. \OCP\Util::connectHook('OCP\Share', 'post_update_permissions', $hookListner2, 'post');
  2094. $hookListner2->expects($this->never())->method('post');
  2095. $manager->updateShare($share);
  2096. }
  2097. public function testUpdateShareLink() {
  2098. $manager = $this->createManagerMock()
  2099. ->setMethods([
  2100. 'canShare',
  2101. 'getShareById',
  2102. 'generalCreateChecks',
  2103. 'linkCreateChecks',
  2104. 'pathCreateChecks',
  2105. 'verifyPassword',
  2106. 'validateExpirationDate',
  2107. ])
  2108. ->getMock();
  2109. $originalShare = $this->manager->newShare();
  2110. $originalShare->setShareType(\OCP\Share::SHARE_TYPE_LINK)
  2111. ->setPermissions(15);
  2112. $tomorrow = new \DateTime();
  2113. $tomorrow->setTime(0,0,0);
  2114. $tomorrow->add(new \DateInterval('P1D'));
  2115. $file = $this->createMock(File::class);
  2116. $file->method('getId')->willReturn(100);
  2117. $share = $this->manager->newShare();
  2118. $share->setProviderId('foo')
  2119. ->setId('42')
  2120. ->setShareType(\OCP\Share::SHARE_TYPE_LINK)
  2121. ->setToken('token')
  2122. ->setSharedBy('owner')
  2123. ->setShareOwner('owner')
  2124. ->setPassword('password')
  2125. ->setExpirationDate($tomorrow)
  2126. ->setNode($file)
  2127. ->setPermissions(15);
  2128. $manager->expects($this->once())->method('canShare')->willReturn(true);
  2129. $manager->expects($this->once())->method('getShareById')->with('foo:42')->willReturn($originalShare);
  2130. $manager->expects($this->once())->method('validateExpirationDate')->with($share);
  2131. $manager->expects($this->once())->method('verifyPassword')->with('password');
  2132. $this->hasher->expects($this->once())
  2133. ->method('hash')
  2134. ->with('password')
  2135. ->willReturn('hashed');
  2136. $this->defaultProvider->expects($this->once())
  2137. ->method('update')
  2138. ->with($share)
  2139. ->willReturn($share);
  2140. $hookListner = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  2141. \OCP\Util::connectHook('OCP\Share', 'post_set_expiration_date', $hookListner, 'post');
  2142. $hookListner->expects($this->once())->method('post')->with([
  2143. 'itemType' => 'file',
  2144. 'itemSource' => 100,
  2145. 'date' => $tomorrow,
  2146. 'uidOwner' => 'owner',
  2147. ]);
  2148. $hookListner2 = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  2149. \OCP\Util::connectHook('OCP\Share', 'post_update_password', $hookListner2, 'post');
  2150. $hookListner2->expects($this->once())->method('post')->with([
  2151. 'itemType' => 'file',
  2152. 'itemSource' => 100,
  2153. 'uidOwner' => 'owner',
  2154. 'token' => 'token',
  2155. 'disabled' => false,
  2156. ]);
  2157. $hookListner3 = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  2158. \OCP\Util::connectHook('OCP\Share', 'post_update_permissions', $hookListner3, 'post');
  2159. $hookListner3->expects($this->never())->method('post');
  2160. $manager->updateShare($share);
  2161. }
  2162. public function testUpdateShareMail() {
  2163. $manager = $this->createManagerMock()
  2164. ->setMethods([
  2165. 'canShare',
  2166. 'getShareById',
  2167. 'generalCreateChecks',
  2168. 'verifyPassword',
  2169. 'pathCreateChecks',
  2170. 'linkCreateChecks',
  2171. 'validateExpirationDate',
  2172. ])
  2173. ->getMock();
  2174. $originalShare = $this->manager->newShare();
  2175. $originalShare->setShareType(\OCP\Share::SHARE_TYPE_EMAIL)
  2176. ->setPermissions(\OCP\Constants::PERMISSION_ALL);
  2177. $tomorrow = new \DateTime();
  2178. $tomorrow->setTime(0,0,0);
  2179. $tomorrow->add(new \DateInterval('P1D'));
  2180. $file = $this->createMock(File::class);
  2181. $file->method('getId')->willReturn(100);
  2182. $share = $this->manager->newShare();
  2183. $share->setProviderId('foo')
  2184. ->setId('42')
  2185. ->setShareType(\OCP\Share::SHARE_TYPE_EMAIL)
  2186. ->setToken('token')
  2187. ->setSharedBy('owner')
  2188. ->setShareOwner('owner')
  2189. ->setPassword('password')
  2190. ->setExpirationDate($tomorrow)
  2191. ->setNode($file)
  2192. ->setPermissions(\OCP\Constants::PERMISSION_ALL);
  2193. $manager->expects($this->once())->method('canShare')->willReturn(true);
  2194. $manager->expects($this->once())->method('getShareById')->with('foo:42')->willReturn($originalShare);
  2195. $manager->expects($this->once())->method('generalCreateChecks')->with($share);
  2196. $manager->expects($this->once())->method('verifyPassword')->with('password');
  2197. $manager->expects($this->once())->method('pathCreateChecks')->with($file);
  2198. $manager->expects($this->never())->method('linkCreateChecks');
  2199. $manager->expects($this->never())->method('validateExpirationDate');
  2200. $this->hasher->expects($this->once())
  2201. ->method('hash')
  2202. ->with('password')
  2203. ->willReturn('hashed');
  2204. $this->defaultProvider->expects($this->once())
  2205. ->method('update')
  2206. ->with($share, 'password')
  2207. ->willReturn($share);
  2208. $hookListner = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  2209. \OCP\Util::connectHook('OCP\Share', 'post_set_expiration_date', $hookListner, 'post');
  2210. $hookListner->expects($this->never())->method('post');
  2211. $hookListner2 = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  2212. \OCP\Util::connectHook('OCP\Share', 'post_update_password', $hookListner2, 'post');
  2213. $hookListner2->expects($this->once())->method('post')->with([
  2214. 'itemType' => 'file',
  2215. 'itemSource' => 100,
  2216. 'uidOwner' => 'owner',
  2217. 'token' => 'token',
  2218. 'disabled' => false,
  2219. ]);
  2220. $hookListner3 = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  2221. \OCP\Util::connectHook('OCP\Share', 'post_update_permissions', $hookListner3, 'post');
  2222. $hookListner3->expects($this->never())->method('post');
  2223. $manager->updateShare($share);
  2224. }
  2225. /**
  2226. * @expectedException \InvalidArgumentException
  2227. * @expectedExceptionMessage Can’t change target of link share
  2228. */
  2229. public function testMoveShareLink() {
  2230. $share = $this->manager->newShare();
  2231. $share->setShareType(\OCP\Share::SHARE_TYPE_LINK);
  2232. $recipient = $this->createMock(IUser::class);
  2233. $this->manager->moveShare($share, $recipient);
  2234. }
  2235. /**
  2236. * @expectedException \InvalidArgumentException
  2237. * @expectedExceptionMessage Invalid recipient
  2238. */
  2239. public function testMoveShareUserNotRecipient() {
  2240. $share = $this->manager->newShare();
  2241. $share->setShareType(\OCP\Share::SHARE_TYPE_USER);
  2242. $share->setSharedWith('sharedWith');
  2243. $this->manager->moveShare($share, 'recipient');
  2244. }
  2245. public function testMoveShareUser() {
  2246. $share = $this->manager->newShare();
  2247. $share->setShareType(\OCP\Share::SHARE_TYPE_USER)
  2248. ->setId('42')
  2249. ->setProviderId('foo');
  2250. $share->setSharedWith('recipient');
  2251. $this->defaultProvider->method('move')->with($share, 'recipient')->will($this->returnArgument(0));
  2252. $this->manager->moveShare($share, 'recipient');
  2253. $this->addToAssertionCount(1);
  2254. }
  2255. /**
  2256. * @expectedException \InvalidArgumentException
  2257. * @expectedExceptionMessage Invalid recipient
  2258. */
  2259. public function testMoveShareGroupNotRecipient() {
  2260. $share = $this->manager->newShare();
  2261. $share->setShareType(\OCP\Share::SHARE_TYPE_GROUP);
  2262. $sharedWith = $this->createMock(IGroup::class);
  2263. $share->setSharedWith('shareWith');
  2264. $recipient = $this->createMock(IUser::class);
  2265. $sharedWith->method('inGroup')->with($recipient)->willReturn(false);
  2266. $this->groupManager->method('get')->with('shareWith')->willReturn($sharedWith);
  2267. $this->userManager->method('get')->with('recipient')->willReturn($recipient);
  2268. $this->manager->moveShare($share, 'recipient');
  2269. }
  2270. /**
  2271. * @expectedException \InvalidArgumentException
  2272. * @expectedExceptionMessage Group "shareWith" does not exist
  2273. */
  2274. public function testMoveShareGroupNull() {
  2275. $share = $this->manager->newShare();
  2276. $share->setShareType(\OCP\Share::SHARE_TYPE_GROUP);
  2277. $share->setSharedWith('shareWith');
  2278. $recipient = $this->createMock(IUser::class);
  2279. $this->groupManager->method('get')->with('shareWith')->willReturn(null);
  2280. $this->userManager->method('get')->with('recipient')->willReturn($recipient);
  2281. $this->manager->moveShare($share, 'recipient');
  2282. }
  2283. public function testMoveShareGroup() {
  2284. $share = $this->manager->newShare();
  2285. $share->setShareType(\OCP\Share::SHARE_TYPE_GROUP)
  2286. ->setId('42')
  2287. ->setProviderId('foo');
  2288. $group = $this->createMock(IGroup::class);
  2289. $share->setSharedWith('group');
  2290. $recipient = $this->createMock(IUser::class);
  2291. $group->method('inGroup')->with($recipient)->willReturn(true);
  2292. $this->groupManager->method('get')->with('group')->willReturn($group);
  2293. $this->userManager->method('get')->with('recipient')->willReturn($recipient);
  2294. $this->defaultProvider->method('move')->with($share, 'recipient')->will($this->returnArgument(0));
  2295. $this->manager->moveShare($share, 'recipient');
  2296. $this->addToAssertionCount(1);
  2297. }
  2298. /**
  2299. * @dataProvider dataTestShareProviderExists
  2300. */
  2301. public function testShareProviderExists($shareType, $expected) {
  2302. $factory = $this->getMockBuilder('OCP\Share\IProviderFactory')->getMock();
  2303. $factory->expects($this->any())->method('getProviderForType')
  2304. ->willReturnCallback(function ($id) {
  2305. if ($id === \OCP\Share::SHARE_TYPE_USER) {
  2306. return true;
  2307. }
  2308. throw new Exception\ProviderException();
  2309. });
  2310. $manager = new Manager(
  2311. $this->logger,
  2312. $this->config,
  2313. $this->secureRandom,
  2314. $this->hasher,
  2315. $this->mountManager,
  2316. $this->groupManager,
  2317. $this->l,
  2318. $this->l10nFactory,
  2319. $factory,
  2320. $this->userManager,
  2321. $this->rootFolder,
  2322. $this->eventDispatcher,
  2323. $this->mailer,
  2324. $this->urlGenerator,
  2325. $this->defaults
  2326. );
  2327. $this->assertSame($expected,
  2328. $manager->shareProviderExists($shareType)
  2329. );
  2330. }
  2331. public function dataTestShareProviderExists() {
  2332. return [
  2333. [\OCP\Share::SHARE_TYPE_USER, true],
  2334. [42, false],
  2335. ];
  2336. }
  2337. public function testGetSharesInFolder() {
  2338. $factory = new DummyFactory2($this->createMock(IServerContainer::class));
  2339. $manager = new Manager(
  2340. $this->logger,
  2341. $this->config,
  2342. $this->secureRandom,
  2343. $this->hasher,
  2344. $this->mountManager,
  2345. $this->groupManager,
  2346. $this->l,
  2347. $this->l10nFactory,
  2348. $factory,
  2349. $this->userManager,
  2350. $this->rootFolder,
  2351. $this->eventDispatcher,
  2352. $this->mailer,
  2353. $this->urlGenerator,
  2354. $this->defaults
  2355. );
  2356. $factory->setProvider($this->defaultProvider);
  2357. $extraProvider = $this->createMock(IShareProvider::class);
  2358. $factory->setSecondProvider($extraProvider);
  2359. $share1 = $this->createMock(IShare::class);
  2360. $share2 = $this->createMock(IShare::class);
  2361. $share3 = $this->createMock(IShare::class);
  2362. $share4 = $this->createMock(IShare::class);
  2363. $folder = $this->createMock(Folder::class);
  2364. $this->defaultProvider->method('getSharesInFolder')
  2365. ->with(
  2366. $this->equalTo('user'),
  2367. $this->equalTo($folder),
  2368. $this->equalTo(false)
  2369. )->willReturn([
  2370. 1 => [$share1],
  2371. 2 => [$share2],
  2372. ]);
  2373. $extraProvider->method('getSharesInFolder')
  2374. ->with(
  2375. $this->equalTo('user'),
  2376. $this->equalTo($folder),
  2377. $this->equalTo(false)
  2378. )->willReturn([
  2379. 2 => [$share3],
  2380. 3 => [$share4],
  2381. ]);
  2382. $result = $manager->getSharesInFolder('user', $folder, false);
  2383. $expects = [
  2384. 1 => [$share1],
  2385. 2 => [$share2, $share3],
  2386. 3 => [$share4],
  2387. ];
  2388. $this->assertSame($expects, $result);
  2389. }
  2390. public function testGetAccessList() {
  2391. $factory = new DummyFactory2($this->createMock(IServerContainer::class));
  2392. $manager = new Manager(
  2393. $this->logger,
  2394. $this->config,
  2395. $this->secureRandom,
  2396. $this->hasher,
  2397. $this->mountManager,
  2398. $this->groupManager,
  2399. $this->l,
  2400. $this->l10nFactory,
  2401. $factory,
  2402. $this->userManager,
  2403. $this->rootFolder,
  2404. $this->eventDispatcher,
  2405. $this->mailer,
  2406. $this->urlGenerator,
  2407. $this->defaults
  2408. );
  2409. $factory->setProvider($this->defaultProvider);
  2410. $extraProvider = $this->createMock(IShareProvider::class);
  2411. $factory->setSecondProvider($extraProvider);
  2412. $owner = $this->createMock(IUser::class);
  2413. $owner->expects($this->once())
  2414. ->method('getUID')
  2415. ->willReturn('owner');
  2416. $node = $this->createMock(Node::class);
  2417. $node->expects($this->once())
  2418. ->method('getOwner')
  2419. ->willReturn($owner);
  2420. $node->method('getId')
  2421. ->willReturn(42);
  2422. $userFolder = $this->createMock(Folder::class);
  2423. $file = $this->createMock(File::class);
  2424. $folder = $this->createMock(Folder::class);
  2425. $file->method('getParent')
  2426. ->willReturn($folder);
  2427. $file->method('getPath')
  2428. ->willReturn('/owner/files/folder/file');
  2429. $file->method('getId')
  2430. ->willReturn(23);
  2431. $folder->method('getParent')
  2432. ->willReturn($userFolder);
  2433. $folder->method('getPath')
  2434. ->willReturn('/owner/files/folder');
  2435. $userFolder->method('getById')
  2436. ->with($this->equalTo(42))
  2437. ->willReturn([$file]);
  2438. $userFolder->method('getPath')
  2439. ->willReturn('/owner/files');
  2440. $this->userManager->method('userExists')
  2441. ->with($this->equalTo('owner'))
  2442. ->willReturn(true);
  2443. $this->defaultProvider->method('getAccessList')
  2444. ->with(
  2445. $this->equalTo([$file, $folder]),
  2446. false
  2447. )
  2448. ->willReturn([
  2449. 'users' => [
  2450. 'user1',
  2451. 'user2',
  2452. 'user3',
  2453. '123456',
  2454. ],
  2455. 'public' => true,
  2456. ]);
  2457. $extraProvider->method('getAccessList')
  2458. ->with(
  2459. $this->equalTo([$file, $folder]),
  2460. false
  2461. )
  2462. ->willReturn([
  2463. 'users' => [
  2464. 'user3',
  2465. 'user4',
  2466. 'user5',
  2467. '234567',
  2468. ],
  2469. 'remote' => true,
  2470. ]);
  2471. $this->rootFolder->method('getUserFolder')
  2472. ->with($this->equalTo('owner'))
  2473. ->willReturn($userFolder);
  2474. $expected = [
  2475. 'users' => ['owner', 'user1', 'user2', 'user3', '123456','user4', 'user5', '234567'],
  2476. 'remote' => true,
  2477. 'public' => true,
  2478. ];
  2479. $result = $manager->getAccessList($node, true, false);
  2480. $this->assertSame($expected['public'], $result['public']);
  2481. $this->assertSame($expected['remote'], $result['remote']);
  2482. $this->assertSame($expected['users'], $result['users']);
  2483. }
  2484. public function testGetAccessListWithCurrentAccess() {
  2485. $factory = new DummyFactory2($this->createMock(IServerContainer::class));
  2486. $manager = new Manager(
  2487. $this->logger,
  2488. $this->config,
  2489. $this->secureRandom,
  2490. $this->hasher,
  2491. $this->mountManager,
  2492. $this->groupManager,
  2493. $this->l,
  2494. $this->l10nFactory,
  2495. $factory,
  2496. $this->userManager,
  2497. $this->rootFolder,
  2498. $this->eventDispatcher,
  2499. $this->mailer,
  2500. $this->urlGenerator,
  2501. $this->defaults
  2502. );
  2503. $factory->setProvider($this->defaultProvider);
  2504. $extraProvider = $this->createMock(IShareProvider::class);
  2505. $factory->setSecondProvider($extraProvider);
  2506. $owner = $this->createMock(IUser::class);
  2507. $owner->expects($this->once())
  2508. ->method('getUID')
  2509. ->willReturn('owner');
  2510. $node = $this->createMock(Node::class);
  2511. $node->expects($this->once())
  2512. ->method('getOwner')
  2513. ->willReturn($owner);
  2514. $node->method('getId')
  2515. ->willReturn(42);
  2516. $userFolder = $this->createMock(Folder::class);
  2517. $file = $this->createMock(File::class);
  2518. $folder = $this->createMock(Folder::class);
  2519. $file->method('getParent')
  2520. ->willReturn($folder);
  2521. $file->method('getPath')
  2522. ->willReturn('/owner/files/folder/file');
  2523. $file->method('getId')
  2524. ->willReturn(23);
  2525. $folder->method('getParent')
  2526. ->willReturn($userFolder);
  2527. $folder->method('getPath')
  2528. ->willReturn('/owner/files/folder');
  2529. $userFolder->method('getById')
  2530. ->with($this->equalTo(42))
  2531. ->willReturn([$file]);
  2532. $userFolder->method('getPath')
  2533. ->willReturn('/owner/files');
  2534. $this->userManager->method('userExists')
  2535. ->with($this->equalTo('owner'))
  2536. ->willReturn(true);
  2537. $this->defaultProvider->method('getAccessList')
  2538. ->with(
  2539. $this->equalTo([$file, $folder]),
  2540. true
  2541. )
  2542. ->willReturn([
  2543. 'users' => [
  2544. 'user1' => [],
  2545. 'user2' => [],
  2546. 'user3' => [],
  2547. '123456' => [],
  2548. ],
  2549. 'public' => true,
  2550. ]);
  2551. $extraProvider->method('getAccessList')
  2552. ->with(
  2553. $this->equalTo([$file, $folder]),
  2554. true
  2555. )
  2556. ->willReturn([
  2557. 'users' => [
  2558. 'user3' => [],
  2559. 'user4' => [],
  2560. 'user5' => [],
  2561. '234567' => [],
  2562. ],
  2563. 'remote' => [
  2564. 'remote1',
  2565. ],
  2566. ]);
  2567. $this->rootFolder->method('getUserFolder')
  2568. ->with($this->equalTo('owner'))
  2569. ->willReturn($userFolder);
  2570. $expected = [
  2571. 'users' => [
  2572. 'owner' => [
  2573. 'node_id' => 23,
  2574. 'node_path' => '/folder/file'
  2575. ]
  2576. , 'user1' => [], 'user2' => [], 'user3' => [], '123456' => [], 'user4' => [], 'user5' => [], '234567' => []],
  2577. 'remote' => [
  2578. 'remote1',
  2579. ],
  2580. 'public' => true,
  2581. ];
  2582. $result = $manager->getAccessList($node, true, true);
  2583. $this->assertSame($expected['public'], $result['public']);
  2584. $this->assertSame($expected['remote'], $result['remote']);
  2585. $this->assertSame($expected['users'], $result['users']);
  2586. }
  2587. }
  2588. class DummyFactory implements IProviderFactory {
  2589. /** @var IShareProvider */
  2590. protected $provider;
  2591. public function __construct(\OCP\IServerContainer $serverContainer) {
  2592. }
  2593. /**
  2594. * @param IShareProvider $provider
  2595. */
  2596. public function setProvider($provider) {
  2597. $this->provider = $provider;
  2598. }
  2599. /**
  2600. * @param string $id
  2601. * @return IShareProvider
  2602. */
  2603. public function getProvider($id) {
  2604. return $this->provider;
  2605. }
  2606. /**
  2607. * @param int $shareType
  2608. * @return IShareProvider
  2609. */
  2610. public function getProviderForType($shareType) {
  2611. return $this->provider;
  2612. }
  2613. /**
  2614. * @return IShareProvider[]
  2615. */
  2616. public function getAllProviders() {
  2617. return [$this->provider];
  2618. }
  2619. }
  2620. class DummyFactory2 extends DummyFactory {
  2621. /** @var IShareProvider */
  2622. private $provider2;
  2623. /**
  2624. * @param IShareProvider $provider
  2625. */
  2626. public function setSecondProvider($provider) {
  2627. $this->provider2 = $provider;
  2628. }
  2629. public function getAllProviders() {
  2630. return [$this->provider, $this->provider2];
  2631. }
  2632. }