LegacyHooksTest.php 9.8 KB

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