LegacyHooksTest.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. <?php
  2. /**
  3. * @copyright 2017, Roeland Jago Douma <roeland@famdouma.nl>
  4. *
  5. * @author Roeland Jago Douma <roeland@famdouma.nl>
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. namespace Test\Share20;
  24. use OC\EventDispatcher\SymfonyAdapter;
  25. use OC\Share20\LegacyHooks;
  26. use OC\Share20\Manager;
  27. use OCP\Constants;
  28. use OCP\Files\Cache\ICacheEntry;
  29. use OCP\Files\File;
  30. use OCP\IServerContainer;
  31. use OCP\Share\IShare;
  32. use Psr\Log\LoggerInterface;
  33. use Symfony\Component\EventDispatcher\EventDispatcher;
  34. use Symfony\Component\EventDispatcher\GenericEvent;
  35. use Test\TestCase;
  36. class LegacyHooksTest extends TestCase {
  37. /** @var LegacyHooks */
  38. private $hooks;
  39. /** @var EventDispatcher */
  40. private $eventDispatcher;
  41. /** @var Manager */
  42. private $manager;
  43. protected function setUp(): void {
  44. parent::setUp();
  45. $symfonyDispatcher = new \Symfony\Component\EventDispatcher\EventDispatcher();
  46. $logger = $this->createMock(LoggerInterface::class);
  47. $eventDispatcher = new \OC\EventDispatcher\EventDispatcher($symfonyDispatcher, \OC::$server->get(IServerContainer::class), $logger);
  48. $this->eventDispatcher = new SymfonyAdapter($eventDispatcher, $logger);
  49. $this->hooks = new LegacyHooks($this->eventDispatcher);
  50. $this->manager = \OC::$server->getShareManager();
  51. }
  52. public function testPreUnshare() {
  53. $path = $this->createMock(File::class);
  54. $path->method('getId')->willReturn(1);
  55. $info = $this->createMock(ICacheEntry::class);
  56. $info->method('getMimeType')->willReturn('text/plain');
  57. $share = $this->manager->newShare();
  58. $share->setId(42)
  59. ->setProviderId('prov')
  60. ->setShareType(IShare::TYPE_USER)
  61. ->setSharedWith('awesomeUser')
  62. ->setSharedBy('sharedBy')
  63. ->setNode($path)
  64. ->setTarget('myTarget')
  65. ->setNodeCacheEntry($info);
  66. $hookListner = $this->getMockBuilder('Dummy')->setMethods(['pre'])->getMock();
  67. \OCP\Util::connectHook('OCP\Share', 'pre_unshare', $hookListner, 'pre');
  68. $hookListnerExpectsPre = [
  69. 'id' => 42,
  70. 'itemType' => 'file',
  71. 'itemSource' => 1,
  72. 'shareType' => IShare::TYPE_USER,
  73. 'shareWith' => 'awesomeUser',
  74. 'itemparent' => null,
  75. 'uidOwner' => 'sharedBy',
  76. 'fileSource' => 1,
  77. 'fileTarget' => 'myTarget',
  78. ];
  79. $hookListner
  80. ->expects($this->exactly(1))
  81. ->method('pre')
  82. ->with($hookListnerExpectsPre);
  83. $event = new GenericEvent($share);
  84. $this->eventDispatcher->dispatch('OCP\Share::preUnshare', $event);
  85. }
  86. public function testPostUnshare() {
  87. $path = $this->createMock(File::class);
  88. $path->method('getId')->willReturn(1);
  89. $info = $this->createMock(ICacheEntry::class);
  90. $info->method('getMimeType')->willReturn('text/plain');
  91. $share = $this->manager->newShare();
  92. $share->setId(42)
  93. ->setProviderId('prov')
  94. ->setShareType(IShare::TYPE_USER)
  95. ->setSharedWith('awesomeUser')
  96. ->setSharedBy('sharedBy')
  97. ->setNode($path)
  98. ->setTarget('myTarget')
  99. ->setNodeCacheEntry($info);
  100. $hookListner = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  101. \OCP\Util::connectHook('OCP\Share', 'post_unshare', $hookListner, 'post');
  102. $hookListnerExpectsPost = [
  103. 'id' => 42,
  104. 'itemType' => 'file',
  105. 'itemSource' => 1,
  106. 'shareType' => IShare::TYPE_USER,
  107. 'shareWith' => 'awesomeUser',
  108. 'itemparent' => null,
  109. 'uidOwner' => 'sharedBy',
  110. 'fileSource' => 1,
  111. 'fileTarget' => 'myTarget',
  112. 'deletedShares' => [
  113. [
  114. 'id' => 42,
  115. 'itemType' => 'file',
  116. 'itemSource' => 1,
  117. 'shareType' => IShare::TYPE_USER,
  118. 'shareWith' => 'awesomeUser',
  119. 'itemparent' => null,
  120. 'uidOwner' => 'sharedBy',
  121. 'fileSource' => 1,
  122. 'fileTarget' => 'myTarget',
  123. ],
  124. ],
  125. ];
  126. $hookListner
  127. ->expects($this->exactly(1))
  128. ->method('post')
  129. ->with($hookListnerExpectsPost);
  130. $event = new GenericEvent($share);
  131. $event->setArgument('deletedShares', [$share]);
  132. $this->eventDispatcher->dispatch('OCP\Share::postUnshare', $event);
  133. }
  134. public function testPostUnshareFromSelf() {
  135. $path = $this->createMock(File::class);
  136. $path->method('getId')->willReturn(1);
  137. $info = $this->createMock(ICacheEntry::class);
  138. $info->method('getMimeType')->willReturn('text/plain');
  139. $share = $this->manager->newShare();
  140. $share->setId(42)
  141. ->setProviderId('prov')
  142. ->setShareType(IShare::TYPE_USER)
  143. ->setSharedWith('awesomeUser')
  144. ->setSharedBy('sharedBy')
  145. ->setNode($path)
  146. ->setTarget('myTarget')
  147. ->setNodeCacheEntry($info);
  148. $hookListner = $this->getMockBuilder('Dummy')->setMethods(['postFromSelf'])->getMock();
  149. \OCP\Util::connectHook('OCP\Share', 'post_unshareFromSelf', $hookListner, 'postFromSelf');
  150. $hookListnerExpectsPostFromSelf = [
  151. 'id' => 42,
  152. 'itemType' => 'file',
  153. 'itemSource' => 1,
  154. 'shareType' => IShare::TYPE_USER,
  155. 'shareWith' => 'awesomeUser',
  156. 'itemparent' => null,
  157. 'uidOwner' => 'sharedBy',
  158. 'fileSource' => 1,
  159. 'fileTarget' => 'myTarget',
  160. 'itemTarget' => 'myTarget',
  161. 'unsharedItems' => [
  162. [
  163. 'id' => 42,
  164. 'itemType' => 'file',
  165. 'itemSource' => 1,
  166. 'shareType' => IShare::TYPE_USER,
  167. 'shareWith' => 'awesomeUser',
  168. 'itemparent' => null,
  169. 'uidOwner' => 'sharedBy',
  170. 'fileSource' => 1,
  171. 'fileTarget' => 'myTarget',
  172. 'itemTarget' => 'myTarget',
  173. ],
  174. ],
  175. ];
  176. $hookListner
  177. ->expects($this->exactly(1))
  178. ->method('postFromSelf')
  179. ->with($hookListnerExpectsPostFromSelf);
  180. $event = new GenericEvent($share);
  181. $this->eventDispatcher->dispatch('OCP\Share::postUnshareFromSelf', $event);
  182. }
  183. public function testPreShare() {
  184. $path = $this->createMock(File::class);
  185. $path->method('getId')->willReturn(1);
  186. $date = new \DateTime();
  187. $share = $this->manager->newShare();
  188. $share->setShareType(IShare::TYPE_LINK)
  189. ->setSharedWith('awesomeUser')
  190. ->setSharedBy('sharedBy')
  191. ->setNode($path)
  192. ->setTarget('myTarget')
  193. ->setPermissions(Constants::PERMISSION_ALL)
  194. ->setExpirationDate($date)
  195. ->setPassword('password')
  196. ->setToken('token');
  197. $hookListner = $this->getMockBuilder('Dummy')->setMethods(['preShare'])->getMock();
  198. \OCP\Util::connectHook('OCP\Share', 'pre_shared', $hookListner, 'preShare');
  199. $run = true;
  200. $error = '';
  201. $expected = [
  202. 'itemType' => 'file',
  203. 'itemSource' => 1,
  204. 'shareType' => IShare::TYPE_LINK,
  205. 'shareWith' => 'awesomeUser',
  206. 'uidOwner' => 'sharedBy',
  207. 'fileSource' => 1,
  208. 'itemTarget' => 'myTarget',
  209. 'permissions' => Constants::PERMISSION_ALL,
  210. 'expiration' => $date,
  211. 'token' => 'token',
  212. 'run' => &$run,
  213. 'error' => &$error,
  214. ];
  215. $hookListner
  216. ->expects($this->exactly(1))
  217. ->method('preShare')
  218. ->with($expected);
  219. $event = new GenericEvent($share);
  220. $this->eventDispatcher->dispatch('OCP\Share::preShare', $event);
  221. }
  222. public function testPreShareError() {
  223. $path = $this->createMock(File::class);
  224. $path->method('getId')->willReturn(1);
  225. $date = new \DateTime();
  226. $share = $this->manager->newShare();
  227. $share->setShareType(IShare::TYPE_LINK)
  228. ->setSharedWith('awesomeUser')
  229. ->setSharedBy('sharedBy')
  230. ->setNode($path)
  231. ->setTarget('myTarget')
  232. ->setPermissions(Constants::PERMISSION_ALL)
  233. ->setExpirationDate($date)
  234. ->setPassword('password')
  235. ->setToken('token');
  236. $hookListner = $this->getMockBuilder('Dummy')->setMethods(['preShare'])->getMock();
  237. \OCP\Util::connectHook('OCP\Share', 'pre_shared', $hookListner, 'preShare');
  238. $run = true;
  239. $error = '';
  240. $expected = [
  241. 'itemType' => 'file',
  242. 'itemSource' => 1,
  243. 'shareType' => IShare::TYPE_LINK,
  244. 'shareWith' => 'awesomeUser',
  245. 'uidOwner' => 'sharedBy',
  246. 'fileSource' => 1,
  247. 'itemTarget' => 'myTarget',
  248. 'permissions' => Constants::PERMISSION_ALL,
  249. 'expiration' => $date,
  250. 'token' => 'token',
  251. 'run' => &$run,
  252. 'error' => &$error,
  253. ];
  254. $hookListner
  255. ->expects($this->exactly(1))
  256. ->method('preShare')
  257. ->with($expected)
  258. ->willReturnCallback(function ($data) {
  259. $data['run'] = false;
  260. $data['error'] = 'I error';
  261. });
  262. $event = new GenericEvent($share);
  263. $this->eventDispatcher->dispatch('OCP\Share::preShare', $event);
  264. $this->assertTrue($event->isPropagationStopped());
  265. $this->assertSame('I error', $event->getArgument('error'));
  266. }
  267. public function testPostShare() {
  268. $path = $this->createMock(File::class);
  269. $path->method('getId')->willReturn(1);
  270. $date = new \DateTime();
  271. $share = $this->manager->newShare();
  272. $share->setId(42)
  273. ->setShareType(IShare::TYPE_LINK)
  274. ->setSharedWith('awesomeUser')
  275. ->setSharedBy('sharedBy')
  276. ->setNode($path)
  277. ->setTarget('myTarget')
  278. ->setPermissions(Constants::PERMISSION_ALL)
  279. ->setExpirationDate($date)
  280. ->setPassword('password')
  281. ->setToken('token');
  282. $hookListner = $this->getMockBuilder('Dummy')->setMethods(['postShare'])->getMock();
  283. \OCP\Util::connectHook('OCP\Share', 'post_shared', $hookListner, 'postShare');
  284. $expected = [
  285. 'id' => 42,
  286. 'itemType' => 'file',
  287. 'itemSource' => 1,
  288. 'shareType' => IShare::TYPE_LINK,
  289. 'shareWith' => 'awesomeUser',
  290. 'uidOwner' => 'sharedBy',
  291. 'fileSource' => 1,
  292. 'itemTarget' => 'myTarget',
  293. 'fileTarget' => 'myTarget',
  294. 'permissions' => Constants::PERMISSION_ALL,
  295. 'expiration' => $date,
  296. 'token' => 'token',
  297. 'path' => null,
  298. ];
  299. $hookListner
  300. ->expects($this->exactly(1))
  301. ->method('postShare')
  302. ->with($expected);
  303. $event = new GenericEvent($share);
  304. $this->eventDispatcher->dispatch('OCP\Share::postShare', $event);
  305. }
  306. }