LegacyHooksTest.php 9.5 KB

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