LegacyHooksTest.php 9.8 KB

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