1
0

ManagerTest.php 78 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567
  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 OCP\Files\File;
  25. use OCP\Files\Folder;
  26. use OCP\Files\IRootFolder;
  27. use OCP\Files\Mount\IMountPoint;
  28. use OCP\Files\Node;
  29. use OCP\Files\Storage;
  30. use OCP\IGroup;
  31. use OCP\IUser;
  32. use OCP\IUserManager;
  33. use OCP\Share\Exceptions\ShareNotFound;
  34. use OCP\Share\IProviderFactory;
  35. use OCP\Share\IShare;
  36. use OC\Share20\Manager;
  37. use OC\Share20\Exception;
  38. use OC\Share20\Share;
  39. use OCP\IL10N;
  40. use OCP\ILogger;
  41. use OCP\IConfig;
  42. use OCP\Share\IShareProvider;
  43. use OCP\Security\ISecureRandom;
  44. use OCP\Security\IHasher;
  45. use OCP\Files\Mount\IMountManager;
  46. use OCP\IGroupManager;
  47. use Symfony\Component\EventDispatcher\EventDispatcher;
  48. use Symfony\Component\EventDispatcher\GenericEvent;
  49. /**
  50. * Class ManagerTest
  51. *
  52. * @package Test\Share20
  53. * @group DB
  54. */
  55. class ManagerTest extends \Test\TestCase {
  56. /** @var Manager */
  57. protected $manager;
  58. /** @var ILogger */
  59. protected $logger;
  60. /** @var IConfig */
  61. protected $config;
  62. /** @var ISecureRandom */
  63. protected $secureRandom;
  64. /** @var IHasher */
  65. protected $hasher;
  66. /** @var IShareProvider | \PHPUnit_Framework_MockObject_MockObject */
  67. protected $defaultProvider;
  68. /** @var IMountManager */
  69. protected $mountManager;
  70. /** @var IGroupManager */
  71. protected $groupManager;
  72. /** @var IL10N */
  73. protected $l;
  74. /** @var DummyFactory */
  75. protected $factory;
  76. /** @var IUserManager */
  77. protected $userManager;
  78. /** @var IRootFolder | \PHPUnit_Framework_MockObject_MockObject */
  79. protected $rootFolder;
  80. /** @var EventDispatcher | \PHPUnit_Framework_MockObject_MockObject */
  81. protected $eventDispatcher;
  82. public function setUp() {
  83. $this->logger = $this->createMock(ILogger::class);
  84. $this->config = $this->createMock(IConfig::class);
  85. $this->secureRandom = $this->createMock(ISecureRandom::class);
  86. $this->hasher = $this->createMock(IHasher::class);
  87. $this->mountManager = $this->createMock(IMountManager::class);
  88. $this->groupManager = $this->createMock(IGroupManager::class);
  89. $this->userManager = $this->createMock(IUserManager::class);
  90. $this->rootFolder = $this->createMock(IRootFolder::class);
  91. $this->eventDispatcher = $this->createMock(EventDispatcher::class);
  92. $this->l = $this->createMock(IL10N::class);
  93. $this->l->method('t')
  94. ->will($this->returnCallback(function($text, $parameters = []) {
  95. return vsprintf($text, $parameters);
  96. }));
  97. $this->factory = new DummyFactory(\OC::$server);
  98. $this->manager = new Manager(
  99. $this->logger,
  100. $this->config,
  101. $this->secureRandom,
  102. $this->hasher,
  103. $this->mountManager,
  104. $this->groupManager,
  105. $this->l,
  106. $this->factory,
  107. $this->userManager,
  108. $this->rootFolder,
  109. $this->eventDispatcher
  110. );
  111. $this->defaultProvider = $this->getMockBuilder('\OC\Share20\DefaultShareProvider')
  112. ->disableOriginalConstructor()
  113. ->getMock();
  114. $this->defaultProvider->method('identifier')->willReturn('default');
  115. $this->factory->setProvider($this->defaultProvider);
  116. }
  117. /**
  118. * @return \PHPUnit_Framework_MockObject_MockBuilder
  119. */
  120. private function createManagerMock() {
  121. return $this->getMockBuilder('\OC\Share20\Manager')
  122. ->setConstructorArgs([
  123. $this->logger,
  124. $this->config,
  125. $this->secureRandom,
  126. $this->hasher,
  127. $this->mountManager,
  128. $this->groupManager,
  129. $this->l,
  130. $this->factory,
  131. $this->userManager,
  132. $this->rootFolder,
  133. $this->eventDispatcher
  134. ]);
  135. }
  136. /**
  137. * @expectedException \InvalidArgumentException
  138. */
  139. public function testDeleteNoShareId() {
  140. $share = $this->manager->newShare();
  141. $this->manager->deleteShare($share);
  142. }
  143. public function dataTestDelete() {
  144. $user = $this->createMock(IUser::class);
  145. $user->method('getUID')->willReturn('sharedWithUser');
  146. $group = $this->createMock(IGroup::class);
  147. $group->method('getGID')->willReturn('sharedWithGroup');
  148. return [
  149. [\OCP\Share::SHARE_TYPE_USER, 'sharedWithUser'],
  150. [\OCP\Share::SHARE_TYPE_GROUP, 'sharedWithGroup'],
  151. [\OCP\Share::SHARE_TYPE_LINK, ''],
  152. [\OCP\Share::SHARE_TYPE_REMOTE, 'foo@bar.com'],
  153. ];
  154. }
  155. /**
  156. * @dataProvider dataTestDelete
  157. */
  158. public function testDelete($shareType, $sharedWith) {
  159. $manager = $this->createManagerMock()
  160. ->setMethods(['getShareById', 'deleteChildren'])
  161. ->getMock();
  162. $path = $this->createMock(File::class);
  163. $path->method('getId')->willReturn(1);
  164. $share = $this->manager->newShare();
  165. $share->setId(42)
  166. ->setProviderId('prov')
  167. ->setShareType($shareType)
  168. ->setSharedWith($sharedWith)
  169. ->setSharedBy('sharedBy')
  170. ->setNode($path)
  171. ->setTarget('myTarget');
  172. $manager->expects($this->once())->method('deleteChildren')->with($share);
  173. $this->defaultProvider
  174. ->expects($this->once())
  175. ->method('delete')
  176. ->with($share);
  177. $hookListner = $this->getMockBuilder('Dummy')->setMethods(['pre', 'post'])->getMock();
  178. \OCP\Util::connectHook('OCP\Share', 'pre_unshare', $hookListner, 'pre');
  179. \OCP\Util::connectHook('OCP\Share', 'post_unshare', $hookListner, 'post');
  180. $hookListnerExpectsPre = [
  181. 'id' => 42,
  182. 'itemType' => 'file',
  183. 'itemSource' => 1,
  184. 'shareType' => $shareType,
  185. 'shareWith' => $sharedWith,
  186. 'itemparent' => null,
  187. 'uidOwner' => 'sharedBy',
  188. 'fileSource' => 1,
  189. 'fileTarget' => 'myTarget',
  190. ];
  191. $hookListnerExpectsPost = [
  192. 'id' => 42,
  193. 'itemType' => 'file',
  194. 'itemSource' => 1,
  195. 'shareType' => $shareType,
  196. 'shareWith' => $sharedWith,
  197. 'itemparent' => null,
  198. 'uidOwner' => 'sharedBy',
  199. 'fileSource' => 1,
  200. 'fileTarget' => 'myTarget',
  201. 'deletedShares' => [
  202. [
  203. 'id' => 42,
  204. 'itemType' => 'file',
  205. 'itemSource' => 1,
  206. 'shareType' => $shareType,
  207. 'shareWith' => $sharedWith,
  208. 'itemparent' => null,
  209. 'uidOwner' => 'sharedBy',
  210. 'fileSource' => 1,
  211. 'fileTarget' => 'myTarget',
  212. ],
  213. ],
  214. ];
  215. $hookListner
  216. ->expects($this->exactly(1))
  217. ->method('pre')
  218. ->with($hookListnerExpectsPre);
  219. $hookListner
  220. ->expects($this->exactly(1))
  221. ->method('post')
  222. ->with($hookListnerExpectsPost);
  223. $manager->deleteShare($share);
  224. }
  225. public function testDeleteLazyShare() {
  226. $manager = $this->createManagerMock()
  227. ->setMethods(['getShareById', 'deleteChildren'])
  228. ->getMock();
  229. $share = $this->manager->newShare();
  230. $share->setId(42)
  231. ->setProviderId('prov')
  232. ->setShareType(\OCP\Share::SHARE_TYPE_USER)
  233. ->setSharedWith('sharedWith')
  234. ->setSharedBy('sharedBy')
  235. ->setShareOwner('shareOwner')
  236. ->setTarget('myTarget')
  237. ->setNodeId(1)
  238. ->setNodeType('file');
  239. $this->rootFolder->expects($this->never())->method($this->anything());
  240. $manager->expects($this->once())->method('deleteChildren')->with($share);
  241. $this->defaultProvider
  242. ->expects($this->once())
  243. ->method('delete')
  244. ->with($share);
  245. $hookListner = $this->getMockBuilder('Dummy')->setMethods(['pre', 'post'])->getMock();
  246. \OCP\Util::connectHook('OCP\Share', 'pre_unshare', $hookListner, 'pre');
  247. \OCP\Util::connectHook('OCP\Share', 'post_unshare', $hookListner, 'post');
  248. $hookListnerExpectsPre = [
  249. 'id' => 42,
  250. 'itemType' => 'file',
  251. 'itemSource' => 1,
  252. 'shareType' => \OCP\Share::SHARE_TYPE_USER,
  253. 'shareWith' => 'sharedWith',
  254. 'itemparent' => null,
  255. 'uidOwner' => 'sharedBy',
  256. 'fileSource' => 1,
  257. 'fileTarget' => 'myTarget',
  258. ];
  259. $hookListnerExpectsPost = [
  260. 'id' => 42,
  261. 'itemType' => 'file',
  262. 'itemSource' => 1,
  263. 'shareType' => \OCP\Share::SHARE_TYPE_USER,
  264. 'shareWith' => 'sharedWith',
  265. 'itemparent' => null,
  266. 'uidOwner' => 'sharedBy',
  267. 'fileSource' => 1,
  268. 'fileTarget' => 'myTarget',
  269. 'deletedShares' => [
  270. [
  271. 'id' => 42,
  272. 'itemType' => 'file',
  273. 'itemSource' => 1,
  274. 'shareType' => \OCP\Share::SHARE_TYPE_USER,
  275. 'shareWith' => 'sharedWith',
  276. 'itemparent' => null,
  277. 'uidOwner' => 'sharedBy',
  278. 'fileSource' => 1,
  279. 'fileTarget' => 'myTarget',
  280. ],
  281. ],
  282. ];
  283. $hookListner
  284. ->expects($this->exactly(1))
  285. ->method('pre')
  286. ->with($hookListnerExpectsPre);
  287. $hookListner
  288. ->expects($this->exactly(1))
  289. ->method('post')
  290. ->with($hookListnerExpectsPost);
  291. $manager->deleteShare($share);
  292. }
  293. public function testDeleteNested() {
  294. $manager = $this->createManagerMock()
  295. ->setMethods(['getShareById'])
  296. ->getMock();
  297. $path = $this->createMock(File::class);
  298. $path->method('getId')->willReturn(1);
  299. $share1 = $this->manager->newShare();
  300. $share1->setId(42)
  301. ->setProviderId('prov')
  302. ->setShareType(\OCP\Share::SHARE_TYPE_USER)
  303. ->setSharedWith('sharedWith1')
  304. ->setSharedBy('sharedBy1')
  305. ->setNode($path)
  306. ->setTarget('myTarget1');
  307. $share2 = $this->manager->newShare();
  308. $share2->setId(43)
  309. ->setProviderId('prov')
  310. ->setShareType(\OCP\Share::SHARE_TYPE_GROUP)
  311. ->setSharedWith('sharedWith2')
  312. ->setSharedBy('sharedBy2')
  313. ->setNode($path)
  314. ->setTarget('myTarget2')
  315. ->setParent(42);
  316. $share3 = $this->manager->newShare();
  317. $share3->setId(44)
  318. ->setProviderId('prov')
  319. ->setShareType(\OCP\Share::SHARE_TYPE_LINK)
  320. ->setSharedBy('sharedBy3')
  321. ->setNode($path)
  322. ->setTarget('myTarget3')
  323. ->setParent(43);
  324. $this->defaultProvider
  325. ->method('getChildren')
  326. ->will($this->returnValueMap([
  327. [$share1, [$share2]],
  328. [$share2, [$share3]],
  329. [$share3, []],
  330. ]));
  331. $this->defaultProvider
  332. ->method('delete')
  333. ->withConsecutive($share3, $share2, $share1);
  334. $hookListner = $this->getMockBuilder('Dummy')->setMethods(['pre', 'post'])->getMock();
  335. \OCP\Util::connectHook('OCP\Share', 'pre_unshare', $hookListner, 'pre');
  336. \OCP\Util::connectHook('OCP\Share', 'post_unshare', $hookListner, 'post');
  337. $hookListnerExpectsPre = [
  338. 'id' => 42,
  339. 'itemType' => 'file',
  340. 'itemSource' => 1,
  341. 'shareType' => \OCP\Share::SHARE_TYPE_USER,
  342. 'shareWith' => 'sharedWith1',
  343. 'itemparent' => null,
  344. 'uidOwner' => 'sharedBy1',
  345. 'fileSource' => 1,
  346. 'fileTarget' => 'myTarget1',
  347. ];
  348. $hookListnerExpectsPost = [
  349. 'id' => 42,
  350. 'itemType' => 'file',
  351. 'itemSource' => 1,
  352. 'shareType' => \OCP\Share::SHARE_TYPE_USER,
  353. 'shareWith' => 'sharedWith1',
  354. 'itemparent' => null,
  355. 'uidOwner' => 'sharedBy1',
  356. 'fileSource' => 1,
  357. 'fileTarget' => 'myTarget1',
  358. 'deletedShares' => [
  359. [
  360. 'id' => 44,
  361. 'itemType' => 'file',
  362. 'itemSource' => 1,
  363. 'shareType' => \OCP\Share::SHARE_TYPE_LINK,
  364. 'shareWith' => '',
  365. 'itemparent' => 43,
  366. 'uidOwner' => 'sharedBy3',
  367. 'fileSource' => 1,
  368. 'fileTarget' => 'myTarget3',
  369. ],
  370. [
  371. 'id' => 43,
  372. 'itemType' => 'file',
  373. 'itemSource' => 1,
  374. 'shareType' => \OCP\Share::SHARE_TYPE_GROUP,
  375. 'shareWith' => 'sharedWith2',
  376. 'itemparent' => 42,
  377. 'uidOwner' => 'sharedBy2',
  378. 'fileSource' => 1,
  379. 'fileTarget' => 'myTarget2',
  380. ],
  381. [
  382. 'id' => 42,
  383. 'itemType' => 'file',
  384. 'itemSource' => 1,
  385. 'shareType' => \OCP\Share::SHARE_TYPE_USER,
  386. 'shareWith' => 'sharedWith1',
  387. 'itemparent' => null,
  388. 'uidOwner' => 'sharedBy1',
  389. 'fileSource' => 1,
  390. 'fileTarget' => 'myTarget1',
  391. ],
  392. ],
  393. ];
  394. $hookListner
  395. ->expects($this->exactly(1))
  396. ->method('pre')
  397. ->with($hookListnerExpectsPre);
  398. $hookListner
  399. ->expects($this->exactly(1))
  400. ->method('post')
  401. ->with($hookListnerExpectsPost);
  402. $manager->deleteShare($share1);
  403. }
  404. public function testDeleteChildren() {
  405. $manager = $this->createManagerMock()
  406. ->setMethods(['deleteShare'])
  407. ->getMock();
  408. $share = $this->createMock(IShare::class);
  409. $share->method('getShareType')->willReturn(\OCP\Share::SHARE_TYPE_USER);
  410. $child1 = $this->createMock(IShare::class);
  411. $child1->method('getShareType')->willReturn(\OCP\Share::SHARE_TYPE_USER);
  412. $child2 = $this->createMock(IShare::class);
  413. $child2->method('getShareType')->willReturn(\OCP\Share::SHARE_TYPE_USER);
  414. $child3 = $this->createMock(IShare::class);
  415. $child3->method('getShareType')->willReturn(\OCP\Share::SHARE_TYPE_USER);
  416. $shares = [
  417. $child1,
  418. $child2,
  419. $child3,
  420. ];
  421. $this->defaultProvider
  422. ->expects($this->exactly(4))
  423. ->method('getChildren')
  424. ->will($this->returnCallback(function($_share) use ($share, $shares) {
  425. if ($_share === $share) {
  426. return $shares;
  427. }
  428. return [];
  429. }));
  430. $this->defaultProvider
  431. ->expects($this->exactly(3))
  432. ->method('delete')
  433. ->withConsecutive($child1, $child2, $child3);
  434. $result = $this->invokePrivate($manager, 'deleteChildren', [$share]);
  435. $this->assertSame($shares, $result);
  436. }
  437. public function testGetShareById() {
  438. $share = $this->createMock(IShare::class);
  439. $this->defaultProvider
  440. ->expects($this->once())
  441. ->method('getShareById')
  442. ->with(42)
  443. ->willReturn($share);
  444. $this->assertEquals($share, $this->manager->getShareById('default:42'));
  445. }
  446. /**
  447. * @expectedException \OCP\Share\Exceptions\ShareNotFound
  448. */
  449. public function testGetExpiredShareById() {
  450. $manager = $this->createManagerMock()
  451. ->setMethods(['deleteShare'])
  452. ->getMock();
  453. $date = new \DateTime();
  454. $date->setTime(0,0,0);
  455. $share = $this->manager->newShare();
  456. $share->setExpirationDate($date)
  457. ->setShareType(\OCP\Share::SHARE_TYPE_LINK);
  458. $this->defaultProvider->expects($this->once())
  459. ->method('getShareById')
  460. ->with('42')
  461. ->willReturn($share);
  462. $manager->expects($this->once())
  463. ->method('deleteShare')
  464. ->with($share);
  465. $manager->getShareById('default:42');
  466. }
  467. /**
  468. * @expectedException InvalidArgumentException
  469. * @expectedExceptionMessage Passwords are enforced for link shares
  470. */
  471. public function testVerifyPasswordNullButEnforced() {
  472. $this->config->method('getAppValue')->will($this->returnValueMap([
  473. ['core', 'shareapi_enforce_links_password', 'no', 'yes'],
  474. ]));
  475. $this->invokePrivate($this->manager, 'verifyPassword', [null]);
  476. }
  477. public function testVerifyPasswordNull() {
  478. $this->config->method('getAppValue')->will($this->returnValueMap([
  479. ['core', 'shareapi_enforce_links_password', 'no', 'no'],
  480. ]));
  481. $result = $this->invokePrivate($this->manager, 'verifyPassword', [null]);
  482. $this->assertNull($result);
  483. }
  484. public function testVerifyPasswordHook() {
  485. $this->config->method('getAppValue')->will($this->returnValueMap([
  486. ['core', 'shareapi_enforce_links_password', 'no', 'no'],
  487. ]));
  488. $this->eventDispatcher->expects($this->once())->method('dispatch')
  489. ->willReturnCallback(function($eventName, GenericEvent $event) {
  490. $this->assertSame('OCP\PasswordPolicy::validate', $eventName);
  491. $this->assertSame('password', $event->getSubject());
  492. }
  493. );
  494. $result = $this->invokePrivate($this->manager, 'verifyPassword', ['password']);
  495. $this->assertNull($result);
  496. }
  497. /**
  498. * @expectedException Exception
  499. * @expectedExceptionMessage password not accepted
  500. */
  501. public function testVerifyPasswordHookFails() {
  502. $this->config->method('getAppValue')->will($this->returnValueMap([
  503. ['core', 'shareapi_enforce_links_password', 'no', 'no'],
  504. ]));
  505. $this->eventDispatcher->expects($this->once())->method('dispatch')
  506. ->willReturnCallback(function($eventName, GenericEvent $event) {
  507. $this->assertSame('OCP\PasswordPolicy::validate', $eventName);
  508. $this->assertSame('password', $event->getSubject());
  509. throw new HintException('message', 'password not accepted');
  510. }
  511. );
  512. $this->invokePrivate($this->manager, 'verifyPassword', ['password']);
  513. }
  514. public function createShare($id, $type, $path, $sharedWith, $sharedBy, $shareOwner,
  515. $permissions, $expireDate = null, $password = null) {
  516. $share = $this->createMock(IShare::class);
  517. $share->method('getShareType')->willReturn($type);
  518. $share->method('getSharedWith')->willReturn($sharedWith);
  519. $share->method('getSharedBy')->willReturn($sharedBy);
  520. $share->method('getShareOwner')->willReturn($shareOwner);
  521. $share->method('getNode')->willReturn($path);
  522. $share->method('getPermissions')->willReturn($permissions);
  523. $share->method('getExpirationDate')->willReturn($expireDate);
  524. $share->method('getPassword')->willReturn($password);
  525. return $share;
  526. }
  527. public function dataGeneralChecks() {
  528. $user0 = 'user0';
  529. $user2 = 'user1';
  530. $group0 = 'group0';
  531. $file = $this->createMock(File::class);
  532. $node = $this->createMock(Node::class);
  533. $data = [
  534. [$this->createShare(null, \OCP\Share::SHARE_TYPE_USER, $file, null, $user0, $user0, 31, null, null), 'SharedWith is not a valid user', true],
  535. [$this->createShare(null, \OCP\Share::SHARE_TYPE_USER, $file, $group0, $user0, $user0, 31, null, null), 'SharedWith is not a valid user', true],
  536. [$this->createShare(null, \OCP\Share::SHARE_TYPE_USER, $file, 'foo@bar.com', $user0, $user0, 31, null, null), 'SharedWith is not a valid user', true],
  537. [$this->createShare(null, \OCP\Share::SHARE_TYPE_GROUP, $file, null, $user0, $user0, 31, null, null), 'SharedWith is not a valid group', true],
  538. [$this->createShare(null, \OCP\Share::SHARE_TYPE_GROUP, $file, $user2, $user0, $user0, 31, null, null), 'SharedWith is not a valid group', true],
  539. [$this->createShare(null, \OCP\Share::SHARE_TYPE_GROUP, $file, 'foo@bar.com', $user0, $user0, 31, null, null), 'SharedWith is not a valid group', true],
  540. [$this->createShare(null, \OCP\Share::SHARE_TYPE_LINK, $file, $user2, $user0, $user0, 31, null, null), 'SharedWith should be empty', true],
  541. [$this->createShare(null, \OCP\Share::SHARE_TYPE_LINK, $file, $group0, $user0, $user0, 31, null, null), 'SharedWith should be empty', true],
  542. [$this->createShare(null, \OCP\Share::SHARE_TYPE_LINK, $file, 'foo@bar.com', $user0, $user0, 31, null, null), 'SharedWith should be empty', true],
  543. [$this->createShare(null, -1, $file, null, $user0, $user0, 31, null, null), 'unkown share type', true],
  544. [$this->createShare(null, \OCP\Share::SHARE_TYPE_USER, $file, $user2, null, $user0, 31, null, null), 'SharedBy should be set', true],
  545. [$this->createShare(null, \OCP\Share::SHARE_TYPE_GROUP, $file, $group0, null, $user0, 31, null, null), 'SharedBy should be set', true],
  546. [$this->createShare(null, \OCP\Share::SHARE_TYPE_LINK, $file, null, null, $user0, 31, null, null), 'SharedBy should be set', true],
  547. [$this->createShare(null, \OCP\Share::SHARE_TYPE_USER, $file, $user0, $user0, $user0, 31, null, null), 'Can\'t share with yourself', true],
  548. [$this->createShare(null, \OCP\Share::SHARE_TYPE_USER, null, $user2, $user0, $user0, 31, null, null), 'Path should be set', true],
  549. [$this->createShare(null, \OCP\Share::SHARE_TYPE_GROUP, null, $group0, $user0, $user0, 31, null, null), 'Path should be set', true],
  550. [$this->createShare(null, \OCP\Share::SHARE_TYPE_LINK, null, null, $user0, $user0, 31, null, null), 'Path should be set', true],
  551. [$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],
  552. [$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],
  553. [$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],
  554. ];
  555. $nonShareAble = $this->createMock(Folder::class);
  556. $nonShareAble->method('isShareable')->willReturn(false);
  557. $nonShareAble->method('getPath')->willReturn('path');
  558. $data[] = [$this->createShare(null, \OCP\Share::SHARE_TYPE_USER, $nonShareAble, $user2, $user0, $user0, 31, null, null), 'You are not allowed to share path', true];
  559. $data[] = [$this->createShare(null, \OCP\Share::SHARE_TYPE_GROUP, $nonShareAble, $group0, $user0, $user0, 31, null, null), 'You are not allowed to share path', true];
  560. $data[] = [$this->createShare(null, \OCP\Share::SHARE_TYPE_LINK, $nonShareAble, null, $user0, $user0, 31, null, null), 'You are not allowed to share path', true];
  561. $limitedPermssions = $this->createMock(File::class);
  562. $limitedPermssions->method('isShareable')->willReturn(true);
  563. $limitedPermssions->method('getPermissions')->willReturn(\OCP\Constants::PERMISSION_READ);
  564. $limitedPermssions->method('getPath')->willReturn('path');
  565. $data[] = [$this->createShare(null, \OCP\Share::SHARE_TYPE_USER, $limitedPermssions, $user2, $user0, $user0, null, null, null), 'A share requires permissions', true];
  566. $data[] = [$this->createShare(null, \OCP\Share::SHARE_TYPE_GROUP, $limitedPermssions, $group0, $user0, $user0, null, null, null), 'A share requires permissions', true];
  567. $data[] = [$this->createShare(null, \OCP\Share::SHARE_TYPE_LINK, $limitedPermssions, null, $user0, $user0, null, null, null), 'A share requires permissions', true];
  568. $mount = $this->createMock(MoveableMount::class);
  569. $limitedPermssions->method('getMountPoint')->willReturn($mount);
  570. $data[] = [$this->createShare(null, \OCP\Share::SHARE_TYPE_USER, $limitedPermssions, $user2, $user0, $user0, 31, null, null), 'Cannot increase permissions of path', true];
  571. $data[] = [$this->createShare(null, \OCP\Share::SHARE_TYPE_GROUP, $limitedPermssions, $group0, $user0, $user0, 17, null, null), 'Cannot increase permissions of path', true];
  572. $data[] = [$this->createShare(null, \OCP\Share::SHARE_TYPE_LINK, $limitedPermssions, null, $user0, $user0, 3, null, null), 'Cannot increase permissions of path', true];
  573. $nonMoveableMountPermssions = $this->createMock(Folder::class);
  574. $nonMoveableMountPermssions->method('isShareable')->willReturn(true);
  575. $nonMoveableMountPermssions->method('getPermissions')->willReturn(\OCP\Constants::PERMISSION_READ);
  576. $nonMoveableMountPermssions->method('getPath')->willReturn('path');
  577. $data[] = [$this->createShare(null, \OCP\Share::SHARE_TYPE_USER, $nonMoveableMountPermssions, $user2, $user0, $user0, 11, null, null), 'Cannot increase permissions of path', false];
  578. $data[] = [$this->createShare(null, \OCP\Share::SHARE_TYPE_GROUP, $nonMoveableMountPermssions, $group0, $user0, $user0, 11, null, null), 'Cannot increase permissions of path', false];
  579. $rootFolder = $this->createMock(Folder::class);
  580. $rootFolder->method('isShareable')->willReturn(true);
  581. $rootFolder->method('getPermissions')->willReturn(\OCP\Constants::PERMISSION_ALL);
  582. $rootFolder->method('getPath')->willReturn('myrootfolder');
  583. $data[] = [$this->createShare(null, \OCP\Share::SHARE_TYPE_USER, $rootFolder, $user2, $user0, $user0, 30, null, null), 'You can\'t share your root folder', true];
  584. $data[] = [$this->createShare(null, \OCP\Share::SHARE_TYPE_GROUP, $rootFolder, $group0, $user0, $user0, 2, null, null), 'You can\'t share your root folder', true];
  585. $data[] = [$this->createShare(null, \OCP\Share::SHARE_TYPE_LINK, $rootFolder, null, $user0, $user0, 16, null, null), 'You can\'t share your root folder', true];
  586. $allPermssions = $this->createMock(Folder::class);
  587. $allPermssions->method('isShareable')->willReturn(true);
  588. $allPermssions->method('getPermissions')->willReturn(\OCP\Constants::PERMISSION_ALL);
  589. $data[] = [$this->createShare(null, \OCP\Share::SHARE_TYPE_USER, $allPermssions, $user2, $user0, $user0, 30, null, null), 'Shares need at least read permissions', true];
  590. $data[] = [$this->createShare(null, \OCP\Share::SHARE_TYPE_GROUP, $allPermssions, $group0, $user0, $user0, 2, null, null), 'Shares need at least read permissions', true];
  591. $data[] = [$this->createShare(null, \OCP\Share::SHARE_TYPE_USER, $allPermssions, $user2, $user0, $user0, 31, null, null), null, false];
  592. $data[] = [$this->createShare(null, \OCP\Share::SHARE_TYPE_GROUP, $allPermssions, $group0, $user0, $user0, 3, null, null), null, false];
  593. $data[] = [$this->createShare(null, \OCP\Share::SHARE_TYPE_LINK, $allPermssions, null, $user0, $user0, 17, null, null), null, false];
  594. return $data;
  595. }
  596. /**
  597. * @dataProvider dataGeneralChecks
  598. *
  599. * @param $share
  600. * @param $exceptionMessage
  601. */
  602. public function testGeneralChecks($share, $exceptionMessage, $exception) {
  603. $thrown = null;
  604. $this->userManager->method('userExists')->will($this->returnValueMap([
  605. ['user0', true],
  606. ['user1', true],
  607. ]));
  608. $this->groupManager->method('groupExists')->will($this->returnValueMap([
  609. ['group0', true],
  610. ]));
  611. $userFolder = $this->createMock(Folder::class);
  612. $userFolder->method('getPath')->willReturn('myrootfolder');
  613. $this->rootFolder->method('getUserFolder')->willReturn($userFolder);
  614. try {
  615. $this->invokePrivate($this->manager, 'generalCreateChecks', [$share]);
  616. $thrown = false;
  617. } catch (\OCP\Share\Exceptions\GenericShareException $e) {
  618. $this->assertEquals($exceptionMessage, $e->getHint());
  619. $thrown = true;
  620. } catch(\InvalidArgumentException $e) {
  621. $this->assertEquals($exceptionMessage, $e->getMessage());
  622. $thrown = true;
  623. }
  624. $this->assertSame($exception, $thrown);
  625. }
  626. /**
  627. * @expectedException \InvalidArgumentException
  628. * @expectedExceptionMessage You can't share your root folder
  629. */
  630. public function testGeneralCheckShareRoot() {
  631. $thrown = null;
  632. $this->userManager->method('userExists')->will($this->returnValueMap([
  633. ['user0', true],
  634. ['user1', true],
  635. ]));
  636. $userFolder = $this->createMock(Folder::class);
  637. $userFolder->method('isSubNode')->with($userFolder)->willReturn(false);
  638. $this->rootFolder->method('getUserFolder')->willReturn($userFolder);
  639. $share = $this->manager->newShare();
  640. $share->setShareType(\OCP\Share::SHARE_TYPE_USER)
  641. ->setSharedWith('user0')
  642. ->setSharedBy('user1')
  643. ->setNode($userFolder);
  644. $this->invokePrivate($this->manager, 'generalCreateChecks', [$share]);
  645. }
  646. /**
  647. * @expectedException \OCP\Share\Exceptions\GenericShareException
  648. * @expectedExceptionMessage Expiration date is in the past
  649. */
  650. public function testvalidateExpirationDateInPast() {
  651. // Expire date in the past
  652. $past = new \DateTime();
  653. $past->sub(new \DateInterval('P1D'));
  654. $share = $this->manager->newShare();
  655. $share->setExpirationDate($past);
  656. $this->invokePrivate($this->manager, 'validateExpirationDate', [$share]);
  657. }
  658. /**
  659. * @expectedException InvalidArgumentException
  660. * @expectedExceptionMessage Expiration date is enforced
  661. */
  662. public function testvalidateExpirationDateEnforceButNotSet() {
  663. $share = $this->manager->newShare();
  664. $share->setProviderId('foo')->setId('bar');
  665. $this->config->method('getAppValue')
  666. ->will($this->returnValueMap([
  667. ['core', 'shareapi_default_expire_date', 'no', 'yes'],
  668. ['core', 'shareapi_enforce_expire_date', 'no', 'yes'],
  669. ]));
  670. $this->invokePrivate($this->manager, 'validateExpirationDate', [$share]);
  671. }
  672. public function testvalidateExpirationDateEnforceButNotEnabledAndNotSet() {
  673. $share = $this->manager->newShare();
  674. $share->setProviderId('foo')->setId('bar');
  675. $this->config->method('getAppValue')
  676. ->will($this->returnValueMap([
  677. ['core', 'shareapi_enforce_expire_date', 'no', 'yes'],
  678. ]));
  679. $this->invokePrivate($this->manager, 'validateExpirationDate', [$share]);
  680. $this->assertNull($share->getExpirationDate());
  681. }
  682. public function testvalidateExpirationDateEnforceButNotSetNewShare() {
  683. $share = $this->manager->newShare();
  684. $this->config->method('getAppValue')
  685. ->will($this->returnValueMap([
  686. ['core', 'shareapi_enforce_expire_date', 'no', 'yes'],
  687. ['core', 'shareapi_expire_after_n_days', '7', '3'],
  688. ['core', 'shareapi_default_expire_date', 'no', 'yes'],
  689. ]));
  690. $expected = new \DateTime();
  691. $expected->setTime(0,0,0);
  692. $expected->add(new \DateInterval('P3D'));
  693. $this->invokePrivate($this->manager, 'validateExpirationDate', [$share]);
  694. $this->assertNotNull($share->getExpirationDate());
  695. $this->assertEquals($expected, $share->getExpirationDate());
  696. }
  697. public function testvalidateExpirationDateEnforceToFarIntoFuture() {
  698. // Expire date in the past
  699. $future = new \DateTime();
  700. $future->add(new \DateInterval('P7D'));
  701. $share = $this->manager->newShare();
  702. $share->setExpirationDate($future);
  703. $this->config->method('getAppValue')
  704. ->will($this->returnValueMap([
  705. ['core', 'shareapi_enforce_expire_date', 'no', 'yes'],
  706. ['core', 'shareapi_expire_after_n_days', '7', '3'],
  707. ]));
  708. try {
  709. $this->invokePrivate($this->manager, 'validateExpirationDate', [$share]);
  710. } catch (\OCP\Share\Exceptions\GenericShareException $e) {
  711. $this->assertEquals('Cannot set expiration date more than 3 days in the future', $e->getMessage());
  712. $this->assertEquals('Cannot set expiration date more than 3 days in the future', $e->getHint());
  713. $this->assertEquals(404, $e->getCode());
  714. }
  715. }
  716. public function testvalidateExpirationDateEnforceValid() {
  717. // Expire date in the past
  718. $future = new \DateTime();
  719. $future->add(new \DateInterval('P2D'));
  720. $future->setTime(0,0,0);
  721. $expected = clone $future;
  722. $future->setTime(1,2,3);
  723. $share = $this->manager->newShare();
  724. $share->setExpirationDate($future);
  725. $this->config->method('getAppValue')
  726. ->will($this->returnValueMap([
  727. ['core', 'shareapi_enforce_expire_date', 'no', 'yes'],
  728. ['core', 'shareapi_expire_after_n_days', '7', '3'],
  729. ]));
  730. $hookListner = $this->getMockBuilder('Dummy')->setMethods(['listener'])->getMock();
  731. \OCP\Util::connectHook('\OC\Share', 'verifyExpirationDate', $hookListner, 'listener');
  732. $hookListner->expects($this->once())->method('listener')->with($this->callback(function ($data) use ($future) {
  733. return $data['expirationDate'] == $future;
  734. }));
  735. $this->invokePrivate($this->manager, 'validateExpirationDate', [$share]);
  736. $this->assertEquals($expected, $share->getExpirationDate());
  737. }
  738. public function testvalidateExpirationDateNoDateNoDefaultNull() {
  739. $date = new \DateTime();
  740. $date->add(new \DateInterval('P5D'));
  741. $expected = clone $date;
  742. $expected->setTime(0,0,0);
  743. $share = $this->manager->newShare();
  744. $share->setExpirationDate($date);
  745. $hookListner = $this->getMockBuilder('Dummy')->setMethods(['listener'])->getMock();
  746. \OCP\Util::connectHook('\OC\Share', 'verifyExpirationDate', $hookListner, 'listener');
  747. $hookListner->expects($this->once())->method('listener')->with($this->callback(function ($data) use ($expected) {
  748. return $data['expirationDate'] == $expected && $data['passwordSet'] === false;
  749. }));
  750. $this->invokePrivate($this->manager, 'validateExpirationDate', [$share]);
  751. $this->assertEquals($expected, $share->getExpirationDate());
  752. }
  753. public function testvalidateExpirationDateNoDateNoDefault() {
  754. $hookListner = $this->getMockBuilder('Dummy')->setMethods(['listener'])->getMock();
  755. \OCP\Util::connectHook('\OC\Share', 'verifyExpirationDate', $hookListner, 'listener');
  756. $hookListner->expects($this->once())->method('listener')->with($this->callback(function ($data) {
  757. return $data['expirationDate'] === null && $data['passwordSet'] === true;
  758. }));
  759. $share = $this->manager->newShare();
  760. $share->setPassword('password');
  761. $this->invokePrivate($this->manager, 'validateExpirationDate', [$share]);
  762. $this->assertNull($share->getExpirationDate());
  763. }
  764. public function testvalidateExpirationDateNoDateDefault() {
  765. $future = new \DateTime();
  766. $future->add(new \DateInterval('P3D'));
  767. $future->setTime(0,0,0);
  768. $expected = clone $future;
  769. $share = $this->manager->newShare();
  770. $share->setExpirationDate($future);
  771. $this->config->method('getAppValue')
  772. ->will($this->returnValueMap([
  773. ['core', 'shareapi_default_expire_date', 'no', 'yes'],
  774. ['core', 'shareapi_expire_after_n_days', '7', '3'],
  775. ]));
  776. $hookListner = $this->getMockBuilder('Dummy')->setMethods(['listener'])->getMock();
  777. \OCP\Util::connectHook('\OC\Share', 'verifyExpirationDate', $hookListner, 'listener');
  778. $hookListner->expects($this->once())->method('listener')->with($this->callback(function ($data) use ($expected) {
  779. return $data['expirationDate'] == $expected;
  780. }));
  781. $this->invokePrivate($this->manager, 'validateExpirationDate', [$share]);
  782. $this->assertEquals($expected, $share->getExpirationDate());
  783. }
  784. public function testValidateExpirationDateHookModification() {
  785. $nextWeek = new \DateTime();
  786. $nextWeek->add(new \DateInterval('P7D'));
  787. $nextWeek->setTime(0,0,0);
  788. $save = clone $nextWeek;
  789. $hookListner = $this->getMockBuilder('Dummy')->setMethods(['listener'])->getMock();
  790. \OCP\Util::connectHook('\OC\Share', 'verifyExpirationDate', $hookListner, 'listener');
  791. $hookListner->expects($this->once())->method('listener')->will($this->returnCallback(function ($data) {
  792. $data['expirationDate']->sub(new \DateInterval('P2D'));
  793. }));
  794. $share = $this->manager->newShare();
  795. $share->setExpirationDate($nextWeek);
  796. $this->invokePrivate($this->manager, 'validateExpirationDate', [$share]);
  797. $save->sub(new \DateInterval('P2D'));
  798. $this->assertEquals($save, $share->getExpirationDate());
  799. }
  800. /**
  801. * @expectedException \Exception
  802. * @expectedExceptionMessage Invalid date!
  803. */
  804. public function testValidateExpirationDateHookException() {
  805. $nextWeek = new \DateTime();
  806. $nextWeek->add(new \DateInterval('P7D'));
  807. $nextWeek->setTime(0,0,0);
  808. $share = $this->manager->newShare();
  809. $share->setExpirationDate($nextWeek);
  810. $hookListner = $this->getMockBuilder('Dummy')->setMethods(['listener'])->getMock();
  811. \OCP\Util::connectHook('\OC\Share', 'verifyExpirationDate', $hookListner, 'listener');
  812. $hookListner->expects($this->once())->method('listener')->will($this->returnCallback(function ($data) {
  813. $data['accepted'] = false;
  814. $data['message'] = 'Invalid date!';
  815. }));
  816. $this->invokePrivate($this->manager, 'validateExpirationDate', [$share]);
  817. }
  818. public function testValidateExpirationDateExistingShareNoDefault() {
  819. $share = $this->manager->newShare();
  820. $share->setId('42')->setProviderId('foo');
  821. $this->config->method('getAppValue')
  822. ->will($this->returnValueMap([
  823. ['core', 'shareapi_default_expire_date', 'no', 'yes'],
  824. ['core', 'shareapi_expire_after_n_days', '7', '6'],
  825. ]));
  826. $this->invokePrivate($this->manager, 'validateExpirationDate', [$share]);
  827. $this->assertEquals(null, $share->getExpirationDate());
  828. }
  829. /**
  830. * @expectedException Exception
  831. * @expectedExceptionMessage Only sharing with group members is allowed
  832. */
  833. public function testUserCreateChecksShareWithGroupMembersOnlyDifferentGroups() {
  834. $share = $this->manager->newShare();
  835. $sharedBy = $this->createMock(IUser::class);
  836. $sharedWith = $this->createMock(IUser::class);
  837. $share->setSharedBy('sharedBy')->setSharedWith('sharedWith');
  838. $this->groupManager
  839. ->method('getUserGroupIds')
  840. ->will(
  841. $this->returnValueMap([
  842. [$sharedBy, ['group1']],
  843. [$sharedWith, ['group2']],
  844. ])
  845. );
  846. $this->userManager->method('get')->will($this->returnValueMap([
  847. ['sharedBy', $sharedBy],
  848. ['sharedWith', $sharedWith],
  849. ]));
  850. $this->config
  851. ->method('getAppValue')
  852. ->will($this->returnValueMap([
  853. ['core', 'shareapi_only_share_with_group_members', 'no', 'yes'],
  854. ]));
  855. $this->invokePrivate($this->manager, 'userCreateChecks', [$share]);
  856. }
  857. public function testUserCreateChecksShareWithGroupMembersOnlySharedGroup() {
  858. $share = $this->manager->newShare();
  859. $sharedBy = $this->createMock(IUser::class);
  860. $sharedWith = $this->createMock(IUser::class);
  861. $share->setSharedBy('sharedBy')->setSharedWith('sharedWith');
  862. $path = $this->createMock(Node::class);
  863. $share->setNode($path);
  864. $this->groupManager
  865. ->method('getUserGroupIds')
  866. ->will(
  867. $this->returnValueMap([
  868. [$sharedBy, ['group1', 'group3']],
  869. [$sharedWith, ['group2', 'group3']],
  870. ])
  871. );
  872. $this->userManager->method('get')->will($this->returnValueMap([
  873. ['sharedBy', $sharedBy],
  874. ['sharedWith', $sharedWith],
  875. ]));
  876. $this->config
  877. ->method('getAppValue')
  878. ->will($this->returnValueMap([
  879. ['core', 'shareapi_only_share_with_group_members', 'no', 'yes'],
  880. ]));
  881. $this->defaultProvider
  882. ->method('getSharesByPath')
  883. ->with($path)
  884. ->willReturn([]);
  885. $this->invokePrivate($this->manager, 'userCreateChecks', [$share]);
  886. }
  887. /**
  888. * @expectedException Exception
  889. * @expectedExceptionMessage Path already shared with this user
  890. */
  891. public function testUserCreateChecksIdenticalShareExists() {
  892. $share = $this->manager->newShare();
  893. $share2 = $this->manager->newShare();
  894. $sharedWith = $this->createMock(IUser::class);
  895. $path = $this->createMock(Node::class);
  896. $share->setSharedWith('sharedWith')->setNode($path)
  897. ->setProviderId('foo')->setId('bar');
  898. $share2->setSharedWith('sharedWith')->setNode($path)
  899. ->setProviderId('foo')->setId('baz');
  900. $this->defaultProvider
  901. ->method('getSharesByPath')
  902. ->with($path)
  903. ->willReturn([$share2]);
  904. $this->invokePrivate($this->manager, 'userCreateChecks', [$share]);
  905. }
  906. /**
  907. * @expectedException Exception
  908. * @expectedExceptionMessage Path already shared with this user
  909. */
  910. public function testUserCreateChecksIdenticalPathSharedViaGroup() {
  911. $share = $this->manager->newShare();
  912. $sharedWith = $this->createMock(IUser::class);
  913. $sharedWith->method('getUID')->willReturn('sharedWith');
  914. $this->userManager->method('get')->with('sharedWith')->willReturn($sharedWith);
  915. $path = $this->createMock(Node::class);
  916. $share->setSharedWith('sharedWith')
  917. ->setNode($path)
  918. ->setShareOwner('shareOwner')
  919. ->setProviderId('foo')
  920. ->setId('bar');
  921. $share2 = $this->manager->newShare();
  922. $share2->setShareType(\OCP\Share::SHARE_TYPE_GROUP)
  923. ->setShareOwner('shareOwner2')
  924. ->setProviderId('foo')
  925. ->setId('baz')
  926. ->setSharedWith('group');
  927. $group = $this->createMock(IGroup::class);
  928. $group->method('inGroup')
  929. ->with($sharedWith)
  930. ->willReturn(true);
  931. $this->groupManager->method('get')->with('group')->willReturn($group);
  932. $this->defaultProvider
  933. ->method('getSharesByPath')
  934. ->with($path)
  935. ->willReturn([$share2]);
  936. $this->invokePrivate($this->manager, 'userCreateChecks', [$share]);
  937. }
  938. public function testUserCreateChecksIdenticalPathNotSharedWithUser() {
  939. $share = $this->manager->newShare();
  940. $sharedWith = $this->createMock(IUser::class);
  941. $path = $this->createMock(Node::class);
  942. $share->setSharedWith('sharedWith')
  943. ->setNode($path)
  944. ->setShareOwner('shareOwner')
  945. ->setProviderId('foo')
  946. ->setId('bar');
  947. $this->userManager->method('get')->with('sharedWith')->willReturn($sharedWith);
  948. $share2 = $this->manager->newShare();
  949. $share2->setShareType(\OCP\Share::SHARE_TYPE_GROUP)
  950. ->setShareOwner('shareOwner2')
  951. ->setProviderId('foo')
  952. ->setId('baz');
  953. $group = $this->createMock(IGroup::class);
  954. $group->method('inGroup')
  955. ->with($sharedWith)
  956. ->willReturn(false);
  957. $this->groupManager->method('get')->with('group')->willReturn($group);
  958. $share2->setSharedWith('group');
  959. $this->defaultProvider
  960. ->method('getSharesByPath')
  961. ->with($path)
  962. ->willReturn([$share2]);
  963. $this->invokePrivate($this->manager, 'userCreateChecks', [$share]);
  964. }
  965. /**
  966. * @expectedException Exception
  967. * @expectedExceptionMessage Group sharing is now allowed
  968. */
  969. public function testGroupCreateChecksShareWithGroupMembersGroupSharingNotAllowed() {
  970. $share = $this->manager->newShare();
  971. $this->config
  972. ->method('getAppValue')
  973. ->will($this->returnValueMap([
  974. ['core', 'shareapi_allow_group_sharing', 'yes', 'no'],
  975. ]));
  976. $this->invokePrivate($this->manager, 'groupCreateChecks', [$share]);
  977. }
  978. /**
  979. * @expectedException Exception
  980. * @expectedExceptionMessage Only sharing within your own groups is allowed
  981. */
  982. public function testGroupCreateChecksShareWithGroupMembersOnlyNotInGroup() {
  983. $share = $this->manager->newShare();
  984. $user = $this->createMock(IUser::class);
  985. $group = $this->createMock(IGroup::class);
  986. $share->setSharedBy('user')->setSharedWith('group');
  987. $group->method('inGroup')->with($user)->willReturn(false);
  988. $this->groupManager->method('get')->with('group')->willReturn($group);
  989. $this->userManager->method('get')->with('user')->willReturn($user);
  990. $this->config
  991. ->method('getAppValue')
  992. ->will($this->returnValueMap([
  993. ['core', 'shareapi_only_share_with_group_members', 'no', 'yes'],
  994. ['core', 'shareapi_allow_group_sharing', 'yes', 'yes'],
  995. ]));
  996. $this->invokePrivate($this->manager, 'groupCreateChecks', [$share]);
  997. }
  998. public function testGroupCreateChecksShareWithGroupMembersOnlyInGroup() {
  999. $share = $this->manager->newShare();
  1000. $user = $this->createMock(IUser::class);
  1001. $group = $this->createMock(IGroup::class);
  1002. $share->setSharedBy('user')->setSharedWith('group');
  1003. $this->userManager->method('get')->with('user')->willReturn($user);
  1004. $this->groupManager->method('get')->with('group')->willReturn($group);
  1005. $group->method('inGroup')->with($user)->willReturn(true);
  1006. $path = $this->createMock(Node::class);
  1007. $share->setNode($path);
  1008. $this->defaultProvider->method('getSharesByPath')
  1009. ->with($path)
  1010. ->willReturn([]);
  1011. $this->config
  1012. ->method('getAppValue')
  1013. ->will($this->returnValueMap([
  1014. ['core', 'shareapi_only_share_with_group_members', 'no', 'yes'],
  1015. ['core', 'shareapi_allow_group_sharing', 'yes', 'yes'],
  1016. ]));
  1017. $this->invokePrivate($this->manager, 'groupCreateChecks', [$share]);
  1018. }
  1019. /**
  1020. * @expectedException Exception
  1021. * @expectedExceptionMessage Path already shared with this group
  1022. */
  1023. public function testGroupCreateChecksPathAlreadySharedWithSameGroup() {
  1024. $share = $this->manager->newShare();
  1025. $path = $this->createMock(Node::class);
  1026. $share->setSharedWith('sharedWith')
  1027. ->setNode($path)
  1028. ->setProviderId('foo')
  1029. ->setId('bar');
  1030. $share2 = $this->manager->newShare();
  1031. $share2->setSharedWith('sharedWith')
  1032. ->setProviderId('foo')
  1033. ->setId('baz');
  1034. $this->defaultProvider->method('getSharesByPath')
  1035. ->with($path)
  1036. ->willReturn([$share2]);
  1037. $this->config
  1038. ->method('getAppValue')
  1039. ->will($this->returnValueMap([
  1040. ['core', 'shareapi_allow_group_sharing', 'yes', 'yes'],
  1041. ]));
  1042. $this->invokePrivate($this->manager, 'groupCreateChecks', [$share]);
  1043. }
  1044. public function testGroupCreateChecksPathAlreadySharedWithDifferentGroup() {
  1045. $share = $this->manager->newShare();
  1046. $share->setSharedWith('sharedWith');
  1047. $path = $this->createMock(Node::class);
  1048. $share->setNode($path);
  1049. $share2 = $this->manager->newShare();
  1050. $share2->setSharedWith('sharedWith2');
  1051. $this->defaultProvider->method('getSharesByPath')
  1052. ->with($path)
  1053. ->willReturn([$share2]);
  1054. $this->config
  1055. ->method('getAppValue')
  1056. ->will($this->returnValueMap([
  1057. ['core', 'shareapi_allow_group_sharing', 'yes', 'yes'],
  1058. ]));
  1059. $this->invokePrivate($this->manager, 'groupCreateChecks', [$share]);
  1060. }
  1061. /**
  1062. * @expectedException Exception
  1063. * @expectedExceptionMessage Link sharing not allowed
  1064. */
  1065. public function testLinkCreateChecksNoLinkSharesAllowed() {
  1066. $share = $this->manager->newShare();
  1067. $this->config
  1068. ->method('getAppValue')
  1069. ->will($this->returnValueMap([
  1070. ['core', 'shareapi_allow_links', 'yes', 'no'],
  1071. ]));
  1072. $this->invokePrivate($this->manager, 'linkCreateChecks', [$share]);
  1073. }
  1074. /**
  1075. * @expectedException Exception
  1076. * @expectedExceptionMessage Link shares can't have reshare permissions
  1077. */
  1078. public function testLinkCreateChecksSharePermissions() {
  1079. $share = $this->manager->newShare();
  1080. $share->setPermissions(\OCP\Constants::PERMISSION_SHARE);
  1081. $this->config
  1082. ->method('getAppValue')
  1083. ->will($this->returnValueMap([
  1084. ['core', 'shareapi_allow_links', 'yes', 'yes'],
  1085. ]));
  1086. $this->invokePrivate($this->manager, 'linkCreateChecks', [$share]);
  1087. }
  1088. /**
  1089. * @expectedException Exception
  1090. * @expectedExceptionMessage Public upload not allowed
  1091. */
  1092. public function testLinkCreateChecksNoPublicUpload() {
  1093. $share = $this->manager->newShare();
  1094. $share->setPermissions(\OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE);
  1095. $this->config
  1096. ->method('getAppValue')
  1097. ->will($this->returnValueMap([
  1098. ['core', 'shareapi_allow_links', 'yes', 'yes'],
  1099. ['core', 'shareapi_allow_public_upload', 'yes', 'no']
  1100. ]));
  1101. $this->invokePrivate($this->manager, 'linkCreateChecks', [$share]);
  1102. }
  1103. public function testLinkCreateChecksPublicUpload() {
  1104. $share = $this->manager->newShare();
  1105. $share->setPermissions(\OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE);
  1106. $this->config
  1107. ->method('getAppValue')
  1108. ->will($this->returnValueMap([
  1109. ['core', 'shareapi_allow_links', 'yes', 'yes'],
  1110. ['core', 'shareapi_allow_public_upload', 'yes', 'yes']
  1111. ]));
  1112. $this->invokePrivate($this->manager, 'linkCreateChecks', [$share]);
  1113. }
  1114. public function testLinkCreateChecksReadOnly() {
  1115. $share = $this->manager->newShare();
  1116. $share->setPermissions(\OCP\Constants::PERMISSION_READ);
  1117. $this->config
  1118. ->method('getAppValue')
  1119. ->will($this->returnValueMap([
  1120. ['core', 'shareapi_allow_links', 'yes', 'yes'],
  1121. ['core', 'shareapi_allow_public_upload', 'yes', 'no']
  1122. ]));
  1123. $this->invokePrivate($this->manager, 'linkCreateChecks', [$share]);
  1124. }
  1125. /**
  1126. * @expectedException \InvalidArgumentException
  1127. * @expectedExceptionMessage Path contains files shared with you
  1128. */
  1129. public function testPathCreateChecksContainsSharedMount() {
  1130. $path = $this->createMock(Folder::class);
  1131. $path->method('getPath')->willReturn('path');
  1132. $mount = $this->createMock(IMountPoint::class);
  1133. $storage = $this->createMock(Storage::class);
  1134. $mount->method('getStorage')->willReturn($storage);
  1135. $storage->method('instanceOfStorage')->with('\OCA\Files_Sharing\ISharedStorage')->willReturn(true);
  1136. $this->mountManager->method('findIn')->with('path')->willReturn([$mount]);
  1137. $this->invokePrivate($this->manager, 'pathCreateChecks', [$path]);
  1138. }
  1139. public function testPathCreateChecksContainsNoSharedMount() {
  1140. $path = $this->createMock(Folder::class);
  1141. $path->method('getPath')->willReturn('path');
  1142. $mount = $this->createMock(IMountPoint::class);
  1143. $storage = $this->createMock(Storage::class);
  1144. $mount->method('getStorage')->willReturn($storage);
  1145. $storage->method('instanceOfStorage')->with('\OCA\Files_Sharing\ISharedStorage')->willReturn(false);
  1146. $this->mountManager->method('findIn')->with('path')->willReturn([$mount]);
  1147. $this->invokePrivate($this->manager, 'pathCreateChecks', [$path]);
  1148. }
  1149. public function testPathCreateChecksContainsNoFolder() {
  1150. $path = $this->createMock(File::class);
  1151. $this->invokePrivate($this->manager, 'pathCreateChecks', [$path]);
  1152. }
  1153. public function dataIsSharingDisabledForUser() {
  1154. $data = [];
  1155. // No exclude groups
  1156. $data[] = ['no', null, null, null, false];
  1157. // empty exclude list, user no groups
  1158. $data[] = ['yes', '', json_encode(['']), [], false];
  1159. // empty exclude list, user groups
  1160. $data[] = ['yes', '', json_encode(['']), ['group1', 'group2'], false];
  1161. // Convert old list to json
  1162. $data[] = ['yes', 'group1,group2', json_encode(['group1', 'group2']), [], false];
  1163. // Old list partly groups in common
  1164. $data[] = ['yes', 'group1,group2', json_encode(['group1', 'group2']), ['group1', 'group3'], false];
  1165. // Old list only groups in common
  1166. $data[] = ['yes', 'group1,group2', json_encode(['group1', 'group2']), ['group1'], true];
  1167. // New list partly in common
  1168. $data[] = ['yes', json_encode(['group1', 'group2']), null, ['group1', 'group3'], false];
  1169. // New list only groups in common
  1170. $data[] = ['yes', json_encode(['group1', 'group2']), null, ['group2'], true];
  1171. return $data;
  1172. }
  1173. /**
  1174. * @dataProvider dataIsSharingDisabledForUser
  1175. *
  1176. * @param string $excludeGroups
  1177. * @param string $groupList
  1178. * @param string $setList
  1179. * @param string[] $groupIds
  1180. * @param bool $expected
  1181. */
  1182. public function testIsSharingDisabledForUser($excludeGroups, $groupList, $setList, $groupIds, $expected) {
  1183. $user = $this->createMock(IUser::class);
  1184. $this->config->method('getAppValue')
  1185. ->will($this->returnValueMap([
  1186. ['core', 'shareapi_exclude_groups', 'no', $excludeGroups],
  1187. ['core', 'shareapi_exclude_groups_list', '', $groupList],
  1188. ]));
  1189. if ($setList !== null) {
  1190. $this->config->expects($this->once())
  1191. ->method('setAppValue')
  1192. ->with('core', 'shareapi_exclude_groups_list', $setList);
  1193. } else {
  1194. $this->config->expects($this->never())
  1195. ->method('setAppValue');
  1196. }
  1197. $this->groupManager->method('getUserGroupIds')
  1198. ->with($user)
  1199. ->willReturn($groupIds);
  1200. $this->userManager->method('get')->with('user')->willReturn($user);
  1201. $res = $this->manager->sharingDisabledForUser('user');
  1202. $this->assertEquals($expected, $res);
  1203. }
  1204. public function dataCanShare() {
  1205. $data = [];
  1206. /*
  1207. * [expected, sharing enabled, disabled for user]
  1208. */
  1209. $data[] = [false, 'no', false];
  1210. $data[] = [false, 'no', true];
  1211. $data[] = [true, 'yes', false];
  1212. $data[] = [false, 'yes', true];
  1213. return $data;
  1214. }
  1215. /**
  1216. * @dataProvider dataCanShare
  1217. *
  1218. * @param bool $expected
  1219. * @param string $sharingEnabled
  1220. * @param bool $disabledForUser
  1221. */
  1222. public function testCanShare($expected, $sharingEnabled, $disabledForUser) {
  1223. $this->config->method('getAppValue')
  1224. ->will($this->returnValueMap([
  1225. ['core', 'shareapi_enabled', 'yes', $sharingEnabled],
  1226. ]));
  1227. $manager = $this->createManagerMock()
  1228. ->setMethods(['sharingDisabledForUser'])
  1229. ->getMock();
  1230. $manager->method('sharingDisabledForUser')
  1231. ->with('user')
  1232. ->willReturn($disabledForUser);
  1233. $share = $this->manager->newShare();
  1234. $share->setSharedBy('user');
  1235. $exception = false;
  1236. try {
  1237. $res = $this->invokePrivate($manager, 'canShare', [$share]);
  1238. } catch (\Exception $e) {
  1239. $exception = true;
  1240. }
  1241. $this->assertEquals($expected, !$exception);
  1242. }
  1243. public function testCreateShareUser() {
  1244. $manager = $this->createManagerMock()
  1245. ->setMethods(['canShare', 'generalCreateChecks', 'userCreateChecks', 'pathCreateChecks'])
  1246. ->getMock();
  1247. $shareOwner = $this->createMock(IUser::class);
  1248. $shareOwner->method('getUID')->willReturn('shareOwner');
  1249. $storage = $this->createMock(Storage::class);
  1250. $path = $this->createMock(File::class);
  1251. $path->method('getOwner')->willReturn($shareOwner);
  1252. $path->method('getName')->willReturn('target');
  1253. $path->method('getStorage')->willReturn($storage);
  1254. $share = $this->createShare(
  1255. null,
  1256. \OCP\Share::SHARE_TYPE_USER,
  1257. $path,
  1258. 'sharedWith',
  1259. 'sharedBy',
  1260. null,
  1261. \OCP\Constants::PERMISSION_ALL);
  1262. $manager->expects($this->once())
  1263. ->method('canShare')
  1264. ->with($share)
  1265. ->willReturn(true);
  1266. $manager->expects($this->once())
  1267. ->method('generalCreateChecks')
  1268. ->with($share);;
  1269. $manager->expects($this->once())
  1270. ->method('userCreateChecks')
  1271. ->with($share);;
  1272. $manager->expects($this->once())
  1273. ->method('pathCreateChecks')
  1274. ->with($path);
  1275. $this->defaultProvider
  1276. ->expects($this->once())
  1277. ->method('create')
  1278. ->with($share)
  1279. ->will($this->returnArgument(0));
  1280. $share->expects($this->once())
  1281. ->method('setShareOwner')
  1282. ->with('shareOwner');
  1283. $share->expects($this->once())
  1284. ->method('setTarget')
  1285. ->with('/target');
  1286. $manager->createShare($share);
  1287. }
  1288. public function testCreateShareGroup() {
  1289. $manager = $this->createManagerMock()
  1290. ->setMethods(['canShare', 'generalCreateChecks', 'groupCreateChecks', 'pathCreateChecks'])
  1291. ->getMock();
  1292. $shareOwner = $this->createMock(IUser::class);
  1293. $shareOwner->method('getUID')->willReturn('shareOwner');
  1294. $storage = $this->createMock(Storage::class);
  1295. $path = $this->createMock(File::class);
  1296. $path->method('getOwner')->willReturn($shareOwner);
  1297. $path->method('getName')->willReturn('target');
  1298. $path->method('getStorage')->willReturn($storage);
  1299. $share = $this->createShare(
  1300. null,
  1301. \OCP\Share::SHARE_TYPE_GROUP,
  1302. $path,
  1303. 'sharedWith',
  1304. 'sharedBy',
  1305. null,
  1306. \OCP\Constants::PERMISSION_ALL);
  1307. $manager->expects($this->once())
  1308. ->method('canShare')
  1309. ->with($share)
  1310. ->willReturn(true);
  1311. $manager->expects($this->once())
  1312. ->method('generalCreateChecks')
  1313. ->with($share);;
  1314. $manager->expects($this->once())
  1315. ->method('groupCreateChecks')
  1316. ->with($share);;
  1317. $manager->expects($this->once())
  1318. ->method('pathCreateChecks')
  1319. ->with($path);
  1320. $this->defaultProvider
  1321. ->expects($this->once())
  1322. ->method('create')
  1323. ->with($share)
  1324. ->will($this->returnArgument(0));
  1325. $share->expects($this->once())
  1326. ->method('setShareOwner')
  1327. ->with('shareOwner');
  1328. $share->expects($this->once())
  1329. ->method('setTarget')
  1330. ->with('/target');
  1331. $manager->createShare($share);
  1332. }
  1333. public function testCreateShareLink() {
  1334. $manager = $this->createManagerMock()
  1335. ->setMethods([
  1336. 'canShare',
  1337. 'generalCreateChecks',
  1338. 'linkCreateChecks',
  1339. 'pathCreateChecks',
  1340. 'validateExpirationDate',
  1341. 'verifyPassword',
  1342. 'setLinkParent',
  1343. ])
  1344. ->getMock();
  1345. $shareOwner = $this->createMock(IUser::class);
  1346. $shareOwner->method('getUID')->willReturn('shareOwner');
  1347. $storage = $this->createMock(Storage::class);
  1348. $path = $this->createMock(File::class);
  1349. $path->method('getOwner')->willReturn($shareOwner);
  1350. $path->method('getName')->willReturn('target');
  1351. $path->method('getId')->willReturn(1);
  1352. $path->method('getStorage')->willReturn($storage);
  1353. $date = new \DateTime();
  1354. $share = $this->manager->newShare();
  1355. $share->setShareType(\OCP\Share::SHARE_TYPE_LINK)
  1356. ->setNode($path)
  1357. ->setSharedBy('sharedBy')
  1358. ->setPermissions(\OCP\Constants::PERMISSION_ALL)
  1359. ->setExpirationDate($date)
  1360. ->setPassword('password');
  1361. $manager->expects($this->once())
  1362. ->method('canShare')
  1363. ->with($share)
  1364. ->willReturn(true);
  1365. $manager->expects($this->once())
  1366. ->method('generalCreateChecks')
  1367. ->with($share);;
  1368. $manager->expects($this->once())
  1369. ->method('linkCreateChecks')
  1370. ->with($share);;
  1371. $manager->expects($this->once())
  1372. ->method('pathCreateChecks')
  1373. ->with($path);
  1374. $manager->expects($this->once())
  1375. ->method('validateExpirationDate')
  1376. ->with($share);
  1377. $manager->expects($this->once())
  1378. ->method('verifyPassword')
  1379. ->with('password');
  1380. $manager->expects($this->once())
  1381. ->method('setLinkParent')
  1382. ->with($share);
  1383. $this->hasher->expects($this->once())
  1384. ->method('hash')
  1385. ->with('password')
  1386. ->willReturn('hashed');
  1387. $this->secureRandom->method('getMediumStrengthGenerator')
  1388. ->will($this->returnSelf());
  1389. $this->secureRandom->method('generate')
  1390. ->willReturn('token');
  1391. $this->defaultProvider
  1392. ->expects($this->once())
  1393. ->method('create')
  1394. ->with($share)
  1395. ->will($this->returnCallback(function(Share $share) {
  1396. return $share->setId(42);
  1397. }));
  1398. $hookListner = $this->getMockBuilder('Dummy')->setMethods(['pre', 'post'])->getMock();
  1399. \OCP\Util::connectHook('OCP\Share', 'pre_shared', $hookListner, 'pre');
  1400. \OCP\Util::connectHook('OCP\Share', 'post_shared', $hookListner, 'post');
  1401. $hookListnerExpectsPre = [
  1402. 'itemType' => 'file',
  1403. 'itemSource' => 1,
  1404. 'shareType' => \OCP\Share::SHARE_TYPE_LINK,
  1405. 'uidOwner' => 'sharedBy',
  1406. 'permissions' => 31,
  1407. 'fileSource' => 1,
  1408. 'expiration' => $date,
  1409. 'token' => 'token',
  1410. 'run' => true,
  1411. 'error' => '',
  1412. 'itemTarget' => '/target',
  1413. 'shareWith' => null,
  1414. ];
  1415. $hookListnerExpectsPost = [
  1416. 'itemType' => 'file',
  1417. 'itemSource' => 1,
  1418. 'shareType' => \OCP\Share::SHARE_TYPE_LINK,
  1419. 'uidOwner' => 'sharedBy',
  1420. 'permissions' => 31,
  1421. 'fileSource' => 1,
  1422. 'expiration' => $date,
  1423. 'token' => 'token',
  1424. 'id' => 42,
  1425. 'itemTarget' => '/target',
  1426. 'fileTarget' => '/target',
  1427. 'shareWith' => null,
  1428. ];
  1429. $hookListner->expects($this->once())
  1430. ->method('pre')
  1431. ->with($this->equalTo($hookListnerExpectsPre));
  1432. $hookListner->expects($this->once())
  1433. ->method('post')
  1434. ->with($this->equalTo($hookListnerExpectsPost));
  1435. /** @var IShare $share */
  1436. $share = $manager->createShare($share);
  1437. $this->assertSame('shareOwner', $share->getShareOwner());
  1438. $this->assertEquals('/target', $share->getTarget());
  1439. $this->assertSame($date, $share->getExpirationDate());
  1440. $this->assertEquals('token', $share->getToken());
  1441. $this->assertEquals('hashed', $share->getPassword());
  1442. }
  1443. /**
  1444. * @expectedException Exception
  1445. * @expectedExceptionMessage I won't let you share
  1446. */
  1447. public function testCreateShareHookError() {
  1448. $manager = $this->createManagerMock()
  1449. ->setMethods([
  1450. 'canShare',
  1451. 'generalCreateChecks',
  1452. 'userCreateChecks',
  1453. 'pathCreateChecks',
  1454. ])
  1455. ->getMock();
  1456. $shareOwner = $this->createMock(IUser::class);
  1457. $shareOwner->method('getUID')->willReturn('shareOwner');
  1458. $storage = $this->createMock(Storage::class);
  1459. $path = $this->createMock(File::class);
  1460. $path->method('getOwner')->willReturn($shareOwner);
  1461. $path->method('getName')->willReturn('target');
  1462. $path->method('getStorage')->willReturn($storage);
  1463. $share = $this->createShare(
  1464. null,
  1465. \OCP\Share::SHARE_TYPE_USER,
  1466. $path,
  1467. 'sharedWith',
  1468. 'sharedBy',
  1469. null,
  1470. \OCP\Constants::PERMISSION_ALL);
  1471. $manager->expects($this->once())
  1472. ->method('canShare')
  1473. ->with($share)
  1474. ->willReturn(true);
  1475. $manager->expects($this->once())
  1476. ->method('generalCreateChecks')
  1477. ->with($share);;
  1478. $manager->expects($this->once())
  1479. ->method('userCreateChecks')
  1480. ->with($share);;
  1481. $manager->expects($this->once())
  1482. ->method('pathCreateChecks')
  1483. ->with($path);
  1484. $share->expects($this->once())
  1485. ->method('setShareOwner')
  1486. ->with('shareOwner');
  1487. $share->expects($this->once())
  1488. ->method('setTarget')
  1489. ->with('/target');
  1490. $hookListner = $this->getMockBuilder('Dummy')->setMethods(['pre'])->getMock();
  1491. \OCP\Util::connectHook('OCP\Share', 'pre_shared', $hookListner, 'pre');
  1492. $hookListner->expects($this->once())
  1493. ->method('pre')
  1494. ->will($this->returnCallback(function (array $data) {
  1495. $data['run'] = false;
  1496. $data['error'] = 'I won\'t let you share!';
  1497. }));
  1498. $manager->createShare($share);
  1499. }
  1500. public function testCreateShareOfIncommingFederatedShare() {
  1501. $manager = $this->createManagerMock()
  1502. ->setMethods(['canShare', 'generalCreateChecks', 'userCreateChecks', 'pathCreateChecks'])
  1503. ->getMock();
  1504. $shareOwner = $this->createMock(IUser::class);
  1505. $shareOwner->method('getUID')->willReturn('shareOwner');
  1506. $storage = $this->createMock(Storage::class);
  1507. $storage->method('instanceOfStorage')
  1508. ->with('OCA\Files_Sharing\External\Storage')
  1509. ->willReturn(true);
  1510. $storage2 = $this->createMock(Storage::class);
  1511. $storage2->method('instanceOfStorage')
  1512. ->with('OCA\Files_Sharing\External\Storage')
  1513. ->willReturn(false);
  1514. $path = $this->createMock(File::class);
  1515. $path->expects($this->never())->method('getOwner');
  1516. $path->method('getName')->willReturn('target');
  1517. $path->method('getStorage')->willReturn($storage);
  1518. $parent = $this->createMock(Folder::class);
  1519. $parent->method('getStorage')->willReturn($storage);
  1520. $parentParent = $this->createMock(Folder::class);
  1521. $parentParent->method('getStorage')->willReturn($storage2);
  1522. $parentParent->method('getOwner')->willReturn($shareOwner);
  1523. $path->method('getParent')->willReturn($parent);
  1524. $parent->method('getParent')->willReturn($parentParent);
  1525. $share = $this->createShare(
  1526. null,
  1527. \OCP\Share::SHARE_TYPE_USER,
  1528. $path,
  1529. 'sharedWith',
  1530. 'sharedBy',
  1531. null,
  1532. \OCP\Constants::PERMISSION_ALL);
  1533. $manager->expects($this->once())
  1534. ->method('canShare')
  1535. ->with($share)
  1536. ->willReturn(true);
  1537. $manager->expects($this->once())
  1538. ->method('generalCreateChecks')
  1539. ->with($share);;
  1540. $manager->expects($this->once())
  1541. ->method('userCreateChecks')
  1542. ->with($share);;
  1543. $manager->expects($this->once())
  1544. ->method('pathCreateChecks')
  1545. ->with($path);
  1546. $this->defaultProvider
  1547. ->expects($this->once())
  1548. ->method('create')
  1549. ->with($share)
  1550. ->will($this->returnArgument(0));
  1551. $share->expects($this->once())
  1552. ->method('setShareOwner')
  1553. ->with('shareOwner');
  1554. $share->expects($this->once())
  1555. ->method('setTarget')
  1556. ->with('/target');
  1557. $manager->createShare($share);
  1558. }
  1559. public function testGetSharesBy() {
  1560. $share = $this->manager->newShare();
  1561. $node = $this->createMock(Folder::class);
  1562. $this->defaultProvider->expects($this->once())
  1563. ->method('getSharesBy')
  1564. ->with(
  1565. $this->equalTo('user'),
  1566. $this->equalTo(\OCP\Share::SHARE_TYPE_USER),
  1567. $this->equalTo($node),
  1568. $this->equalTo(true),
  1569. $this->equalTo(1),
  1570. $this->equalTo(1)
  1571. )->willReturn([$share]);
  1572. $shares = $this->manager->getSharesBy('user', \OCP\Share::SHARE_TYPE_USER, $node, true, 1, 1);
  1573. $this->assertCount(1, $shares);
  1574. $this->assertSame($share, $shares[0]);
  1575. }
  1576. /**
  1577. * Test to ensure we correctly remove expired link shares
  1578. *
  1579. * We have 8 Shares and we want the 3 first valid shares.
  1580. * share 3-6 and 8 are expired. Thus at the end of this test we should
  1581. * have received share 1,2 and 7. And from the manager. Share 3-6 should be
  1582. * deleted (as they are evaluated). but share 8 should still be there.
  1583. */
  1584. public function testGetSharesByExpiredLinkShares() {
  1585. $manager = $this->createManagerMock()
  1586. ->setMethods(['deleteShare'])
  1587. ->getMock();
  1588. /** @var \OCP\Share\IShare[] $shares */
  1589. $shares = [];
  1590. /*
  1591. * This results in an array of 8 IShare elements
  1592. */
  1593. for ($i = 0; $i < 8; $i++) {
  1594. $share = $this->manager->newShare();
  1595. $share->setId($i);
  1596. $shares[] = $share;
  1597. }
  1598. $today = new \DateTime();
  1599. $today->setTime(0,0,0);
  1600. /*
  1601. * Set the expiration date to today for some shares
  1602. */
  1603. $shares[2]->setExpirationDate($today);
  1604. $shares[3]->setExpirationDate($today);
  1605. $shares[4]->setExpirationDate($today);
  1606. $shares[5]->setExpirationDate($today);
  1607. /** @var \OCP\Share\IShare[] $i */
  1608. $shares2 = [];
  1609. for ($i = 0; $i < 8; $i++) {
  1610. $shares2[] = clone $shares[$i];
  1611. }
  1612. $node = $this->createMock(File::class);
  1613. /*
  1614. * Simulate the getSharesBy call.
  1615. */
  1616. $this->defaultProvider
  1617. ->method('getSharesBy')
  1618. ->will($this->returnCallback(function($uid, $type, $node, $reshares, $limit, $offset) use (&$shares2) {
  1619. return array_slice($shares2, $offset, $limit);
  1620. }));
  1621. /*
  1622. * Simulate the deleteShare call.
  1623. */
  1624. $manager->method('deleteShare')
  1625. ->will($this->returnCallback(function($share) use (&$shares2) {
  1626. for($i = 0; $i < count($shares2); $i++) {
  1627. if ($shares2[$i]->getId() === $share->getId()) {
  1628. array_splice($shares2, $i, 1);
  1629. break;
  1630. }
  1631. }
  1632. }));
  1633. $res = $manager->getSharesBy('user', \OCP\Share::SHARE_TYPE_LINK, $node, true, 3, 0);
  1634. $this->assertCount(3, $res);
  1635. $this->assertEquals($shares[0]->getId(), $res[0]->getId());
  1636. $this->assertEquals($shares[1]->getId(), $res[1]->getId());
  1637. $this->assertEquals($shares[6]->getId(), $res[2]->getId());
  1638. $this->assertCount(4, $shares2);
  1639. $this->assertEquals(0, $shares2[0]->getId());
  1640. $this->assertEquals(1, $shares2[1]->getId());
  1641. $this->assertEquals(6, $shares2[2]->getId());
  1642. $this->assertEquals(7, $shares2[3]->getId());
  1643. $this->assertSame($today, $shares[3]->getExpirationDate());
  1644. }
  1645. public function testGetShareByToken() {
  1646. $factory = $this->createMock(IProviderFactory::class);
  1647. $manager = new Manager(
  1648. $this->logger,
  1649. $this->config,
  1650. $this->secureRandom,
  1651. $this->hasher,
  1652. $this->mountManager,
  1653. $this->groupManager,
  1654. $this->l,
  1655. $factory,
  1656. $this->userManager,
  1657. $this->rootFolder,
  1658. $this->eventDispatcher
  1659. );
  1660. $share = $this->createMock(IShare::class);
  1661. $factory->expects($this->once())
  1662. ->method('getProviderForType')
  1663. ->with(\OCP\Share::SHARE_TYPE_LINK)
  1664. ->willReturn($this->defaultProvider);
  1665. $this->defaultProvider->expects($this->once())
  1666. ->method('getShareByToken')
  1667. ->with('token')
  1668. ->willReturn($share);
  1669. $ret = $manager->getShareByToken('token');
  1670. $this->assertSame($share, $ret);
  1671. }
  1672. public function testGetShareByTokenWithException() {
  1673. $factory = $this->createMock(IProviderFactory::class);
  1674. $manager = new Manager(
  1675. $this->logger,
  1676. $this->config,
  1677. $this->secureRandom,
  1678. $this->hasher,
  1679. $this->mountManager,
  1680. $this->groupManager,
  1681. $this->l,
  1682. $factory,
  1683. $this->userManager,
  1684. $this->rootFolder,
  1685. $this->eventDispatcher
  1686. );
  1687. $share = $this->createMock(IShare::class);
  1688. $factory->expects($this->at(0))
  1689. ->method('getProviderForType')
  1690. ->with(\OCP\Share::SHARE_TYPE_LINK)
  1691. ->willReturn($this->defaultProvider);
  1692. $factory->expects($this->at(1))
  1693. ->method('getProviderForType')
  1694. ->with(\OCP\Share::SHARE_TYPE_REMOTE)
  1695. ->willReturn($this->defaultProvider);
  1696. $this->defaultProvider->expects($this->at(0))
  1697. ->method('getShareByToken')
  1698. ->with('token')
  1699. ->will($this->throwException(new ShareNotFound()));
  1700. $this->defaultProvider->expects($this->at(1))
  1701. ->method('getShareByToken')
  1702. ->with('token')
  1703. ->willReturn($share);
  1704. $ret = $manager->getShareByToken('token');
  1705. $this->assertSame($share, $ret);
  1706. }
  1707. /**
  1708. * @expectedException \OCP\Share\Exceptions\ShareNotFound
  1709. */
  1710. public function testGetShareByTokenExpired() {
  1711. $manager = $this->createManagerMock()
  1712. ->setMethods(['deleteShare'])
  1713. ->getMock();
  1714. $date = new \DateTime();
  1715. $date->setTime(0,0,0);
  1716. $share = $this->manager->newShare();
  1717. $share->setExpirationDate($date);
  1718. $this->defaultProvider->expects($this->once())
  1719. ->method('getShareByToken')
  1720. ->with('expiredToken')
  1721. ->willReturn($share);
  1722. $manager->expects($this->once())
  1723. ->method('deleteShare')
  1724. ->with($this->equalTo($share));
  1725. $manager->getShareByToken('expiredToken');
  1726. }
  1727. public function testGetShareByTokenNotExpired() {
  1728. $date = new \DateTime();
  1729. $date->setTime(0,0,0);
  1730. $date->add(new \DateInterval('P2D'));
  1731. $share = $this->manager->newShare();
  1732. $share->setExpirationDate($date);
  1733. $this->defaultProvider->expects($this->once())
  1734. ->method('getShareByToken')
  1735. ->with('expiredToken')
  1736. ->willReturn($share);
  1737. $res = $this->manager->getShareByToken('expiredToken');
  1738. $this->assertSame($share, $res);
  1739. }
  1740. public function testGetShareByTokenPublicSharingDisabled() {
  1741. $share = $this->manager->newShare();
  1742. $share->setShareType(\OCP\Share::SHARE_TYPE_LINK)
  1743. ->setPermissions(\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE);
  1744. $this->config->method('getAppValue')->will($this->returnValueMap([
  1745. ['core', 'shareapi_allow_public_upload', 'yes', 'no'],
  1746. ]));
  1747. $this->defaultProvider->expects($this->once())
  1748. ->method('getShareByToken')
  1749. ->willReturn('validToken')
  1750. ->willReturn($share);
  1751. $res = $this->manager->getShareByToken('validToken');
  1752. $this->assertSame(\OCP\Constants::PERMISSION_READ, $res->getPermissions());
  1753. }
  1754. public function testCheckPasswordNoLinkShare() {
  1755. $share = $this->createMock(IShare::class);
  1756. $share->method('getShareType')->willReturn(\OCP\Share::SHARE_TYPE_USER);
  1757. $this->assertFalse($this->manager->checkPassword($share, 'password'));
  1758. }
  1759. public function testCheckPasswordNoPassword() {
  1760. $share = $this->createMock(IShare::class);
  1761. $share->method('getShareType')->willReturn(\OCP\Share::SHARE_TYPE_LINK);
  1762. $this->assertFalse($this->manager->checkPassword($share, 'password'));
  1763. $share->method('getPassword')->willReturn('password');
  1764. $this->assertFalse($this->manager->checkPassword($share, null));
  1765. }
  1766. public function testCheckPasswordInvalidPassword() {
  1767. $share = $this->createMock(IShare::class);
  1768. $share->method('getShareType')->willReturn(\OCP\Share::SHARE_TYPE_LINK);
  1769. $share->method('getPassword')->willReturn('password');
  1770. $this->hasher->method('verify')->with('invalidpassword', 'password', '')->willReturn(false);
  1771. $this->assertFalse($this->manager->checkPassword($share, 'invalidpassword'));
  1772. }
  1773. public function testCheckPasswordValidPassword() {
  1774. $share = $this->createMock(IShare::class);
  1775. $share->method('getShareType')->willReturn(\OCP\Share::SHARE_TYPE_LINK);
  1776. $share->method('getPassword')->willReturn('passwordHash');
  1777. $this->hasher->method('verify')->with('password', 'passwordHash', '')->willReturn(true);
  1778. $this->assertTrue($this->manager->checkPassword($share, 'password'));
  1779. }
  1780. public function testCheckPasswordUpdateShare() {
  1781. $share = $this->manager->newShare();
  1782. $share->setShareType(\OCP\Share::SHARE_TYPE_LINK)
  1783. ->setPassword('passwordHash');
  1784. $this->hasher->method('verify')->with('password', 'passwordHash', '')
  1785. ->will($this->returnCallback(function($pass, $hash, &$newHash) {
  1786. $newHash = 'newHash';
  1787. return true;
  1788. }));
  1789. $this->defaultProvider->expects($this->once())
  1790. ->method('update')
  1791. ->with($this->callback(function (\OCP\Share\IShare $share) {
  1792. return $share->getPassword() === 'newHash';
  1793. }));
  1794. $this->assertTrue($this->manager->checkPassword($share, 'password'));
  1795. }
  1796. /**
  1797. * @expectedException Exception
  1798. * @expectedExceptionMessage Can't change share type
  1799. */
  1800. public function testUpdateShareCantChangeShareType() {
  1801. $manager = $this->createManagerMock()
  1802. ->setMethods([
  1803. 'canShare',
  1804. 'getShareById'
  1805. ])
  1806. ->getMock();
  1807. $originalShare = $this->manager->newShare();
  1808. $originalShare->setShareType(\OCP\Share::SHARE_TYPE_GROUP);
  1809. $manager->expects($this->once())->method('canShare')->willReturn(true);
  1810. $manager->expects($this->once())->method('getShareById')->with('foo:42')->willReturn($originalShare);
  1811. $share = $this->manager->newShare();
  1812. $share->setProviderId('foo')
  1813. ->setId('42')
  1814. ->setShareType(\OCP\Share::SHARE_TYPE_USER);
  1815. $manager->updateShare($share);
  1816. }
  1817. /**
  1818. * @expectedException Exception
  1819. * @expectedExceptionMessage Can only update recipient on user shares
  1820. */
  1821. public function testUpdateShareCantChangeRecipientForGroupShare() {
  1822. $manager = $this->createManagerMock()
  1823. ->setMethods([
  1824. 'canShare',
  1825. 'getShareById'
  1826. ])
  1827. ->getMock();
  1828. $originalShare = $this->manager->newShare();
  1829. $originalShare->setShareType(\OCP\Share::SHARE_TYPE_GROUP)
  1830. ->setSharedWith('origGroup');
  1831. $manager->expects($this->once())->method('canShare')->willReturn(true);
  1832. $manager->expects($this->once())->method('getShareById')->with('foo:42')->willReturn($originalShare);
  1833. $share = $this->manager->newShare();
  1834. $share->setProviderId('foo')
  1835. ->setId('42')
  1836. ->setShareType(\OCP\Share::SHARE_TYPE_GROUP)
  1837. ->setSharedWith('newGroup');
  1838. $manager->updateShare($share);
  1839. }
  1840. /**
  1841. * @expectedException Exception
  1842. * @expectedExceptionMessage Can't share with the share owner
  1843. */
  1844. public function testUpdateShareCantShareWithOwner() {
  1845. $manager = $this->createManagerMock()
  1846. ->setMethods([
  1847. 'canShare',
  1848. 'getShareById'
  1849. ])
  1850. ->getMock();
  1851. $originalShare = $this->manager->newShare();
  1852. $originalShare->setShareType(\OCP\Share::SHARE_TYPE_USER)
  1853. ->setSharedWith('sharedWith');
  1854. $manager->expects($this->once())->method('canShare')->willReturn(true);
  1855. $manager->expects($this->once())->method('getShareById')->with('foo:42')->willReturn($originalShare);
  1856. $share = $this->manager->newShare();
  1857. $share->setProviderId('foo')
  1858. ->setId('42')
  1859. ->setShareType(\OCP\Share::SHARE_TYPE_USER)
  1860. ->setSharedWith('newUser')
  1861. ->setShareOwner('newUser');
  1862. $manager->updateShare($share);
  1863. }
  1864. public function testUpdateShareUser() {
  1865. $this->userManager->expects($this->any())->method('userExists')->willReturn(true);
  1866. $manager = $this->createManagerMock()
  1867. ->setMethods([
  1868. 'canShare',
  1869. 'getShareById',
  1870. 'generalCreateChecks',
  1871. 'userCreateChecks',
  1872. 'pathCreateChecks',
  1873. ])
  1874. ->getMock();
  1875. $originalShare = $this->manager->newShare();
  1876. $originalShare->setShareType(\OCP\Share::SHARE_TYPE_USER)
  1877. ->setSharedWith('origUser')
  1878. ->setPermissions(1);
  1879. $node = $this->createMock(File::class);
  1880. $node->method('getId')->willReturn(100);
  1881. $node->method('getPath')->willReturn('/newUser/files/myPath');
  1882. $manager->expects($this->once())->method('canShare')->willReturn(true);
  1883. $manager->expects($this->once())->method('getShareById')->with('foo:42')->willReturn($originalShare);
  1884. $share = $this->manager->newShare();
  1885. $share->setProviderId('foo')
  1886. ->setId('42')
  1887. ->setShareType(\OCP\Share::SHARE_TYPE_USER)
  1888. ->setSharedWith('origUser')
  1889. ->setShareOwner('newUser')
  1890. ->setSharedBy('sharer')
  1891. ->setPermissions(31)
  1892. ->setNode($node);
  1893. $this->defaultProvider->expects($this->once())
  1894. ->method('update')
  1895. ->with($share)
  1896. ->willReturn($share);
  1897. $hookListner = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  1898. \OCP\Util::connectHook('OCP\Share', 'post_set_expiration_date', $hookListner, 'post');
  1899. $hookListner->expects($this->never())->method('post');
  1900. $this->rootFolder->method('getUserFolder')->with('newUser')->will($this->returnSelf());
  1901. $this->rootFolder->method('getRelativePath')->with('/newUser/files/myPath')->willReturn('/myPath');
  1902. $hookListner2 = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  1903. \OCP\Util::connectHook('OCP\Share', 'post_update_permissions', $hookListner2, 'post');
  1904. $hookListner2->expects($this->once())->method('post')->with([
  1905. 'itemType' => 'file',
  1906. 'itemSource' => 100,
  1907. 'shareType' => \OCP\Share::SHARE_TYPE_USER,
  1908. 'shareWith' => 'origUser',
  1909. 'uidOwner' => 'sharer',
  1910. 'permissions' => 31,
  1911. 'path' => '/myPath',
  1912. ]);
  1913. $manager->updateShare($share);
  1914. }
  1915. public function testUpdateShareGroup() {
  1916. $manager = $this->createManagerMock()
  1917. ->setMethods([
  1918. 'canShare',
  1919. 'getShareById',
  1920. 'generalCreateChecks',
  1921. 'groupCreateChecks',
  1922. 'pathCreateChecks',
  1923. ])
  1924. ->getMock();
  1925. $originalShare = $this->manager->newShare();
  1926. $originalShare->setShareType(\OCP\Share::SHARE_TYPE_GROUP)
  1927. ->setSharedWith('origUser')
  1928. ->setPermissions(31);
  1929. $manager->expects($this->once())->method('canShare')->willReturn(true);
  1930. $manager->expects($this->once())->method('getShareById')->with('foo:42')->willReturn($originalShare);
  1931. $node = $this->createMock(File::class);
  1932. $share = $this->manager->newShare();
  1933. $share->setProviderId('foo')
  1934. ->setId('42')
  1935. ->setShareType(\OCP\Share::SHARE_TYPE_GROUP)
  1936. ->setSharedWith('origUser')
  1937. ->setShareOwner('owner')
  1938. ->setNode($node)
  1939. ->setPermissions(31);
  1940. $this->defaultProvider->expects($this->once())
  1941. ->method('update')
  1942. ->with($share)
  1943. ->willReturn($share);
  1944. $hookListner = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  1945. \OCP\Util::connectHook('OCP\Share', 'post_set_expiration_date', $hookListner, 'post');
  1946. $hookListner->expects($this->never())->method('post');
  1947. $hookListner2 = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  1948. \OCP\Util::connectHook('OCP\Share', 'post_update_permissions', $hookListner2, 'post');
  1949. $hookListner2->expects($this->never())->method('post');
  1950. $manager->updateShare($share);
  1951. }
  1952. public function testUpdateShareLink() {
  1953. $manager = $this->createManagerMock()
  1954. ->setMethods([
  1955. 'canShare',
  1956. 'getShareById',
  1957. 'generalCreateChecks',
  1958. 'linkCreateChecks',
  1959. 'pathCreateChecks',
  1960. 'verifyPassword',
  1961. 'validateExpirationDate',
  1962. ])
  1963. ->getMock();
  1964. $originalShare = $this->manager->newShare();
  1965. $originalShare->setShareType(\OCP\Share::SHARE_TYPE_LINK)
  1966. ->setPermissions(15);
  1967. $tomorrow = new \DateTime();
  1968. $tomorrow->setTime(0,0,0);
  1969. $tomorrow->add(new \DateInterval('P1D'));
  1970. $file = $this->createMock(File::class);
  1971. $file->method('getId')->willReturn(100);
  1972. $share = $this->manager->newShare();
  1973. $share->setProviderId('foo')
  1974. ->setId('42')
  1975. ->setShareType(\OCP\Share::SHARE_TYPE_LINK)
  1976. ->setSharedBy('owner')
  1977. ->setShareOwner('owner')
  1978. ->setPassword('password')
  1979. ->setExpirationDate($tomorrow)
  1980. ->setNode($file)
  1981. ->setPermissions(15);
  1982. $manager->expects($this->once())->method('canShare')->willReturn(true);
  1983. $manager->expects($this->once())->method('getShareById')->with('foo:42')->willReturn($originalShare);
  1984. $manager->expects($this->once())->method('validateExpirationDate')->with($share);
  1985. $this->defaultProvider->expects($this->once())
  1986. ->method('update')
  1987. ->with($share)
  1988. ->willReturn($share);
  1989. $hookListner = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  1990. \OCP\Util::connectHook('OCP\Share', 'post_set_expiration_date', $hookListner, 'post');
  1991. $hookListner->expects($this->once())->method('post')->with([
  1992. 'itemType' => 'file',
  1993. 'itemSource' => 100,
  1994. 'date' => $tomorrow,
  1995. 'uidOwner' => 'owner',
  1996. ]);
  1997. $hookListner2 = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  1998. \OCP\Util::connectHook('OCP\Share', 'post_update_permissions', $hookListner2, 'post');
  1999. $hookListner2->expects($this->never())->method('post');
  2000. $manager->updateShare($share);
  2001. }
  2002. /**
  2003. * @expectedException \InvalidArgumentException
  2004. * @expectedExceptionMessage Can't change target of link share
  2005. */
  2006. public function testMoveShareLink() {
  2007. $share = $this->manager->newShare();
  2008. $share->setShareType(\OCP\Share::SHARE_TYPE_LINK);
  2009. $recipient = $this->createMock(IUser::class);
  2010. $this->manager->moveShare($share, $recipient);
  2011. }
  2012. /**
  2013. * @expectedException \InvalidArgumentException
  2014. * @expectedExceptionMessage Invalid recipient
  2015. */
  2016. public function testMoveShareUserNotRecipient() {
  2017. $share = $this->manager->newShare();
  2018. $share->setShareType(\OCP\Share::SHARE_TYPE_USER);
  2019. $share->setSharedWith('sharedWith');
  2020. $this->manager->moveShare($share, 'recipient');
  2021. }
  2022. public function testMoveShareUser() {
  2023. $share = $this->manager->newShare();
  2024. $share->setShareType(\OCP\Share::SHARE_TYPE_USER)
  2025. ->setId('42')
  2026. ->setProviderId('foo');
  2027. $share->setSharedWith('recipient');
  2028. $this->defaultProvider->method('move')->with($share, 'recipient')->will($this->returnArgument(0));
  2029. $this->manager->moveShare($share, 'recipient');
  2030. }
  2031. /**
  2032. * @expectedException \InvalidArgumentException
  2033. * @expectedExceptionMessage Invalid recipient
  2034. */
  2035. public function testMoveShareGroupNotRecipient() {
  2036. $share = $this->manager->newShare();
  2037. $share->setShareType(\OCP\Share::SHARE_TYPE_GROUP);
  2038. $sharedWith = $this->createMock(IGroup::class);
  2039. $share->setSharedWith('shareWith');
  2040. $recipient = $this->createMock(IUser::class);
  2041. $sharedWith->method('inGroup')->with($recipient)->willReturn(false);
  2042. $this->groupManager->method('get')->with('shareWith')->willReturn($sharedWith);
  2043. $this->userManager->method('get')->with('recipient')->willReturn($recipient);
  2044. $this->manager->moveShare($share, 'recipient');
  2045. }
  2046. public function testMoveShareGroup() {
  2047. $share = $this->manager->newShare();
  2048. $share->setShareType(\OCP\Share::SHARE_TYPE_GROUP)
  2049. ->setId('42')
  2050. ->setProviderId('foo');
  2051. $group = $this->createMock(IGroup::class);
  2052. $share->setSharedWith('group');
  2053. $recipient = $this->createMock(IUser::class);
  2054. $group->method('inGroup')->with($recipient)->willReturn(true);
  2055. $this->groupManager->method('get')->with('group')->willReturn($group);
  2056. $this->userManager->method('get')->with('recipient')->willReturn($recipient);
  2057. $this->defaultProvider->method('move')->with($share, 'recipient')->will($this->returnArgument(0));
  2058. $this->manager->moveShare($share, 'recipient');
  2059. }
  2060. }
  2061. class DummyFactory implements IProviderFactory {
  2062. /** @var IShareProvider */
  2063. private $provider;
  2064. public function __construct(\OCP\IServerContainer $serverContainer) {
  2065. }
  2066. /**
  2067. * @param IShareProvider $provider
  2068. */
  2069. public function setProvider($provider) {
  2070. $this->provider = $provider;
  2071. }
  2072. /**
  2073. * @param string $id
  2074. * @return IShareProvider
  2075. */
  2076. public function getProvider($id) {
  2077. return $this->provider;
  2078. }
  2079. /**
  2080. * @param int $shareType
  2081. * @return IShareProvider
  2082. */
  2083. public function getProviderForType($shareType) {
  2084. return $this->provider;
  2085. }
  2086. }