node.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. <?php
  2. /**
  3. * Copyright (c) 2013 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. class Node extends \PHPUnit_Framework_TestCase {
  10. private $user;
  11. public function setUp() {
  12. $this->user = new \OC\User\User('', new \OC_User_Dummy);
  13. }
  14. public function testStat() {
  15. $manager = $this->getMock('\OC\Files\Mount\Manager');
  16. /**
  17. * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
  18. */
  19. $view = $this->getMock('\OC\Files\View');
  20. $root = $this->getMock('\OC\Files\Node\Root', array(), array($manager, $view, $this->user));
  21. $root->expects($this->any())
  22. ->method('getUser')
  23. ->will($this->returnValue($this->user));
  24. $stat = array(
  25. 'fileid' => 1,
  26. 'size' => 100,
  27. 'etag' => 'qwerty',
  28. 'mtime' => 50,
  29. 'permissions' => 0
  30. );
  31. $view->expects($this->once())
  32. ->method('stat')
  33. ->with('/bar/foo')
  34. ->will($this->returnValue($stat));
  35. $node = new \OC\Files\Node\File($root, $view, '/bar/foo');
  36. $this->assertEquals($stat, $node->stat());
  37. }
  38. public function testGetId() {
  39. $manager = $this->getMock('\OC\Files\Mount\Manager');
  40. /**
  41. * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
  42. */
  43. $view = $this->getMock('\OC\Files\View');
  44. $root = $this->getMock('\OC\Files\Node\Root', array(), array($manager, $view, $this->user));
  45. $root->expects($this->any())
  46. ->method('getUser')
  47. ->will($this->returnValue($this->user));
  48. $stat = array(
  49. 'fileid' => 1,
  50. 'size' => 100,
  51. 'etag' => 'qwerty',
  52. 'mtime' => 50
  53. );
  54. $view->expects($this->once())
  55. ->method('getFileInfo')
  56. ->with('/bar/foo')
  57. ->will($this->returnValue($stat));
  58. $node = new \OC\Files\Node\File($root, $view, '/bar/foo');
  59. $this->assertEquals(1, $node->getId());
  60. }
  61. public function testGetSize() {
  62. $manager = $this->getMock('\OC\Files\Mount\Manager');
  63. /**
  64. * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
  65. */
  66. $view = $this->getMock('\OC\Files\View');
  67. $root = $this->getMock('\OC\Files\Node\Root', array(), array($manager, $view, $this->user));
  68. $root->expects($this->any())
  69. ->method('getUser')
  70. ->will($this->returnValue($this->user));
  71. $view->expects($this->once())
  72. ->method('filesize')
  73. ->with('/bar/foo')
  74. ->will($this->returnValue(100));
  75. $node = new \OC\Files\Node\File($root, $view, '/bar/foo');
  76. $this->assertEquals(100, $node->getSize());
  77. }
  78. public function testGetEtag() {
  79. $manager = $this->getMock('\OC\Files\Mount\Manager');
  80. /**
  81. * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
  82. */
  83. $view = $this->getMock('\OC\Files\View');
  84. $root = $this->getMock('\OC\Files\Node\Root', array(), array($manager, $view, $this->user));
  85. $root->expects($this->any())
  86. ->method('getUser')
  87. ->will($this->returnValue($this->user));
  88. $stat = array(
  89. 'fileid' => 1,
  90. 'size' => 100,
  91. 'etag' => 'qwerty',
  92. 'mtime' => 50
  93. );
  94. $view->expects($this->once())
  95. ->method('getFileInfo')
  96. ->with('/bar/foo')
  97. ->will($this->returnValue($stat));
  98. $node = new \OC\Files\Node\File($root, $view, '/bar/foo');
  99. $this->assertEquals('qwerty', $node->getEtag());
  100. }
  101. public function testGetMTime() {
  102. $manager = $this->getMock('\OC\Files\Mount\Manager');
  103. /**
  104. * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
  105. */
  106. $view = $this->getMock('\OC\Files\View');
  107. $root = $this->getMock('\OC\Files\Node\Root', array(), array($manager, $view, $this->user));
  108. $root->expects($this->any())
  109. ->method('getUser')
  110. ->will($this->returnValue($this->user));
  111. /**
  112. * @var \OC\Files\Storage\Storage | \PHPUnit_Framework_MockObject_MockObject $storage
  113. */
  114. $storage = $this->getMock('\OC\Files\Storage\Storage');
  115. $view->expects($this->once())
  116. ->method('filemtime')
  117. ->with('/bar/foo')
  118. ->will($this->returnValue(50));
  119. $node = new \OC\Files\Node\File($root, $view, '/bar/foo');
  120. $this->assertEquals(50, $node->getMTime());
  121. }
  122. public function testGetStorage() {
  123. $manager = $this->getMock('\OC\Files\Mount\Manager');
  124. /**
  125. * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
  126. */
  127. $view = $this->getMock('\OC\Files\View');
  128. $root = $this->getMock('\OC\Files\Node\Root', array(), array($manager, $view, $this->user));
  129. $root->expects($this->any())
  130. ->method('getUser')
  131. ->will($this->returnValue($this->user));
  132. /**
  133. * @var \OC\Files\Storage\Storage | \PHPUnit_Framework_MockObject_MockObject $storage
  134. */
  135. $storage = $this->getMock('\OC\Files\Storage\Storage');
  136. $view->expects($this->once())
  137. ->method('resolvePath')
  138. ->with('/bar/foo')
  139. ->will($this->returnValue(array($storage, 'foo')));
  140. $node = new \OC\Files\Node\File($root, $view, '/bar/foo');
  141. $this->assertEquals($storage, $node->getStorage());
  142. }
  143. public function testGetPath() {
  144. $manager = $this->getMock('\OC\Files\Mount\Manager');
  145. /**
  146. * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
  147. */
  148. $view = $this->getMock('\OC\Files\View');
  149. $root = $this->getMock('\OC\Files\Node\Root', array(), array($manager, $view, $this->user));
  150. $root->expects($this->any())
  151. ->method('getUser')
  152. ->will($this->returnValue($this->user));
  153. $node = new \OC\Files\Node\File($root, $view, '/bar/foo');
  154. $this->assertEquals('/bar/foo', $node->getPath());
  155. }
  156. public function testGetInternalPath() {
  157. $manager = $this->getMock('\OC\Files\Mount\Manager');
  158. /**
  159. * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
  160. */
  161. $view = $this->getMock('\OC\Files\View');
  162. $root = $this->getMock('\OC\Files\Node\Root', array(), array($manager, $view, $this->user));
  163. $root->expects($this->any())
  164. ->method('getUser')
  165. ->will($this->returnValue($this->user));
  166. /**
  167. * @var \OC\Files\Storage\Storage | \PHPUnit_Framework_MockObject_MockObject $storage
  168. */
  169. $storage = $this->getMock('\OC\Files\Storage\Storage');
  170. $view->expects($this->once())
  171. ->method('resolvePath')
  172. ->with('/bar/foo')
  173. ->will($this->returnValue(array($storage, 'foo')));
  174. $node = new \OC\Files\Node\File($root, $view, '/bar/foo');
  175. $this->assertEquals('foo', $node->getInternalPath());
  176. }
  177. public function testGetName() {
  178. $manager = $this->getMock('\OC\Files\Mount\Manager');
  179. /**
  180. * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
  181. */
  182. $view = $this->getMock('\OC\Files\View');
  183. $root = $this->getMock('\OC\Files\Node\Root', array(), array($manager, $view, $this->user));
  184. $root->expects($this->any())
  185. ->method('getUser')
  186. ->will($this->returnValue($this->user));
  187. $node = new \OC\Files\Node\File($root, $view, '/bar/foo');
  188. $this->assertEquals('foo', $node->getName());
  189. }
  190. public function testTouchSetMTime() {
  191. $manager = $this->getMock('\OC\Files\Mount\Manager');
  192. /**
  193. * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
  194. */
  195. $view = $this->getMock('\OC\Files\View');
  196. $root = $this->getMock('\OC\Files\Node\Root', array(), array($manager, $view, $this->user));
  197. $root->expects($this->any())
  198. ->method('getUser')
  199. ->will($this->returnValue($this->user));
  200. $view->expects($this->once())
  201. ->method('touch')
  202. ->with('/bar/foo', 100)
  203. ->will($this->returnValue(true));
  204. $view->expects($this->once())
  205. ->method('filemtime')
  206. ->with('/bar/foo')
  207. ->will($this->returnValue(100));
  208. $view->expects($this->once())
  209. ->method('getFileInfo')
  210. ->with('/bar/foo')
  211. ->will($this->returnValue(array('permissions' => \OCP\PERMISSION_ALL)));
  212. $node = new \OC\Files\Node\Node($root, $view, '/bar/foo');
  213. $node->touch(100);
  214. $this->assertEquals(100, $node->getMTime());
  215. }
  216. public function testTouchHooks() {
  217. $test = $this;
  218. $hooksRun = 0;
  219. /**
  220. * @param \OC\Files\Node\File $node
  221. */
  222. $preListener = function ($node) use (&$test, &$hooksRun) {
  223. $test->assertEquals('foo', $node->getInternalPath());
  224. $test->assertEquals('/bar/foo', $node->getPath());
  225. $hooksRun++;
  226. };
  227. /**
  228. * @param \OC\Files\Node\File $node
  229. */
  230. $postListener = function ($node) use (&$test, &$hooksRun) {
  231. $test->assertEquals('foo', $node->getInternalPath());
  232. $test->assertEquals('/bar/foo', $node->getPath());
  233. $hooksRun++;
  234. };
  235. /**
  236. * @var \OC\Files\Mount\Manager $manager
  237. */
  238. $manager = $this->getMock('\OC\Files\Mount\Manager');
  239. /**
  240. * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
  241. */
  242. $view = $this->getMock('\OC\Files\View');
  243. $root = new \OC\Files\Node\Root($manager, $view, $this->user);
  244. $root->listen('\OC\Files', 'preTouch', $preListener);
  245. $root->listen('\OC\Files', 'postTouch', $postListener);
  246. $view->expects($this->once())
  247. ->method('touch')
  248. ->with('/bar/foo', 100)
  249. ->will($this->returnValue(true));
  250. $view->expects($this->any())
  251. ->method('resolvePath')
  252. ->with('/bar/foo')
  253. ->will($this->returnValue(array(null, 'foo')));
  254. $view->expects($this->any())
  255. ->method('getFileInfo')
  256. ->with('/bar/foo')
  257. ->will($this->returnValue(array('permissions' => \OCP\PERMISSION_ALL)));
  258. $node = new \OC\Files\Node\Node($root, $view, '/bar/foo');
  259. $node->touch(100);
  260. $this->assertEquals(2, $hooksRun);
  261. }
  262. /**
  263. * @expectedException \OCP\Files\NotPermittedException
  264. */
  265. public function testTouchNotPermitted() {
  266. $manager = $this->getMock('\OC\Files\Mount\Manager');
  267. /**
  268. * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
  269. */
  270. $view = $this->getMock('\OC\Files\View');
  271. $root = $this->getMock('\OC\Files\Node\Root', array(), array($manager, $view, $this->user));
  272. $root->expects($this->any())
  273. ->method('getUser')
  274. ->will($this->returnValue($this->user));
  275. $view->expects($this->any())
  276. ->method('getFileInfo')
  277. ->with('/bar/foo')
  278. ->will($this->returnValue(array('permissions' => \OCP\PERMISSION_READ)));
  279. $node = new \OC\Files\Node\Node($root, $view, '/bar/foo');
  280. $node->touch(100);
  281. }
  282. }