HookConnectorTest.php 11 KB

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