node.php 10 KB

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