You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ManagerTest.php 78KB

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