folder.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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\Cache\Cache;
  10. use OC\Files\Node\Node;
  11. use OCP\Files\NotFoundException;
  12. use OCP\Files\NotPermittedException;
  13. use OC\Files\View;
  14. class Folder extends \PHPUnit_Framework_TestCase {
  15. private $user;
  16. public function setUp() {
  17. $this->user = new \OC\User\User('', new \OC_User_Dummy);
  18. }
  19. public function testDelete() {
  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. $root->expects($this->exactly(2))
  30. ->method('emit')
  31. ->will($this->returnValue(true));
  32. $view->expects($this->any())
  33. ->method('getFileInfo')
  34. ->will($this->returnValue(array('permissions' => \OCP\PERMISSION_ALL)));
  35. $view->expects($this->once())
  36. ->method('rmdir')
  37. ->with('/bar/foo')
  38. ->will($this->returnValue(true));
  39. $node = new \OC\Files\Node\Folder($root, $view, '/bar/foo');
  40. $node->delete();
  41. }
  42. public function testDeleteHooks() {
  43. $test = $this;
  44. $hooksRun = 0;
  45. /**
  46. * @param \OC\Files\Node\File $node
  47. */
  48. $preListener = function ($node) use (&$test, &$hooksRun) {
  49. $test->assertInstanceOf('\OC\Files\Node\Folder', $node);
  50. $test->assertEquals('foo', $node->getInternalPath());
  51. $test->assertEquals('/bar/foo', $node->getPath());
  52. $hooksRun++;
  53. };
  54. /**
  55. * @param \OC\Files\Node\File $node
  56. */
  57. $postListener = function ($node) use (&$test, &$hooksRun) {
  58. $test->assertInstanceOf('\OC\Files\Node\NonExistingFolder', $node);
  59. $test->assertEquals('foo', $node->getInternalPath());
  60. $test->assertEquals('/bar/foo', $node->getPath());
  61. $hooksRun++;
  62. };
  63. /**
  64. * @var \OC\Files\Mount\Manager $manager
  65. */
  66. $manager = $this->getMock('\OC\Files\Mount\Manager');
  67. /**
  68. * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
  69. */
  70. $view = $this->getMock('\OC\Files\View');
  71. $root = new \OC\Files\Node\Root($manager, $view, $this->user);
  72. $root->listen('\OC\Files', 'preDelete', $preListener);
  73. $root->listen('\OC\Files', 'postDelete', $postListener);
  74. $view->expects($this->any())
  75. ->method('getFileInfo')
  76. ->will($this->returnValue(array('permissions' => \OCP\PERMISSION_ALL, 'fileid' => 1)));
  77. $view->expects($this->once())
  78. ->method('rmdir')
  79. ->with('/bar/foo')
  80. ->will($this->returnValue(true));
  81. $view->expects($this->any())
  82. ->method('resolvePath')
  83. ->with('/bar/foo')
  84. ->will($this->returnValue(array(null, 'foo')));
  85. $node = new \OC\Files\Node\Folder($root, $view, '/bar/foo');
  86. $node->delete();
  87. $this->assertEquals(2, $hooksRun);
  88. }
  89. /**
  90. * @expectedException \OCP\Files\NotPermittedException
  91. */
  92. public function testDeleteNotPermitted() {
  93. $manager = $this->getMock('\OC\Files\Mount\Manager');
  94. /**
  95. * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
  96. */
  97. $view = $this->getMock('\OC\Files\View');
  98. $root = $this->getMock('\OC\Files\Node\Root', array(), array($manager, $view, $this->user));
  99. $root->expects($this->any())
  100. ->method('getUser')
  101. ->will($this->returnValue($this->user));
  102. $view->expects($this->once())
  103. ->method('getFileInfo')
  104. ->with('/bar/foo')
  105. ->will($this->returnValue(array('permissions' => \OCP\PERMISSION_READ)));
  106. $node = new \OC\Files\Node\Folder($root, $view, '/bar/foo');
  107. $node->delete();
  108. }
  109. public function testGetDirectoryContent() {
  110. $manager = $this->getMock('\OC\Files\Mount\Manager');
  111. /**
  112. * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
  113. */
  114. $view = $this->getMock('\OC\Files\View');
  115. $root = $this->getMock('\OC\Files\Node\Root', array(), array($manager, $view, $this->user));
  116. $root->expects($this->any())
  117. ->method('getUser')
  118. ->will($this->returnValue($this->user));
  119. /**
  120. * @var \OC\Files\Storage\Storage | \PHPUnit_Framework_MockObject_MockObject $storage
  121. */
  122. $storage = $this->getMock('\OC\Files\Storage\Storage');
  123. $cache = $this->getMock('\OC\Files\Cache\Cache', array(), array(''));
  124. $cache->expects($this->any())
  125. ->method('getStatus')
  126. ->with('foo')
  127. ->will($this->returnValue(Cache::COMPLETE));
  128. $cache->expects($this->once())
  129. ->method('getFolderContents')
  130. ->with('foo')
  131. ->will($this->returnValue(array(
  132. array('fileid' => 2, 'path' => '/bar/foo/asd', 'name' => 'asd', 'size' => 100, 'mtime' => 50, 'mimetype' => 'text/plain'),
  133. array('fileid' => 3, 'path' => '/bar/foo/qwerty', 'name' => 'qwerty', 'size' => 200, 'mtime' => 55, 'mimetype' => 'httpd/unix-directory')
  134. )));
  135. $permissionsCache = $this->getMock('\OC\Files\Cache\Permissions', array(), array('/'));
  136. $permissionsCache->expects($this->once())
  137. ->method('getDirectoryPermissions')
  138. ->will($this->returnValue(array(2 => \OCP\PERMISSION_ALL)));
  139. $root->expects($this->once())
  140. ->method('getMountsIn')
  141. ->with('/bar/foo')
  142. ->will($this->returnValue(array()));
  143. $storage->expects($this->any())
  144. ->method('getPermissionsCache')
  145. ->will($this->returnValue($permissionsCache));
  146. $storage->expects($this->any())
  147. ->method('getCache')
  148. ->will($this->returnValue($cache));
  149. $view->expects($this->any())
  150. ->method('resolvePath')
  151. ->with('/bar/foo')
  152. ->will($this->returnValue(array($storage, 'foo')));
  153. $node = new \OC\Files\Node\Folder($root, $view, '/bar/foo');
  154. $children = $node->getDirectoryListing();
  155. $this->assertEquals(2, count($children));
  156. $this->assertInstanceOf('\OC\Files\Node\File', $children[0]);
  157. $this->assertInstanceOf('\OC\Files\Node\Folder', $children[1]);
  158. $this->assertEquals('asd', $children[0]->getName());
  159. $this->assertEquals('qwerty', $children[1]->getName());
  160. }
  161. public function testGet() {
  162. $manager = $this->getMock('\OC\Files\Mount\Manager');
  163. /**
  164. * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
  165. */
  166. $view = $this->getMock('\OC\Files\View');
  167. $root = $this->getMock('\OC\Files\Node\Root', array(), array($manager, $view, $this->user));
  168. $root->expects($this->any())
  169. ->method('getUser')
  170. ->will($this->returnValue($this->user));
  171. $root->expects($this->once())
  172. ->method('get')
  173. ->with('/bar/foo/asd');
  174. $node = new \OC\Files\Node\Folder($root, $view, '/bar/foo');
  175. $node->get('asd');
  176. }
  177. public function testNodeExists() {
  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. $child = new \OC\Files\Node\Folder($root, $view, '/bar/foo/asd');
  188. $root->expects($this->once())
  189. ->method('get')
  190. ->with('/bar/foo/asd')
  191. ->will($this->returnValue($child));
  192. $node = new \OC\Files\Node\Folder($root, $view, '/bar/foo');
  193. $this->assertTrue($node->nodeExists('asd'));
  194. }
  195. public function testNodeExistsNotExists() {
  196. $manager = $this->getMock('\OC\Files\Mount\Manager');
  197. /**
  198. * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
  199. */
  200. $view = $this->getMock('\OC\Files\View');
  201. $root = $this->getMock('\OC\Files\Node\Root', array(), array($manager, $view, $this->user));
  202. $root->expects($this->any())
  203. ->method('getUser')
  204. ->will($this->returnValue($this->user));
  205. $root->expects($this->once())
  206. ->method('get')
  207. ->with('/bar/foo/asd')
  208. ->will($this->throwException(new NotFoundException()));
  209. $node = new \OC\Files\Node\Folder($root, $view, '/bar/foo');
  210. $this->assertFalse($node->nodeExists('asd'));
  211. }
  212. public function testNewFolder() {
  213. $manager = $this->getMock('\OC\Files\Mount\Manager');
  214. /**
  215. * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
  216. */
  217. $view = $this->getMock('\OC\Files\View');
  218. $root = $this->getMock('\OC\Files\Node\Root', array(), array($manager, $view, $this->user));
  219. $root->expects($this->any())
  220. ->method('getUser')
  221. ->will($this->returnValue($this->user));
  222. $view->expects($this->once())
  223. ->method('getFileInfo')
  224. ->with('/bar/foo')
  225. ->will($this->returnValue(array('permissions' => \OCP\PERMISSION_ALL)));
  226. $view->expects($this->once())
  227. ->method('mkdir')
  228. ->with('/bar/foo/asd')
  229. ->will($this->returnValue(true));
  230. $node = new \OC\Files\Node\Folder($root, $view, '/bar/foo');
  231. $child = new \OC\Files\Node\Folder($root, $view, '/bar/foo/asd');
  232. $result = $node->newFolder('asd');
  233. $this->assertEquals($child, $result);
  234. }
  235. /**
  236. * @expectedException \OCP\Files\NotPermittedException
  237. */
  238. public function testNewFolderNotPermitted() {
  239. $manager = $this->getMock('\OC\Files\Mount\Manager');
  240. /**
  241. * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
  242. */
  243. $view = $this->getMock('\OC\Files\View');
  244. $root = $this->getMock('\OC\Files\Node\Root', array(), array($manager, $view, $this->user));
  245. $root->expects($this->any())
  246. ->method('getUser')
  247. ->will($this->returnValue($this->user));
  248. $view->expects($this->once())
  249. ->method('getFileInfo')
  250. ->with('/bar/foo')
  251. ->will($this->returnValue(array('permissions' => \OCP\PERMISSION_READ)));
  252. $node = new \OC\Files\Node\Folder($root, $view, '/bar/foo');
  253. $node->newFolder('asd');
  254. }
  255. public function testNewFile() {
  256. $manager = $this->getMock('\OC\Files\Mount\Manager');
  257. /**
  258. * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
  259. */
  260. $view = $this->getMock('\OC\Files\View');
  261. $root = $this->getMock('\OC\Files\Node\Root', array(), array($manager, $view, $this->user));
  262. $root->expects($this->any())
  263. ->method('getUser')
  264. ->will($this->returnValue($this->user));
  265. $view->expects($this->once())
  266. ->method('getFileInfo')
  267. ->with('/bar/foo')
  268. ->will($this->returnValue(array('permissions' => \OCP\PERMISSION_ALL)));
  269. $view->expects($this->once())
  270. ->method('touch')
  271. ->with('/bar/foo/asd')
  272. ->will($this->returnValue(true));
  273. $node = new \OC\Files\Node\Folder($root, $view, '/bar/foo');
  274. $child = new \OC\Files\Node\File($root, $view, '/bar/foo/asd');
  275. $result = $node->newFile('asd');
  276. $this->assertEquals($child, $result);
  277. }
  278. /**
  279. * @expectedException \OCP\Files\NotPermittedException
  280. */
  281. public function testNewFileNotPermitted() {
  282. $manager = $this->getMock('\OC\Files\Mount\Manager');
  283. /**
  284. * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
  285. */
  286. $view = $this->getMock('\OC\Files\View');
  287. $root = $this->getMock('\OC\Files\Node\Root', array(), array($manager, $view, $this->user));
  288. $root->expects($this->any())
  289. ->method('getUser')
  290. ->will($this->returnValue($this->user));
  291. $view->expects($this->once())
  292. ->method('getFileInfo')
  293. ->with('/bar/foo')
  294. ->will($this->returnValue(array('permissions' => \OCP\PERMISSION_READ)));
  295. $node = new \OC\Files\Node\Folder($root, $view, '/bar/foo');
  296. $node->newFile('asd');
  297. }
  298. public function testGetFreeSpace() {
  299. $manager = $this->getMock('\OC\Files\Mount\Manager');
  300. /**
  301. * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
  302. */
  303. $view = $this->getMock('\OC\Files\View');
  304. $root = $this->getMock('\OC\Files\Node\Root', array(), array($manager, $view, $this->user));
  305. $root->expects($this->any())
  306. ->method('getUser')
  307. ->will($this->returnValue($this->user));
  308. $view->expects($this->once())
  309. ->method('free_space')
  310. ->with('/bar/foo')
  311. ->will($this->returnValue(100));
  312. $node = new \OC\Files\Node\Folder($root, $view, '/bar/foo');
  313. $this->assertEquals(100, $node->getFreeSpace());
  314. }
  315. public function testSearch() {
  316. $manager = $this->getMock('\OC\Files\Mount\Manager');
  317. /**
  318. * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
  319. */
  320. $view = $this->getMock('\OC\Files\View');
  321. $root = $this->getMock('\OC\Files\Node\Root', array(), array($manager, $view, $this->user));
  322. $root->expects($this->any())
  323. ->method('getUser')
  324. ->will($this->returnValue($this->user));
  325. $storage = $this->getMock('\OC\Files\Storage\Storage');
  326. $cache = $this->getMock('\OC\Files\Cache\Cache', array(), array(''));
  327. $storage->expects($this->once())
  328. ->method('getCache')
  329. ->will($this->returnValue($cache));
  330. $cache->expects($this->once())
  331. ->method('search')
  332. ->with('%qw%')
  333. ->will($this->returnValue(array(
  334. array('fileid' => 3, 'path' => 'foo/qwerty', 'name' => 'qwerty', 'size' => 200, 'mtime' => 55, 'mimetype' => 'text/plain')
  335. )));
  336. $root->expects($this->once())
  337. ->method('getMountsIn')
  338. ->with('/bar/foo')
  339. ->will($this->returnValue(array()));
  340. $view->expects($this->once())
  341. ->method('resolvePath')
  342. ->will($this->returnValue(array($storage, 'foo')));
  343. $node = new \OC\Files\Node\Folder($root, $view, '/bar/foo');
  344. $result = $node->search('qw');
  345. $this->assertEquals(1, count($result));
  346. $this->assertEquals('/bar/foo/qwerty', $result[0]->getPath());
  347. }
  348. public function testSearchSubStorages() {
  349. $manager = $this->getMock('\OC\Files\Mount\Manager');
  350. /**
  351. * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
  352. */
  353. $view = $this->getMock('\OC\Files\View');
  354. $root = $this->getMock('\OC\Files\Node\Root', array(), array($manager, $view, $this->user));
  355. $root->expects($this->any())
  356. ->method('getUser')
  357. ->will($this->returnValue($this->user));
  358. $storage = $this->getMock('\OC\Files\Storage\Storage');
  359. $cache = $this->getMock('\OC\Files\Cache\Cache', array(), array(''));
  360. $subCache = $this->getMock('\OC\Files\Cache\Cache', array(), array(''));
  361. $subStorage = $this->getMock('\OC\Files\Storage\Storage');
  362. $subMount = $this->getMock('\OC\Files\Mount\Mount', array(), array(null, ''));
  363. $subMount->expects($this->once())
  364. ->method('getStorage')
  365. ->will($this->returnValue($subStorage));
  366. $subMount->expects($this->once())
  367. ->method('getMountPoint')
  368. ->will($this->returnValue('/bar/foo/bar/'));
  369. $storage->expects($this->once())
  370. ->method('getCache')
  371. ->will($this->returnValue($cache));
  372. $subStorage->expects($this->once())
  373. ->method('getCache')
  374. ->will($this->returnValue($subCache));
  375. $cache->expects($this->once())
  376. ->method('search')
  377. ->with('%qw%')
  378. ->will($this->returnValue(array(
  379. array('fileid' => 3, 'path' => 'foo/qwerty', 'name' => 'qwerty', 'size' => 200, 'mtime' => 55, 'mimetype' => 'text/plain')
  380. )));
  381. $subCache->expects($this->once())
  382. ->method('search')
  383. ->with('%qw%')
  384. ->will($this->returnValue(array(
  385. array('fileid' => 4, 'path' => 'asd/qweasd', 'name' => 'qweasd', 'size' => 200, 'mtime' => 55, 'mimetype' => 'text/plain')
  386. )));
  387. $root->expects($this->once())
  388. ->method('getMountsIn')
  389. ->with('/bar/foo')
  390. ->will($this->returnValue(array($subMount)));
  391. $view->expects($this->once())
  392. ->method('resolvePath')
  393. ->will($this->returnValue(array($storage, 'foo')));
  394. $node = new \OC\Files\Node\Folder($root, $view, '/bar/foo');
  395. $result = $node->search('qw');
  396. $this->assertEquals(2, count($result));
  397. }
  398. public function testIsSubNode() {
  399. $file = new Node(null, null, '/foo/bar');
  400. $folder = new \OC\Files\Node\Folder(null, null, '/foo');
  401. $this->assertTrue($folder->isSubNode($file));
  402. $this->assertFalse($folder->isSubNode($folder));
  403. $file = new Node(null, null, '/foobar');
  404. $this->assertFalse($folder->isSubNode($file));
  405. }
  406. }