LegacyHooksTest.php 9.1 KB

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