1
0

HookConnectorTest.php 11 KB

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