1
0

HookConnectorTest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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\Root;
  11. use OC\Files\Storage\Temporary;
  12. use OC\Files\View;
  13. use OCP\Files\Node;
  14. use OCP\ILogger;
  15. use OCP\IUserManager;
  16. use Test\TestCase;
  17. use Test\Traits\MountProviderTrait;
  18. use Test\Traits\UserTrait;
  19. /**
  20. * Class HookConnectorTest
  21. *
  22. * @group DB
  23. *
  24. * @package Test\Files\Node
  25. */
  26. class HookConnectorTest extends TestCase {
  27. use UserTrait;
  28. use MountProviderTrait;
  29. /**
  30. * @var View
  31. */
  32. private $view;
  33. /**
  34. * @var Root
  35. */
  36. private $root;
  37. /**
  38. * @var string
  39. */
  40. private $userId;
  41. public function setUp() {
  42. parent::setUp();
  43. $this->userId = $this->getUniqueID();
  44. $this->createUser($this->userId, 'pass');
  45. $this->registerMount($this->userId, new Temporary(), '/' . $this->userId . '/files/');
  46. \OC_Util::setupFS($this->userId);
  47. $this->view = new View();
  48. $this->root = new Root(
  49. Filesystem::getMountManager(),
  50. $this->view,
  51. \OC::$server->getUserManager()->get($this->userId),
  52. \OC::$server->getUserMountCache(),
  53. $this->createMock(ILogger::class),
  54. $this->createMock(IUserManager::class)
  55. );
  56. }
  57. public function tearDown() {
  58. parent::tearDown();
  59. \OC_Hook::clear('OC_Filesystem');
  60. \OC_Util::tearDownFS();
  61. }
  62. public function viewToNodeProvider() {
  63. return [
  64. [function () {
  65. Filesystem::file_put_contents('test.txt', 'asd');
  66. }, 'preWrite'],
  67. [function () {
  68. Filesystem::file_put_contents('test.txt', 'asd');
  69. }, 'postWrite'],
  70. [function () {
  71. Filesystem::file_put_contents('test.txt', 'asd');
  72. }, 'preCreate'],
  73. [function () {
  74. Filesystem::file_put_contents('test.txt', 'asd');
  75. }, 'postCreate'],
  76. [function () {
  77. Filesystem::mkdir('test.txt');
  78. }, 'preCreate'],
  79. [function () {
  80. Filesystem::mkdir('test.txt');
  81. }, 'postCreate'],
  82. [function () {
  83. Filesystem::touch('test.txt');
  84. }, 'preTouch'],
  85. [function () {
  86. Filesystem::touch('test.txt');
  87. }, 'postTouch'],
  88. [function () {
  89. Filesystem::touch('test.txt');
  90. }, 'preCreate'],
  91. [function () {
  92. Filesystem::touch('test.txt');
  93. }, 'postCreate'],
  94. [function () {
  95. Filesystem::file_put_contents('test.txt', 'asd');
  96. Filesystem::unlink('test.txt');
  97. }, 'preDelete'],
  98. [function () {
  99. Filesystem::file_put_contents('test.txt', 'asd');
  100. Filesystem::unlink('test.txt');
  101. }, 'postDelete'],
  102. [function () {
  103. Filesystem::mkdir('test.txt');
  104. Filesystem::rmdir('test.txt');
  105. }, 'preDelete'],
  106. [function () {
  107. Filesystem::mkdir('test.txt');
  108. Filesystem::rmdir('test.txt');
  109. }, 'postDelete'],
  110. ];
  111. }
  112. /**
  113. * @param callable $operation
  114. * @param string $expectedHook
  115. * @dataProvider viewToNodeProvider
  116. */
  117. public function testViewToNode(callable $operation, $expectedHook) {
  118. $connector = new \OC\Files\Node\HookConnector($this->root, $this->view);
  119. $connector->viewToNode();
  120. $hookCalled = false;
  121. /** @var Node $hookNode */
  122. $hookNode = null;
  123. $this->root->listen('\OC\Files', $expectedHook, function ($node) use (&$hookNode, &$hookCalled) {
  124. $hookCalled = true;
  125. $hookNode = $node;
  126. });
  127. $operation();
  128. $this->assertTrue($hookCalled);
  129. $this->assertEquals('/' . $this->userId . '/files/test.txt', $hookNode->getPath());
  130. }
  131. public function viewToNodeProviderCopyRename() {
  132. return [
  133. [function () {
  134. Filesystem::file_put_contents('source', 'asd');
  135. Filesystem::rename('source', 'target');
  136. }, 'preRename'],
  137. [function () {
  138. Filesystem::file_put_contents('source', 'asd');
  139. Filesystem::rename('source', 'target');
  140. }, 'postRename'],
  141. [function () {
  142. Filesystem::file_put_contents('source', 'asd');
  143. Filesystem::copy('source', 'target');
  144. }, 'preCopy'],
  145. [function () {
  146. Filesystem::file_put_contents('source', 'asd');
  147. Filesystem::copy('source', 'target');
  148. }, 'postCopy'],
  149. ];
  150. }
  151. /**
  152. * @param callable $operation
  153. * @param string $expectedHook
  154. * @dataProvider viewToNodeProviderCopyRename
  155. */
  156. public function testViewToNodeCopyRename(callable $operation, $expectedHook) {
  157. $connector = new \OC\Files\Node\HookConnector($this->root, $this->view);
  158. $connector->viewToNode();
  159. $hookCalled = false;
  160. /** @var Node $hookSourceNode */
  161. $hookSourceNode = null;
  162. /** @var Node $hookTargetNode */
  163. $hookTargetNode = null;
  164. $this->root->listen('\OC\Files', $expectedHook, function ($sourceNode, $targetNode) use (&$hookCalled, &$hookSourceNode, &$hookTargetNode) {
  165. $hookCalled = true;
  166. $hookSourceNode = $sourceNode;
  167. $hookTargetNode = $targetNode;
  168. });
  169. $operation();
  170. $this->assertTrue($hookCalled);
  171. $this->assertEquals('/' . $this->userId . '/files/source', $hookSourceNode->getPath());
  172. $this->assertEquals('/' . $this->userId . '/files/target', $hookTargetNode->getPath());
  173. }
  174. public function testPostDeleteMeta() {
  175. $connector = new \OC\Files\Node\HookConnector($this->root, $this->view);
  176. $connector->viewToNode();
  177. $hookCalled = false;
  178. /** @var Node $hookNode */
  179. $hookNode = null;
  180. $this->root->listen('\OC\Files', 'postDelete', function ($node) use (&$hookNode, &$hookCalled) {
  181. $hookCalled = true;
  182. $hookNode = $node;
  183. });
  184. Filesystem::file_put_contents('test.txt', 'asd');
  185. $info = Filesystem::getFileInfo('test.txt');
  186. Filesystem::unlink('test.txt');
  187. $this->assertTrue($hookCalled);
  188. $this->assertEquals($hookNode->getId(), $info->getId());
  189. }
  190. }