1
0

HookConnectorTest.php 11 KB

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