1
0

LegacyHooksTest.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace Test\Share20;
  7. use OC\Share20\LegacyHooks;
  8. use OC\Share20\Manager;
  9. use OCP\Constants;
  10. use OCP\EventDispatcher\IEventDispatcher;
  11. use OCP\Files\Cache\ICacheEntry;
  12. use OCP\Files\File;
  13. use OCP\IServerContainer;
  14. use OCP\Share\Events\BeforeShareCreatedEvent;
  15. use OCP\Share\Events\BeforeShareDeletedEvent;
  16. use OCP\Share\Events\ShareCreatedEvent;
  17. use OCP\Share\Events\ShareDeletedEvent;
  18. use OCP\Share\Events\ShareDeletedFromSelfEvent;
  19. use OCP\Share\IManager as IShareManager;
  20. use OCP\Share\IShare;
  21. use Psr\Log\LoggerInterface;
  22. use Test\TestCase;
  23. class LegacyHooksTest extends TestCase {
  24. /** @var LegacyHooks */
  25. private $hooks;
  26. /** @var IEventDispatcher */
  27. private $eventDispatcher;
  28. /** @var Manager */
  29. private $manager;
  30. protected function setUp(): void {
  31. parent::setUp();
  32. $symfonyDispatcher = new \Symfony\Component\EventDispatcher\EventDispatcher();
  33. $logger = $this->createMock(LoggerInterface::class);
  34. $this->eventDispatcher = new \OC\EventDispatcher\EventDispatcher($symfonyDispatcher, \OC::$server->get(IServerContainer::class), $logger);
  35. $this->hooks = new LegacyHooks($this->eventDispatcher);
  36. $this->manager = \OC::$server->get(IShareManager::class);
  37. }
  38. public function testPreUnshare(): void {
  39. $path = $this->createMock(File::class);
  40. $path->method('getId')->willReturn(1);
  41. $info = $this->createMock(ICacheEntry::class);
  42. $info->method('getMimeType')->willReturn('text/plain');
  43. $share = $this->manager->newShare();
  44. $share->setId(42)
  45. ->setProviderId('prov')
  46. ->setShareType(IShare::TYPE_USER)
  47. ->setSharedWith('awesomeUser')
  48. ->setSharedBy('sharedBy')
  49. ->setNode($path)
  50. ->setTarget('myTarget')
  51. ->setNodeCacheEntry($info);
  52. $hookListner = $this->getMockBuilder('Dummy')->setMethods(['pre'])->getMock();
  53. \OCP\Util::connectHook('OCP\Share', 'pre_unshare', $hookListner, 'pre');
  54. $hookListnerExpectsPre = [
  55. 'id' => 42,
  56. 'itemType' => 'file',
  57. 'itemSource' => 1,
  58. 'shareType' => IShare::TYPE_USER,
  59. 'shareWith' => 'awesomeUser',
  60. 'itemparent' => null,
  61. 'uidOwner' => 'sharedBy',
  62. 'fileSource' => 1,
  63. 'fileTarget' => 'myTarget',
  64. ];
  65. $hookListner
  66. ->expects($this->exactly(1))
  67. ->method('pre')
  68. ->with($hookListnerExpectsPre);
  69. $event = new BeforeShareDeletedEvent($share);
  70. $this->eventDispatcher->dispatchTyped($event);
  71. }
  72. public function testPostUnshare(): void {
  73. $path = $this->createMock(File::class);
  74. $path->method('getId')->willReturn(1);
  75. $info = $this->createMock(ICacheEntry::class);
  76. $info->method('getMimeType')->willReturn('text/plain');
  77. $share = $this->manager->newShare();
  78. $share->setId(42)
  79. ->setProviderId('prov')
  80. ->setShareType(IShare::TYPE_USER)
  81. ->setSharedWith('awesomeUser')
  82. ->setSharedBy('sharedBy')
  83. ->setNode($path)
  84. ->setTarget('myTarget')
  85. ->setNodeCacheEntry($info);
  86. $hookListner = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  87. \OCP\Util::connectHook('OCP\Share', 'post_unshare', $hookListner, 'post');
  88. $hookListnerExpectsPost = [
  89. 'id' => 42,
  90. 'itemType' => 'file',
  91. 'itemSource' => 1,
  92. 'shareType' => IShare::TYPE_USER,
  93. 'shareWith' => 'awesomeUser',
  94. 'itemparent' => null,
  95. 'uidOwner' => 'sharedBy',
  96. 'fileSource' => 1,
  97. 'fileTarget' => 'myTarget',
  98. 'deletedShares' => [
  99. [
  100. 'id' => 42,
  101. 'itemType' => 'file',
  102. 'itemSource' => 1,
  103. 'shareType' => IShare::TYPE_USER,
  104. 'shareWith' => 'awesomeUser',
  105. 'itemparent' => null,
  106. 'uidOwner' => 'sharedBy',
  107. 'fileSource' => 1,
  108. 'fileTarget' => 'myTarget',
  109. ],
  110. ],
  111. ];
  112. $hookListner
  113. ->expects($this->exactly(1))
  114. ->method('post')
  115. ->with($hookListnerExpectsPost);
  116. $event = new ShareDeletedEvent($share);
  117. $this->eventDispatcher->dispatchTyped($event);
  118. }
  119. public function testPostUnshareFromSelf(): void {
  120. $path = $this->createMock(File::class);
  121. $path->method('getId')->willReturn(1);
  122. $info = $this->createMock(ICacheEntry::class);
  123. $info->method('getMimeType')->willReturn('text/plain');
  124. $share = $this->manager->newShare();
  125. $share->setId(42)
  126. ->setProviderId('prov')
  127. ->setShareType(IShare::TYPE_USER)
  128. ->setSharedWith('awesomeUser')
  129. ->setSharedBy('sharedBy')
  130. ->setNode($path)
  131. ->setTarget('myTarget')
  132. ->setNodeCacheEntry($info);
  133. $hookListner = $this->getMockBuilder('Dummy')->setMethods(['postFromSelf'])->getMock();
  134. \OCP\Util::connectHook('OCP\Share', 'post_unshareFromSelf', $hookListner, 'postFromSelf');
  135. $hookListnerExpectsPostFromSelf = [
  136. 'id' => 42,
  137. 'itemType' => 'file',
  138. 'itemSource' => 1,
  139. 'shareType' => IShare::TYPE_USER,
  140. 'shareWith' => 'awesomeUser',
  141. 'itemparent' => null,
  142. 'uidOwner' => 'sharedBy',
  143. 'fileSource' => 1,
  144. 'fileTarget' => 'myTarget',
  145. 'itemTarget' => 'myTarget',
  146. 'unsharedItems' => [
  147. [
  148. 'id' => 42,
  149. 'itemType' => 'file',
  150. 'itemSource' => 1,
  151. 'shareType' => IShare::TYPE_USER,
  152. 'shareWith' => 'awesomeUser',
  153. 'itemparent' => null,
  154. 'uidOwner' => 'sharedBy',
  155. 'fileSource' => 1,
  156. 'fileTarget' => 'myTarget',
  157. 'itemTarget' => 'myTarget',
  158. ],
  159. ],
  160. ];
  161. $hookListner
  162. ->expects($this->exactly(1))
  163. ->method('postFromSelf')
  164. ->with($hookListnerExpectsPostFromSelf);
  165. $event = new ShareDeletedFromSelfEvent($share);
  166. $this->eventDispatcher->dispatchTyped($event);
  167. }
  168. public function testPreShare(): void {
  169. $path = $this->createMock(File::class);
  170. $path->method('getId')->willReturn(1);
  171. $date = new \DateTime();
  172. $share = $this->manager->newShare();
  173. $share->setShareType(IShare::TYPE_LINK)
  174. ->setSharedWith('awesomeUser')
  175. ->setSharedBy('sharedBy')
  176. ->setNode($path)
  177. ->setTarget('myTarget')
  178. ->setPermissions(Constants::PERMISSION_ALL)
  179. ->setExpirationDate($date)
  180. ->setPassword('password')
  181. ->setToken('token');
  182. $hookListner = $this->getMockBuilder('Dummy')->setMethods(['preShare'])->getMock();
  183. \OCP\Util::connectHook('OCP\Share', 'pre_shared', $hookListner, 'preShare');
  184. $run = true;
  185. $error = '';
  186. $expected = [
  187. 'itemType' => 'file',
  188. 'itemSource' => 1,
  189. 'shareType' => IShare::TYPE_LINK,
  190. 'shareWith' => 'awesomeUser',
  191. 'uidOwner' => 'sharedBy',
  192. 'fileSource' => 1,
  193. 'itemTarget' => 'myTarget',
  194. 'permissions' => Constants::PERMISSION_ALL,
  195. 'expiration' => $date,
  196. 'token' => 'token',
  197. 'run' => &$run,
  198. 'error' => &$error,
  199. ];
  200. $hookListner
  201. ->expects($this->exactly(1))
  202. ->method('preShare')
  203. ->with($expected);
  204. $event = new BeforeShareCreatedEvent($share);
  205. $this->eventDispatcher->dispatchTyped($event);
  206. }
  207. public function testPreShareError(): void {
  208. $path = $this->createMock(File::class);
  209. $path->method('getId')->willReturn(1);
  210. $date = new \DateTime();
  211. $share = $this->manager->newShare();
  212. $share->setShareType(IShare::TYPE_LINK)
  213. ->setSharedWith('awesomeUser')
  214. ->setSharedBy('sharedBy')
  215. ->setNode($path)
  216. ->setTarget('myTarget')
  217. ->setPermissions(Constants::PERMISSION_ALL)
  218. ->setExpirationDate($date)
  219. ->setPassword('password')
  220. ->setToken('token');
  221. $hookListner = $this->getMockBuilder('Dummy')->setMethods(['preShare'])->getMock();
  222. \OCP\Util::connectHook('OCP\Share', 'pre_shared', $hookListner, 'preShare');
  223. $run = true;
  224. $error = '';
  225. $expected = [
  226. 'itemType' => 'file',
  227. 'itemSource' => 1,
  228. 'shareType' => IShare::TYPE_LINK,
  229. 'shareWith' => 'awesomeUser',
  230. 'uidOwner' => 'sharedBy',
  231. 'fileSource' => 1,
  232. 'itemTarget' => 'myTarget',
  233. 'permissions' => Constants::PERMISSION_ALL,
  234. 'expiration' => $date,
  235. 'token' => 'token',
  236. 'run' => &$run,
  237. 'error' => &$error,
  238. ];
  239. $hookListner
  240. ->expects($this->exactly(1))
  241. ->method('preShare')
  242. ->with($expected)
  243. ->willReturnCallback(function ($data) {
  244. $data['run'] = false;
  245. $data['error'] = 'I error';
  246. });
  247. $event = new BeforeShareCreatedEvent($share);
  248. $this->eventDispatcher->dispatchTyped($event);
  249. $this->assertTrue($event->isPropagationStopped());
  250. $this->assertSame('I error', $event->getError());
  251. }
  252. public function testPostShare(): void {
  253. $path = $this->createMock(File::class);
  254. $path->method('getId')->willReturn(1);
  255. $date = new \DateTime();
  256. $share = $this->manager->newShare();
  257. $share->setId(42)
  258. ->setShareType(IShare::TYPE_LINK)
  259. ->setSharedWith('awesomeUser')
  260. ->setSharedBy('sharedBy')
  261. ->setNode($path)
  262. ->setTarget('myTarget')
  263. ->setPermissions(Constants::PERMISSION_ALL)
  264. ->setExpirationDate($date)
  265. ->setPassword('password')
  266. ->setToken('token');
  267. $hookListner = $this->getMockBuilder('Dummy')->setMethods(['postShare'])->getMock();
  268. \OCP\Util::connectHook('OCP\Share', 'post_shared', $hookListner, 'postShare');
  269. $expected = [
  270. 'id' => 42,
  271. 'itemType' => 'file',
  272. 'itemSource' => 1,
  273. 'shareType' => IShare::TYPE_LINK,
  274. 'shareWith' => 'awesomeUser',
  275. 'uidOwner' => 'sharedBy',
  276. 'fileSource' => 1,
  277. 'itemTarget' => 'myTarget',
  278. 'fileTarget' => 'myTarget',
  279. 'permissions' => Constants::PERMISSION_ALL,
  280. 'expiration' => $date,
  281. 'token' => 'token',
  282. 'path' => null,
  283. ];
  284. $hookListner
  285. ->expects($this->exactly(1))
  286. ->method('postShare')
  287. ->with($expected);
  288. $event = new ShareCreatedEvent($share);
  289. $this->eventDispatcher->dispatchTyped($event);
  290. }
  291. }