HookConnectorTest.php 11 KB

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