1
0

HookConnectorTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. <?php
  2. /**
  3. * Copyright (c) 2015 Robin Appelman <icewind@owncloud.com>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. namespace Test\Files\Node;
  9. use OC\Files\Filesystem;
  10. use OC\Files\Node\HookConnector;
  11. use OC\Files\Node\Root;
  12. use OC\Files\Storage\Temporary;
  13. use OC\Files\View;
  14. use OCP\EventDispatcher\GenericEvent as APIGenericEvent;
  15. use OCP\EventDispatcher\IEventDispatcher;
  16. use OCP\Files\Events\Node\AbstractNodeEvent;
  17. use OCP\Files\Events\Node\AbstractNodesEvent;
  18. use OCP\Files\Events\Node\BeforeNodeCopiedEvent;
  19. use OCP\Files\Events\Node\BeforeNodeCreatedEvent;
  20. use OCP\Files\Events\Node\BeforeNodeDeletedEvent;
  21. use OCP\Files\Events\Node\BeforeNodeRenamedEvent;
  22. use OCP\Files\Events\Node\BeforeNodeTouchedEvent;
  23. use OCP\Files\Events\Node\BeforeNodeWrittenEvent;
  24. use OCP\Files\Events\Node\NodeCopiedEvent;
  25. use OCP\Files\Events\Node\NodeCreatedEvent;
  26. use OCP\Files\Events\Node\NodeDeletedEvent;
  27. use OCP\Files\Events\Node\NodeRenamedEvent;
  28. use OCP\Files\Events\Node\NodeTouchedEvent;
  29. use OCP\Files\Events\Node\NodeWrittenEvent;
  30. use OCP\Files\Node;
  31. use OCP\IUserManager;
  32. use Psr\Log\LoggerInterface;
  33. use Symfony\Component\EventDispatcher\GenericEvent;
  34. use Test\TestCase;
  35. use Test\Traits\MountProviderTrait;
  36. use Test\Traits\UserTrait;
  37. /**
  38. * Class HookConnectorTest
  39. *
  40. * @group DB
  41. *
  42. * @package Test\Files\Node
  43. */
  44. class HookConnectorTest extends TestCase {
  45. use UserTrait;
  46. use MountProviderTrait;
  47. /** @var IEventDispatcher */
  48. protected $eventDispatcher;
  49. /** @var View */
  50. private $view;
  51. /** @var Root */
  52. private $root;
  53. /** @var string */
  54. private $userId;
  55. protected function setUp(): void {
  56. parent::setUp();
  57. $this->userId = $this->getUniqueID();
  58. $this->createUser($this->userId, 'pass');
  59. // this will setup the FS
  60. $this->loginAsUser($this->userId);
  61. $this->registerMount($this->userId, new Temporary(), '/' . $this->userId . '/files/');
  62. $this->view = new View();
  63. $this->root = new Root(
  64. Filesystem::getMountManager(),
  65. $this->view,
  66. \OC::$server->getUserManager()->get($this->userId),
  67. \OC::$server->getUserMountCache(),
  68. $this->createMock(LoggerInterface::class),
  69. $this->createMock(IUserManager::class),
  70. $this->createMock(IEventDispatcher::class)
  71. );
  72. $this->eventDispatcher = \OC::$server->query(IEventDispatcher::class);
  73. }
  74. protected function tearDown(): void {
  75. parent::tearDown();
  76. \OC_Hook::clear('OC_Filesystem');
  77. \OC_Util::tearDownFS();
  78. }
  79. public function viewToNodeProvider() {
  80. return [
  81. [function () {
  82. Filesystem::file_put_contents('test.txt', 'asd');
  83. }, 'preWrite', '\OCP\Files::preWrite', BeforeNodeWrittenEvent::class],
  84. [function () {
  85. Filesystem::file_put_contents('test.txt', 'asd');
  86. }, 'postWrite', '\OCP\Files::postWrite', NodeWrittenEvent::class],
  87. [function () {
  88. Filesystem::file_put_contents('test.txt', 'asd');
  89. }, 'preCreate', '\OCP\Files::preCreate', BeforeNodeCreatedEvent::class],
  90. [function () {
  91. Filesystem::file_put_contents('test.txt', 'asd');
  92. }, 'postCreate', '\OCP\Files::postCreate', NodeCreatedEvent::class],
  93. [function () {
  94. Filesystem::mkdir('test.txt');
  95. }, 'preCreate', '\OCP\Files::preCreate', BeforeNodeCreatedEvent::class],
  96. [function () {
  97. Filesystem::mkdir('test.txt');
  98. }, 'postCreate', '\OCP\Files::postCreate', NodeCreatedEvent::class],
  99. [function () {
  100. Filesystem::touch('test.txt');
  101. }, 'preTouch', '\OCP\Files::preTouch', BeforeNodeTouchedEvent::class],
  102. [function () {
  103. Filesystem::touch('test.txt');
  104. }, 'postTouch', '\OCP\Files::postTouch', NodeTouchedEvent::class],
  105. [function () {
  106. Filesystem::touch('test.txt');
  107. }, 'preCreate', '\OCP\Files::preCreate', BeforeNodeCreatedEvent::class],
  108. [function () {
  109. Filesystem::touch('test.txt');
  110. }, 'postCreate', '\OCP\Files::postCreate', NodeCreatedEvent::class],
  111. [function () {
  112. Filesystem::file_put_contents('test.txt', 'asd');
  113. Filesystem::unlink('test.txt');
  114. }, 'preDelete', '\OCP\Files::preDelete', BeforeNodeDeletedEvent::class],
  115. [function () {
  116. Filesystem::file_put_contents('test.txt', 'asd');
  117. Filesystem::unlink('test.txt');
  118. }, 'postDelete', '\OCP\Files::postDelete', NodeDeletedEvent::class],
  119. [function () {
  120. Filesystem::mkdir('test.txt');
  121. Filesystem::rmdir('test.txt');
  122. }, 'preDelete', '\OCP\Files::preDelete', BeforeNodeDeletedEvent::class],
  123. [function () {
  124. Filesystem::mkdir('test.txt');
  125. Filesystem::rmdir('test.txt');
  126. }, 'postDelete', '\OCP\Files::postDelete', NodeDeletedEvent::class],
  127. ];
  128. }
  129. /**
  130. * @param callable $operation
  131. * @param string $expectedHook
  132. * @dataProvider viewToNodeProvider
  133. */
  134. public function testViewToNode(callable $operation, $expectedHook, $expectedLegacyEvent, $expectedEvent) {
  135. $connector = new HookConnector($this->root, $this->view, $this->eventDispatcher);
  136. $connector->viewToNode();
  137. $hookCalled = false;
  138. /** @var Node $hookNode */
  139. $hookNode = null;
  140. $this->root->listen('\OC\Files', $expectedHook, function ($node) use (&$hookNode, &$hookCalled) {
  141. $hookCalled = true;
  142. $hookNode = $node;
  143. });
  144. $dispatcherCalled = false;
  145. /** @var Node $dispatcherNode */
  146. $dispatcherNode = null;
  147. $this->eventDispatcher->addListener($expectedLegacyEvent, function ($event) use (&$dispatcherCalled, &$dispatcherNode) {
  148. /** @var GenericEvent|APIGenericEvent $event */
  149. $dispatcherCalled = true;
  150. $dispatcherNode = $event->getSubject();
  151. });
  152. $newDispatcherCalled = false;
  153. $newDispatcherNode = null;
  154. $this->eventDispatcher->addListener($expectedEvent, function ($event) use ($expectedEvent, &$newDispatcherCalled, &$newDispatcherNode) {
  155. if ($event instanceof $expectedEvent) {
  156. /** @var AbstractNodeEvent $event */
  157. $newDispatcherCalled = true;
  158. $newDispatcherNode = $event->getNode();
  159. }
  160. });
  161. $operation();
  162. $this->assertTrue($hookCalled);
  163. $this->assertEquals('/' . $this->userId . '/files/test.txt', $hookNode->getPath());
  164. $this->assertTrue($dispatcherCalled);
  165. $this->assertEquals('/' . $this->userId . '/files/test.txt', $dispatcherNode->getPath());
  166. $this->assertTrue($newDispatcherCalled);
  167. $this->assertEquals('/' . $this->userId . '/files/test.txt', $newDispatcherNode->getPath());
  168. }
  169. public function viewToNodeProviderCopyRename() {
  170. return [
  171. [function () {
  172. Filesystem::file_put_contents('source', 'asd');
  173. Filesystem::rename('source', 'target');
  174. }, 'preRename', '\OCP\Files::preRename', BeforeNodeRenamedEvent::class],
  175. [function () {
  176. Filesystem::file_put_contents('source', 'asd');
  177. Filesystem::rename('source', 'target');
  178. }, 'postRename', '\OCP\Files::postRename', NodeRenamedEvent::class],
  179. [function () {
  180. Filesystem::file_put_contents('source', 'asd');
  181. Filesystem::copy('source', 'target');
  182. }, 'preCopy', '\OCP\Files::preCopy', BeforeNodeCopiedEvent::class],
  183. [function () {
  184. Filesystem::file_put_contents('source', 'asd');
  185. Filesystem::copy('source', 'target');
  186. }, 'postCopy', '\OCP\Files::postCopy', NodeCopiedEvent::class],
  187. ];
  188. }
  189. /**
  190. * @param callable $operation
  191. * @param string $expectedHook
  192. * @dataProvider viewToNodeProviderCopyRename
  193. */
  194. public function testViewToNodeCopyRename(callable $operation, $expectedHook, $expectedLegacyEvent, $expectedEvent) {
  195. $connector = new HookConnector($this->root, $this->view, $this->eventDispatcher);
  196. $connector->viewToNode();
  197. $hookCalled = false;
  198. /** @var Node $hookSourceNode */
  199. $hookSourceNode = null;
  200. /** @var Node $hookTargetNode */
  201. $hookTargetNode = null;
  202. $this->root->listen('\OC\Files', $expectedHook, function ($sourceNode, $targetNode) use (&$hookCalled, &$hookSourceNode, &$hookTargetNode) {
  203. $hookCalled = true;
  204. $hookSourceNode = $sourceNode;
  205. $hookTargetNode = $targetNode;
  206. });
  207. $dispatcherCalled = false;
  208. /** @var Node $dispatcherSourceNode */
  209. $dispatcherSourceNode = null;
  210. /** @var Node $dispatcherTargetNode */
  211. $dispatcherTargetNode = null;
  212. $this->eventDispatcher->addListener($expectedLegacyEvent, function ($event) use (&$dispatcherSourceNode, &$dispatcherTargetNode, &$dispatcherCalled) {
  213. /** @var GenericEvent|APIGenericEvent $event */
  214. $dispatcherCalled = true;
  215. [$dispatcherSourceNode, $dispatcherTargetNode] = $event->getSubject();
  216. });
  217. $newDispatcherCalled = false;
  218. /** @var Node $dispatcherSourceNode */
  219. $newDispatcherSourceNode = null;
  220. /** @var Node $dispatcherTargetNode */
  221. $newDispatcherTargetNode = null;
  222. $this->eventDispatcher->addListener($expectedEvent, function ($event) use ($expectedEvent, &$newDispatcherSourceNode, &$newDispatcherTargetNode, &$newDispatcherCalled) {
  223. if ($event instanceof $expectedEvent) {
  224. /** @var AbstractNodesEvent$event */
  225. $newDispatcherCalled = true;
  226. $newDispatcherSourceNode = $event->getSource();
  227. $newDispatcherTargetNode = $event->getTarget();
  228. }
  229. });
  230. $operation();
  231. $this->assertTrue($hookCalled);
  232. $this->assertEquals('/' . $this->userId . '/files/source', $hookSourceNode->getPath());
  233. $this->assertEquals('/' . $this->userId . '/files/target', $hookTargetNode->getPath());
  234. $this->assertTrue($dispatcherCalled);
  235. $this->assertEquals('/' . $this->userId . '/files/source', $dispatcherSourceNode->getPath());
  236. $this->assertEquals('/' . $this->userId . '/files/target', $dispatcherTargetNode->getPath());
  237. $this->assertTrue($newDispatcherCalled);
  238. $this->assertEquals('/' . $this->userId . '/files/source', $newDispatcherSourceNode->getPath());
  239. $this->assertEquals('/' . $this->userId . '/files/target', $newDispatcherTargetNode->getPath());
  240. }
  241. public function testPostDeleteMeta() {
  242. $connector = new HookConnector($this->root, $this->view, $this->eventDispatcher);
  243. $connector->viewToNode();
  244. $hookCalled = false;
  245. /** @var Node $hookNode */
  246. $hookNode = null;
  247. $this->root->listen('\OC\Files', 'postDelete', function ($node) use (&$hookNode, &$hookCalled) {
  248. $hookCalled = true;
  249. $hookNode = $node;
  250. });
  251. $dispatcherCalled = false;
  252. /** @var Node $dispatcherNode */
  253. $dispatcherNode = null;
  254. $this->eventDispatcher->addListener('\OCP\Files::postDelete', function ($event) use (&$dispatcherCalled, &$dispatcherNode) {
  255. /** @var GenericEvent|APIGenericEvent $event */
  256. $dispatcherCalled = true;
  257. $dispatcherNode = $event->getSubject();
  258. });
  259. $newDispatcherCalled = false;
  260. /** @var Node $dispatcherNode */
  261. $newDispatcherNode = null;
  262. $this->eventDispatcher->addListener(NodeDeletedEvent::class, function ($event) use (&$newDispatcherCalled, &$newDispatcherNode) {
  263. if ($event instanceof NodeDeletedEvent) {
  264. /** @var AbstractNodeEvent $event */
  265. $newDispatcherCalled = true;
  266. $newDispatcherNode = $event->getNode();
  267. }
  268. });
  269. Filesystem::file_put_contents('test.txt', 'asd');
  270. $info = Filesystem::getFileInfo('test.txt');
  271. Filesystem::unlink('test.txt');
  272. $this->assertTrue($hookCalled);
  273. $this->assertEquals($hookNode->getId(), $info->getId());
  274. $this->assertTrue($dispatcherCalled);
  275. $this->assertEquals($dispatcherNode->getId(), $info->getId());
  276. $this->assertTrue($newDispatcherCalled);
  277. $this->assertEquals($newDispatcherNode->getId(), $info->getId());
  278. }
  279. }