HookConnectorTest.php 11 KB

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